Skip to content

Commit b6acc6a

Browse files
committed
Consolidate lazy import hook tests into stdlib_imp
Move the distinct direct-hook argument and hashing contracts into the existing _imp snippet and remove the separate file. Preserve every pre-existing assertion and the CPython version guard; canonical 3.15 lazy-import tests already cover feature-level registry and reification behavior. Assisted-by: Codex:model-version-unavailable
1 parent 29d5ba9 commit b6acc6a

2 files changed

Lines changed: 43 additions & 46 deletions

File tree

‎extra_tests/snippets/stdlib_imp.py‎

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import _imp
22
import time as import_time
33

4-
from testutils import assert_raises
4+
from testutils import assert_raises, skip_if_unsupported
55

66
assert _imp.is_builtin("time") == True
77
assert _imp.is_builtin("os") == False
@@ -42,3 +42,45 @@ def __init__(self, name):
4242
assert ispkg is False
4343
assert origname == "__hello__"
4444
assert _imp.get_frozen_object("__hello__", data).co_name == "<module>"
45+
46+
47+
def test_lazy_attributes_eager_registry():
48+
# CPython accepts any first argument and a string name. In an eager-only
49+
# interpreter there are no pending lazy module names to remove.
50+
for modobj in (None, object(), {}, 42):
51+
assert _imp._set_lazy_attributes(modobj, "package.module") is None
52+
assert _imp._set_lazy_attributes(modobj, "\ud800") is None
53+
54+
for name in (None, 1, b"package.module", [], {}):
55+
with assert_raises(TypeError):
56+
_imp._set_lazy_attributes(None, name)
57+
58+
with assert_raises(TypeError):
59+
_imp._set_lazy_attributes(modobj=None, name="package.module")
60+
61+
class ModuleName(str):
62+
hashes = 0
63+
64+
def __hash__(self):
65+
self.hashes += 1
66+
return 42
67+
68+
name = ModuleName("package.module")
69+
assert _imp._set_lazy_attributes(None, name) is None
70+
assert name.hashes == 1
71+
72+
class UnhashableName(str):
73+
__hash__ = None
74+
75+
with assert_raises(TypeError):
76+
_imp._set_lazy_attributes(None, UnhashableName("package.module"))
77+
78+
class HashErrorName(str):
79+
def __hash__(self):
80+
raise RuntimeError("name hash failed")
81+
82+
with assert_raises(RuntimeError):
83+
_imp._set_lazy_attributes(None, HashErrorName("package.module"))
84+
85+
86+
skip_if_unsupported(3, 15, test_lazy_attributes_eager_registry)

‎extra_tests/snippets/stdlib_imp_lazy_attributes.py‎

Lines changed: 0 additions & 45 deletions
This file was deleted.

0 commit comments

Comments
 (0)