-
Notifications
You must be signed in to change notification settings - Fork 1
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
Add PyImport_ImportModuleAttr(mod_name, attr_name) helper function #53
Comments
On the one hand: yes, this sure sounds useful! On the other hand, the fact that we need a helper to wrap a sequence of a few function calls shows that the C-API has some issues with composability. The error handling is too verbose. Should we make these |
Why not I don't think there's anything wrong with the composability of the existing APIs - this is the kind of thing I do all the time in my native code - but I certainly wouldn't refuse a helper function. |
I'm fine with either names. I have no preference. |
+1 to
This would make sense if done consistently for a larger subset of the API. No point in doing it for two functions -- that would just be a weird special case to document. +1 for adding these. |
Ok, I renamed the functions to I would prefer a regular function rather than a static inline function. Static inline functions cannot be used in Rust or C projects only loading symbols such as the |
Ping @mdboom and @serhiy-storchaka. |
I am the one who introduced these functions in python/cpython#93742. Currently they are used 40 times in the CPython code, this is almost a half of uses of other PyImport functions combined. So we can suppose that they can be useful in third-party code. On other hand, this is not a fundamental C API. It can be easily implemented using existing C API. Large projects perhaps already implemented a helper for this, and small projects only need it one or two times. There is also one question which I avoided while it was merely a private helper. This function is not equivalent to Python code There is also a great inconsistency in naming conventions of |
IMO it's convenient to get an AttributeError if the attribute doesn't exist.
What do you think of the current documentation?
|
The documentation does it right. I asked to be sure that this is an intended behavior and not an artifact of the current implementation. It will be difficult to change after release. |
The C API Working Group approved the |
Python has a convenient 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. I also propose to make its variant_PyImport_GetModuleAttr(mod_name, attr_name)
public (which takes objects rather than strings).API:
API:
For example,
PyImport_ImportModuleAttrString("sys", "argv")
getssys.argv
. It's similar tofrom sys import argv
in Python.The function is used by 24 C files in Python.
Links:
Vote:
UPDATE: Rename the functions from
PyImport_GetModuleAttr[String]()
toPyImport_ImportModuleAttr[String]()
.The text was updated successfully, but these errors were encountered: