Skip to content

Commit 8634db5

Browse files
committed
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
1 parent c521597 commit 8634db5

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

Lib/ctypes/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,8 @@ def _load_library(self, name, mode, handle, winmode):
458458
if name and name.endswith(")") and ".a(" in name:
459459
mode |= _os.RTLD_MEMBER | _os.RTLD_NOW
460460
self._name = name
461+
if handle is not None:
462+
return handle
461463
return _dlopen(name, mode)
462464

463465
def __repr__(self):

Lib/test/test_ctypes/test_loading.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,20 @@ def test_load_without_name_and_with_handle(self):
106106
lib = ctypes.WinDLL(name=None, handle=handle)
107107
self.assertIs(handle, lib._handle)
108108

109+
@unittest.skipIf(os.name == "nt", 'POSIX-specific test')
110+
def test_load_without_name_and_with_handle_posix(self):
111+
# Test that CDLL honors the handle parameter on POSIX systems
112+
# This is a regression test for gh-143304
113+
if libc_name is None:
114+
self.skipTest('could not find libc')
115+
# First load a library normally to get a handle
116+
lib1 = CDLL(libc_name)
117+
handle = lib1._handle
118+
# Now create a new CDLL instance with the same handle
119+
lib2 = CDLL(name=None, handle=handle)
120+
# The handle should be used directly, not ignored
121+
self.assertIs(handle, lib2._handle)
122+
109123
@unittest.skipUnless(os.name == "nt", 'Windows-specific test')
110124
def test_1703286_A(self):
111125
# On winXP 64-bit, advapi32 loads at an address that does

0 commit comments

Comments
 (0)