|
29 | 29 | from unittest import mock |
30 | 30 | import _imp |
31 | 31 |
|
| 32 | +from test import support |
32 | 33 | from test.support import os_helper |
33 | 34 | from test.support import ( |
34 | 35 | STDLIB_DIR, |
@@ -416,22 +417,26 @@ def test_from_import_missing_attr_path_is_canonical(self): |
416 | 417 | self.assertIn(cm.exception.name, {'posixpath', 'ntpath'}) |
417 | 418 | self.assertIsNotNone(cm.exception) |
418 | 419 |
|
| 420 | + @support.requires_subprocess() |
419 | 421 | def test_from_import_module_attr_from_non_package(self): |
420 | | - module_name = 'test_from_import_module_attr_from_non_package' |
421 | | - child_name = f'{module_name}.child' |
422 | | - module = types.ModuleType(module_name) |
423 | | - child = types.ModuleType(child_name) |
424 | | - module.child = child |
425 | | - self.addCleanup(unload, module_name) |
426 | | - self.addCleanup(unload, child_name) |
427 | | - sys.modules[module_name] = module |
428 | | - |
429 | | - # A plain module can expose a module-valued attribute without being a |
430 | | - # package, so from-import must return the attribute as-is. |
431 | | - ns = {} |
432 | | - exec(f'from {module_name} import child as imported', ns) |
433 | | - self.assertIs(ns['imported'], child) |
434 | | - self.assertNotIn(child_name, sys.modules) |
| 422 | + code = textwrap.dedent(""" |
| 423 | + import sys |
| 424 | + import types |
| 425 | +
|
| 426 | + module_name = 'test_from_import_module_attr_from_non_package' |
| 427 | + child_name = f'{module_name}.child' |
| 428 | + module = types.ModuleType(module_name) |
| 429 | + child = types.ModuleType(child_name) |
| 430 | + module.child = child |
| 431 | + sys.modules[module_name] = module |
| 432 | +
|
| 433 | + # A plain module can expose a module-valued attribute without |
| 434 | + # being a package, so from-import must return the attribute as-is. |
| 435 | + from test_from_import_module_attr_from_non_package import child as imported |
| 436 | + assert imported is child |
| 437 | + assert child_name not in sys.modules, child_name |
| 438 | + """) |
| 439 | + script_helper.assert_python_ok("-c", code) |
435 | 440 |
|
436 | 441 | def test_from_import_star_invalid_type(self): |
437 | 442 | with ready_to_import() as (name, path): |
|
0 commit comments