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

How to register a built-in extension module? #354

Closed
vadimcn opened this issue Feb 13, 2019 · 5 comments
Closed

How to register a built-in extension module? #354

vadimcn opened this issue Feb 13, 2019 · 5 comments
Labels

Comments

@vadimcn
Copy link

vadimcn commented Feb 13, 2019

I have a Rust app that hosts Python interpreter. I want to register a built-in module so that Python code can call back into the app.
Not sure how to convert #[pymodule] fn ..., into something I can give to PyImport_AppendInittab... Also, I need to support both Python 2 and 3, and it looks like I can't use wrap_pymodule! with Python2?

Would you mind providing an example?

@kngwyu
Copy link
Member

kngwyu commented Feb 18, 2019

I made it https://github.com/kngwyu/pyo3/blob/append-init-tab-example/examples/word-count/src/main.rs

But honestly I don't know when we should use PyImport_AppendInittab and feel this function difficult.
I also wrote an example of exposing function via locals(example2) and think it's enough for most cases.
Could you please let me know about your use case more concretely?

@konstin
Copy link
Member

konstin commented Feb 18, 2019

Sidenote: It's normally better to use PyModule::new() instead of make_module because make_module is not part of the public api (it's marked as #[doc(hidden)])

Also, I need to support both Python 2 and 3, and it looks like I can't use wrap_pymodule! with Python2?

Yes, wrap_pymodule! currently only supports python 3, because python 2 support would required some work on the python2 part which is going to be removed soon anyway.

@vadimcn
Copy link
Author

vadimcn commented Feb 19, 2019

@kngwyu: PyImport_AppendInittab is typically used when a module only makes sense in the context of an application (for example if Python is used for scripting application logic), so module code is linked statically to the main app binary.

The solution I can up with is this:

unsafe {
    #[cfg(Py_2)]
    let module_init = initmymodule;
    #[cfg(Py_3)]
    let module_init = PyInit_mymodule;
    pyo3::ffi::PyImport_AppendInittab("mymodule\0".as_ptr() as *const c_char, Some(module_init));
}

...but I was wondering if there's a more elegant way to do it..

because python 2 support would required some work on the python2 part which is going to be removed soon anyway.

I hope not before 1/1/2020?

@konstin
Copy link
Member

konstin commented Feb 19, 2019

I hope not before 1/1/2020?

No, definitely before. See https://python3statement.org/

@davidhewitt
Copy link
Member

As there has been no discussion on this question for some time, I'm going to close it. Thank you for the discussion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants