Skip to content

Commit 27ded24

Browse files
authored
gh-143304: Fix ctypes.CDLL to honor handle parameter on POSIX systems (GH-143318)
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.
1 parent d6a71f4 commit 27ded24

File tree

3 files changed

+11
-0
lines changed

3 files changed

+11
-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: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,14 @@ 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+
@unittest.skipIf(libc_name is None, 'could not find libc')
111+
def test_load_without_name_and_with_handle_posix(self):
112+
lib1 = CDLL(libc_name)
113+
handle = lib1._handle
114+
lib2 = CDLL(name=None, handle=handle)
115+
self.assertIs(lib2._handle, handle)
116+
109117
@unittest.skipUnless(os.name == "nt", 'Windows-specific test')
110118
def test_1703286_A(self):
111119
# On winXP 64-bit, advapi32 loads at an address that does
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix :class:`ctypes.CDLL` to honor the ``handle`` parameter on POSIX systems.

0 commit comments

Comments
 (0)