Skip to content

Commit 79de24d

Browse files
committed
Test eval mode in PyRun_String
1 parent b7202b5 commit 79de24d

File tree

1 file changed

+29
-41
lines changed

1 file changed

+29
-41
lines changed

graalpython/com.oracle.graal.python.test/src/tests/cpyext/test_pythonrun.py

Lines changed: 29 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -37,54 +37,42 @@
3737
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
3838
# SOFTWARE.
3939

40-
import sys
41-
42-
from . import CPyExtTestCase, CPyExtFunction, unhandled_error_compare
40+
from . import CPyExtTestCase, CPyExtFunction, unhandled_error_compare, CPyExtType
4341

4442
__dir__ = __file__.rpartition("/")[0]
4543

4644

47-
# python_run_test_result = None
48-
49-
def _reference_run_string(args):
50-
if not isinstance(args[2], dict):
51-
if sys.version_info.minor >= 6:
52-
raise SystemError
53-
else:
54-
raise TypeError
55-
if not isinstance(args[3], dict):
56-
raise TypeError
57-
return None
58-
59-
60-
def _run_string_compare(x, y):
61-
res = unhandled_error_compare(x, y)
62-
if (isinstance(x, Exception)):
63-
return res
45+
class TestPythonRun(CPyExtTestCase):
6446

65-
global python_run_test_result
66-
pr = python_run_test_result
67-
res = res and pr == 42
68-
python_run_test_result = None
69-
if not res:
70-
assert False, "python_run_test_result is %s" % pr
71-
return res
47+
def test_PyRun_String(self):
48+
Tester = CPyExtType(
49+
"PyRunStringTester",
50+
code="""
51+
static PyObject* call_PyRun_String(PyObject* unused, PyObject* args) {
52+
int eval;
53+
char *string;
54+
PyObject *globals, *locals;
55+
if (PyArg_ParseTuple(args, "spOO", &string, &eval, &globals, &locals) < 0)
56+
return NULL;
57+
int start = eval ? Py_eval_input : Py_file_input;
58+
return PyRun_String(string, start, globals, locals);
59+
}
60+
""",
61+
tp_methods='{"call_PyRun_String", (PyCFunction)call_PyRun_String, METH_VARARGS | METH_STATIC}'
62+
)
63+
g = {}
64+
l = {}
65+
assert Tester.call_PyRun_String("a = 1", False, g, l) is None
66+
assert l.get('a') == 1
67+
assert 'a' not in g
68+
g = {}
69+
l = {}
7270

71+
assert Tester.call_PyRun_String("global a\na = 1", False, g, l) is None
72+
assert g.get('a') == 1
73+
assert 'a' not in l
7374

74-
class TestPythonRun(CPyExtTestCase):
75-
76-
test_PyRun_StringFlags = CPyExtFunction(
77-
_reference_run_string,
78-
lambda: (
79-
("globals().update({'python_run_test_result':42})", 256, globals(), locals(), 0),
80-
("globals().update({'python_run_test_result':42})", 256, 'globals()', locals(), 0),
81-
("globals().update({'python_run_test_result':42})", 256, globals(), 'locals()', 0),
82-
),
83-
resultspec="O",
84-
argspec='siOOk',
85-
arguments=["char* source", "int type", "PyObject* globals", "PyObject* locals", "PyCompilerFlags* ignored"],
86-
cmpfunc=unhandled_error_compare
87-
)
75+
assert Tester.call_PyRun_String("1 + a", True, {}, {'a': 2}) == 3
8876

8977
test_Py_CompileString = CPyExtFunction(
9078
lambda args: compile(

0 commit comments

Comments
 (0)