Skip to content

Commit b57f4c9

Browse files
committed
Update test format to be more uniform
1 parent 1f219b1 commit b57f4c9

1 file changed

Lines changed: 20 additions & 15 deletions

File tree

Lib/test/test_import/__init__.py

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
from unittest import mock
3030
import _imp
3131

32+
from test import support
3233
from test.support import os_helper
3334
from test.support import (
3435
STDLIB_DIR,
@@ -416,22 +417,26 @@ def test_from_import_missing_attr_path_is_canonical(self):
416417
self.assertIn(cm.exception.name, {'posixpath', 'ntpath'})
417418
self.assertIsNotNone(cm.exception)
418419

420+
@support.requires_subprocess()
419421
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)
435440

436441
def test_from_import_star_invalid_type(self):
437442
with ready_to_import() as (name, path):

0 commit comments

Comments
 (0)