Skip to content

Commit cd2e408

Browse files
committed
Add _linked_libpython_windows
1 parent 47f4a06 commit cd2e408

File tree

2 files changed

+22
-10
lines changed

2 files changed

+22
-10
lines changed

julia/find_libpython.py

+21-3
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,13 @@ def linked_libpython():
3333
"""
3434
Find the linked libpython using dladdr (in *nix).
3535
36-
Calling this in Windows always return `None` at the moment.
37-
3836
Returns
3937
-------
4038
path : str or None
4139
A path to linked libpython. Return `None` if statically linked.
4240
"""
4341
if is_windows:
44-
return None
42+
return _linked_libpython_windows()
4543
return _linked_libpython_unix()
4644

4745

@@ -71,6 +69,26 @@ def _linked_libpython_unix():
7169
return path
7270

7371

72+
def _linked_libpython_windows():
73+
"""
74+
Based on: https://stackoverflow.com/a/16659821
75+
"""
76+
from ctypes.wintypes import HANDLE, LPWSTR, DWORD
77+
78+
GetModuleFileName = ctypes.windll.kernel32.GetModuleFileNameW
79+
GetModuleFileName.argtypes = [HANDLE, LPWSTR, DWORD]
80+
GetModuleFileName.restype = DWORD
81+
82+
MAX_PATH = 260
83+
try:
84+
buf = ctypes.create_unicode_buffer(MAX_PATH)
85+
GetModuleFileName(ctypes.pythonapi._handle, buf, MAX_PATH)
86+
return buf.value
87+
except (ValueError, OSError):
88+
return None
89+
90+
91+
7492
def library_name(name, suffix=SHLIB_SUFFIX, is_windows=is_windows):
7593
"""
7694
Convert a file basename `name` to a library name (no "lib" and ".so" etc.)

test/test_find_libpython.py

+1-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
import platform
2-
3-
import pytest
4-
51
from julia.find_libpython import finding_libpython, linked_libpython
62
from julia.core import determine_if_statically_linked
73

@@ -18,8 +14,6 @@ def test_finding_libpython_yield_type():
1814
# let's just check returned type of finding_libpython.
1915

2016

21-
@pytest.mark.xfail(platform.system() == "Windows",
22-
reason="linked_libpython is not implemented for Windows")
2317
def test_linked_libpython():
24-
if determine_if_statically_linked():
18+
if not determine_if_statically_linked():
2519
assert linked_libpython() is not None

0 commit comments

Comments
 (0)