Skip to content

Commit 31b2f53

Browse files
committed
Handle statically linked libpython more gracefully
1 parent b921e42 commit 31b2f53

4 files changed

Lines changed: 18 additions & 12 deletions

File tree

‎pythonnet/__init__.py‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def load():
2626
if _LOADED:
2727
return
2828

29-
from .find_libpython import find_libpython
29+
from .find_libpython import linked_libpython
3030
from os.path import join, dirname
3131

3232
if _RUNTIME is None:
@@ -35,10 +35,9 @@ def load():
3535
set_default_runtime()
3636

3737
dll_path = join(dirname(__file__), "runtime", "Python.Runtime.dll")
38-
libpython = find_libpython()
39-
# TODO: Add dirname of libpython to (DY)LD_LIBRARY_PATH or PATH
38+
libpython = linked_libpython()
4039

41-
if _FFI is None and sys.platform != "win32":
40+
if libpython and _FFI is None and sys.platform != "win32":
4241
# Load and leak libpython handle s.t. the .NET runtime doesn't dlcloses
4342
# it
4443
import posix
@@ -50,7 +49,7 @@ def load():
5049
_LOADER_ASSEMBLY = _RUNTIME.get_assembly(dll_path)
5150

5251
func = _LOADER_ASSEMBLY["Python.Runtime.Loader.Initialize"]
53-
if func(f"{libpython}".encode("utf8")) != 0:
52+
if func(f"{libpython or ''}".encode("utf8")) != 0:
5453
raise RuntimeError("Failed to initialize Python.Runtime.dll")
5554

5655
import atexit

‎pythonnet/find_libpython/__init__.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,11 +171,11 @@ def candidate_names(suffix=SHLIB_SUFFIX):
171171
Candidate name libpython.
172172
"""
173173
LDLIBRARY = sysconfig.get_config_var("LDLIBRARY")
174-
if LDLIBRARY:
174+
if LDLIBRARY and not LDLIBRARY.endswith(".a"):
175175
yield LDLIBRARY
176176

177177
LIBRARY = sysconfig.get_config_var("LIBRARY")
178-
if LIBRARY:
178+
if LIBRARY and not LIBRARY.endswith(".a"):
179179
yield os.path.splitext(LIBRARY)[0] + suffix
180180

181181
dlprefix = "" if is_windows else "lib"

‎src/domain_tests/test_domain_reload.py‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44

55
import pytest
66

7+
from pythonnet.find_libpython import find_libpython
8+
libpython = find_libpython()
9+
10+
pytestmark = pytest.mark.xfail(libpython is None, reason="Can't find suitable libpython")
11+
12+
713
def _run_test(testname):
814
dirname = os.path.split(__file__)[0]
915
exename = os.path.join(dirname, 'bin', 'Python.DomainReloadTests.exe')
@@ -12,9 +18,6 @@ def _run_test(testname):
1218
if platform.system() != 'Windows':
1319
args = ['mono'] + args
1420

15-
from pythonnet.find_libpython import find_libpython
16-
libpython = find_libpython()
17-
1821
env = os.environ.copy()
1922
env["PYTHONNET_PYDLL"] = libpython
2023

‎src/runtime/loader.cs‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,12 @@ public static int Initialize(IntPtr data, int size)
1919
Marshal.Copy(data, buf, 0, size);
2020
var dllPath = Encoding.UTF8.GetString(buf);
2121

22-
PythonDLL = dllPath;
23-
// PythonDLL = null;
22+
if (!string.IsNullOrEmpty(dllPath)) {
23+
PythonDLL = dllPath;
24+
}
25+
else {
26+
PythonDLL = null;
27+
}
2428

2529
gs = PyGILState_Ensure();
2630

0 commit comments

Comments
 (0)