FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-128911: Add PyImport_ImportModuleAttr() function by vstinner · Pull Request #128912 · python/cpython · GitHub

/ cpython Public

gh-128911: Add PyImport_ImportModuleAttr() function - #128912

Merged
vstinner merged 11 commits into
python:mainfrom
vstinner:import_getattr
Jan 30, 2025
Merged

gh-128911: Add PyImport_ImportModuleAttr() function#128912
vstinner merged 11 commits into
python:mainfrom
vstinner:import_getattr

Conversation

vstinner commented Jan 16, 2025
edited by github-actions Bot
Loading

Copy link
Copy Markdown
Member

Remove "pycore_import.h" includes, no longer needed.


📚 Documentation preview 📚: https://cpython-previews--128912.org.readthedocs.build/

Remove "pycore_import.h" includes, no longer needed.

vstinner commented Jan 16, 2025
edited
Loading

Copy link
Copy Markdown
Member Author

TODO: Write tests. But PyImport C API is currently not tested, we need maybe to first write tests for existing functions?

vstinner added a commit to vstinner/cpython that referenced this pull request Jan 16, 2025
* Add Modules/_testlimitedcapi/import.c
* Add Lib/test/test_capi/test_import.py

Copy link
Copy Markdown
Member Author

But PyImport C API is currently not tested, we need maybe to first write tests for existing functions?

I wrote #128915 to add these tests first.

vstinner requested a review from a team as a code owner January 17, 2025 21:17

Copy link
Copy Markdown
Member Author

TODO: Write tests.

Done, I added tests.

Comment thread Python/import.c Outdated

corona10 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

looks good to me.

Comment thread Lib/test/test_capi/test_import.py Outdated
# CRASHES execute_code_func(name, NULL, NULL, NULL)

def check_getmoduleattr(self, getmoduleattr):
self.assertIs(getmoduleattr('sys', 'argv'), sys.argv)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Check also a module name containing dots.

Check errors:

  • non-existent module
  • relative module name
  • non-existent attribute
  • attribute name containing dots

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

I added requested tests.

attribute name containing dots

getmoduleattr('sys', 'implementation.name') raises an AttributeError. I prefer to not implement "nested" getattr(). One should write getmoduleattr('sys', 'implementation') + PyObject_GetAttrString(attr, "name").

Comment thread Modules/_testcapi/import.c Outdated

Copy link
Copy Markdown
Member Author

I renamed the function to PyImport_ImportModuleAttr().

vstinner changed the title gh-128911: Add PyImport_GetModuleAttrString() function gh-128911: Add PyImport_ImportModuleAttr() function Jan 26, 2025

serhiy-storchaka left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

LGTM! 👍

Copy link
Copy Markdown
Member Author

I created a C API Working Group decision issue: capi-workgroup/decisions#53

The C API Working Group approved the PyImport_ImportModuleAttr() API.

vstinner enabled auto-merge (squash) January 30, 2025 11:12
vstinner merged commit 3bebe46 into python:main Jan 30, 2025
vstinner deleted the import_getattr branch January 30, 2025 11:17

Copy link
Copy Markdown
Member Author

Merged, thanks for reviews!

srinivasreddy pushed a commit to srinivasreddy/cpython that referenced this pull request Feb 7, 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.
vfazio added a commit to vfazio/libgpiod that referenced this pull request Apr 9, 2026
Commit 035ad74 introduced Py_gpiod_GetModuleAttrString as a copy of
an internal function used by CPython to get module attributes.

CPython 3.14 added PyImport_ImportModuleAttrString to the public API
which allows us to use the standard function when available.

[0]: python/cpython#128912

Signed-off-by: Vincent Fazio <vfazio@gmail.com>
vfazio added a commit to vfazio/libgpiod that referenced this pull request Apr 9, 2026
Commit 035ad74 introduced Py_gpiod_GetModuleAttrString as a copy of
an internal function used by CPython to get module attributes.

CPython 3.14 added PyImport_ImportModuleAttrString to the public API
which allows us to use the standard function when available.

[0]: python/cpython#128912

Signed-off-by: Vincent Fazio <vfazio@gmail.com>
vfazio added a commit to vfazio/libgpiod that referenced this pull request Apr 9, 2026
Commit 035ad74 introduced Py_gpiod_GetModuleAttrString as a copy of
an internal function used by CPython to get module attributes.

CPython 3.14 added PyImport_ImportModuleAttrString to the public API
which allows us to use the standard function when available.

[0]: python/cpython#128912

