https://github.com/python/cpython/commit/3440ff9fd15a8bc4b3aa7d45dd43cebe065f6cfd
commit: 3440ff9fd15a8bc4b3aa7d45dd43cebe065f6cfd
branch: 3.15
author: Miss Islington (bot) <[email protected]>
committer: serhiy-storchaka <[email protected]>
date: 2026-10-10T13:36:31+03:00
summary:

[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 5637f4e38a68cd015daa25cce91e902c01c1279b)

Co-authored-by: Peter Bierma <[email protected]>

files:
A Misc/NEWS.d/next/Library/2026-07-18-04-09-44.gh-issue-81241.gGRbBr.rst
M Doc/library/ctypes.rst
M Lib/ctypes/__init__.py

diff --git a/Doc/library/ctypes.rst b/Doc/library/ctypes.rst
index 53b05cfd66f2596..cc573936e4f4a67 100644
--- a/Doc/library/ctypes.rst
+++ b/Doc/library/ctypes.rst
@@ -1713,6 +1713,10 @@ These prefabricated library loaders are available:
       :c:expr:`int`, which is of course not always the truth, so you have to 
assign
       the correct :attr:`!restype` attribute to use these functions.
 
+      .. note::
+
+         If the Python interpreter is statically linked, this may be ``None``.
+
 .. audit-event:: ctypes.dlopen name ctypes.LibraryLoader
 
    Loading a library through any of these objects raises an
diff --git a/Lib/ctypes/__init__.py b/Lib/ctypes/__init__.py
index 0b84a27f8c6d613..0aebd5877d4a9dc 100644
--- a/Lib/ctypes/__init__.py
+++ b/Lib/ctypes/__init__.py
@@ -553,7 +553,10 @@ def LoadLibrary(self, name):
     # These are Unix-like platforms which use a dynamically-linked libpython.
     pythonapi = PyDLL(_sysconfig.get_config_var("LDLIBRARY"))
 else:
-    pythonapi = PyDLL(None)
+    try:
+        pythonapi = PyDLL(None)
+    except OSError:
+        pythonapi = None
 
 
 if _os.name == "nt":
diff --git 
a/Misc/NEWS.d/next/Library/2026-07-18-04-09-44.gh-issue-81241.gGRbBr.rst 
b/Misc/NEWS.d/next/Library/2026-07-18-04-09-44.gh-issue-81241.gGRbBr.rst
new file mode 100644
index 000000000000000..6110b7404a811e4
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2026-07-18-04-09-44.gh-issue-81241.gGRbBr.rst
@@ -0,0 +1,2 @@
+Fix :exc:`OSError` when importing :mod:`ctypes` from a statically linked
+interpreter.

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to