-
-
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
Add a ctypes.util function to list loaded shared libraries #119349
Comments
If anyone is subscribed to this issue, #122946 is still awaiting a review. I'd appreciate anyone taking a look! |
This would be a great addition! |
This should be considered a partial improvement related to jpy-consortium#75. A fuller solution might involve listing which shared objects were actually loaded via [`dl_iterate_phdr`](https://man7.org/linux/man-pages/man3/dl_iterate_phdr.3.html). python/cpython#119349
@encukou, this has been open for a while and I think we need someone to just make a decision on the API. The main point of contention has been what to do for errors; I'm in favor of raising an exception, but there's been concern that an exception wouldn't be particularly useful to the user, so a |
Thanks for the ping! Sorry that I missed the discussion here. The fact that a |
I think debugging is a valid use case, but there are a few others:
I also searched through GitHub for existing python code that uses this functionality (usually implemented in an ad-hoc way or only supporting 1 platform)
My personal sense is that all of these use cases already must have a case for if the library they are looking for is not found, and in many cases this case is identical to the desired behavior if lookup fails entirely. E.g., in my warning case, I just omit the warning, so I could write the code as something like |
From the zen:
I think this falls under that category. In userland, it's a lot easier to implement a wrapper that returns A reasonable compromise could be to add two functions: |
Julia's At any rate, I believe errors would be relatively rare. The following is basically a complete list of what the function could raise, if modified to do so:
I think many users would more or less be fine letting these percolate, and others could write a helper or just the more verbose version of my try:
loaded = dllist()
except Exception:
loaded = []
if plugin in loaded:
... So, where I'm at right now is having a slight preference towards returning None, but I'm not married to it. If one choice or the other makes the PR more appealing to merge, I'll jump |
Please raise an exception if the list can't be retrieved. Julia is a great inspiration, but in Python let's On a similar theme: the platform APIs include the executable as the first item in the list (sometimes as an empty string). The current PR removes it.
In that case, don't define the |
Thanks for taking a look @encukou. I've made those changes in my latest push, along with the more granular comments you left in the PR.
We can tell when the libc api we need is available during import, which the previous implementation was doing to just define a stub, so not defining it is easy |
Pinging maintainers of platforms we don't officially support: AIX: @edelsohn, @ayappanec |
Thanks for the notification. We will check whether this capability can be brought for AIX platform also. |
Other additional platform notes: Pyodide-based wasm targets could use some variant of |
Hi, I tested the patch on Solaris and everything works out of the box - (and I will add myself to the list :)) Thank you. |
Yay for Solaris! Emscripten: Looks like they're considering to add |
…-122946) Add function to list the currently loaded libraries to ctypes.util The dllist() function calls platform-specific APIs in order to list the runtime libraries loaded by Python and any imported modules. On unsupported platforms the function may be missing. Co-authored-by: Eryk Sun <[email protected]> Co-authored-by: Peter Bierma <[email protected]>
Merged. |
Thank you all for the discussion and careful review! This was my first time contributing to cpython but I hope not the last |
Feature or enhancement
Proposal:
When writing code which loads dynamic libraries, it is often very useful to be able to query which shared libraries are already in use by the current process. There are a few well-known tricks to do this, but they all end up being platform dependent.
For example, on Linux, you can use
dl_iterate_phdr
, and it seems that quite a bit of Python code does. This won’t work on macOS or Windows, which provide other functions for this same functionality.Julia provides this function in the standard library under
Libdl.dllist
.A Python re-implementation of the same platform-specific code can be found at GitHub - WardBrian/dllist: List DLLs loaded by the current process . This essentially just wraps the platform specific code in an if-else based on the runtime platform
I would like to take the next step toward adding a similar function to the standard library
Has this already been discussed elsewhere?
I have already discussed this feature proposal on Discourse
Links to previous discussion of this feature:
https://discuss.python.org/t/a-ctypes-function-to-list-all-loaded-shared-libraries/36370
Linked PRs
The text was updated successfully, but these errors were encountered: