Skip to content

Commit e3d8d3b

Browse files
[3.13] gh-81241: Fix importing ctypes from statically linked interpreters (GH-153890) (GH-158306)
See astral-sh/python-build-standalone#873. (cherry picked from commit 5637f4e) Co-authored-by: Peter Bierma <zintensitydev@gmail.com>
1 parent 08e590e commit e3d8d3b

3 files changed

Lines changed: 9 additions & 1 deletion

File tree

‎Doc/library/ctypes.rst‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1625,6 +1625,9 @@ object is available:
16251625
:c:expr:`int`, which is of course not always the truth, so you have to assign
16261626
the correct :attr:`!restype` attribute to use these functions.
16271627

1628+
Note that if the Python interpreter is statically linked, this will be
1629+
``None``, as ``dlopen`` is not possible in this case.
1630+
16281631
.. audit-event:: ctypes.dlopen name ctypes.LibraryLoader
16291632

16301633
Loading a library through any of these objects raises an

‎Lib/ctypes/__init__.py‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,10 @@ def LoadLibrary(self, name):
492492
# These are Unix-like platforms which use a dynamically-linked libpython.
493493
pythonapi = PyDLL(_sysconfig.get_config_var("LDLIBRARY"))
494494
else:
495-
pythonapi = PyDLL(None)
495+
try:
496+
pythonapi = PyDLL(None)
497+
except OSError:
498+
pythonapi = None
496499

497500

498501
if _os.name == "nt":
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix :exc:`OSError` when importing :mod:`ctypes` from a statically linked
2+
interpreter.

0 commit comments

Comments
 (0)