-
Notifications
You must be signed in to change notification settings - Fork 804
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Add bindings for PyImport_AddModuleRef and use it in Python::run_code #4529
Conversation
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.
Thanks!
src/marker.rs
Outdated
@@ -634,14 +634,15 @@ impl<'py> Python<'py> { | |||
locals: Option<&Bound<'py, PyDict>>, | |||
) -> PyResult<Bound<'py, PyAny>> { | |||
unsafe { | |||
let mptr = ffi::PyImport_AddModule(ffi::c_str!("__main__").as_ptr()); | |||
let mptr = ffi::compat::PyImport_AddModuleRef(ffi::c_str!("__main__").as_ptr()); |
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.
It'd be better to use Bound
here (same for the other raw pointers in this function...)
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.
👍 on avoiding the manual reference counting. This can change to
let mptr = ffi::compat::PyImport_AddModuleRef(ffi::c_str!("__main__").as_ptr()); | |
let mptr = ffi::compat::PyImport_AddModuleRef(ffi::c_str!("__main__").as_ptr()).assume_owned_or_err(py)?; |
It looks like PyModule_GetDict
below returns a borrowed reference. Is that problematic under freethreading? I assume so, do we need to call and incref under a critical section? It doesn't look like there is a PyModule_GetDictRef
. Should we just do .getattr("__dict__")
?
Similarly it looks like PyEval_GetBuiltins
is deprecated in 3.13, so I guess we need to update that in a follow up...
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.
Good call about PyModule_GetDict
. I wonder if we don't actually need that on free-threaded builds since we don't need to update globals
on Python 3.10 and newer. I'll see if we can avoid it with conditional compilation.
I think we could also probably use PyModule_AddObjectRef
or a compat definition based on PyModule_AddObject
instead.
Last push refactors the |
f0cc2b1
to
8fe6f40
Compare
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.
Thanks, that's brilliant 👍
Refs #4265.