Skip to content

Commit 8ae70c8

Browse files
serhiy-storchakaNGRsoftlaberlend-aasland
authored
[3.12] gh-117968: Add tests for the part of the PyRun family of the C API (GH-117982) (GH-118011)
(cherry picked from commit 6078f20) Co-authored-by: NGRsoftlab <[email protected]> Co-authored-by: Erlend E. Aasland <[email protected]>
1 parent c869f4e commit 8ae70c8

File tree

7 files changed

+238
-1
lines changed

7 files changed

+238
-1
lines changed

Lib/test/test_capi/test_run.py

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
import os
2+
import unittest
3+
from collections import UserDict
4+
from test.support import import_helper
5+
from test.support.os_helper import unlink, TESTFN, TESTFN_ASCII, TESTFN_UNDECODABLE
6+
7+
NULL = None
8+
_testcapi = import_helper.import_module('_testcapi')
9+
Py_single_input = _testcapi.Py_single_input
10+
Py_file_input = _testcapi.Py_file_input
11+
Py_eval_input = _testcapi.Py_eval_input
12+
13+
14+
class CAPITest(unittest.TestCase):
15+
# TODO: Test the following functions:
16+
#
17+
# PyRun_SimpleStringFlags
18+
# PyRun_AnyFileExFlags
19+
# PyRun_SimpleFileExFlags
20+
# PyRun_InteractiveOneFlags
21+
# PyRun_InteractiveOneObject
22+
# PyRun_InteractiveLoopFlags
23+
# PyRun_String (may be a macro)
24+
# PyRun_AnyFile (may be a macro)
25+
# PyRun_AnyFileEx (may be a macro)
26+
# PyRun_AnyFileFlags (may be a macro)
27+
# PyRun_SimpleString (may be a macro)
28+
# PyRun_SimpleFile (may be a macro)
29+
# PyRun_SimpleFileEx (may be a macro)
30+
# PyRun_InteractiveOne (may be a macro)
31+
# PyRun_InteractiveLoop (may be a macro)
32+
# PyRun_File (may be a macro)
33+
# PyRun_FileEx (may be a macro)
34+
# PyRun_FileFlags (may be a macro)
35+
36+
def test_run_stringflags(self):
37+
# Test PyRun_StringFlags().
38+
def run(s, *args):
39+
return _testcapi.run_stringflags(s, Py_file_input, *args)
40+
source = b'a\n'
41+
42+
self.assertIsNone(run(b'a\n', dict(a=1)))
43+
self.assertIsNone(run(b'a\n', dict(a=1), {}))
44+
self.assertIsNone(run(b'a\n', {}, dict(a=1)))
45+
self.assertIsNone(run(b'a\n', {}, UserDict(a=1)))
46+
47+
self.assertRaises(NameError, run, b'a\n', {})
48+
self.assertRaises(NameError, run, b'a\n', {}, {})
49+
self.assertRaises(TypeError, run, b'a\n', dict(a=1), [])
50+
self.assertRaises(TypeError, run, b'a\n', dict(a=1), 1)
51+
52+
self.assertIsNone(run(b'\xc3\xa4\n', {'\xe4': 1}))
53+
self.assertRaises(SyntaxError, run, b'\xe4\n', {})
54+
55+
# CRASHES run(b'a\n', NULL)
56+
# CRASHES run(b'a\n', NULL, {})
57+
# CRASHES run(b'a\n', NULL, dict(a=1))
58+
# CRASHES run(b'a\n', UserDict())
59+
# CRASHES run(b'a\n', UserDict(), {})
60+
# CRASHES run(b'a\n', UserDict(), dict(a=1))
61+
62+
# CRASHES run(NULL, {})
63+
64+
def test_run_fileexflags(self):
65+
# Test PyRun_FileExFlags().
66+
# XXX: fopen() uses different path encoding than Python on Windows.
67+
filename = os.fsencode(TESTFN if os.name != 'nt' else TESTFN_ASCII)
68+
with open(filename, 'wb') as fp:
69+
fp.write(b'a\n')
70+
self.addCleanup(unlink, filename)
71+
def run(*args):
72+
return _testcapi.run_fileexflags(filename, Py_file_input, *args)
73+
74+
self.assertIsNone(run(dict(a=1)))
75+
self.assertIsNone(run(dict(a=1), {}))
76+
self.assertIsNone(run({}, dict(a=1)))
77+
self.assertIsNone(run({}, UserDict(a=1)))
78+
self.assertIsNone(run(dict(a=1), {}, 1)) # closeit = True
79+
80+
self.assertRaises(NameError, run, {})
81+
self.assertRaises(NameError, run, {}, {})
82+
self.assertRaises(TypeError, run, dict(a=1), [])
83+
self.assertRaises(TypeError, run, dict(a=1), 1)
84+
85+
# CRASHES run(NULL)
86+
# CRASHES run(NULL, {})
87+
# CRASHES run(NULL, dict(a=1))
88+
# CRASHES run(UserDict())
89+
# CRASHES run(UserDict(), {})
90+
# CRASHES run(UserDict(), dict(a=1))
91+
92+
@unittest.skipUnless(TESTFN_UNDECODABLE, 'only works if there are undecodable paths')
93+
@unittest.skipIf(os.name == 'nt', 'does not work on Windows')
94+
def test_run_fileexflags_with_undecodable_filename(self):
95+
run = _testcapi.run_fileexflags
96+
try:
97+
with open(TESTFN_UNDECODABLE, 'wb') as fp:
98+
fp.write(b'a\n')
99+
self.addCleanup(unlink, TESTFN_UNDECODABLE)
100+
except OSError:
101+
self.skipTest('undecodable paths are not supported')
102+
self.assertIsNone(run(TESTFN_UNDECODABLE, Py_file_input, dict(a=1)))
103+
104+
105+
if __name__ == '__main__':
106+
unittest.main()

