Skip to content

Commit 36f1821

Browse files
committed
gh-152298: Cover module attribute lazy reification
1 parent a407b55 commit 36f1821

1 file changed

Lines changed: 30 additions & 0 deletions

File tree

Lib/test/test_lazy_import/__init__.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,36 @@ def custom_import(name, globals=None, locals=None, fromlist=None,
544544
""")
545545
assert_python_ok("-c", code)
546546

547+
@support.requires_subprocess()
548+
def test_module_attribute_reification_does_not_clobber_changed_global(self):
549+
code = textwrap.dedent("""
550+
import builtins
551+
import sys
552+
import types
553+
554+
real_import = builtins.__import__
555+
module = sys.modules[__name__]
556+
557+
lazy import target_module as target
558+
559+
def custom_import(name, globals=None, locals=None, fromlist=None,
560+
level=0):
561+
if name == "target_module":
562+
module.__dict__["target"] = "user value"
563+
resolved = types.ModuleType(name)
564+
resolved.VALUE = "resolved"
565+
return resolved
566+
return real_import(name, globals, locals, fromlist, level)
567+
568+
builtins.__import__ = custom_import
569+
try:
570+
assert module.target.VALUE == "resolved"
571+
assert module.__dict__["target"] == "user value"
572+
finally:
573+
builtins.__import__ = real_import
574+
""")
575+
assert_python_ok("-c", code)
576+
547577

548578
class SyntaxRestrictionTests(LazyImportTestCase):
549579
"""Tests for syntax restrictions on lazy imports."""

0 commit comments

Comments
 (0)