-
-
Notifications
You must be signed in to change notification settings - Fork 33.2k
gh-119349: Add ctypes.util function to list loaded shared libraries #122946
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
Merged
Merged
Changes from all commits
Commits
Show all changes
33 commits
Select commit
Hold shift + click to select a range
d9c92c3
Add function to list the currently loaded libraries to ctypes.util
WardBrian 89a3c71
PEP-8 compilance
WardBrian c29b348
Detect availability at runtime on non-Apple POSIX
WardBrian 97d2eb1
use os.fsdecode for library paths
WardBrian 2e12e22
Test improvements
WardBrian 6e18c36
Add What's New entry
WardBrian 2ce1cb9
Remove warning code
WardBrian de2c048
Lint fix
WardBrian 8728393
Avoid editing cached versions of symbols
WardBrian dd97ba0
use ctypes.WinError in _get_module_handles
WardBrian 297c71e
Format WinError message
WardBrian a059573
Remove unnecessary error handling
WardBrian 586f37c
Tighten except statement
WardBrian efb7c1c
Mention reasoning for lack of support in doc
WardBrian 4aba7d6
Merge branch 'main' into ctypes-util-list-libraries
WardBrian fc54910
Lint fix
WardBrian ab739d2
Merge branch 'main' into ctypes-util-list-libraries
WardBrian 1626a25
Merge branch 'main' into ctypes-util-list-libraries
WardBrian a23b9c1
Raise if list cannot be produced
WardBrian 58edf0f
Stop removing first entry
WardBrian c91849e
Set argtypes on dl_iterate_phdr call
WardBrian 9c351d1
Leave dllist undefined if platform doesn't support it
WardBrian eedf35e
Documentation updates
WardBrian 3918675
Move availability above versionadded
WardBrian 5ab1b02
Update to work on musl systems
WardBrian ca0a8c6
Merge branch 'main' into ctypes-util-list-libraries
WardBrian 94b1157
Update test code
WardBrian 5dc0ecd
Update documentation
WardBrian 43ff300
Lint fix
WardBrian 80be2ab
Fix test failing on RHEL8
WardBrian e1dae92
test_dllist: check for one of several known libraries
WardBrian cb22479
test updates per review
WardBrian 1c3594e
Various documentation updates per reviews
WardBrian File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| import os | ||
| import sys | ||
| import unittest | ||
| from ctypes import CDLL | ||
| import ctypes.util | ||
| from test.support import import_helper | ||
|
|
||
|
|
||
| WINDOWS = os.name == "nt" | ||
| APPLE = sys.platform in {"darwin", "ios", "tvos", "watchos"} | ||
WardBrian marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| if WINDOWS: | ||
| KNOWN_LIBRARIES = ["KERNEL32.DLL"] | ||
| elif APPLE: | ||
| KNOWN_LIBRARIES = ["libSystem.B.dylib"] | ||
| else: | ||
| # trickier than it seems, because libc may not be present | ||
| # on musl systems, and sometimes goes by different names. | ||
| # However, ctypes itself loads libffi | ||
| KNOWN_LIBRARIES = ["libc.so", "libffi.so"] | ||
|
|
||
|
|
||
| @unittest.skipUnless( | ||
| hasattr(ctypes.util, "dllist"), | ||
| "ctypes.util.dllist is not available on this platform", | ||
| ) | ||
| class ListSharedLibraries(unittest.TestCase): | ||
|
|
||
| def test_lists_system(self): | ||
| dlls = ctypes.util.dllist() | ||
|
|
||
| self.assertGreater(len(dlls), 0, f"loaded={dlls}") | ||
| self.assertTrue( | ||
| any(lib in dll for dll in dlls for lib in KNOWN_LIBRARIES), f"loaded={dlls}" | ||
| ) | ||
|
|
||
| def test_lists_updates(self): | ||
| dlls = ctypes.util.dllist() | ||
|
|
||
| # this test relies on being able to import a library which is | ||
| # not already loaded. | ||
| # If it is (e.g. by a previous test in the same process), we skip | ||
| if any("_ctypes_test" in dll for dll in dlls): | ||
| self.skipTest("Test library is already loaded") | ||
|
|
||
| _ctypes_test = import_helper.import_module("_ctypes_test") | ||
| test_module = CDLL(_ctypes_test.__file__) | ||
| dlls2 = ctypes.util.dllist() | ||
| self.assertIsNotNone(dlls2) | ||
|
|
||
| dlls1 = set(dlls) | ||
| dlls2 = set(dlls2) | ||
|
|
||
WardBrian marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| self.assertGreater(dlls2, dlls1, f"newly loaded libraries: {dlls2 - dlls1}") | ||
| self.assertTrue(any("_ctypes_test" in dll for dll in dlls2), f"loaded={dlls2}") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1992,6 +1992,7 @@ Edward C Wang | |
| Jiahua Wang | ||
| Ke Wang | ||
| Liang-Bo Wang | ||
| Brian Ward | ||
| Greg Ward | ||
| Tom Wardill | ||
| Zachary Ware | ||
|
|
||
2 changes: 2 additions & 0 deletions
2
Misc/NEWS.d/next/Library/2024-08-12-11-58-15.gh-issue-119349.-xTnHl.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| Add the :func:`ctypes.util.dllist` function to list the loaded shared | ||
| libraries for the current process. |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.