GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-119349: Add ctypes.util function to list loaded shared libraries by WardBrian · Pull Request #122946 · python/cpython

Conversation

@WardBrian

@WardBrian WardBrian commented Aug 12, 2024 •
edited by github-actions Bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

The dllist() function calls platform-specific APIs in order to list the runtime libraries loaded by Python and any imported modules. If these fail for any reason, None is returned.

The name could surely be bikeshedded, at the moment it is taken from the Julia function of the same name

Previous discussions:

  • Add a ctypes.util function to list loaded shared libraries #119349
  • https://discuss.python.org/t/a-ctypes-function-to-list-all-loaded-shared-libraries/36370

Code based on https://github.com/WardBrian/dllist by myself with a contribution from @eryksun

  • Issue: Add a ctypes.util function to list loaded shared libraries #119349

📚 Documentation preview 📚: https://cpython-previews--122946.org.readthedocs.build/

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@ghost

ghost commented Aug 12, 2024 •
edited by ghost
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown

The following commit authors need to sign the Contributor License Agreement:

  • [email protected]

Click the button to sign:
CLA not signed

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot mentioned this pull request Aug 12, 2024
Add a ctypes.util function to list loaded shared libraries #119349
Closed
@bedevere-app bedevere-app Bot added the awaiting review label Aug 12, 2024
ZeroIntensity
ZeroIntensity reviewed Aug 13, 2024
View reviewed changes
Comment thread Doc/library/ctypes.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

ZeroIntensity
ZeroIntensity reviewed Aug 13, 2024
View reviewed changes
Comment thread Lib/ctypes/util.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

ZeroIntensity
ZeroIntensity reviewed Aug 13, 2024
View reviewed changes
Comment thread Lib/ctypes/util.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

@ZeroIntensity

ZeroIntensity commented Aug 13, 2024

Copy link
Copy Markdown
Member

I'm not a fan of returning None and emitting a warning instead of raising an error. That seems like a potential footgun; I could see developers being very confused as to why their code isn't working if they have warnings suppressed, or simply don't see them (e.g. they're buried in logs).

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

ZeroIntensity
ZeroIntensity reviewed Aug 13, 2024
View reviewed changes
Comment thread Lib/test/test_ctypes/test_dllist.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

ZeroIntensity
ZeroIntensity reviewed Aug 13, 2024
View reviewed changes
Comment thread Lib/test/test_ctypes/test_dllist.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

@WardBrian

WardBrian commented Aug 13, 2024

Copy link
Copy Markdown
Contributor Author

I'm not a fan of returning None and emitting a warning instead of raising an error. That seems like a potential footgun; I could see developers being very confused as to why their code isn't working if they have warnings suppressed, or simply don't see them (e.g. they're buried in logs).

Yes, what to do in the error case is certainly interesting. In my original implementation I returned [], but I can imagine a situation where the platform reports that there actually aren't any shared libraries (if you're in some MUSL, very static, environment, perhaps), so None seemed like a good distinction from [] and matched what ctypes.util.find_library() does

We certainly could let the error propagate, but there isn't much you can do as a user with the exceptions. Most use cases I can imagine would catch the error and then return something falsey, either None or [], and code that deals with this will need to handle that anyway.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ZeroIntensity

ZeroIntensity commented Aug 13, 2024

Copy link
Copy Markdown
Member

We certainly could let the error propagate, but there isn't much you can do as a user with the exceptions. Most use cases I can imagine would catch the error and then return something falsey, either None or [], and code that deals with this will need to handle that anyway.

I think we should offload that choice to the user.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@kailando

kailando commented Aug 13, 2024 via email

Copy link
Copy Markdown
It's a good way to show issues without flat-on taking down the (maybe server) program. Could you imagine that happening in GitHub's code storage server code? Kai Griswold
…
On Tue, Aug 13, 2024, 3:34 AM Peter Bierma ***@***.***> wrote: I'm not a fan of returning None and emitting a warning instead of raising an error. That seems like a potential footgun; I could see developers being very confused as to why their code isn't working if they have warnings suppressed, or simply don't see them (e.g. they're buried in logs). — Reply to this email directly, view it on GitHub <#122946 (comment)>, or unsubscribe <https://github.com/notifications/unsubscribe-auth/AWUL3JYBIXZW4JVUOVEW4S3ZRHAIPAVCNFSM6AAAAABMMPSC6SVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDEOBVGY3TGMRWHE> . You are receiving this because you are subscribed to this thread.Message ID: ***@***.***>
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ZeroIntensity

