Skip to content
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

[needs-design]Can we let macro generate function to append module to init table? #2359

Closed
herquan opened this issue May 5, 2022 · 2 comments · Fixed by #2377
Closed

[needs-design]Can we let macro generate function to append module to init table? #2359

herquan opened this issue May 5, 2022 · 2 comments · Fixed by #2377

Comments

@herquan
Copy link
Contributor

herquan commented May 5, 2022

Hi,
In order to debug module sometimes we need to run a python script inside the rust environment. One way to do this is through calling function PyImport_AppendInittab before pyo3::prepare_freethreaded_python(). We don't have this in pyo3 directly but one workaround would be adding such piece of code

pub extern "C" fn pyinit() -> *mut ::pyo3::ffi::PyObject {
	unsafe { PyInit_module() }
}
pub fn append_inittab(){
	unsafe {
		pyo3::ffi::PyImport_AppendInittab("module\0".as_ptr() as *const std::os::raw::c_char, Option::Some(pyinit));
	}
}

And I'm thinking of adding such support in macro pymodule with similar code like

pub fn append_inittab_wrapper(){
    unsafe {
        #krate::ffi::PyImport_AppendInittab(concat!(stringify!(#name), "\0").as_ptr() as *const std::os::raw::c_char, Option::Some(pyinit));
    }
}

Meanwhile make sure PyImport_AppendInittab contains unsafe so this function can be reused. One thing I'm not sure is how to let the rust project that uses pyo3 recognize the append_inittab_wrapper without guessing the function name.
Please let me know if there is any concern for this or if there is any better idea?

@herquan herquan changed the title Can we let macro generate function to append module to init table? [needs-design]Can we let macro generate function to append module to init table? May 5, 2022
@davidhewitt
Copy link
Member

I would be ok with having a safe function PyModule::append_to_inittab(). It would probably make sense for it to panic if Py_IsInitialized() returns true.

@davidhewitt
Copy link
Member

davidhewitt commented May 8, 2022

Actually maybe this would need to a a macro pyo3::append_to_inittab!(my_module) which uses the same kind of tricks done in wrap_pymodule. PRs to experiment with this are welcome!

herquan added a commit to herquan/pyo3 that referenced this issue May 9, 2022
Sometimes we need to debug in a real environment with our module installed. `append_to_inittab` will be a wrapper for PyImport_AppendInittab (https://docs.python.org/3/c-api/import.html#c.PyImport_AppendInittab) and help us to do this
herquan added a commit to herquan/pyo3 that referenced this issue May 9, 2022
Sometimes we need to debug in a real environment with our module installed. `append_to_inittab` will be a wrapper for PyImport_AppendInittab (https://docs.python.org/3/c-api/import.html#c.PyImport_AppendInittab) and help us to do this
herquan added a commit to herquan/pyo3 that referenced this issue May 10, 2022
Sometimes we need to debug in a real environment with our module installed. `append_to_inittab` will be a wrapper for PyImport_AppendInittab (https://docs.python.org/3/c-api/import.html#c.PyImport_AppendInittab) and help us to do this
@davidhewitt davidhewitt linked a pull request May 24, 2022 that will close this issue
davidhewitt added a commit that referenced this issue May 24, 2022
Add macro append_to_inittab (issue #2359)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants