Skip to content

Conversation

@vstinner
Copy link
Member

@vstinnervstinner 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
MemberAuthor

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
MemberAuthor

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.

@vstinnervstinner requested a review from a team as a code ownerJanuary 17, 2025 21:17
@vstinner
Copy link
MemberAuthor

TODO: Write tests.

Done, I added tests.

Copy link
Member

@corona10corona10 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
MemberAuthor

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

# CRASHES execute_code_func(name, NULL, NULL, NULL)

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

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
MemberAuthor

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

@vstinner
Copy link
MemberAuthor

I renamed the function to PyImport_ImportModuleAttr().

@vstinnervstinner changed the title gh-128911: Add PyImport_GetModuleAttrString() functiongh-128911: Add PyImport_ImportModuleAttr() functionJan 26, 2025
Copy link
Member

@serhiy-storchakaserhiy-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
MemberAuthor

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

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

@vstinnervstinner enabled auto-merge (squash) January 30, 2025 11:12
@vstinnervstinner merged commit 3bebe46 into python:mainJan 30, 2025
41 checks passed
@vstinnervstinner deleted the import_getattr branch January 30, 2025 11:17
@vstinner
Copy link
MemberAuthor

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

@vstinner@serhiy-storchaka@corona10@erlend-aasland