From b84c3cbc362bf2770f6c1b94268fa169b72e3002 Mon Sep 17 00:00:00 2001 From: Miles Cranmer Date: Tue, 13 Feb 2024 20:08:43 +0000 Subject: [PATCH] Allow multi-block expressions in `seval` (#452) * Allow multi-line strings in `seval` * Add test for issue 433 * Fix newline --- pytest/test_all.py | 19 +++++++++++++++++++ src/JlWrap/module.jl | 2 +- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/pytest/test_all.py b/pytest/test_all.py index af7111e6..d34cd1b9 100644 --- a/pytest/test_all.py +++ b/pytest/test_all.py @@ -17,3 +17,22 @@ def test_issue_394(): assert jl.f is f assert jl.y is y assert jl.seval("f(x)") == 4 + +def test_issue_433(): + "https://github.com/JuliaPy/PythonCall.jl/issues/433" + from juliacall import Main as jl + + # Smoke test + jl.seval("x=1\nx=1") + assert jl.x == 1 + + # Do multiple things + out = jl.seval( + """ + function _issue_433_g(x) + return x^2 + end + _issue_433_g(5) + """ + ) + assert out == 25 diff --git a/src/JlWrap/module.jl b/src/JlWrap/module.jl index e88a6a4e..8d879ae5 100644 --- a/src/JlWrap/module.jl +++ b/src/JlWrap/module.jl @@ -10,7 +10,7 @@ function pyjlmodule_dir(self::Module) end function pyjlmodule_seval(self::Module, expr::Py) - Py(Base.eval(self, Meta.parse(strip(pyconvert(String, expr))))) + Py(Base.eval(self, Meta.parseall(strip(pyconvert(String, expr))))) end function init_module()