Skip to content

Commit b4024e7

Browse files
committed
Allow pythonapi dlopen to fail
Static binaries cannot use dlopen, so the Python API will not be accessible in ctypes.
1 parent 2f4c02f commit b4024e7

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

Lib/ctypes/__init__.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -464,13 +464,16 @@ def LoadLibrary(self, name):
464464
cdll = LibraryLoader(CDLL)
465465
pydll = LibraryLoader(PyDLL)
466466

467-
if _os.name == "nt":
468-
pythonapi = PyDLL("python dll", None, _sys.dllhandle)
469-
elif _sys.platform == "cygwin":
470-
pythonapi = PyDLL("libpython%d.%d.dll" % _sys.version_info[:2])
471-
else:
472-
pythonapi = PyDLL(None)
473-
467+
# dlopen is not supported in statically linked programs.
468+
try:
469+
if _os.name == "nt":
470+
pythonapi = PyDLL("python dll", None, _sys.dllhandle)
471+
elif _sys.platform == "cygwin":
472+
pythonapi = PyDLL("libpython%d.%d.dll" % _sys.version_info[:2])
473+
else:
474+
pythonapi = PyDLL(None)
475+
except OSError:
476+
pythonapi = None
474477

475478
if _os.name == "nt":
476479
windll = LibraryLoader(WinDLL)

0 commit comments

Comments
 (0)