|
3 | 3 | Nick Mathewson |
4 | 4 | ''' |
5 | 5 |
|
6 | | -import importlib.machinery |
| 6 | +import os |
7 | 7 | import sys |
8 | | -from contextlib import contextmanager |
9 | 8 | from textwrap import dedent |
10 | | -from types import FunctionType, MethodType, BuiltinFunctionType |
| 9 | +from types import FunctionType, MethodType, BuiltinFunctionType, ModuleType |
11 | 10 | import pyclbr |
12 | 11 | from unittest import TestCase, main as unittest_main |
| 12 | +from unittest.mock import patch |
| 13 | +from test.support import os_helper |
13 | 14 | from test.test_importlib import util as test_importlib_util |
14 | 15 |
|
15 | 16 |
|
|
24 | 25 | # members to ignore. |
25 | 26 |
|
26 | 27 |
|
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 | | - |
49 | 28 | class PyclbrTest(TestCase): |
50 | 29 |
|
51 | 30 | def assertListEq(self, l1, l2, ignore): |
@@ -169,9 +148,8 @@ def test_easy(self): |
169 | 148 | self.checkModule('pyclbr') |
170 | 149 | # XXX: Metaclasses are not supported |
171 | 150 | # 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')) |
175 | 153 | self.checkModule('difflib', ignore=("Match",)) |
176 | 154 |
|
177 | 155 | def test_cases(self): |
@@ -245,13 +223,12 @@ def test_others(self): |
245 | 223 | # These were once some of the longest modules. |
246 | 224 | cm('random', ignore=('Random',)) # from _random import Random as CoreGenerator |
247 | 225 | 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 | + ) |
255 | 232 | cm('pydoc', ignore=('input', 'output', # properties |
256 | 233 | 'getpager', 'plainpager', )) # aliases |
257 | 234 |
|
@@ -283,6 +260,96 @@ def test_module_has_no_spec(self): |
283 | 260 | with self.assertRaises(ModuleNotFoundError): |
284 | 261 | pyclbr.readmodule_ex(module_name) |
285 | 262 |
|
| 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 | + |
286 | 353 |
|
287 | 354 | if __name__ == "__main__": |
288 | 355 | unittest_main() |
0 commit comments