Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions crates/vm/src/lazy_import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,13 +232,16 @@ pub(crate) fn init_type(ctx: &'static Context) {
PyLazyImport::extend_class(ctx, ctx.types.lazy_import_type);
}

pub(crate) fn is_module_level(vm: &VirtualMachine) -> bool {
pub(crate) fn is_module_level() -> bool {
let frame = frame::current_thread_iframe();
if frame.is_null() {
return false;
}
let frame = unsafe { &*frame };
frame.globals().as_object().is(frame.locals.as_object(vm))
frame
.locals
.get()
.is_some_and(|locals| frame.globals().as_object().is(locals.obj()))
}

fn optional_item(
Expand Down Expand Up @@ -266,9 +269,9 @@ pub(crate) fn import_name(
let explicit = policy & 1 != 0;
let globals = vm.current_globals();
let mut lazy =
explicit || (vm.state.lazy_imports.all.load(Ordering::Acquire) && is_module_level(vm));
explicit || (vm.state.lazy_imports.all.load(Ordering::Acquire) && is_module_level());
if !lazy
&& is_module_level(vm)
&& is_module_level()
&& let Some(names) = optional_item(
globals.as_object(),
vm.ctx.intern_str("__lazy_modules__"),
Expand Down Expand Up @@ -316,7 +319,7 @@ pub(crate) fn create(
vm: &VirtualMachine,
) -> PyResult {
let absolute = crate::import::absolute_import_name(name, Some(globals), level, vm)?;
if !is_module_level(vm) {
if !is_module_level() {
return Err(vm.new_exception_msg(
vm.ctx.exceptions.syntax_error.to_owned(),
"'lazy import' is only allowed at module level".into(),
Expand Down
6 changes: 1 addition & 5 deletions crates/vm/src/stdlib/_ast/python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ pub(crate) mod _ast {
impl Constructor for NodeAst {
type Args = FuncArgs;

fn slot_new(cls: PyTypeRef, args: FuncArgs, vm: &VirtualMachine) -> PyResult {
fn slot_new(cls: PyTypeRef, _args: FuncArgs, vm: &VirtualMachine) -> PyResult {
// Keep _instance for parser-internal shared operator/context nodes,
// but match CPython's public constructor behavior by allocating a
// fresh object for Python-level ast.Load()/ast.Add()/... calls.
Expand All @@ -311,10 +311,6 @@ pub(crate) mod _ast {
};
let zelf = vm.ctx.new_base_object(cls, dict);

// type.__call__ does not invoke slot_init after slot_new
// for types with a custom slot_new, so we must call it here.
Self::slot_init(&zelf, args, vm)?;

Ok(zelf)
}

Expand Down
Loading