Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Fix ctypes.CDLL to honor handle parameter on POSIX systems
The handle parameter was being ignored in the POSIX implementation
of CDLL._load_library(), causing it to always call _dlopen() even
when a valid handle was provided. This was a regression introduced
in recent refactoring.

This commit adds the missing handle check to match the Windows
implementation behavior, and includes a regression test.

Fixes gh-143304
  • Loading branch information
Koolvansh07 committed Dec 31, 2025
commit 8634db59e345563daa3566ab72bb0dbcd61724d9
2 changes: 2 additions & 0 deletions Lib/ctypes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,8 @@ def _load_library(self, name, mode, handle, winmode):
if name and name.endswith(")") and ".a(" in name:
mode |= _os.RTLD_MEMBER | _os.RTLD_NOW
self._name = name
if handle is not None:
return handle
Comment thread
picnixz marked this conversation as resolved.
return _dlopen(name, mode)

def __repr__(self):
Expand Down
14 changes: 14 additions & 0 deletions Lib/test/test_ctypes/test_loading.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,20 @@ def test_load_without_name_and_with_handle(self):
lib = ctypes.WinDLL(name=None, handle=handle)
self.assertIs(handle, lib._handle)

@unittest.skipIf(os.name == "nt", 'POSIX-specific test')
def test_load_without_name_and_with_handle_posix(self):
# Test that CDLL honors the handle parameter on POSIX systems
# This is a regression test for gh-143304
if libc_name is None:
Comment thread
picnixz marked this conversation as resolved.
Outdated
self.skipTest('could not find libc')
# First load a library normally to get a handle
Comment thread
picnixz marked this conversation as resolved.
Outdated
lib1 = CDLL(libc_name)
handle = lib1._handle
# Now create a new CDLL instance with the same handle
lib2 = CDLL(name=None, handle=handle)
# The handle should be used directly, not ignored
self.assertIs(handle, lib2._handle)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
self.assertIs(handle, lib2._handle)
self.assertIs(lib2._handle, handle)

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).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I checked the 3.12 branch source code:

  1. Lib/ctypes/__init__.py in 3.12 DID support the handle parameter (it checked if handle is None:).
    1. Lib/test/test_ctypes/test_loading.py in 3.12 DID NOT have any test using CDLL(..., handle=...) on POSIX.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok thx!


@unittest.skipUnless(os.name == "nt", 'Windows-specific test')
def test_1703286_A(self):
# On winXP 64-bit, advapi32 loads at an address that does
Expand Down
Loading