Modules/Setup.stdlib.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@
168168
@MODULE__XXTESTFUZZ_TRUE@_xxtestfuzz _xxtestfuzz/_xxtestfuzz.c _xxtestfuzz/fuzzer.c
169169
@MODULE__TESTBUFFER_TRUE@_testbuffer _testbuffer.c
170170
@MODULE__TESTINTERNALCAPI_TRUE@_testinternalcapi _testinternalcapi.c
171-
@MODULE__TESTCAPI_TRUE@_testcapi _testcapimodule.c _testcapi/vectorcall.c _testcapi/vectorcall_limited.c _testcapi/heaptype.c _testcapi/abstract.c _testcapi/bytearray.c _testcapi/bytes.c _testcapi/unicode.c _testcapi/dict.c _testcapi/set.c _testcapi/list.c _testcapi/tuple.c _testcapi/getargs.c _testcapi/pytime.c _testcapi/datetime.c _testcapi/docstring.c _testcapi/mem.c _testcapi/watchers.c _testcapi/long.c _testcapi/float.c _testcapi/complex.c _testcapi/numbers.c _testcapi/structmember.c _testcapi/exceptions.c _testcapi/code.c _testcapi/buffer.c _testcapi/pyos.c _testcapi/file.c _testcapi/codec.c _testcapi/immortal.c _testcapi/heaptype_relative.c _testcapi/gc.c _testcapi/sys.c
171+
@MODULE__TESTCAPI_TRUE@_testcapi _testcapimodule.c _testcapi/vectorcall.c _testcapi/vectorcall_limited.c _testcapi/heaptype.c _testcapi/abstract.c _testcapi/bytearray.c _testcapi/bytes.c _testcapi/unicode.c _testcapi/dict.c _testcapi/set.c _testcapi/list.c _testcapi/tuple.c _testcapi/getargs.c _testcapi/pytime.c _testcapi/datetime.c _testcapi/docstring.c _testcapi/mem.c _testcapi/watchers.c _testcapi/long.c _testcapi/float.c _testcapi/complex.c _testcapi/numbers.c _testcapi/structmember.c _testcapi/exceptions.c _testcapi/code.c _testcapi/buffer.c _testcapi/pyos.c _testcapi/run.c _testcapi/file.c _testcapi/codec.c _testcapi/immortal.c _testcapi/heaptype_relative.c _testcapi/gc.c _testcapi/sys.c
172172
@MODULE__TESTCLINIC_TRUE@_testclinic _testclinic.c
173173

174174
# Some testing modules MUST be built as shared libraries.

Modules/_testcapi/parts.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ int _PyTestCapi_Init_Exceptions(PyObject *module);
4949
int _PyTestCapi_Init_Code(PyObject *module);
5050
int _PyTestCapi_Init_Buffer(PyObject *module);
5151
int _PyTestCapi_Init_PyOS(PyObject *module);
52+
int _PyTestCapi_Init_Run(PyObject *module);
5253
int _PyTestCapi_Init_File(PyObject *module);
5354
int _PyTestCapi_Init_Codec(PyObject *module);
5455
int _PyTestCapi_Init_Immortal(PyObject *module);