Signed-off-by: Vincent Fazio <vfazio@gmail.com>
vfazio added a commit to vfazio/libgpiod that referenced this pull request Apr 9, 2026
Commit 035ad74 introduced Py_gpiod_GetModuleAttrString as a copy of
an internal function used by CPython to get module attributes.

CPython 3.14 added PyImport_ImportModuleAttrString to the public API
which allows us to use the standard function when available.

[0]: python/cpython#128912

Signed-off-by: Vincent Fazio <vfazio@gmail.com>
vfazio added a commit to vfazio/libgpiod that referenced this pull request Apr 11, 2026
Commit 035ad74 introduced Py_gpiod_GetModuleAttrString as a copy of
an internal function used by CPython to get module attributes.

CPython 3.14 added PyImport_ImportModuleAttrString to the public API
which allows us to use the standard function when available.

[0]: python/cpython#128912

Signed-off-by: Vincent Fazio <vfazio@gmail.com>
vfazio added a commit to vfazio/libgpiod that referenced this pull request Apr 13, 2026
Commit 035ad74 introduced Py_gpiod_GetModuleAttrString as a way to
get a module's attribute and was inspired by an internal function used
by CPython to get module attributes (PyImport_ImportModuleAttrString).

CPython 3.14 added PyImport_ImportModuleAttrString to the public API
which allows us to use the standard function when available.

[0]: python/cpython#128912

Signed-off-by: Vincent Fazio <vfazio@gmail.com>
vfazio added a commit to vfazio/libgpiod that referenced this pull request Apr 14, 2026
Commit 035ad74 introduced Py_gpiod_GetModuleAttrString as a way to
get a module's attribute and was inspired by an internal function used
by CPython to get module attributes (PyImport_ImportModuleAttrString).

CPython 3.14 added PyImport_ImportModuleAttrString to the public API
which allows us to use the standard function when available.

[0]: python/cpython#128912

Signed-off-by: Vincent Fazio <vfazio@gmail.com>
vfazio added a commit to vfazio/libgpiod that referenced this pull request Apr 21, 2026
Commit 035ad74 introduced Py_gpiod_GetModuleAttrString as a way to
get a module's attribute and was inspired by an internal function used
by CPython to get module attributes (PyImport_ImportModuleAttrString).

CPython 3.14 added PyImport_ImportModuleAttrString to the public API
which allows us to use the standard function when available.

[0]: python/cpython#128912

Signed-off-by: Vincent Fazio <vfazio@gmail.com>
vfazio added a commit to vfazio/libgpiod that referenced this pull request Apr 23, 2026
Commit 035ad74 introduced Py_gpiod_GetModuleAttrString as a way to
get a module's attribute and was inspired by CPython's internal function
_PyImport_GetModuleAttrString.

CPython 3.14 added PyImport_ImportModuleAttrString to the public API [0]
which allows us to use the standard function when available.

[0]: python/cpython#128912

Signed-off-by: Vincent Fazio <vfazio@gmail.com>
vfazio added a commit to vfazio/libgpiod that referenced this pull request Apr 23, 2026
Commit 035ad74 introduced Py_gpiod_GetModuleAttrString as a way to
get a module's attribute and was inspired by CPython's internal function
_PyImport_GetModuleAttrString.

CPython 3.14 added PyImport_ImportModuleAttrString to the public API [0]
which allows us to use the standard function when available.

[0]: python/cpython#128912

Signed-off-by: Vincent Fazio <vfazio@gmail.com>
vfazio added a commit to vfazio/libgpiod that referenced this pull request Apr 23, 2026
Commit 035ad74 introduced Py_gpiod_GetModuleAttrString as a way to
get a module's attribute and was inspired by CPython's internal function
_PyImport_GetModuleAttrString.

CPython 3.14 added PyImport_ImportModuleAttrString to the public API [0]
which allows us to use the standard function when available.

[0]: python/cpython#128912

Signed-off-by: Vincent Fazio <vfazio@gmail.com>
brgl pushed a commit to brgl/libgpiod that referenced this pull request Apr 24, 2026
Commit 035ad74 introduced Py_gpiod_GetModuleAttrString() as a way to
get a module's attribute and was inspired by CPython's internal function
_PyImport_GetModuleAttrString().

CPython 3.14 added PyImport_ImportModuleAttrString() to the public
API [0] which allows us to use the standard function when available.

[0]: python/cpython#128912

Signed-off-by: Vincent Fazio <vfazio@gmail.com>
Link: https://patch.msgid.link/20260423222125.29097-6-vfazio@gmail.com
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants


Back | FazBrowse Home | New Git URL