ZeroIntensity commented Aug 14, 2024

Copy link
Copy Markdown
Member

I am seeing that we're special-casing not having support for AIX. Does CPython even officially support AIX?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@WardBrian

WardBrian commented Aug 14, 2024

Copy link
Copy Markdown
Contributor Author

It's not listed in PEP 11, but there are ~150 other mentions of the platform in the codebase, mostly in test code, like the mentions here.

ZeroIntensity reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

picnixz
picnixz reviewed Aug 14, 2024
View reviewed changes

@picnixz picnixz left a comment

Copy link
Copy Markdown
Member

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'm not a fan of emitting warnings. Either you silently skip the failed names, or you raise. Or you return bytes and you won't have decoding issues for instance. But if you can list the DLLs being loaded, you should always return the complete list, and if you can't, then you should return None (other functions seem to do it as well when they can't get the result for whatever reason).

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Lib/ctypes/util.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Lib/ctypes/util.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Lib/ctypes/util.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Lib/ctypes/util.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Lib/ctypes/util.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Misc/NEWS.d/next/Library/2024-08-12-11-58-15.gh-issue-119349.-xTnHl.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Lib/test/test_ctypes/test_dllist.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Lib/test/test_ctypes/test_dllist.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Lib/test/test_ctypes/test_dllist.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Lib/ctypes/util.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

@WardBrian

WardBrian commented Aug 16, 2024

Copy link
Copy Markdown
Contributor Author

I'm not sure how long it takes to be reviewed and for the bot to update, but I wanted to note that my employer has filled out the CLA at this point

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

ZeroIntensity
ZeroIntensity reviewed Aug 16, 2024
View reviewed changes
Comment thread Lib/ctypes/util.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

ZeroIntensity
ZeroIntensity reviewed Aug 16, 2024
View reviewed changes
Comment thread Lib/ctypes/util.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

eryksun
eryksun reviewed Aug 16, 2024
View reviewed changes
Comment thread Lib/ctypes/util.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

@ZeroIntensity

ZeroIntensity commented Aug 16, 2024

Copy link
Copy Markdown
Member

Bikeshedding: if we're going to allow returning None in some cases, we should name this something like find_libraries, for consistency with the None return of find_library.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

ZeroIntensity
ZeroIntensity reviewed Aug 17, 2024
View reviewed changes
Comment thread Lib/ctypes/util.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

ZeroIntensity
ZeroIntensity reviewed Aug 27, 2024
View reviewed changes
Comment thread Doc/library/ctypes.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

@ghost

ghost commented Oct 4, 2024 •
edited by ghost
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.
CLA signed

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

WardBrian added 7 commits November 20, 2024 16:50
@WardBrian
Add function to list the currently loaded libraries to ctypes.util
d9c92c3
The dllist() function calls platform-specific APIs in order to
list the runtime libraries loaded by Python and any imported modules.
If these fail for any reason, None is returned.
@WardBrian
PEP-8 compilance
89a3c71
@WardBrian
Detect availability at runtime on non-Apple POSIX
c29b348
@WardBrian
use os.fsdecode for library paths
97d2eb1
@WardBrian
Test improvements
2e12e22
@WardBrian
Add What's New entry
6e18c36
@WardBrian
Remove warning code
2ce1cb9
encukou
encukou reviewed Feb 4, 2025
View reviewed changes
Comment thread Doc/library/ctypes.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Lib/ctypes/util.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

@encukou encukou added the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label Feb 4, 2025
@bedevere-bot

bedevere-bot commented Feb 4, 2025

Copy link
Copy Markdown

🤖 New build scheduled with the buildbot fleet by @encukou for commit 5ab1b02 🤖

Results will be shown at:

https://buildbot.python.org/all/#/grid?branch=refs%2Fpull%2F30617%2Fmerge

If you want to schedule another build, you need to add the 🔨 test-with-buildbots label again.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-bot bedevere-bot removed the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label Feb 4, 2025
encukou
encukou reviewed Feb 4, 2025
View reviewed changes
Comment thread Doc/library/ctypes.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

WardBrian added 5 commits February 4, 2025 09:55
@WardBrian
Merge branch 'main' into ctypes-util-list-libraries
ca0a8c6
@WardBrian
Update test code
94b1157
@WardBrian
Update documentation
5dc0ecd
@WardBrian
Lint fix
43ff300
@WardBrian
Fix test failing on RHEL8
80be2ab
On that platform, libc is called libc-2.28.so, not libc.so. Instead, the test searches for libffi.so, which is a dependency of ctypes
@WardBrian WardBrian changed the title gh-119349: Add function to list the currently loaded libraries to ctypes.util gh-119349: Add ctypes.util function to list loaded shared libraries Feb 4, 2025
@encukou encukou added the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label Feb 5, 2025
@bedevere-bot

