]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-33591: Add support for path like objects to `ctypes.CDLL` (#7032)
authormrh1997 <robert.hoelzl@posteo.de>
Sun, 5 Feb 2023 17:36:57 +0000 (18:36 +0100)
committerGitHub <noreply@github.com>
Sun, 5 Feb 2023 17:36:57 +0000 (23:06 +0530)
Co-authored-by: Oleg Iarygin <oleg@arhadthedev.net>
Doc/library/ctypes.rst
Lib/ctypes/__init__.py
Lib/test/test_ctypes/test_loading.py
Misc/ACKS
Misc/NEWS.d/next/Library/2018-05-21-17-18-00.gh-issue-77772.Fhg84L.rst [new file with mode: 0644]

index 0642bbe9f99d0dbf2d1504ab910ade99ed879c70..8fd681286b812dd6ae3a0fa4c67fb2e459585d1a 100644 (file)
@@ -1380,6 +1380,10 @@ way is to instantiate one of the following classes:
    DLLs and determine which one is not found using Windows debugging and
    tracing tools.
 
+   .. versionchanged:: 3.12
+
+      The *name* parameter can now be a :term:`path-like object`.
+
 .. seealso::
 
     `Microsoft DUMPBIN tool <https://docs.microsoft.com/cpp/build/reference/dependents>`_
@@ -1398,6 +1402,10 @@ way is to instantiate one of the following classes:
    .. versionchanged:: 3.3
       :exc:`WindowsError` used to be raised.
 
+   .. versionchanged:: 3.12
+
+      The *name* parameter can now be a :term:`path-like object`.
+
 
 .. class:: WinDLL(name, mode=DEFAULT_MODE, handle=None, use_errno=False, use_last_error=False, winmode=None)
 
@@ -1405,6 +1413,10 @@ way is to instantiate one of the following classes:
    functions in these libraries use the ``stdcall`` calling convention, and are
    assumed to return :c:expr:`int` by default.
 
+   .. versionchanged:: 3.12
+
+      The *name* parameter can now be a :term:`path-like object`.
+
 The Python :term:`global interpreter lock` is released before calling any
 function exported by these libraries, and reacquired afterwards.
 
@@ -1418,6 +1430,10 @@ function exported by these libraries, and reacquired afterwards.
 
    Thus, this is only useful to call Python C api functions directly.
 
+   .. versionchanged:: 3.12
+
+      The *name* parameter can now be a :term:`path-like object`.
+
 All these classes can be instantiated by calling them with at least one
 argument, the pathname of the shared library.  If you have an existing handle to
 an already loaded shared library, it can be passed as the ``handle`` named
index 2e9d4c5e7238e9502cf4549334657d51d8dbb964..95353bab26cc71a3ae9a741c51338776c64e77a2 100644 (file)
@@ -344,6 +344,8 @@ class CDLL(object):
                  use_errno=False,
                  use_last_error=False,
                  winmode=None):
+        if name:
+            name = _os.fspath(name)
         self._name = name
         flags = self._func_flags_
         if use_errno:
index 15e365ed267d9b6f2f1a21559c236bb9fb286547..f2434926a51714ba69307a0a04cb66f577c58790 100644 (file)
@@ -28,10 +28,20 @@ class LoaderTest(unittest.TestCase):
     unknowndll = "xxrandomnamexx"
 
     def test_load(self):
-        if libc_name is None:
-            self.skipTest('could not find libc')
-        CDLL(libc_name)
-        CDLL(os.path.basename(libc_name))
+        if libc_name is not None:
+            test_lib = libc_name
+        else:
+            if os.name == "nt":
+                import _ctypes_test
+                test_lib = _ctypes_test.__file__
+            else:
+                self.skipTest('could not find library to load')
+        CDLL(test_lib)
+        CDLL(os.path.basename(test_lib))
+        class CTypesTestPathLikeCls:
+            def __fspath__(self):
+                return test_lib
+        CDLL(CTypesTestPathLikeCls())
         self.assertRaises(OSError, CDLL, self.unknowndll)
 
     def test_load_version(self):
index 74abcebe21ea6003b40982f35d277969f816857e..d27d60f5b3605e3e790ea77a7493ac7a64dea06a 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -754,6 +754,7 @@ Tim Hochberg
 Benjamin Hodgson
 Joerg-Cyril Hoehle
 Douwe Hoekstra
+Robert Hoelzl
 Gregor Hoffleit
 Chris Hoffman
 Tim Hoffmann
diff --git a/Misc/NEWS.d/next/Library/2018-05-21-17-18-00.gh-issue-77772.Fhg84L.rst b/Misc/NEWS.d/next/Library/2018-05-21-17-18-00.gh-issue-77772.Fhg84L.rst
new file mode 100644 (file)
index 0000000..3a7c6d4
--- /dev/null
@@ -0,0 +1,3 @@
+:class:`ctypes.CDLL`, :class:`ctypes.OleDLL`, :class:`ctypes.WinDLL`,
+and :class:`ctypes.PyDLL` now accept :term:`path-like objects
+<path-like object>` as their ``name`` argument. Patch by Robert Hoelzl.