Skip to content
Merged
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
2 changes: 0 additions & 2 deletions Lib/test/test_import/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1380,8 +1380,6 @@ def test_crossreference2(self):
self.assertIn('partially initialized module', errmsg)
self.assertIn('circular import', errmsg)

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_circular_from_import(self):
with self.assertRaises(ImportError) as cm:
import test.test_import.data.circular_imports.from_cycle1
Expand Down
48 changes: 40 additions & 8 deletions vm/src/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1363,19 +1363,38 @@ impl ExecutingFrame<'_> {
fn import_from(&mut self, vm: &VirtualMachine, idx: bytecode::NameIdx) -> PyResult {
let module = self.top_value();
let name = self.code.names[idx as usize];
let err = || vm.new_import_error(format!("cannot import name '{name}'"), name.to_owned());

// Load attribute, and transform any error into import error.
if let Some(obj) = vm.get_attribute_opt(module.to_owned(), name)? {
return Ok(obj);
}
// fallback to importing '{module.__name__}.{name}' from sys.modules
let mod_name = module
.get_attr(identifier!(vm, __name__), vm)
.map_err(|_| err())?;
let mod_name = mod_name.downcast::<PyStr>().map_err(|_| err())?;
let full_mod_name = format!("{mod_name}.{name}");
let sys_modules = vm.sys_module.get_attr("modules", vm).map_err(|_| err())?;
sys_modules.get_item(&full_mod_name, vm).map_err(|_| err())
let fallback_module = (|| {
let mod_name = module.get_attr(identifier!(vm, __name__), vm).ok()?;
let mod_name = mod_name.downcast_ref::<PyStr>()?;
let full_mod_name = format!("{mod_name}.{name}");
let sys_modules = vm.sys_module.get_attr("modules", vm).ok()?;
sys_modules.get_item(&full_mod_name, vm).ok()
})();

if let Some(sub_module) = fallback_module {
return Ok(sub_module);
}

if is_module_initializing(module, vm) {
let module_name = module
.get_attr(identifier!(vm, __name__), vm)
.ok()
.and_then(|n| n.downcast_ref::<PyStr>().map(|s| s.as_str().to_owned()))
.unwrap_or_else(|| "<unknown>".to_owned());

let msg = format!(
"cannot import name '{name}' from partially initialized module '{module_name}' (most likely due to a circular import)",
);
Err(vm.new_import_error(msg, name.to_owned()))
} else {
Err(vm.new_import_error(format!("cannot import name '{name}'"), name.to_owned()))
}
}

#[cfg_attr(feature = "flame-it", flame("Frame"))]
Expand Down Expand Up @@ -2372,3 +2391,16 @@ impl fmt::Debug for Frame {
)
}
}

fn is_module_initializing(module: &PyObject, vm: &VirtualMachine) -> bool {
let Ok(spec) = module.get_attr(&vm.ctx.new_str("__spec__"), vm) else {
return false;
};
if vm.is_none(&spec) {
return false;
}
let Ok(initializing_attr) = spec.get_attr(&vm.ctx.new_str("_initializing"), vm) else {
return false;
};
initializing_attr.try_to_bool(vm).unwrap_or(false)
}
Comment on lines +2395 to +2406
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Verification agent

🧩 Analysis chain

Helper function implementation is sound but relies on unset attribute.

The is_module_initializing function has proper error handling, but it checks for __spec__._initializing which isn't being set by the import system. This function needs to either:

  1. Check the internal PyModule.initializing field if the module is a PyModule instance
  2. Ensure that __spec__._initializing is properly set during import

Consider checking the internal field directly:

