| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
| Back | FazBrowse Home | New Git URL |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality💡 Verification agent
🧩 Analysis chainHelper 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:
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:
💡 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:
Summary Table
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.
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 }Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.