FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Add the eager-runtime hook required by Python 3.15 importlib by youknowdot · Pull Request #8946 · RustPython/RustPython · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (1) .rs  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
15 changes: 14 additions & 1 deletion crates/vm/src/stdlib/_imp.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ fn find_frozen(name: &str, vm: &VirtualMachine) -> Result<FrozenModule, FrozenEr
#[pymodule(with(lock))]
mod _imp {
use crate::{
PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine,
AsObject, PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine,
builtins::{PyBytesRef, PyCode, PyMemoryView, PyModule, PyStrRef, PyUtf8StrRef},
import, version,
};
Expand All @@ -214,6 +214,19 @@ mod _imp {
Vec::new()
}

// CPython removes the name from its pending lazy-module registry here.
// RustPython currently performs only eager imports, so that registry is empty.
#[pyfunction]
fn _set_lazy_attributes(
_modobj: PyObjectRef,
name: PyStrRef,
vm: &VirtualMachine,
) -> PyResult<()> {
// Even an empty set checks the hash of a str subclass.
name.as_object().hash(vm)?;
Ok(())
}

#[pyfunction]
fn is_builtin(name: PyUtf8StrRef, vm: &VirtualMachine) -> bool {
vm.state.module_defs.contains_key(name.as_str())
Expand Down
44 changes: 43 additions & 1 deletion extra_tests/snippets/stdlib_imp.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import _imp
import time as import_time

from testutils import assert_raises
from testutils import assert_raises, skip_if_unsupported

assert _imp.is_builtin("time") == True
assert _imp.is_builtin("os") == False
Expand Down Expand Up @@ -42,3 +42,45 @@ def __init__(self, name):
assert ispkg is False
assert origname == "__hello__"
assert _imp.get_frozen_object("__hello__", data).co_name == "<module>"


def test_lazy_attributes_eager_registry():
# CPython accepts any first argument and a string name. In an eager-only
# interpreter there are no pending lazy module names to remove.
for modobj in (None, object(), {}, 42):
assert _imp._set_lazy_attributes(modobj, "package.module") is None
assert _imp._set_lazy_attributes(modobj, "\ud800") is None

for name in (None, 1, b"package.module", [], {}):
with assert_raises(TypeError):
_imp._set_lazy_attributes(None, name)

with assert_raises(TypeError):
_imp._set_lazy_attributes(modobj=None, name="package.module")

class ModuleName(str):
hashes = 0

def __hash__(self):
self.hashes += 1
return 42

name = ModuleName("package.module")
assert _imp._set_lazy_attributes(None, name) is None
assert name.hashes == 1

class UnhashableName(str):
__hash__ = None

with assert_raises(TypeError):
_imp._set_lazy_attributes(None, UnhashableName("package.module"))

class HashErrorName(str):
def __hash__(self):
raise RuntimeError("name hash failed")

with assert_raises(RuntimeError):
_imp._set_lazy_attributes(None, HashErrorName("package.module"))


skip_if_unsupported(3, 15, test_lazy_attributes_eager_registry)
Loading

Back | FazBrowse Home | New Git URL