Skip to content

Commit 3440ff9

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

3 files changed

Lines changed: 10 additions & 1 deletion

File tree

‎Doc/library/ctypes.rst‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1713,6 +1713,10 @@ These prefabricated library loaders are available:
17131713
:c:expr:`int`, which is of course not always the truth, so you have to assign
17141714
the correct :attr:`!restype` attribute to use these functions.
17151715

1716+
.. note::
1717+
1718+
If the Python interpreter is statically linked, this may be ``None``.
1719+
17161720
.. audit-event:: ctypes.dlopen name ctypes.LibraryLoader
17171721

17181722
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
@@ -553,7 +553,10 @@ def LoadLibrary(self, name):
553553
# These are Unix-like platforms which use a dynamically-linked libpython.
554554
pythonapi = PyDLL(_sysconfig.get_config_var("LDLIBRARY"))
555555
else:
556-
pythonapi = PyDLL(None)
556+
try:
557+
pythonapi = PyDLL(None)
558+
except OSError:
559+
pythonapi = None
557560

558561

559562
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)