On Sat, 23 Feb 2019 at 05:34, Stefan Behnel python_capi@behnel.de wrote:
It really shouldn't be that hard to do these things.
Agreed. Something we may want to consider is exposing C APIs corresponding more directly to the Python level syntax.
Absolute imports (this already exists):
"import module.ref as m" -> m = PyImport_Import("module.ref")
Submodule-or-attribute imports (currently requires making your own fromlist, and dealing with circular imports yourself):
"from module import ref as m" -> m = PyImport_FromModule("module", "ref")
Relative imports (currently requires messing with both fromlist and level, and dealing with circular imports yourself):
"from .module import ref as m" -> m =
PyImport_FromRelativeModule(0, "module", "ref") "from ..module import ref as m" -> m = PyImport_FromRelativeModule(1, "module", "ref") "from . import module.ref as m" -> m = PyImport_FromRelativeModule(0, NULL, "module.ref") "from .. import module.ref as m" -> m = PyImport_FromRelativeModule(1, NULL, "module.ref")
The preferred parameter orders could be debated, but I've picked the ones above so that the left-to-right order matches between the Python code and the C API call.
And those would also then have "*Object" variants that accepted Python objects instead of C strings.
Cheers, Nick.