]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-143304: Fix ctypes.CDLL to honor handle parameter on POSIX systems (GH-143318)
authorArjit Singh Grover <143692910+Koolvansh07@users.noreply.github.com>
Tue, 24 Feb 2026 12:16:58 +0000 (17:46 +0530)
committerGitHub <noreply@github.com>
Tue, 24 Feb 2026 12:16:58 +0000 (13:16 +0100)
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.

Lib/ctypes/__init__.py
Lib/test/test_ctypes/test_loading.py
Misc/NEWS.d/next/Library/2026-01-01-05-26-00.gh-issue-143304.Kv7x9Q.rst [new file with mode: 0644]

index aec92f3aee247213a426b7c32a4f8b888fcca721..1c822759eca9127241adf9d3d5c253152ed2da45 100644 (file)
@@ -458,6 +458,8 @@ class CDLL(object):
                 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
             return _dlopen(name, mode)
 
     def __repr__(self):
index 3b8332fbb30928fdcfca5eefc80aa69950fa13d9..343f6a07c0a32c4cd95ecfbb134f8a1aa1ba1ad8 100644 (file)
@@ -106,6 +106,14 @@ class LoaderTest(unittest.TestCase):
         lib = ctypes.WinDLL(name=None, handle=handle)
         self.assertIs(handle, lib._handle)
 
+    @unittest.skipIf(os.name == "nt", 'POSIX-specific test')
+    @unittest.skipIf(libc_name is None, 'could not find libc')
+    def test_load_without_name_and_with_handle_posix(self):
+        lib1 = CDLL(libc_name)
+        handle = lib1._handle
+        lib2 = CDLL(name=None, handle=handle)
+        self.assertIs(lib2._handle, handle)
+
     @unittest.skipUnless(os.name == "nt", 'Windows-specific test')
     def test_1703286_A(self):
         # On winXP 64-bit, advapi32 loads at an address that does
diff --git a/Misc/NEWS.d/next/Library/2026-01-01-05-26-00.gh-issue-143304.Kv7x9Q.rst b/Misc/NEWS.d/next/Library/2026-01-01-05-26-00.gh-issue-143304.Kv7x9Q.rst
new file mode 100644 (file)
index 0000000..826b2e9
--- /dev/null
@@ -0,0 +1 @@
+Fix :class:`ctypes.CDLL` to honor the ``handle`` parameter on POSIX systems.