|
37 | 37 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
38 | 38 | # SOFTWARE.
|
39 | 39 |
|
40 |
| -import sys |
41 |
| - |
42 |
| -from . import CPyExtTestCase, CPyExtFunction, unhandled_error_compare |
| 40 | +from . import CPyExtTestCase, CPyExtFunction, unhandled_error_compare, CPyExtType |
43 | 41 |
|
44 | 42 | __dir__ = __file__.rpartition("/")[0]
|
45 | 43 |
|
46 | 44 |
|
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): |
64 | 46 |
|
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 = {} |
72 | 70 |
|
| 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 |
73 | 74 |
|
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 |
88 | 76 |
|
89 | 77 | test_Py_CompileString = CPyExtFunction(
|
90 | 78 | lambda args: compile(
|
|
0 commit comments