Skip to content

Commit f7e9fba

Browse files
mrh1997arhadthedev
andauthored
bpo-33591: Add support for path like objects to ctypes.CDLL (#7032)
Co-authored-by: Oleg Iarygin <[email protected]>
1 parent 90d85a9 commit f7e9fba

File tree

5 files changed

+36
-4
lines changed

5 files changed

+36
-4
lines changed

Doc/library/ctypes.rst

+16
Original file line numberDiff line numberDiff line change
@@ -1380,6 +1380,10 @@ way is to instantiate one of the following classes:
13801380
DLLs and determine which one is not found using Windows debugging and
13811381
tracing tools.
13821382

1383+
.. versionchanged:: 3.12
1384+
1385+
The *name* parameter can now be a :term:`path-like object`.
1386+
13831387
.. seealso::
13841388

13851389
`Microsoft DUMPBIN tool <https://docs.microsoft.com/cpp/build/reference/dependents>`_
@@ -1398,13 +1402,21 @@ way is to instantiate one of the following classes:
13981402
.. versionchanged:: 3.3
13991403
:exc:`WindowsError` used to be raised.
14001404

1405+
.. versionchanged:: 3.12
1406+
1407+
The *name* parameter can now be a :term:`path-like object`.
1408+
14011409

14021410
.. class:: WinDLL(name, mode=DEFAULT_MODE, handle=None, use_errno=False, use_last_error=False, winmode=None)
14031411

14041412
Windows only: Instances of this class represent loaded shared libraries,
14051413
functions in these libraries use the ``stdcall`` calling convention, and are
14061414
assumed to return :c:expr:`int` by default.
14071415

1416+
.. versionchanged:: 3.12
1417+
1418+
The *name* parameter can now be a :term:`path-like object`.
1419+
14081420
The Python :term:`global interpreter lock` is released before calling any
14091421
function exported by these libraries, and reacquired afterwards.
14101422

@@ -1418,6 +1430,10 @@ function exported by these libraries, and reacquired afterwards.
14181430

14191431
Thus, this is only useful to call Python C api functions directly.
14201432

1433+
.. versionchanged:: 3.12
1434+
1435+
The *name* parameter can now be a :term:`path-like object`.
1436+
14211437
All these classes can be instantiated by calling them with at least one
14221438
argument, the pathname of the shared library. If you have an existing handle to
14231439
an already loaded shared library, it can be passed as the ``handle`` named

Lib/ctypes/__init__.py

+2
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,8 @@ def __init__(self, name, mode=DEFAULT_MODE, handle=None,
344344
use_errno=False,
345345
use_last_error=False,
346346
winmode=None):
347+
if name:
348+
name = _os.fspath(name)
347349
self._name = name
348350
flags = self._func_flags_
349351
if use_errno:

Lib/test/test_ctypes/test_loading.py

+14-4
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,20 @@ class LoaderTest(unittest.TestCase):
2828
unknowndll = "xxrandomnamexx"
2929

3030
def test_load(self):
31-
if libc_name is None:
32-
self.skipTest('could not find libc')
33-
CDLL(libc_name)
34-
CDLL(os.path.basename(libc_name))
31+
if libc_name is not None:
32+
test_lib = libc_name
33+
else:
34+
if os.name == "nt":
35+
import _ctypes_test
36+
test_lib = _ctypes_test.__file__
37+
else:
38+
self.skipTest('could not find library to load')
39+
CDLL(test_lib)
40+
CDLL(os.path.basename(test_lib))
41+
class CTypesTestPathLikeCls:
42+
def __fspath__(self):
43+
return test_lib
44+
CDLL(CTypesTestPathLikeCls())
3545
self.assertRaises(OSError, CDLL, self.unknowndll)
3646

3747
def test_load_version(self):

Misc/ACKS

+1
Original file line numberDiff line numberDiff line change
@@ -754,6 +754,7 @@ Tim Hochberg
754754
Benjamin Hodgson
755755
Joerg-Cyril Hoehle
756756
Douwe Hoekstra
757+
Robert Hoelzl
757758
Gregor Hoffleit
758759
Chris Hoffman
759760
Tim Hoffmann
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
:class:`ctypes.CDLL`, :class:`ctypes.OleDLL`, :class:`ctypes.WinDLL`,
2+
and :class:`ctypes.PyDLL` now accept :term:`path-like objects
3+
<path-like object>` as their ``name`` argument. Patch by Robert Hoelzl.

0 commit comments

Comments
 (0)