Skip to content

Commit

Permalink
Minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
MiguelMarcelino committed Sep 17, 2022
1 parent dfdce3d commit c0ef74e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 24 deletions.
16 changes: 15 additions & 1 deletion pyjl/external/modules/ctypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def visit_load_library(self, node: ast.Call, vargs: list[str], kwargs: list[tupl
def visit_pythonapi(self, node: ast.Call, vargs: list[str], kwargs: list[tuple[str,str]]):
self._usings.add("Libdl")
# Get path of the dll for Python 3.9
python_39 = get_python_dll_path((3, 9))
python_39 = JuliaExternalModulePlugins._get_python_dll_path((3, 9))
self._globals.add(f"pythonapi = Libdl.dlopen({python_39})")
func = self.visit(node.func)
if getattr(node, "in_ccall", None):
Expand All @@ -27,6 +27,20 @@ def visit_pythonapi(self, node: ast.Call, vargs: list[str], kwargs: list[tuple[s
return f"(argtypes, return_type, args) -> @ccall (Libdl.dlsym(pythonapi, :{func}), return_type, argtypes, args)"
return f"ccall({', '.join(vargs)})"

def _get_python_dll_path(version: tuple[str, str]):
"""Receives major and minor version information
and returns the corresponding Python DLL path"""
if len(version) != 2:
return None
# Checks the path of the dll for Python
# given a version number
version_str = f"{version[0]}{version[1]}"
python_path = subprocess.check_output(f'where python{version_str}.dll').decode().strip().split("\n")
python_path = python_path[1] \
if len(python_path) > 1 \
else python_path[0]
return f"\"{'/'.join(python_path.strip().split(os.sep))}\""

def visit_cast(self, node: ast.Call, vargs: list[str], kwargs: list[tuple[str,str]]):
self._usings.add("Libdl")
return f"Base.cconvert({self._map_type(vargs[1])}, {vargs[0]})"
Expand Down
22 changes: 0 additions & 22 deletions pyjl/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,25 +162,3 @@ def pycall_import(self, node: ast.Call, mod_name: str, opt_name: Optional[str] =
self._pycall_imports.add(mod_name)
import_stmt = f'{mod_name} = pyimport("{mod_name}")'
self._globals.add(import_stmt)

def get_python_dll_path(version: tuple[str, str]):
"""Receives major and minor version information
and returns the corresponding Python DLL path"""
if len(version) != 2:
return None
# Checks the path of the dll for Python
# given a version number
version_str = f"{version[0]}{version[1]}"
python_path = subprocess.check_output(f'where python{version_str}.dll').decode().strip().split("\n")
python_path = python_path[1] \
if len(python_path) > 1 \
else python_path[0]
return f"\"{'/'.join(python_path.strip().split(os.sep))}\""

def get_python_exe_path():
"""Returns the Python exe path"""
python_path = subprocess.check_output(f'where python.exe').decode().strip().split("\n")
python_path = python_path[1] \
if len(python_path) > 1 \
else python_path[0]
return f"\"{'/'.join(python_path.strip().split(os.sep))}\""
2 changes: 1 addition & 1 deletion pyjl/plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -1608,7 +1608,7 @@ def visit_Attribute(self, node: ast.Attribute) -> Any:
True,
),
sys.maxsize: (lambda self, node, vargs, kwargs: "typemax(Int)", True),
# TODO: Change sys.executable for "get_python_exe_path" Python dir
# TODO: Change sys.executable for python.exe path
sys.executable: (lambda self, node, vargs, kwargs: "joinpath(Base.Sys.BINDIR, Base.julia_exename())", True),
# calls invoking PyCall
tempfile.mkdtemp: (JuliaTranspilerPlugins.visit_tempfile, True),
Expand Down

0 comments on commit c0ef74e

Please sign in to comment.