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_GetModuleAttrString(mod_name, attr_name) helper function #128911

Open
vstinner opened this issue Jan 16, 2025 · 2 comments
Open
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.

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)
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

2 participants