You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[C API] Add PyImport_ImportModuleAttr(mod_name, attr_name) helper function #128911
Python has an internal _PyImport_GetModuleAttrString(mod_name, attr_name) helper function to import a module and get a module attribute. I propose to make this function public to be able to use it outside Python.
UPDATE: Function renamed to PyImport_ImportModuleAttrString().
The function is convenient to use and is used by the following files in Python:
This sounds like a useful addition. The fact that the internal API is already used a lot in CPython is evidence enough 😄
And BTW: I like that you're starting to approach C API design from the perspective of a C programmer again.
Rich C APIs make it a joy programming against them, while minimalist APIs only result in others creating additional layers on top, which adds fragmentation, overall increased maintenance and loss of design control. 👍
Activity
pythongh-128911: Add PyImport_GetModuleAttrString() function
encukou commentedon Jan 16, 2025
IMO, the
*Stringfunctions should generally have variants that takePyObject*.So, if we add this we should also add
PyImport_GetModuleAttr.pythongh-128911: Add tests on the PyImport C API
pythongh-128911: Add tests on the PyImport C API
vstinner commentedon Jan 16, 2025
Ok, I added PyImport_GetModuleAttr() to my PR.
gh-128911: Add tests on the PyImport C API (#128915)
pythongh-128911: Add tests on the PyImport C API (python#128915)
pythongh-128911: Add tests on the PyImport C API (python#128915)
[3.13] gh-128911: Add tests on the PyImport C API (#128915) (#128960)
[3.12] [3.13] pythongh-128911: Add tests on the PyImport C API (pytho…
[3.12] gh-128911: Add tests on the PyImport C API (GH-128915) (GH-128960
malemburg commentedon Jan 20, 2025
This sounds like a useful addition. The fact that the internal API is already used a lot in CPython is evidence enough 😄
And BTW: I like that you're starting to approach C API design from the perspective of a C programmer again.
Rich C APIs make it a joy programming against them, while minimalist APIs only result in others creating additional layers on top, which adds fragmentation, overall increased maintenance and loss of design control. 👍
pythongh-128911: Add tests on the PyImport C API (python#128915)
[-][C API] Add PyImport_GetModuleAttrString(mod_name, attr_name) helper function[/-][+][C API] Add PyImport_ImportModuleAttr(mod_name, attr_name) helper function[/+]vstinner commentedon Jan 27, 2025
See also #129367 which proposes adding
PySys_GetAttr().gh-128911: Add PyImport_ImportModuleAttr() function (#128912)
pythongh-128911: Use PyImport_ImportModuleAttr() function
gh-128911: Use PyImport_ImportModuleAttr() function (#129657)
pythongh-128911: Add PyImport_ImportModuleAttr() function (python#128912
pythongh-128911: Use PyImport_ImportModuleAttr() function (python#129657
pythongh-128911: Use PyImport_ImportModuleAttr() function (python#129657