Skip to content

Commit 6596cf1

Browse files
committed
gh-87465: Bypass sys.modules when finding module specs in pyclbr
1 parent 9f38636 commit 6596cf1

3 files changed

Lines changed: 111 additions & 37 deletions

File tree

‎Lib/pyclbr.py‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@
4242
"""
4343

4444
import ast
45+
import importlib._bootstrap
4546
import sys
46-
import importlib.util
4747

4848
__all__ = ["readmodule", "readmodule_ex", "Class", "Function"]
4949

@@ -162,7 +162,9 @@ def _readmodule(module, path, inpackage=None):
162162
search_path = path
163163
else:
164164
search_path = path + sys.path
165-
spec = importlib.util._find_spec_from_path(fullmodule, search_path)
165+
# gh-87465: Bypass sys.modules since pyclbr maintains its own _modules
166+
# cache and entries like __main__ may have __spec__ set to None.
167+
spec = importlib._bootstrap._find_spec(fullmodule, search_path)
166168
if spec is None:
167169
raise ModuleNotFoundError(f"no module named {fullmodule!r}", name=fullmodule)
168170
_modules[fullmodule] = tree

‎Lib/test/test_pyclbr.py‎

Lines changed: 102 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33
Nick Mathewson
44
'''
55

6-
import importlib.machinery
6+
import os
77
import sys
8-
from contextlib import contextmanager
98
from textwrap import dedent
10-
from types import FunctionType, MethodType, BuiltinFunctionType
9+
from types import FunctionType, MethodType, BuiltinFunctionType, ModuleType
1110
import pyclbr
1211
from unittest import TestCase, main as unittest_main
12+
from unittest.mock import patch
13+
from test.support import os_helper
1314
from test.test_importlib import util as test_importlib_util
1415

1516

@@ -24,28 +25,6 @@
2425
# members to ignore.
2526

2627

27-
@contextmanager
28-
def temporary_main_spec():
29-
"""
30-
A context manager that temporarily sets the `__spec__` attribute
31-
of the `__main__` module if it's missing.
32-
"""
33-
main_mod = sys.modules.get("__main__")
34-
if main_mod is None:
35-
yield # Do nothing if __main__ is not present
36-
return
37-
38-
original_spec = getattr(main_mod, "__spec__", None)
39-
if original_spec is None:
40-
main_mod.__spec__ = importlib.machinery.ModuleSpec(
41-
name="__main__", loader=None, origin="built-in"
42-
)
43-
try:
44-
yield
45-
finally:
46-
main_mod.__spec__ = original_spec
47-
48-
4928
class PyclbrTest(TestCase):
5029

5130
def assertListEq(self, l1, l2, ignore):
@@ -169,9 +148,8 @@ def test_easy(self):
169148
self.checkModule('pyclbr')
170149
# XXX: Metaclasses are not supported
171150
# self.checkModule('ast')
172-
with temporary_main_spec():
173-
self.checkModule('doctest', ignore=("TestResults", "_SpoofOut",
174-
"DocTestCase", '_DocTestSuite'))
151+
self.checkModule('doctest', ignore=("TestResults", "_SpoofOut",
152+
"DocTestCase", '_DocTestSuite'))
175153
self.checkModule('difflib', ignore=("Match",))
176154

177155
def test_cases(self):
@@ -245,13 +223,12 @@ def test_others(self):
245223
# These were once some of the longest modules.
246224
cm('random', ignore=('Random',)) # from _random import Random as CoreGenerator
247225
cm('pickle', ignore=('partial', 'PickleBuffer'))
248-
with temporary_main_spec():
249-
cm(
250-
'pdb',
251-
# pyclbr does not handle elegantly `typing` or properties
252-
ignore=('Union', '_ModuleTarget', '_ScriptTarget', '_ZipTarget', 'curframe_locals',
253-
'_InteractState', 'rlcompleter'),
254-
)
226+
cm(
227+
'pdb',
228+
# pyclbr does not handle elegantly `typing` or properties
229+
ignore=('Union', '_ModuleTarget', '_ScriptTarget', '_ZipTarget', 'curframe_locals',
230+
'_InteractState', 'rlcompleter'),
231+
)
255232
cm('pydoc', ignore=('input', 'output', # properties
256233
'getpager', 'plainpager', )) # aliases
257234

@@ -283,6 +260,96 @@ def test_module_has_no_spec(self):
283260
with self.assertRaises(ModuleNotFoundError):
284261
pyclbr.readmodule_ex(module_name)
285262

263+
def test_import_main_without_spec(self):
264+
# gh-87465: traversing "import __main__" when __main__.__spec__ is None
265+
# should not raise ValueError.
266+
with (
267+
os_helper.temp_dir() as d,
268+
patch.object(sys, "path", []),
269+
patch.object(sys.modules["__main__"], "__spec__", None),
270+
):
271+
mod_file = os.path.join(d, "mod.py")
272+
with open(mod_file, "w", encoding="utf-8") as f:
273+
f.write(dedent("""\
274+
import __main__
275+
from __main__ import missing_symbol
276+
277+
class A:
278+
pass
279+
280+
def func():
281+
pass
282+
"""))
283+
284+
tree = pyclbr.readmodule_ex("mod", path=[d])
285+
self.assertEqual(tree.keys(), {"A", "func"})
286+
self.assertIsInstance(tree["A"], pyclbr.Class)
287+
self.assertIsInstance(tree["func"], pyclbr.Function)
288+
289+
with self.assertRaises(ModuleNotFoundError):
290+
pyclbr.readmodule_ex("__main__", path=[d])
291+
292+
def test_import_main_from_search_path(self):
293+
# gh-87465: __main__.py on the search path should be used instead of
294+
# whatever __main__ module is cached in sys.modules.
295+
with os_helper.temp_dir() as d:
296+
main_file = os.path.join(d, "__main__.py")
297+
with open(main_file, "w", encoding="utf-8") as f:
298+
f.write("class Base:\n pass\n")
299+
300+
mod_file = os.path.join(d, "mod.py")
301+
with open(mod_file, "w", encoding="utf-8") as f:
302+
f.write(dedent("""\
303+
import __main__
304+
305+
class Sub(__main__.Base):
306+
pass
307+
"""))
308+
309+
for main_spec in (pyclbr.__spec__, None):
310+
with self.subTest(main_spec=main_spec):
311+
pyclbr._modules.clear()
312+
with patch.object(sys.modules["__main__"], "__spec__", main_spec):
313+
tree = pyclbr.readmodule_ex("mod", path=[d])
314+
self.assertIn("Sub", tree)
315+
bases = tree["Sub"].super
316+
self.assertEqual(len(bases), 1)
317+
self.assertIsInstance(bases[0], pyclbr.Class)
318+
self.assertEqual(bases[0].name, "Base")
319+
self.assertEqual(bases[0].module, "__main__")
320+
self.assertEqual(bases[0].file, main_file)
321+
322+
def test_import_cached_module_without_spec(self):
323+
# gh-87465: synthetic modules in sys.modules with __spec__ set to None
324+
# or missing __spec__ should not cause ValueError.
325+
mod_none = ModuleType("pyclbr_synthetic_none")
326+
mod_none.__spec__ = None
327+
mod_unset = ModuleType("pyclbr_synthetic_unset")
328+
del mod_unset.__spec__
329+
330+
for name, mod in (
331+
("pyclbr_synthetic_none", mod_none),
332+
("pyclbr_synthetic_unset", mod_unset),
333+
):
334+
with (
335+
self.subTest(module=name),
336+
test_importlib_util.uncache(name),
337+
os_helper.temp_dir() as d,
338+
):
339+
pyclbr._modules.clear()
340+
sys.modules[name] = mod
341+
342+
mod_file = os.path.join(d, "mod.py")
343+
with open(mod_file, "w", encoding="utf-8") as f:
344+
f.write(f"import {name}\nclass C:\n pass\n")
345+
346+
tree = pyclbr.readmodule_ex("mod", path=[d])
347+
self.assertEqual(list(tree), ["C"])
348+
self.assertIsInstance(tree["C"], pyclbr.Class)
349+
350+
with self.assertRaises(ModuleNotFoundError):
351+
pyclbr.readmodule_ex(name, path=[d])
352+
286353

287354
if __name__ == "__main__":
288355
unittest_main()
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Fix :exc:`ValueError` in :func:`pyclbr.readmodule` and
2+
:func:`pyclbr.readmodule_ex` when browsing a module that imports
3+
:mod:`__main__` or another module in :data:`sys.modules` without a
4+
``__spec__``, and look up module specs from the search path instead of
5+
:data:`sys.modules`.

0 commit comments

Comments
 (0)