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

gh-128911: Add PyImport_ImportModuleAttr() function #128912

Merged
merged 11 commits into from
Jan 30, 2025

Conversation

vstinner
Copy link
Member

@vstinner vstinner commented Jan 16, 2025

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
Copy link
Member Author

vstinner commented Jan 16, 2025

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
@vstinner
Copy link
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 vstinner requested a review from a team as a code owner January 17, 2025 21:17
@vstinner
Copy link
Member Author

TODO: Write tests.

Done, I added tests.

Python/import.c Outdated Show resolved Hide resolved
Copy link
Member

@corona10 corona10 left a comment

Choose a reason for hiding this comment

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

looks good to me.

@vstinner
Copy link
Member Author

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

@@ -317,6 +318,44 @@ def test_executecodemoduleobject(self):
# CRASHES execute_code_func(NULL, code, NULL, NULL)
# CRASHES execute_code_func(name, NULL, NULL, NULL)

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

Choose a reason for hiding this comment

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

Check also a module name containing dots.

Check errors:

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

Copy link
Member Author

Choose a reason for hiding this comment

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

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

Modules/_testcapi/import.c Outdated Show resolved Hide resolved
@vstinner
Copy link
Member Author

I renamed the function to PyImport_ImportModuleAttr().

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

@serhiy-storchaka serhiy-storchaka left a comment

Choose a reason for hiding this comment

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

LGTM! 👍

@vstinner
Copy link
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 vstinner enabled auto-merge (squash) January 30, 2025 11:12
@vstinner vstinner merged commit 3bebe46 into python:main Jan 30, 2025
41 checks passed
@vstinner vstinner deleted the import_getattr branch January 30, 2025 11:17
@vstinner
Copy link
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.
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