gh-131565: Implement ctypes.util.dllist() in the _ctypes extension - #154255
Conversation
On NetBSD dl_iterate_phdr() reports only the link-map group of the calling object. Called through ctypes, the caller is libffi's closure trampoline, which belongs to no object, so only the main executable was reported. Calling dl_iterate_phdr() from the _ctypes extension module makes _ctypes the caller and reports all loaded shared libraries. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
|
(Tagging Peter for his interest in ctypes.) |
|
This patch looks reasonable to me. I find the comments claude generates to be a bit too verbose, but that's personal preference. Nice find on the netbsd trampoline behavior. That's an edge case I never would have considered. Arguably the fact that this can't be implemented in pure python makes it's inclusion in the standard library even more useful |
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
|
Acquiring the GIL in the callback would reverse that order — the callback runs while the loader lock is held — and could deadlock against a thread which is in As for the return value, (This answer was written by Claude.) |
|
Let's add a comment then. It's unclear to me as to why |
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
ZeroIntensity
left a comment
There was a problem hiding this comment.
I'm not a huge fan of the PyErr_Occurred thing, but I won't block the PR on it. LGTM.
|
Thank you for your review. |
|
Thanks @serhiy-storchaka for the PR 🌮🎉.. I'm working now to backport this PR to: 3.14, 3.15. |
|
GH-154882 is a backport of this pull request to the 3.15 branch. |
|
GH-154883 is a backport of this pull request to the 3.14 branch. |
…sion (GH-154255) (GH-154882) On NetBSD dl_iterate_phdr() reports only the link-map group of the calling object. Called through ctypes, the caller is libffi's closure trampoline, which belongs to no object, so only the main executable was reported. Calling dl_iterate_phdr() from the _ctypes extension module makes _ctypes the caller and reports all loaded shared libraries. (cherry picked from commit bfc16a7) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com> Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
…sion (GH-154255) (GH-154883) On NetBSD dl_iterate_phdr() reports only the link-map group of the calling object. Called through ctypes, the caller is libffi's closure trampoline, which belongs to no object, so only the main executable was reported. Calling dl_iterate_phdr() from the _ctypes extension module makes _ctypes the caller and reports all loaded shared libraries. (cherry picked from commit bfc16a7) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com> Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
ctypes.util.dllist()returned only the main executable on NetBSD, because NetBSD'sdl_iterate_phdr()enumerates only the link-map group of the calling object. Called through ctypes, the caller is libffi's closure trampoline, which belongs to no object, so nothing else was reported (verified on NetBSD 10: a C helper.socallingdl_iterate_phdr()reports all objects, while the ctypes path reports 1).Implement
dllist()as a small C helper in the_ctypesextension, so the caller is_ctypesand all loaded shared libraries are reported. The pure-Pythondl_iterate_phdr()wrapper inctypes.utilis replaced byfrom _ctypes import dllist. Apple platforms keep their dyld-based implementation (the C helper is gated out there).Verified on NetBSD 10 (now 24 objects, was 1), Linux (20), OpenBSD 7 (23) and FreeBSD 15 (26);
test_dllistpasses on all four.Fixes the root cause investigated in gh-131565. CC @WardBrian
🤖 Generated with Claude Code