bedevere-bot commented Feb 5, 2025

Copy link
Copy Markdown

🤖 New build scheduled with the buildbot fleet by @encukou for commit 80be2ab 🤖

Results will be shown at:

https://buildbot.python.org/all/#/grid?branch=refs%2Fpull%2F30617%2Fmerge

If you want to schedule another build, you need to add the 🔨 test-with-buildbots label again.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-bot bedevere-bot removed the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label Feb 5, 2025
@encukou

encukou commented Feb 5, 2025

Copy link
Copy Markdown
Member

Android has libc.so, but not libffi.so :(

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

ZeroIntensity
ZeroIntensity reviewed Feb 5, 2025
View reviewed changes

@ZeroIntensity ZeroIntensity left a comment

Copy link
Copy Markdown
Member

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.

Pretty much LGTM, apart from my many nitpicks. Looking forward to seeing this merged!

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Doc/library/ctypes.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Lib/test/test_ctypes/test_dllist.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Lib/test/test_ctypes/test_dllist.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Lib/test/test_ctypes/test_dllist.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Lib/ctypes/util.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Lib/ctypes/util.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Misc/NEWS.d/next/Library/2024-08-12-11-58-15.gh-issue-119349.-xTnHl.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Lib/test/test_ctypes/test_dllist.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Lib/test/test_ctypes/test_dllist.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Doc/library/ctypes.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

@mhsmith

mhsmith commented Feb 5, 2025

Copy link
Copy Markdown
Member

Android has libc.so, but not libffi.so :(

Android links ctypes against libffi statically. We could change this, but if it's only for this test, then it would be easier to just test for libc on Android instead.

encukou and WardBrian reacted with thumbs up emoji
All reactions
  • 👍 2 reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

WardBrian added 3 commits February 5, 2025 10:04
@WardBrian
test_dllist: check for one of several known libraries
e1dae92
This should cover all tested systems, including Andriod which statically links libffi
@WardBrian
test updates per review
cb22479
@WardBrian
Various documentation updates per reviews
1c3594e
@WardBrian

WardBrian commented Feb 5, 2025

Copy link
Copy Markdown
Contributor Author

Android has libc.so, but not libffi.so :(

Android links ctypes against libffi statically

I went ahead and updated the test to check if either libc.so or libffi.so is in the return on unix-alikes. Hopefully this guards against more platform issues, and we can always add a third candidate to this list if we end up with an even weirder platform that has neither...

mhsmith and encukou reacted with thumbs up emoji
All reactions
  • 👍 2 reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@encukou encukou added the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label Feb 6, 2025
@bedevere-bot

bedevere-bot commented Feb 6, 2025

Copy link
Copy Markdown

🤖 New build scheduled with the buildbot fleet by @encukou for commit 1c3594e 🤖

Results will be shown at:

https://buildbot.python.org/all/#/grid?branch=refs%2Fpull%2F30617%2Fmerge

If you want to schedule another build, you need to add the 🔨 test-with-buildbots label again.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-bot bedevere-bot removed the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label Feb 6, 2025
ZeroIntensity
ZeroIntensity approved these changes Feb 6, 2025
View reviewed changes

@ZeroIntensity ZeroIntensity left a comment

Copy link
Copy Markdown
Member

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! Thanks for doing this and for your patience with reviews, @WardBrian

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

WardBrian reacted with hooray emoji
All reactions
  • 🎉 1 reaction
@bedevere-app bedevere-app Bot added awaiting core review and removed awaiting review labels Feb 6, 2025
@encukou
encukou merged commit 421ea12 into python:main Feb 8, 2025
@bedevere-app bedevere-app Bot removed the awaiting core review label Feb 8, 2025
@WardBrian
WardBrian deleted the ctypes-util-list-libraries branch February 10, 2025 15:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@encukou encukou encukou left review comments
@eryksun eryksun eryksun left review comments
@picnixz picnixz picnixz left review comments
@ZeroIntensity ZeroIntensity ZeroIntensity approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

topic-ctypes

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

8 participants

@WardBrian @ZeroIntensity @kailando @bedevere-bot @encukou @mhsmith @eryksun @picnixz
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.