Modules/_testcapi/run.c

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
#define PY_SSIZE_T_CLEAN
2+
#include "parts.h"
3+
#include "util.h"
4+
5+
#include <stdio.h>
6+
#include <errno.h>
7+
8+
9+
static PyObject *
10+
run_stringflags(PyObject *mod, PyObject *pos_args)
11+
{
12+
const char *str;
13+
Py_ssize_t size;
14+
int start;
15+
PyObject *globals = NULL;
16+
PyObject *locals = NULL;
17+
PyCompilerFlags flags = _PyCompilerFlags_INIT;
18+
PyCompilerFlags *pflags = NULL;
19+
int cf_flags = 0;
20+
int cf_feature_version = 0;
21+
22+
if (!PyArg_ParseTuple(pos_args, "z#iO|Oii",
23+
&str, &size, &start, &globals, &locals,
24+
&cf_flags, &cf_feature_version)) {
25+
return NULL;
26+
}
27+
28+
NULLABLE(globals);
29+
NULLABLE(locals);
30+
if (cf_flags || cf_feature_version) {
31+
flags.cf_flags = cf_flags;
32+
flags.cf_feature_version = cf_feature_version;
33+
pflags = &flags;
34+
}
35+
36+
return PyRun_StringFlags(str, start, globals, locals, pflags);
37+
}
38+
39+
static PyObject *
40+
run_fileexflags(PyObject *mod, PyObject *pos_args)
41+
{
42+
PyObject *result = NULL;
43+
const char *filename = NULL;
44+
Py_ssize_t filename_size;
45+
int start;
46+
PyObject *globals = NULL;
47+
PyObject *locals = NULL;
48+
int closeit = 0;
49+
PyCompilerFlags flags = _PyCompilerFlags_INIT;
50+
PyCompilerFlags *pflags = NULL;
51+
int cf_flags = 0;
52+
int cf_feature_version = 0;
53+
54+
FILE *fp = NULL;
55+
56+
if (!PyArg_ParseTuple(pos_args, "z#iO|Oiii",
57+
&filename, &filename_size, &start, &globals, &locals,
58+
&closeit, &cf_flags, &cf_feature_version)) {
59+
return NULL;
60+
}
61+
62+
NULLABLE(globals);
63+
NULLABLE(locals);
64+
if (cf_flags || cf_feature_version) {
65+
flags.cf_flags = cf_flags;
66+
flags.cf_feature_version = cf_feature_version;
67+
pflags = &flags;
68+
}
69+
70+
fp = fopen(filename, "r");
71+
if (fp == NULL) {
72+
PyErr_SetFromErrnoWithFilename(PyExc_OSError, filename);
73+
return NULL;
74+
}
75+
76+
result = PyRun_FileExFlags(fp, filename, start, globals, locals, closeit, pflags);
77+
78+
#if !defined(__wasi__)
79+
/* The behavior of fileno() after fclose() is undefined. */
80+
if (closeit && result && fileno(fp) >= 0) {
81+
PyErr_SetString(PyExc_AssertionError, "File was not closed after excution");
82+
Py_DECREF(result);
83+
fclose(fp);
84+
return NULL;
85+
}
86+
#endif
87+
if (!closeit && fileno(fp) < 0) {
88+
PyErr_SetString(PyExc_AssertionError, "Bad file descriptor after excution");
89+
Py_XDECREF(result);
90+
return NULL;
91+
}
92+
93+
if (!closeit) {
94+
fclose(fp); /* don't need open file any more*/
95+
}
96+
97+
return result;
98+
}
99+
100+
static PyMethodDef test_methods[] = {
101+
{"run_stringflags", run_stringflags, METH_VARARGS},
102+
{"run_fileexflags", run_fileexflags, METH_VARARGS},
103+
{NULL},
104+
};
105+
106+
int
107+
_PyTestCapi_Init_Run(PyObject *mod)
108+
{
109+
if (PyModule_AddFunctions(mod, test_methods) < 0) {
110+
return -1;
111+
}
112+
return 0;
113+
}

Modules/_testcapimodule.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3946,6 +3946,16 @@ PyInit__testcapi(void)
39463946

39473947
PyModule_AddIntConstant(m, "the_number_three", 3);
39483948

3949+
if (PyModule_AddIntMacro(m, Py_single_input)) {
3950+
return NULL;
3951+
}
3952+
if (PyModule_AddIntMacro(m, Py_file_input)) {
3953+
return NULL;
3954+
}
3955+
if (PyModule_AddIntMacro(m, Py_eval_input)) {
3956+
return NULL;
3957+
}
3958+
39493959
TestError = PyErr_NewException("_testcapi.error", NULL, NULL);
39503960
Py_INCREF(TestError);
39513961
PyModule_AddObject(m, "error", TestError);
@@ -4034,6 +4044,9 @@ PyInit__testcapi(void)
40344044
if (_PyTestCapi_Init_PyOS(m) < 0) {
40354045
return NULL;
40364046
}
4047+
if (_PyTestCapi_Init_Run(m) < 0) {
4048+
return NULL;
4049+
}
40374050
if (_PyTestCapi_Init_File(m) < 0) {
40384051
return NULL;
40394052
}

PCbuild/_testcapi.vcxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@
126126
<ClCompile Include="..\Modules\_testcapi\sys.c" />
127127
<ClCompile Include="..\Modules\_testcapi\immortal.c" />
128128
<ClCompile Include="..\Modules\_testcapi\gc.c" />
129+
<ClCompile Include="..\Modules\_testcapi\run.c" />
129130
</ItemGroup>
130131
<ItemGroup>
131132
<ResourceCompile Include="..\PC\python_nt.rc" />

PCbuild/_testcapi.vcxproj.filters

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,9 @@
105105
<ClCompile Include="..\Modules\_testcapi\gc.c">
106106
<Filter>Source Files</Filter>
107107
</ClCompile>
108+
<ClCompile Include="..\Modules\_testcapi\run.c">
109+
<Filter>Source Files</Filter>
110+
</ClCompile>
108111
</ItemGroup>
109112
<ItemGroup>
110113
<ResourceCompile Include="..\PC\python_nt.rc">

0 commit comments

Comments
 (0)