Skip to content

Commit 04d9990

Browse files
authored
Add the eager-runtime hook required by Python 3.15 importlib (#8946)
* Add the eager-runtime importlib lazy-attribute hook Expose _imp._set_lazy_attributes(modobj, name, /), used by CPython 3.15 importlib after eager loading. Match its empty pending-lazy-module registry behavior, including name type and string-subclass hashing checks. This does not implement lazy imports or change the target version or bundled libraries. Assisted-by: Codex:model-version-unavailable * 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 f1ebf16 commit 04d9990

2 files changed

Lines changed: 57 additions & 2 deletions

File tree

‎crates/vm/src/stdlib/_imp.rs‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ fn find_frozen(name: &str, vm: &VirtualMachine) -> Result<FrozenModule, FrozenEr
193193
#[pymodule(with(lock))]
194194
mod _imp {
195195
use crate::{
196-
PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine,
196+
AsObject, PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine,
197197
builtins::{PyBytesRef, PyCode, PyMemoryView, PyModule, PyStrRef, PyUtf8StrRef},
198198
import, version,
199199
};
@@ -214,6 +214,19 @@ mod _imp {
214214
Vec::new()
215215
}
216216

217+
// CPython removes the name from its pending lazy-module registry here.
218+
// RustPython currently performs only eager imports, so that registry is empty.
219+
#[pyfunction]
220+
fn _set_lazy_attributes(
221+
_modobj: PyObjectRef,
222+
name: PyStrRef,
223+
vm: &VirtualMachine,
224+
) -> PyResult<()> {
225+
// Even an empty set checks the hash of a str subclass.
226+
name.as_object().hash(vm)?;
227+
Ok(())
228+
}
229+
217230
#[pyfunction]
218231
fn is_builtin(name: PyUtf8StrRef, vm: &VirtualMachine) -> bool {
219232
vm.state.module_defs.contains_key(name.as_str())

‎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)

0 commit comments

Comments
 (0)