A `ctypes` function to list all loaded shared libraries

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

Does something like this fit in the ctypes.util module of the standard library?

1 Like

It might. I’d suggest seeing how popular the dllist module is as a package on PyPI. If it gets used a lot, then that would be a good argument in support of proposing it gets added to the stdlib.

1 Like

This could be pretty useful for extension module developers. I like it.

1 Like

I have made the module available on PyPI.

I do think a lot of people who need this functionality may already have ad-hoc implementations themselves, so are unlikely to switch to a library by some random developer (e.g., me), but would likely use a version in a standard library. We will see, I suppose!

I’m assuming that if it goes into the stdlib, you’d be creating the PR to add it - so either way, it would be the same code written by the same random developer :wink:

But I get your point. Being in the stdlib adds a level of credibility and convenience. It’s possible that if you just submit the function as a PR, one of the core devs would be happy to approve and merge it (I won’t do so myself, simply because I don’t have enough involvement with ctypes to feel comfortable merging changes to it). Being a popular module on PyPI isn’t necessary, it’s just helpful (if the proposal needs supporting evidence).