-
-
Notifications
You must be signed in to change notification settings - Fork 31.1k
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
Conversation
Remove "pycore_import.h" includes, no longer needed.
|
* Add Modules/_testlimitedcapi/import.c * Add Lib/test/test_capi/test_import.py
I wrote #128915 to add these tests first. |
Done, I added tests. |
There was a problem hiding this 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.
I created a C API Working Group decision issue: capi-workgroup/decisions#53 |
Lib/test/test_capi/test_import.py
Outdated
@@ -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) |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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")
.
I renamed the function to |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM! 👍
The C API Working Group approved the PyImport_ImportModuleAttr() API. |
Merged, thanks for reviews! |
) 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.
Remove "pycore_import.h" includes, no longer needed.
📚 Documentation preview 📚: https://cpython-previews--128912.org.readthedocs.build/