From 47f0473b8cb76336c35a41b25fa438ef4ce234d3 Mon Sep 17 00:00:00 2001 From: Peter Bierma Date: Sun, 27 Sep 2026 20:26:46 -0400 Subject: [PATCH 1/2] gh-81241: Fix importing `ctypes` from statically linked interpreters (GH-153890) See astral-sh/python-build-standalone#873. (cherry picked from commit 5637f4e38a68cd015daa25cce91e902c01c1279b) Co-authored-by: Peter Bierma --- Doc/library/ctypes.rst | 3 +++ Lib/ctypes/__init__.py | 5 ++++- .../Library/2026-07-18-04-09-44.gh-issue-81241.gGRbBr.rst | 2 ++ 3 files changed, 9 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2026-07-18-04-09-44.gh-issue-81241.gGRbBr.rst diff --git a/Doc/library/ctypes.rst b/Doc/library/ctypes.rst index da1aaa89fef7c74..39cfb6ed00f1666 100644 --- a/Doc/library/ctypes.rst +++ b/Doc/library/ctypes.rst @@ -1704,6 +1704,9 @@ 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 that if the Python interpreter is statically linked, this will be + ``None``, as ``dlopen`` is not possible in this case. + .. 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. From 9f4fce3439e25008abba5eca9e2b904e2c4d985f Mon Sep 17 00:00:00 2001 From: Peter Bierma Date: Sun, 4 Oct 2026 17:09:32 -0400 Subject: [PATCH 2/2] Docs: Clarify that `ctypes.pythonapi` *may* be `None` under a statically linked interpreter (GH-158624) --- Doc/library/ctypes.rst | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Doc/library/ctypes.rst b/Doc/library/ctypes.rst index 39cfb6ed00f1666..018d673fa2bc024 100644 --- a/Doc/library/ctypes.rst +++ b/Doc/library/ctypes.rst @@ -1704,8 +1704,9 @@ 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 that if the Python interpreter is statically linked, this will be - ``None``, as ``dlopen`` is not possible in this case. + .. note:: + + If the Python interpreter is statically linked, this may be ``None``. .. audit-event:: ctypes.dlopen name ctypes.LibraryLoader