From python, if I want symbols from another module for example, I do:
from os.path import *
How is this done using the python c api? I've read through the docs here: https://docs.python.org/2/c-api/import.html But it isn't obvious how to do this.
I'm trying to import the symbols into a module I created using Py_InitModule().
There is no C API function for that. You have two options: punt to the Python API, or update your namespace manually.
If you want to punt to the Python API, you'd use something like PyRun_String to run from os.path import * with your module's __dict__ as globals and locals. It'd look something like this:
PyObject *borrowed_dict = PyModule_GetDict(your_module);
PyObject *ret = PyRun_String("from os.path import *",
Py_file_input,
borrowed_dict,
borrowed_dict);
if (!ret) {
// Error in import *!
// Appropriate response is context-dependent.
do_something_about_that();
}
Py_XDECREF(ret);
If you want to update your namespace manually, you should respect the imported module's __all__ list, or skip names with a leading underscore if there is no __all__ list. The Python-level way of doing it manually would be
if hasattr(module, '__all__'):
all_names = module.__all__
else:
all_names = [name for name in dir(module) if not name.startswith('_')]
globals().update({name: getattr(module, name) for name in all_names})
so translate that into the C API: retrieve the imported module's __all__ list, or build a default __all__ if the module doesn't provide one, then update your module's globals with the corresponding globals from the imported module.
It might help to look at how CPython itself implements the namespace updating part of import *.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With