-
-
Notifications
You must be signed in to change notification settings - Fork 34.5k
gh-143304: Fix ctypes.CDLL to honor handle parameter on POSIX systems #143318
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 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8634db5
Fix ctypes.CDLL to honor handle parameter on POSIX systems
Koolvansh07 bb4c92e
Add NEWS entry for gh-143304
Koolvansh07 d8b9ed3
Address review feedback
Koolvansh07 32eb34d
Move handle check to beginning to avoid unnecessary computation
Koolvansh07 6d95eff
Remove debug files that were accidentally committed
Koolvansh07 d3f65b6
Move handle check to end of function as suggested by reviewer
Koolvansh07 cdb8f6e
Update test assertion to match (actual, expected) convention
Koolvansh07 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
Address review feedback
- Remove AI-generated comments from test - Use skipIf decorator instead of runtime check - Simplify NEWS entry (don't mention private _dlopen)
- Loading branch information
commit d8b9ed3686cdb4b9486a16ab0814b8a43fe1567b
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
2 changes: 1 addition & 1 deletion
2
Misc/NEWS.d/next/Library/2026-01-01-05-26-00.gh-issue-143304.Kv7x9Q.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 |
|---|---|---|
| @@ -1 +1 @@ | ||
| Fix :class:`ctypes.CDLL` to honor the ``handle`` parameter on POSIX systems. The parameter was being ignored, causing the library to always call ``_dlopen()`` even when a valid handle was provided. | ||
| Fix :class:`ctypes.CDLL` to honor the ``handle`` parameter on POSIX systems. |
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,51 @@ | ||
| # Demonstration of the fix for gh-143304 | ||
| # This shows the before and after behavior | ||
|
|
||
| print("=" * 60) | ||
| print("BEFORE THE FIX (main branch):") | ||
| print("=" * 60) | ||
| print(""" | ||
| def _load_library(self, name, mode, handle, winmode): | ||
| # ... iOS/tvOS/watchOS .fwork handling ... | ||
| # ... AIX archive handling ... | ||
| self._name = name | ||
| return _dlopen(name, mode) # ❌ handle parameter is IGNORED! | ||
| """) | ||
|
|
||
| print("\n" + "=" * 60) | ||
| print("AFTER THE FIX (our branch):") | ||
| print("=" * 60) | ||
| print(""" | ||
| def _load_library(self, name, mode, handle, winmode): | ||
| # ... iOS/tvOS/watchOS .fwork handling ... | ||
| # ... AIX archive handling ... | ||
| self._name = name | ||
| if handle is not None: # ✅ Check if handle was provided | ||
| return handle # ✅ Use the provided handle | ||
| return _dlopen(name, mode) # Only call _dlopen if no handle | ||
| """) | ||
|
|
||
| print("\n" + "=" * 60) | ||
| print("COMPARISON WITH WINDOWS VERSION:") | ||
| print("=" * 60) | ||
| print(""" | ||
| Windows _load_library (already correct): | ||
| self._name = name | ||
| if handle is not None: | ||
| return handle | ||
| return _LoadLibrary(self._name, winmode) | ||
|
|
||
| POSIX _load_library (now fixed to match): | ||
| self._name = name | ||
| if handle is not None: # ← This was missing! | ||
| return handle # ← This was missing! | ||
| return _dlopen(name, mode) | ||
| """) | ||
|
|
||
| print("\n" + "=" * 60) | ||
| print("VERIFICATION:") | ||
| print("=" * 60) | ||
| print("✓ The fix adds 2 lines to the POSIX implementation") | ||
| print("✓ The fix matches the Windows implementation pattern") | ||
| print("✓ The fix allows users to pass an existing handle to CDLL") | ||
| print("✓ The fix prevents unnecessary _dlopen() calls when handle is provided") |
|
picnixz marked this conversation as resolved.
Outdated
|
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,25 @@ | ||
| import ctypes | ||
| import sys | ||
| import os | ||
|
|
||
| print(f"OS name: {os.name}") | ||
| print(f"Platform: {sys.platform}") | ||
|
|
||
| if os.name == "nt": | ||
| print("\nTesting Windows WinDLL...") | ||
| try: | ||
| # Get a handle from kernel32 | ||
| handle = ctypes.windll.kernel32._handle | ||
| print(f"Got kernel32 handle: {handle}") | ||
|
|
||
| # Try to create a new WinDLL with that handle | ||
| print("Creating WinDLL with name=None and handle...") | ||
| lib = ctypes.WinDLL(name=None, handle=handle) | ||
| print(f"Success! Created lib with handle: {lib._handle}") | ||
| print(f"Handles match: {handle == lib._handle}") | ||
| except Exception as e: | ||
| print(f"Error: {type(e).__name__}: {e}") | ||
| import traceback | ||
| traceback.print_exc() | ||
| else: | ||
| print("Not on Windows, skipping test") |
Empty file.
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.
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.
Can you check whether there were tests for Python 3.12 where we had this handle? or if the tests were rewritten as well (see the PRs mentioned on the issue).
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 checked the
3.12branch source code:Lib/ctypes/__init__.pyin 3.12 DID support thehandleparameter (it checkedif handle is None:).Lib/test/test_ctypes/test_loading.pyin 3.12 DID NOT have any test usingCDLL(..., handle=...)on POSIX.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.
Ok thx!