|
| 1 | +from __future__ import print_function |
| 2 | + |
| 3 | +import os |
| 4 | +import subprocess |
| 5 | +import sys |
| 6 | +import textwrap |
| 7 | + |
| 8 | +import pytest |
| 9 | + |
| 10 | +from .test_core import julia |
| 11 | +from julia.core import _enviorn, which |
| 12 | + |
| 13 | +is_linux = sys.platform.startswith("linux") |
| 14 | +is_windows = os.name == "nt" |
| 15 | +is_apple = sys.platform == "darwin" |
| 16 | + |
| 17 | + |
| 18 | +def _get_paths(path): |
| 19 | + return filter(None, path.split(":")) |
| 20 | + |
| 21 | + |
| 22 | +# Environment variable PYJULIA_TEST_INCOMPATIBLE_PYTHONS is the |
| 23 | +# :-separated list of Python executables incompatible with the current |
| 24 | +# Python: |
| 25 | +incompatible_pythons = _get_paths(os.getenv("PYJULIA_TEST_INCOMPATIBLE_PYTHONS", "")) |
| 26 | + |
| 27 | + |
| 28 | +try: |
| 29 | + from types import SimpleNamespace |
| 30 | +except ImportError: |
| 31 | + from argparse import Namespace as SimpleNamespace |
| 32 | + |
| 33 | + |
| 34 | +def _run_fallback(args, input=None, **kwargs): |
| 35 | + process = subprocess.Popen(args, stdin=subprocess.PIPE, **kwargs) |
| 36 | + stdout, stderr = process.communicate(input) |
| 37 | + retcode = process.wait() |
| 38 | + return SimpleNamespace(args=args, stdout=stdout, stderr=stderr, returncode=retcode) |
| 39 | + |
| 40 | + |
| 41 | +try: |
| 42 | + from subprocess import run |
| 43 | +except ImportError: |
| 44 | + run = _run_fallback |
| 45 | + |
| 46 | + |
| 47 | +def runcode(python, code): |
| 48 | + """Run `code` in `python`.""" |
| 49 | + return run( |
| 50 | + [python], |
| 51 | + input=textwrap.dedent(code), |
| 52 | + stdout=subprocess.PIPE, |
| 53 | + stderr=subprocess.PIPE, |
| 54 | + universal_newlines=True, |
| 55 | + env=dict( |
| 56 | + _enviorn, |
| 57 | + # Make PyJulia importable: |
| 58 | + PYTHONPATH=os.path.dirname(os.path.dirname(os.path.realpath(__file__))), |
| 59 | + ), |
| 60 | + ) |
| 61 | + |
| 62 | + |
| 63 | +def print_completed_proc(proc): |
| 64 | + # Print output (pytest will hide it by default): |
| 65 | + print("Ran:", *proc.args) |
| 66 | + if proc.stdout: |
| 67 | + print("# --- STDOUT from", *proc.args) |
| 68 | + print(proc.stdout) |
| 69 | + if proc.stderr: |
| 70 | + print("# --- STDERR from", *proc.args) |
| 71 | + print(proc.stderr) |
| 72 | + print("# ---") |
| 73 | + |
| 74 | + |
| 75 | +def is_dynamically_linked(executable): |
| 76 | + path = which(executable) |
| 77 | + assert os.path.exists(path) |
| 78 | + if is_linux and which("ldd"): |
| 79 | + proc = run( |
| 80 | + ["ldd", path], stdout=subprocess.PIPE, env=_enviorn, universal_newlines=True |
| 81 | + ) |
| 82 | + print_completed_proc(proc) |
| 83 | + return "libpython" in proc.stdout |
| 84 | + elif is_apple and which("otool"): |
| 85 | + proc = run( |
| 86 | + ["otool", "-L", path], |
| 87 | + stdout=subprocess.PIPE, |
| 88 | + env=_enviorn, |
| 89 | + universal_newlines=True, |
| 90 | + ) |
| 91 | + print_completed_proc(proc) |
| 92 | + return "libpython" in proc.stdout or "/Python" in proc.stdout |
| 93 | + # TODO: support Windows |
| 94 | + return None |
| 95 | + |
| 96 | + |
| 97 | +@pytest.mark.parametrize("python", incompatible_pythons) |
| 98 | +def test_incompatible_python(python): |
| 99 | + if julia.eval("(VERSION.major, VERSION.minor)") == (0, 6): |
| 100 | + # Julia 0.6 implements mixed version |
| 101 | + return |
| 102 | + |
| 103 | + python = which(python) |
| 104 | + proc = runcode( |
| 105 | + python, |
| 106 | + """ |
| 107 | + import os |
| 108 | + from julia import Julia |
| 109 | + Julia(runtime=os.getenv("JULIA_EXE"), debug=True) |
| 110 | + """, |
| 111 | + ) |
| 112 | + print_completed_proc(proc) |
| 113 | + |
| 114 | + assert proc.returncode == 1 |
| 115 | + assert "It seems your Julia and PyJulia setup are not supported." in proc.stderr |
| 116 | + dynamic = is_dynamically_linked(python) |
| 117 | + if dynamic is True: |
| 118 | + assert "`libpython` have to match" in proc.stderr |
| 119 | + elif dynamic is False: |
| 120 | + assert "is statically linked to libpython" in proc.stderr |
0 commit comments