Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
import os
import shlex
import subprocess
import pytest
from julia.core import which
from julia.python_jl import parse_pyjl_args
is_windows = os.name == "nt"
PYJULIA_TEST_REBUILD = os.environ.get("PYJULIA_TEST_REBUILD", "no") == "yes"
python_jl_required = pytest.mark.skipif(
os.environ.get("PYJULIA_TEST_PYTHON_JL_IS_INSTALLED", "no") != "yes"
and not which("python-jl"),
reason="python-jl command not found",
)
@pytest.mark.parametrize("args", [
"-h",
"-i --help",
"--julia false -h",
"--julia false -i --help",
])
def test_help_option(args, capsys):
with pytest.raises(SystemExit) as exc_info:
parse_pyjl_args(shlex.split(args))
assert exc_info.value.code == 0
captured = capsys.readouterr()
Check if Python `executable` is (likely to be) dynamically linked.
It returns three possible values:
* `True`: Likely that it's dynamically linked.
* `False`: Likely that it's statically linked.
* `None`: Unsupported platform.
It's only "likely" since the check is by simple occurrence of a
some substrings like "libpython". For example, if there is
another library existing on the path containing "libpython", this
function may return false-positive.
"""
path = which(executable)
assert os.path.exists(path)
if is_linux and which("ldd"):
proc = run(
["ldd", path], stdout=subprocess.PIPE, env=_enviorn, universal_newlines=True
)
print_completed_proc(proc)
return "libpython" in proc.stdout
elif is_apple and which("otool"):
proc = run(
["otool", "-L", path],
stdout=subprocess.PIPE,
env=_enviorn,
universal_newlines=True,
)
print_completed_proc(proc)
return (
"libpython" in proc.stdout
or "/Python" in proc.stdout
def is_dynamically_linked(executable):
"""
Check if Python `executable` is (likely to be) dynamically linked.
It returns three possible values:
* `True`: Likely that it's dynamically linked.
* `False`: Likely that it's statically linked.
* `None`: Unsupported platform.
It's only "likely" since the check is by simple occurrence of a
some substrings like "libpython". For example, if there is
another library existing on the path containing "libpython", this
function may return false-positive.
"""
path = which(executable)
assert os.path.exists(path)
if is_linux and which("ldd"):
proc = run(
["ldd", path], stdout=subprocess.PIPE, env=_enviorn, universal_newlines=True
)
print_completed_proc(proc)
return "libpython" in proc.stdout
elif is_apple and which("otool"):
proc = run(
["otool", "-L", path],
stdout=subprocess.PIPE,
env=_enviorn,
universal_newlines=True,
)
print_completed_proc(proc)
return (
def test_incompatible_python(python, julia):
if julia.eval("(VERSION.major, VERSION.minor)") == (0, 6):
# Julia 0.6 implements mixed version
return
python = which(python)
proc = runcode(
python,
"""
import os
from julia import Julia
Julia(runtime=os.getenv("PYJULIA_TEST_RUNTIME"), debug=True)
""",
)
assert proc.returncode == 1
assert "It seems your Julia and PyJulia setup are not supported." in proc.stderr
dynamic = is_dynamically_linked(python)
if dynamic is True:
assert "`libpython` have to match" in proc.stderr
elif dynamic is False:
assert "is statically linked to libpython" in proc.stderr
* `None`: Unsupported platform.
It's only "likely" since the check is by simple occurrence of a
some substrings like "libpython". For example, if there is
another library existing on the path containing "libpython", this
function may return false-positive.
"""
path = which(executable)
assert os.path.exists(path)
if is_linux and which("ldd"):
proc = run(
["ldd", path], stdout=subprocess.PIPE, env=_enviorn, universal_newlines=True
)
print_completed_proc(proc)
return "libpython" in proc.stdout
elif is_apple and which("otool"):
proc = run(
["otool", "-L", path],
stdout=subprocess.PIPE,
env=_enviorn,
universal_newlines=True,
)
print_completed_proc(proc)
return (
"libpython" in proc.stdout
or "/Python" in proc.stdout
or "/.Python" in proc.stdout
)
# TODO: support Windows
return None