fn is_module_initializing(module: &PyObject, vm: &VirtualMachine) -> bool {
+    // First try to check the internal field if this is a PyModule
+    if let Ok(py_module) = module.downcast_ref::<PyModule>() {
+        use std::sync::atomic::Ordering;
+        return py_module.initializing.load(Ordering::Relaxed);
+    }
+    
+    // Fallback to checking __spec__._initializing
    let Ok(spec) = module.get_attr(&vm.ctx.new_str("__spec__"), vm) else {
        return false;
    };
    if vm.is_none(&spec) {
        return false;
    }
    let Ok(initializing_attr) = spec.get_attr(&vm.ctx.new_str("_initializing"), vm) else {
        return false;
    };
    initializing_attr.try_to_bool(vm).unwrap_or(false)
}

Run the following script to check how CPython handles module initialization tracking:


🌐 Web query:

How does CPython track module initialization state for circular import detection? Does it use __spec__._initializing?

💡 Result:

CPython tracks module initialization state for circular import detection by leveraging the sys.modules cache. When a module is first imported, a module object is placed into sys.modules immediately—before its code is executed. This ensures that if the module is imported again (such as via a circular import), Python will return this already-existing (but possibly only partially initialized) module object, rather than starting a new import. This mechanism is essential for breaking import cycles and preventing infinite recursion during import[3][7].

The __spec__._initializing attribute (or similar such as __spec__._initialized) is an internal implementation detail and is not the primary mechanism for detection of circular imports in CPython. There have been discussions and experimental ideas about using such an attribute to mark a module’s initialization state for more fine-grained control or for advanced import hacks like async imports[6][8]. However, in normal CPython operation and as of current mainstream Python versions, this attribute is either not present or not used by the import machinery for circular import detection.

Instead, here is how it works:

  • When a module is being imported, it is immediately inserted into sys.modules, signaling "import in progress."
  • If another module tries to import it during this time, it receives a reference to this partially initialized module[3][7].
  • If the code tries to access names that haven’t been defined yet (because initialization hasn't finished), you get errors like:

    ImportError: cannot import name 'X' from 'Y' (most likely due to a circular import)[7]

  • There is no robust, public-facing per-module "initializing" flag used by the import system; the presence of the module object in sys.modules before its code has finished running is the primary state signal.

Summary Table

Mechanism Used by CPython for circular import detection? Notes
sys.modules early insert Yes Inserted immediately, acts as the signal for "import in progress"
__spec__._initializing No (not in standard import) May be used in experimental or advanced import schemes, not standard in CPython as of 2025
Explicit module state flag No Not part of public API or normal workflow

In conclusion:
CPython does not use __spec__._initializing to track module initialization state for the purpose of circular import detection; it relies on the presence of the module in sys.modules during import.[3][7]

Citations:


Streamline is_module_initializing to use internal flag and drop __spec__._initializing fallback.

  • File: vm/src/frame.rs (lines 2405–2416)
  • CPython doesn’t use __spec__._initializing for import tracking—modules in sys.modules signal “in progress.”
  • RustPython’s PyModule already has an initializing flag; use that and return false for all other objects.
 fn is_module_initializing(module: &PyObject, vm: &VirtualMachine) -> bool {
-    // Fallback to checking __spec__._initializing
-    let Ok(spec) = module.get_attr(&vm.ctx.new_str("__spec__"), vm) else {
-        return false;
-    };
-    if vm.is_none(&spec) {
-        return false;
-    }
-    let Ok(initializing_attr) = spec.get_attr(&vm.ctx.new_str("_initializing"), vm) else {
-        return false;
-    };
-    initializing_attr.try_to_bool(vm).unwrap_or(false)
+    // Use internal initializing flag on PyModule
+    if let Ok(py_module) = module.downcast_ref::<PyModule>() {
+        use std::sync::atomic::Ordering;
+        return py_module.initializing.load(Ordering::Relaxed);
+    }
+    false
 }
🤖 Prompt for AI Agents
In vm/src/frame.rs around lines 2405 to 2416, refactor the
is_module_initializing function to remove the fallback logic that checks
__spec__._initializing. Instead, check if the given module is a PyModule and
directly return its initializing flag. For all other object types, return false.
This aligns with CPython's approach and simplifies the function by relying on
the internal initializing flag of PyModule.

Loading