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

[C API] Add PyImport_ImportModuleAttr(mod_name, attr_name) helper function #128911

Closed
vstinner opened this issue Jan 16, 2025 · 4 comments
Closed
Labels
topic-C-API type-feature A feature request or enhancement

Comments

@vstinner
Copy link
Member

vstinner commented Jan 16, 2025

Feature or enhancement

Proposal:

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:

  • Modules/arraymodule.c
  • Modules/cjkcodecs/cjkcodecs.h
  • Modules/_ctypes/callbacks.c
  • Modules/_datetimemodule.c
  • Modules/_decimal/_decimal.c
  • Modules/_elementtree.c
  • Modules/faulthandler.c
  • Modules/_lsprof.c
  • Modules/_operator.c
  • Modules/_pickle.c
  • Modules/posixmodule.c
  • Modules/selectmodule.c
  • Modules/_sqlite/connection.c
  • Modules/_sqlite/module.c
  • Modules/_sre/sre.c
  • Modules/timemodule.c
  • Modules/_zoneinfo.c
  • Objects/abstract.c
  • Objects/fileobject.c
  • Objects/memoryobject.c
  • Parser/pegen.c
  • Parser/tokenizer/file_tokenizer.c
  • Python/import.c
  • Python/pylifecycle.c

Has this already been discussed elsewhere?

No response given

Links to previous discussion of this feature:

No response

Linked PRs

@vstinner vstinner added topic-C-API type-feature A feature request or enhancement labels Jan 16, 2025
vstinner added a commit to vstinner/cpython that referenced this issue Jan 16, 2025
Remove "pycore_import.h" includes, no longer needed.
@encukou
Copy link
Member

encukou commented Jan 16, 2025

IMO, the *String functions should generally have variants that take PyObject*.
So, if we add this we should also add PyImport_GetModuleAttr.

vstinner added a commit to vstinner/cpython that referenced this issue Jan 16, 2025
* Add Modules/_testlimitedcapi/import.c
* Add Lib/test/test_capi/test_import.py
vstinner added a commit to vstinner/cpython that referenced this issue Jan 16, 2025
* Add Modules/_testlimitedcapi/import.c
* Add Lib/test/test_capi/test_import.py
* Remove _testcapi.check_pyimport_addmodule(): tests already covered
  by newly added tests.
@vstinner
Copy link
Member Author

So, if we add this we should also add PyImport_GetModuleAttr.

Ok, I added PyImport_GetModuleAttr() to my PR.

vstinner added a commit that referenced this issue Jan 17, 2025
* Add Modules/_testlimitedcapi/import.c
* Add Lib/test/test_capi/test_import.py
* Remove _testcapi.check_pyimport_addmodule(): tests already covered
  by newly added tests.

Co-authored-by: Serhiy Storchaka <[email protected]>
vstinner added a commit to vstinner/cpython that referenced this issue Jan 17, 2025
* Add Modules/_testlimitedcapi/import.c
* Add Lib/test/test_capi/test_import.py
* Remove _testcapi.check_pyimport_addmodule(): tests already covered
  by newly added tests.

Co-authored-by: Serhiy Storchaka <[email protected]>
(cherry picked from commit d95ba9f)
vstinner added a commit to vstinner/cpython that referenced this issue Jan 17, 2025
* Add Modules/_testlimitedcapi/import.c
* Add Lib/test/test_capi/test_import.py
* Remove _testcapi.check_pyimport_addmodule(): tests already covered
  by newly added tests.

Co-authored-by: Serhiy Storchaka <[email protected]>
(cherry picked from commit d95ba9f)
vstinner added a commit that referenced this issue Jan 17, 2025
gh-128911: Add tests on the PyImport C API (#128915)

* Add Modules/_testlimitedcapi/import.c
* Add Lib/test/test_capi/test_import.py
* Remove _testcapi.check_pyimport_addmodule(): tests already covered
  by newly added tests.

Co-authored-by: Serhiy Storchaka <[email protected]>
(cherry picked from commit d95ba9f)
serhiy-storchaka pushed a commit to serhiy-storchaka/cpython that referenced this issue Jan 18, 2025
…nGH-128915) (pythonGH-128960)

pythongh-128911: Add tests on the PyImport C API (pythonGH-128915)

* Add Modules/_testlimitedcapi/import.c
* Add Lib/test/test_capi/test_import.py
* Remove _testcapi.check_pyimport_addmodule(): tests already covered
  by newly added tests.
(cherry picked from commit 34ded1a)

Co-authored-by: Victor Stinner <[email protected]>
Co-authored-by: Serhiy Storchaka <[email protected]>
(cherry picked from commit d95ba9f)
vstinner pushed a commit that referenced this issue Jan 19, 2025
) (#128989)

* Add Modules/_testlimitedcapi/import.c
* Add Lib/test/test_capi/test_import.py
* Remove _testcapi.check_pyimport_addmodule(): tests already covered
  by newly added tests.
(cherry picked from commit 34ded1a)

Co-authored-by: Victor Stinner <[email protected]>
Co-authored-by: Serhiy Storchaka <[email protected]>
(cherry picked from commit d95ba9f)
@malemburg
Copy link
Member

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. 👍

srinivasreddy pushed a commit to srinivasreddy/cpython that referenced this issue Jan 21, 2025
* Add Modules/_testlimitedcapi/import.c
* Add Lib/test/test_capi/test_import.py
* Remove _testcapi.check_pyimport_addmodule(): tests already covered
  by newly added tests.

Co-authored-by: Serhiy Storchaka <[email protected]>
@vstinner vstinner changed the title [C API] Add PyImport_GetModuleAttrString(mod_name, attr_name) helper function [C API] Add PyImport_ImportModuleAttr(mod_name, attr_name) helper function Jan 26, 2025
@vstinner
Copy link
Member Author

See also #129367 which proposes adding PySys_GetAttr().

vstinner added a commit that referenced this issue Jan 30, 2025
Add PyImport_ImportModuleAttr() and
PyImport_ImportModuleAttrString() functions.

* Add unit tests.
* Replace _PyImport_GetModuleAttr()
  with PyImport_ImportModuleAttr().
* Replace _PyImport_GetModuleAttrString()
  with PyImport_ImportModuleAttrString().
* Remove "pycore_import.h" includes, no longer needed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic-C-API type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

3 participants