-
Notifications
You must be signed in to change notification settings - Fork 776
New issue
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
Accessing the module object in pyfunction
s
#828
Comments
I'm nominating this for |
I'm taking a dive into this, although I have no idea what I'm getting into. I'll let you know if I'm getting stuck or if I don't think I'm the right person for this. |
❤️ Many thanks! I see two options:
I think I slightly prefer the |
Note: you might also need to consider |
I had a first go at the implementation, and we need to make some changes to the ffi-calls for function creation. Currently the pyo3::ffi::PyCFunction_New(
Box::into_raw(Box::new(_def.as_method_def())),
::std::ptr::null_mut(),
),
[...]
pub unsafe fn PyCFunction_New(ml: *mut PyMethodDef, slf: *mut PyObject) -> *mut PyObject {
#[cfg_attr(PyPy, link_name = "PyPyCFunction_NewEx")]
PyCFunction_NewEx(ml, slf, ptr::null_mut())
} This is the signature of PyObject *
PyCFunction_NewEx(PyMethodDef *ml, PyObject *self, PyObject *module)
{
return PyCMethod_New(ml, self, module, NULL);
} So we're setting
The above steps also provide the facilities to set this attribute, all that's left at that point is to get the module name from the let name = unsafe { pyo3::ffi::PyModule_GetNameObject(m.as_ptr()) }; And passing the method_def, the module pointer and the name object to I have only implemented this manually based on expanded code, I haven't touched the proc-macros yet. I hope to find some time to start writing a PR later today or tomorrow! |
It looks like a lot of tests rely on "free" functions, i.e. not associated with any So, what do you think about adding a |
I don't think that we can apply the same heuristics used for To fix this is, we could require that the first argument on a non-free The other way I see is to look for a clean way to communicate which
I'll probably get around to open a WIP PR tonight which includes the FFI & proc-macro changes up to this point. The implementation of passing the |
In the C API, module-level functions get passed their registered module object as the
self
parameter. This is very nice to interact with other module level API.Can we (optionally?) make the module reference available in
pyfunction
s too?The text was updated successfully, but these errors were encountered: