From e07f2a8b2ad979bd21cc076c0b6a206537469d6b Mon Sep 17 00:00:00 2001 From: Alex Fikl Date: Wed, 6 Jun 2018 19:18:43 -0500 Subject: [PATCH 1/9] tests: skip tests if maxima not available --- pymbolic/interop/maxima.py | 32 ++++++++++++++++++++++++++++++++ test/test_maxima.py | 10 ++++++++++ 2 files changed, 42 insertions(+) diff --git a/pymbolic/interop/maxima.py b/pymbolic/interop/maxima.py index 4be1dcf..380ffa9 100644 --- a/pymbolic/interop/maxima.py +++ b/pymbolic/interop/maxima.py @@ -45,6 +45,38 @@ import numpy as np from pymbolic.mapper.stringifier import StringifyMapper from pymbolic.parser import Parser as ParserBase, FinalizedTuple +# {{{ check for maxima executable + + +def _find_maxima_executable(): + import os + + def is_executable(filename): + return os.path.isfile(filename) and os.access(filename, os.X_OK) + + global FOUND_MAXIMA + + executable = os.environ.get("SUMPY_MAXIMA_EXECUTABLE", "maxima") + + FOUND_MAXIMA = False + if is_executable(executable): + FOUND_MAXIMA = True + else: + executable = os.path.basename(executable) + try: + import shutil + FOUND_MAXIMA = bool(shutil.which(executable)) + except ImportError: + for path in os.environ["PATH"].split(os.pathsep): + filename = os.path.join(path, executable) + if is_executable(filename): + FOUND_MAXIMA = True + break + + +_find_maxima_executable() + +# }}} IN_PROMPT_RE = re.compile(br"\(%i([0-9]+)\) ") OUT_PROMPT_RE = re.compile(br"\(%o([0-9]+)\) ") diff --git a/test/test_maxima.py b/test/test_maxima.py index a61f0f2..7a6a7e0 100644 --- a/test/test_maxima.py +++ b/test/test_maxima.py @@ -24,8 +24,10 @@ THE SOFTWARE. import pytest from pymbolic.interop.maxima import MaximaKernel +from pymbolic.interop.maxima import FOUND_MAXIMA +@pytest.mark.skipif(not FOUND_MAXIMA, reason="cannot find maxima executable") def test_kernel(): pytest.importorskip("pexpect") @@ -45,6 +47,7 @@ def knl(request): return knl +@pytest.mark.skipif(not FOUND_MAXIMA, reason="cannot find maxima executable") def test_setup(knl): pytest.importorskip("pexpect") @@ -53,6 +56,7 @@ def test_setup(knl): "sum(diff(k, t,deg)*t^deg,deg,0,6)") +@pytest.mark.skipif(not FOUND_MAXIMA, reason="cannot find maxima executable") def test_error(knl): from pymbolic.interop.maxima import MaximaError try: @@ -66,6 +70,7 @@ def test_error(knl): pass +@pytest.mark.skipif(not FOUND_MAXIMA, reason="cannot find maxima executable") def test_strict_round_trip(knl): from pymbolic import parse from pymbolic.primitives import Quotient @@ -90,6 +95,7 @@ def test_strict_round_trip(knl): assert round_trips_correctly +@pytest.mark.skipif(not FOUND_MAXIMA, reason="cannot find maxima executable") def test_lax_round_trip(knl): from pymbolic.interop.maxima import MaximaParser k_setup = [ @@ -104,6 +110,7 @@ def test_lax_round_trip(knl): "ratsimp(result-result2)") == 0 +@pytest.mark.skipif(not FOUND_MAXIMA, reason="cannot find maxima executable") def test_parse_matrix(knl): z = knl.clean_eval_str_with_setup([ "A:matrix([1,2+0.3*dt], [3,4])", @@ -115,6 +122,7 @@ def test_parse_matrix(knl): print(MaximaParser()(z)) +@pytest.mark.skipif(not FOUND_MAXIMA, reason="cannot find maxima executable") def test_diff(): pytest.importorskip("pexpect") @@ -123,12 +131,14 @@ def test_diff(): diff(parse("sqrt(x**2+y**2)"), parse("x")) +@pytest.mark.skipif(not FOUND_MAXIMA, reason="cannot find maxima executable") def test_long_command(knl): from pymbolic.interop.maxima import set_debug set_debug(4) knl.eval_str("+".join(["1"]*16384)) +@pytest.mark.skipif(not FOUND_MAXIMA, reason="cannot find maxima executable") def test_restart(knl): pytest.importorskip("pexpect") -- GitLab From 6c6c3a5925976fcf926a799e3c94475b18c25c40 Mon Sep 17 00:00:00 2001 From: Alex Fikl Date: Wed, 13 Jun 2018 17:25:32 -0500 Subject: [PATCH 2/9] maxima: hopefully fix executable check on python2 --- pymbolic/interop/maxima.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pymbolic/interop/maxima.py b/pymbolic/interop/maxima.py index 380ffa9..eba2781 100644 --- a/pymbolic/interop/maxima.py +++ b/pymbolic/interop/maxima.py @@ -66,7 +66,7 @@ def _find_maxima_executable(): try: import shutil FOUND_MAXIMA = bool(shutil.which(executable)) - except ImportError: + except AttributeError: for path in os.environ["PATH"].split(os.pathsep): filename = os.path.join(path, executable) if is_executable(filename): -- GitLab From ed934e03156e2655b701665760b2b3d673312528 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20Kl=C3=B6ckner?= Date: Thu, 14 Jun 2018 16:06:27 -0400 Subject: [PATCH 3/9] Test for maxima executable: PYMBOLIC env var, not SUMPY --- pymbolic/interop/maxima.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pymbolic/interop/maxima.py b/pymbolic/interop/maxima.py index eba2781..9a1fc44 100644 --- a/pymbolic/interop/maxima.py +++ b/pymbolic/interop/maxima.py @@ -47,7 +47,6 @@ from pymbolic.parser import Parser as ParserBase, FinalizedTuple # {{{ check for maxima executable - def _find_maxima_executable(): import os @@ -56,7 +55,7 @@ def _find_maxima_executable(): global FOUND_MAXIMA - executable = os.environ.get("SUMPY_MAXIMA_EXECUTABLE", "maxima") + executable = os.environ.get("PYMBOLIC_MAXIMA_EXECUTABLE", "maxima") FOUND_MAXIMA = False if is_executable(executable): -- GitLab From c7528c464ed5e242001de72da26f7b96f138faae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20Kl=C3=B6ckner?= Date: Thu, 14 Jun 2018 16:07:40 -0400 Subject: [PATCH 4/9] Demote Maxima finding to a test-only thing --- pymbolic/interop/maxima.py | 32 -------------------------------- test/test_maxima.py | 34 +++++++++++++++++++++++++++++++++- 2 files changed, 33 insertions(+), 33 deletions(-) diff --git a/pymbolic/interop/maxima.py b/pymbolic/interop/maxima.py index 9a1fc44..2995767 100644 --- a/pymbolic/interop/maxima.py +++ b/pymbolic/interop/maxima.py @@ -45,38 +45,6 @@ import numpy as np from pymbolic.mapper.stringifier import StringifyMapper from pymbolic.parser import Parser as ParserBase, FinalizedTuple -# {{{ check for maxima executable - -def _find_maxima_executable(): - import os - - def is_executable(filename): - return os.path.isfile(filename) and os.access(filename, os.X_OK) - - global FOUND_MAXIMA - - executable = os.environ.get("PYMBOLIC_MAXIMA_EXECUTABLE", "maxima") - - FOUND_MAXIMA = False - if is_executable(executable): - FOUND_MAXIMA = True - else: - executable = os.path.basename(executable) - try: - import shutil - FOUND_MAXIMA = bool(shutil.which(executable)) - except AttributeError: - for path in os.environ["PATH"].split(os.pathsep): - filename = os.path.join(path, executable) - if is_executable(filename): - FOUND_MAXIMA = True - break - - -_find_maxima_executable() - -# }}} - IN_PROMPT_RE = re.compile(br"\(%i([0-9]+)\) ") OUT_PROMPT_RE = re.compile(br"\(%o([0-9]+)\) ") ERROR_PROMPT_RE = re.compile( diff --git a/test/test_maxima.py b/test/test_maxima.py index 7a6a7e0..ce83c4e 100644 --- a/test/test_maxima.py +++ b/test/test_maxima.py @@ -24,7 +24,6 @@ THE SOFTWARE. import pytest from pymbolic.interop.maxima import MaximaKernel -from pymbolic.interop.maxima import FOUND_MAXIMA @pytest.mark.skipif(not FOUND_MAXIMA, reason="cannot find maxima executable") @@ -38,6 +37,39 @@ def test_kernel(): knl.shutdown() +# {{{ check for maxima executable + +def _find_maxima_executable(): + import os + + def is_executable(filename): + return os.path.isfile(filename) and os.access(filename, os.X_OK) + + global FOUND_MAXIMA + + executable = os.environ.get("PYMBOLIC_MAXIMA_EXECUTABLE", "maxima") + + FOUND_MAXIMA = False + if is_executable(executable): + FOUND_MAXIMA = True + else: + executable = os.path.basename(executable) + try: + import shutil + FOUND_MAXIMA = bool(shutil.which(executable)) + except AttributeError: + for path in os.environ["PATH"].split(os.pathsep): + filename = os.path.join(path, executable) + if is_executable(filename): + FOUND_MAXIMA = True + break + + +_find_maxima_executable() + +# }}} + + @pytest.fixture def knl(request): pytest.importorskip("pexpect") -- GitLab From 3551754829e2f6a5b8d172e18f21b70699338492 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20Kl=C3=B6ckner?= Date: Thu, 14 Jun 2018 16:08:53 -0400 Subject: [PATCH 5/9] Restore inadvertent whitespace change --- pymbolic/interop/maxima.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pymbolic/interop/maxima.py b/pymbolic/interop/maxima.py index 2995767..4be1dcf 100644 --- a/pymbolic/interop/maxima.py +++ b/pymbolic/interop/maxima.py @@ -45,6 +45,7 @@ import numpy as np from pymbolic.mapper.stringifier import StringifyMapper from pymbolic.parser import Parser as ParserBase, FinalizedTuple + IN_PROMPT_RE = re.compile(br"\(%i([0-9]+)\) ") OUT_PROMPT_RE = re.compile(br"\(%o([0-9]+)\) ") ERROR_PROMPT_RE = re.compile( -- GitLab From 8d4f981bb08514043608354b8edcbcf33faec6c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20Kl=C3=B6ckner?= Date: Thu, 14 Jun 2018 16:09:43 -0400 Subject: [PATCH 6/9] Move test for maxima executable before actual first use (whoops) --- test/test_maxima.py | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/test/test_maxima.py b/test/test_maxima.py index ce83c4e..e1a50d3 100644 --- a/test/test_maxima.py +++ b/test/test_maxima.py @@ -26,17 +26,6 @@ import pytest from pymbolic.interop.maxima import MaximaKernel -@pytest.mark.skipif(not FOUND_MAXIMA, reason="cannot find maxima executable") -def test_kernel(): - pytest.importorskip("pexpect") - - knl = MaximaKernel() - knl.exec_str("k:1/(sqrt((x0-(a+t*b))^2+(y0-(c+t*d))^2+(z0-(e+t*f))^2))") - knl.eval_str("sum(diff(k, t,deg)*t^deg,deg,0,6)") - assert knl.eval_str("2+2").strip() == "4" - knl.shutdown() - - # {{{ check for maxima executable def _find_maxima_executable(): @@ -70,6 +59,17 @@ _find_maxima_executable() # }}} +@pytest.mark.skipif(not FOUND_MAXIMA, reason="cannot find maxima executable") +def test_kernel(): + pytest.importorskip("pexpect") + + knl = MaximaKernel() + knl.exec_str("k:1/(sqrt((x0-(a+t*b))^2+(y0-(c+t*d))^2+(z0-(e+t*f))^2))") + knl.eval_str("sum(diff(k, t,deg)*t^deg,deg,0,6)") + assert knl.eval_str("2+2").strip() == "4" + knl.shutdown() + + @pytest.fixture def knl(request): pytest.importorskip("pexpect") -- GitLab From 5b54ffaccea4abba3722e753aac9beb6582eae05 Mon Sep 17 00:00:00 2001 From: Alex Fikl Date: Thu, 14 Jun 2018 15:28:13 -0500 Subject: [PATCH 7/9] simplify checking for maxima in tests --- test/test_maxima.py | 26 +++++--------------------- 1 file changed, 5 insertions(+), 21 deletions(-) diff --git a/test/test_maxima.py b/test/test_maxima.py index e1a50d3..2d39d04 100644 --- a/test/test_maxima.py +++ b/test/test_maxima.py @@ -29,30 +29,14 @@ from pymbolic.interop.maxima import MaximaKernel # {{{ check for maxima executable def _find_maxima_executable(): - import os - - def is_executable(filename): - return os.path.isfile(filename) and os.access(filename, os.X_OK) - global FOUND_MAXIMA - executable = os.environ.get("PYMBOLIC_MAXIMA_EXECUTABLE", "maxima") - - FOUND_MAXIMA = False - if is_executable(executable): + try: + knl = MaximaKernel() FOUND_MAXIMA = True - else: - executable = os.path.basename(executable) - try: - import shutil - FOUND_MAXIMA = bool(shutil.which(executable)) - except AttributeError: - for path in os.environ["PATH"].split(os.pathsep): - filename = os.path.join(path, executable) - if is_executable(filename): - FOUND_MAXIMA = True - break - + knl.shutdown() + except (ImportError, RuntimeError): + FOUND_MAXIMA = False _find_maxima_executable() -- GitLab From 1f102613bd7611c8aefde44b988855d3f17446a3 Mon Sep 17 00:00:00 2001 From: Alex Fikl Date: Thu, 14 Jun 2018 15:29:39 -0500 Subject: [PATCH 8/9] fix flake8 --- test/test_maxima.py | 1 + 1 file changed, 1 insertion(+) diff --git a/test/test_maxima.py b/test/test_maxima.py index 2d39d04..675759d 100644 --- a/test/test_maxima.py +++ b/test/test_maxima.py @@ -38,6 +38,7 @@ def _find_maxima_executable(): except (ImportError, RuntimeError): FOUND_MAXIMA = False + _find_maxima_executable() # }}} -- GitLab From c502090ce85c37e6c03d2b945fa5ae456e148cc1 Mon Sep 17 00:00:00 2001 From: Alex Fikl Date: Thu, 14 Jun 2018 15:40:51 -0500 Subject: [PATCH 9/9] remove redundant importorskip on pexpect --- test/test_maxima.py | 45 +++++++++++++++++++-------------------------- 1 file changed, 19 insertions(+), 26 deletions(-) diff --git a/test/test_maxima.py b/test/test_maxima.py index 675759d..ddb5c76 100644 --- a/test/test_maxima.py +++ b/test/test_maxima.py @@ -26,28 +26,29 @@ import pytest from pymbolic.interop.maxima import MaximaKernel -# {{{ check for maxima executable +# {{{ check for maxima -def _find_maxima_executable(): - global FOUND_MAXIMA +def _check_maxima(): + global MAXIMA_UNAVAILABLE + + import os + executable = os.environ.get("PYMBOLIC_MAXIMA_EXECUTABLE", "maxima") try: - knl = MaximaKernel() - FOUND_MAXIMA = True + knl = MaximaKernel(executable=executable) + MAXIMA_UNAVAILABLE = False knl.shutdown() except (ImportError, RuntimeError): - FOUND_MAXIMA = False + MAXIMA_UNAVAILABLE = True -_find_maxima_executable() +_check_maxima() # }}} -@pytest.mark.skipif(not FOUND_MAXIMA, reason="cannot find maxima executable") +@pytest.mark.skipif(MAXIMA_UNAVAILABLE, reason="maxima cannot be launched") def test_kernel(): - pytest.importorskip("pexpect") - knl = MaximaKernel() knl.exec_str("k:1/(sqrt((x0-(a+t*b))^2+(y0-(c+t*d))^2+(z0-(e+t*f))^2))") knl.eval_str("sum(diff(k, t,deg)*t^deg,deg,0,6)") @@ -57,23 +58,19 @@ def test_kernel(): @pytest.fixture def knl(request): - pytest.importorskip("pexpect") - knl = MaximaKernel() request.addfinalizer(knl.shutdown) return knl -@pytest.mark.skipif(not FOUND_MAXIMA, reason="cannot find maxima executable") +@pytest.mark.skipif(MAXIMA_UNAVAILABLE, reason="maxima cannot be launched") def test_setup(knl): - pytest.importorskip("pexpect") - knl.clean_eval_str_with_setup( ["k:1/(sqrt((x0-(a+t*b))^2+(y0-(c+t*d))^2+(z0-(e+t*f))^2))"], "sum(diff(k, t,deg)*t^deg,deg,0,6)") -@pytest.mark.skipif(not FOUND_MAXIMA, reason="cannot find maxima executable") +@pytest.mark.skipif(MAXIMA_UNAVAILABLE, reason="maxima cannot be launched") def test_error(knl): from pymbolic.interop.maxima import MaximaError try: @@ -87,7 +84,7 @@ def test_error(knl): pass -@pytest.mark.skipif(not FOUND_MAXIMA, reason="cannot find maxima executable") +@pytest.mark.skipif(MAXIMA_UNAVAILABLE, reason="maxima cannot be launched") def test_strict_round_trip(knl): from pymbolic import parse from pymbolic.primitives import Quotient @@ -112,7 +109,7 @@ def test_strict_round_trip(knl): assert round_trips_correctly -@pytest.mark.skipif(not FOUND_MAXIMA, reason="cannot find maxima executable") +@pytest.mark.skipif(MAXIMA_UNAVAILABLE, reason="maxima cannot be launched") def test_lax_round_trip(knl): from pymbolic.interop.maxima import MaximaParser k_setup = [ @@ -127,7 +124,7 @@ def test_lax_round_trip(knl): "ratsimp(result-result2)") == 0 -@pytest.mark.skipif(not FOUND_MAXIMA, reason="cannot find maxima executable") +@pytest.mark.skipif(MAXIMA_UNAVAILABLE, reason="maxima cannot be launched") def test_parse_matrix(knl): z = knl.clean_eval_str_with_setup([ "A:matrix([1,2+0.3*dt], [3,4])", @@ -139,26 +136,22 @@ def test_parse_matrix(knl): print(MaximaParser()(z)) -@pytest.mark.skipif(not FOUND_MAXIMA, reason="cannot find maxima executable") +@pytest.mark.skipif(MAXIMA_UNAVAILABLE, reason="maxima cannot be launched") def test_diff(): - pytest.importorskip("pexpect") - from pymbolic.interop.maxima import diff from pymbolic import parse diff(parse("sqrt(x**2+y**2)"), parse("x")) -@pytest.mark.skipif(not FOUND_MAXIMA, reason="cannot find maxima executable") +@pytest.mark.skipif(MAXIMA_UNAVAILABLE, reason="maxima cannot be launched") def test_long_command(knl): from pymbolic.interop.maxima import set_debug set_debug(4) knl.eval_str("+".join(["1"]*16384)) -@pytest.mark.skipif(not FOUND_MAXIMA, reason="cannot find maxima executable") +@pytest.mark.skipif(MAXIMA_UNAVAILABLE, reason="maxima cannot be launched") def test_restart(knl): - pytest.importorskip("pexpect") - knl = MaximaKernel() knl.restart() knl.eval_str("1") -- GitLab