Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
9635b76
check if gobals is null
ngr-ilmarh Mar 12, 2024
3e97671
make NULL check earlier
ngr-ilmarh Mar 12, 2024
348d895
Merge branch 'main' into fix-issue-116180
Mar 12, 2024
259034c
PEP-7
ngr-ilmarh Mar 12, 2024
85d9f47
Merge branch 'main' into fix-issue-116180
Mar 13, 2024
cf578bd
Merge branch 'main' into fix-issue-116180
Mar 14, 2024
bba6927
Merge branch 'main' into fix-issue-116180
Mar 20, 2024
1b5fe4c
Squashed commit of the following:
ngr-ilmarh Apr 2, 2024
a016a54
Squashed commit of the following:
ngr-ilmarh Apr 2, 2024
173b9ce
Merge branch 'fix-issue-116180' of https://github.com/NGRsoftlab/cpyt…
ngr-ilmarh Apr 2, 2024
86c6e57
Merge branch 'main' into fix-issue-116180
ngr-ilmarh Apr 2, 2024
7503772
change if null to assert
ngr-ilmarh Apr 2, 2024
f175046
Revert "change if null to assert"
ngr-ilmarh Apr 2, 2024
dcab696
bring back error message instead of assert
ngr-ilmarh Apr 2, 2024
9532797
return error instead of segfault and test it
ngr-ilmarh Apr 5, 2024
ee0fd8a
Merge branch 'python:main' into fix-issue-116180
Apr 5, 2024
7eca26f
Merge branch 'python:main' into fix-issue-116180
Apr 5, 2024
08b5c3a
Update Python/pythonrun.c
Apr 5, 2024
23b9fe6
Update Modules/_testcapi/pyrun.c
Apr 5, 2024
acc7eb3
Merge branch 'python:main' into fix-issue-116180
Apr 5, 2024
09c71a9
Merge branch 'python:main' into fix-issue-116180
Apr 8, 2024
806d21c
refactor unittests through helper
ngr-ilmarh Apr 8, 2024
907c86d
fix closeit test when expect exception global == NULL
ngr-ilmarh Apr 8, 2024
af9e9fc
Merge branch 'python:main' into fix-issue-116180
Apr 9, 2024
56f0d3c
refactor python test, add some error handling to module init
ngr-ilmarh Apr 9, 2024
b1d7ab2
fix lost bracket
ngr-ilmarh Apr 9, 2024
f8e29ce
cleanup (and rename)
ngr-ilmarh Apr 9, 2024
efe064d
make windows build tests
ngr-ilmarh Apr 9, 2024
8f33e7a
Merge branch 'python:main' into fix-issue-116180
Apr 10, 2024
296705a
next round of refactoring
ngr-ilmarh Apr 10, 2024
cd58eb6
Merge branch 'python:main' into fix-issue-116180
Apr 11, 2024
c461bd9
Merge branch 'python:main' into fix-issue-116180
Apr 12, 2024
7db13d0
Merge branch 'python:main' into fix-issue-116180
Apr 15, 2024
eef557c
Globals must be dict. Fix filename encoding and file closing in the w…
serhiy-storchaka Apr 15, 2024
66646e9
Merge branch 'python:main' into fix-issue-116180
Apr 15, 2024
45cab7f
Skip tests with undecodable filename if not supported.
serhiy-storchaka Apr 15, 2024
1716ac6
Add a test for PyRun_StringFlags(). Test globals at the lower level.
serhiy-storchaka Apr 16, 2024
1a9532c
Pass filename as bytes.
serhiy-storchaka Apr 16, 2024
6f0a0a4
Merge branch 'main' into fix-issue-116180
Apr 17, 2024
2607806
Merge branch 'python:main' into fix-issue-116180
Apr 17, 2024
7f4c496
Merge branch 'main' into fix-issue-116180
Apr 17, 2024
5974757
Merge branch 'python:main' into fix-issue-116180
Apr 17, 2024
2b4ba12
Update Lib/test/test_capi/test_pyrun.py
serhiy-storchaka Apr 17, 2024
474d0bd
Only tests.
serhiy-storchaka Apr 17, 2024
c8f9b4b
Remove the "Py" prefix.
serhiy-storchaka Apr 17, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
refactor unittests through helper
  • Loading branch information
ngr-ilmarh committed Apr 8, 2024
commit 806d21c2c2855cb131525351fd5684c92dd26019
40 changes: 34 additions & 6 deletions Lib/test/test_capi/test_pyrun.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,43 @@
import unittest
import unittest, os
from test.support import import_helper

# Skip this test if the _testcapi module isn't available.
_testcapi = import_helper.import_module('_testcapi')

class PyRunTests(unittest.TestCase):
pass
NULL = None
Py_single_input = _testcapi.Py_single_input
Py_file_input = _testcapi.Py_file_input
Py_eval_input = _testcapi.Py_eval_input

for name in sorted(dir(_testcapi)):
if name.startswith('test_pyrun'):
setattr(PyRunTests, name, getattr(_testcapi, name))
class PyRunTest(unittest.TestCase):

# create file
def __init__(self, *args, **kwargs):
self.filename = "pyrun_fileexflags_sample.py"
with open(self.filename, 'w') as fp:
fp.write("import sysconfig\n\nconfig = sysconfig.get_config_vars()\n")
fp.close()
unittest.TestCase.__init__(self, *args, **kwargs)

# delete file
def __del__(self):
os.remove(self.filename)

# test function args:
# filename -- filename to run script from
# start -- Py_single_input, Py_file_input, Py_eval_input
# globals -- dict
# locals -- mapping (dict)
# closeit -- whether close file or not true/false
# cf_flags -- compile flags bitmap
# cf_feature_version -- compile flags feature version
def test_pyrun_fileexflags_with_differents_args(self):

self.assertIsNone(_testcapi.eval_pyrun_fileexflags(self.filename, Py_file_input, {}, NULL, 1, 0, 0))
self.assertIsNone(_testcapi.eval_pyrun_fileexflags(self.filename, Py_file_input, {}, {}, 1, 0, 0))
self.assertIsNone(_testcapi.eval_pyrun_fileexflags(self.filename, Py_file_input, {}, {}, 0, 0, 0))
self.assertRaises(SystemError, _testcapi.eval_pyrun_fileexflags, self.filename, Py_file_input, NULL, NULL, 1, 0, 0)
self.assertRaises(SystemError, _testcapi.eval_pyrun_fileexflags, self.filename, Py_file_input, NULL, {}, 1, 0, 0)

if __name__ == "__main__":
unittest.main()
168 changes: 65 additions & 103 deletions Modules/_testcapi/pyrun.c
Original file line number Diff line number Diff line change
@@ -1,134 +1,96 @@
//#include <unistd.h>
//#include <stdio.h>
#include "parts.h"
#include "util.h"

#include <stdio.h>
#include <errno.h>

char *filename = "pyrun_sample.py";

static FILE *create_tempscript(void) {
FILE *fp;
const char *script = "import sysconfig\n\nconfig = sysconfig.get_config_vars()\n";
/* XXX: Need to implement
int PyRun_AnyFileExFlags(FILE *fp, const char *filename, int closeit, PyCompilerFlags *flags)
int PyRun_SimpleStringFlags(const char *command, PyCompilerFlags *flags)
int PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit, PyCompilerFlags *flags)
int PyRun_InteractiveOneFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
int PyRun_InteractiveLoopFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
PyObject *PyRun_StringFlags(const char *str, int start, PyObject *globals, PyObject *locals, PyCompilerFlags *flags)

fp = tmpfile();
if (fp == NULL) {
PyErr_SetString(PyExc_LookupError, "tmpname");
return NULL;
}
fprintf(fp, script);
rewind(fp);
XXX: implemented
PyObject *PyRun_FileExFlags(FILE *fp, const char *filename, int start, PyObject *globals, PyObject *locals, int closeit, PyCompilerFlags *flags)
*/

return fp;
}

static PyObject *
test_pyrun_fileex(PyObject *self, PyObject *Py_UNUSED(ignored))
eval_pyrun_fileexflags(PyObject *mod, PyObject *pos_args)
{
PyObject *pdict, *pResult;
FILE *fp;

pdict = PyDict_New();
if (pdict == NULL) {
assert(PyErr_ExceptionMatches(PyExc_MemoryError));
return NULL;
PyObject *result = NULL;
const char *filename = NULL;
int start;
PyObject *globals = NULL;
PyObject *locals = NULL;
int closeit = 1;
PyCompilerFlags flags = _PyCompilerFlags_INIT;
PyCompilerFlags *pflags = NULL;
int cf_flags = 0;
int cf_feature_version = 0;
int fn;

FILE *fp = NULL;

if (!PyArg_ParseTuple(pos_args,
"ziO|Oiii:eval_pyrun_fileexflags",
&filename,
&start,
&globals,
&locals,
&closeit,
&cf_flags,
&cf_feature_version)) {
goto exit;
}

fp = create_tempscript();
if (fp == NULL) {
return NULL;
NULLABLE(globals);
NULLABLE(locals);
if (cf_flags || cf_feature_version) {
flags.cf_flags = cf_flags;
flags.cf_feature_version = cf_feature_version;
pflags = &flags;
}

pResult = PyRun_FileEx(fp, filename, Py_file_input, pdict, pdict, 1);
if (pResult == NULL) {
return NULL;
if (globals && !PyDict_Check(globals)) {
PyErr_SetString(PyExc_TypeError, "globals must be a dict");
goto exit;
}

Py_RETURN_NONE;
}

static PyObject *
test_pyrun_fileex_null_locals(PyObject *self, PyObject *Py_UNUSED(ignored))
{
PyObject *pdict, *pResult;
FILE *fp;

pdict = PyDict_New();
if (pdict == NULL) {
assert(PyErr_ExceptionMatches(PyExc_MemoryError));
return NULL;
if (locals && !PyMapping_Check(locals)) {
PyErr_SetString(PyExc_TypeError, "locals must be a mapping");
goto exit;
}

fp = create_tempscript();
fp = fopen(filename, "r");
if (fp == NULL) {
return NULL;
PyErr_SetFromErrnoWithFilename(PyExc_OSError, filename);
goto exit;
}

pResult = PyRun_FileEx(fp, filename, Py_file_input, pdict, NULL, 1);
if (pResult == NULL) {
return NULL;
}
result = PyRun_FileExFlags(fp, filename, start, globals, locals, closeit, pflags);

Py_RETURN_NONE;
}

static PyObject *
test_pyrun_fileex_null_globals(PyObject *self, PyObject *Py_UNUSED(ignored))
{
PyObject *pdict, *pResult, *exc;
FILE *fp;
char *filename = "pyrun_sample.py";

pdict = PyDict_New();
if (pdict == NULL) {
assert(PyErr_ExceptionMatches(PyExc_MemoryError));
return NULL;
}

fp = create_tempscript();
if (fp == NULL) {
return NULL;
}

pResult = PyRun_FileEx(fp, filename, Py_file_input, NULL, pdict, 1);

exc = PyErr_GetRaisedException();
assert(exc);
assert(PyErr_GivenExceptionMatches(exc, PyExc_SystemError));
PyErr_Clear();

Py_RETURN_NONE;
}

static PyObject *
test_pyrun_fileex_null_globals_locals(PyObject *self, PyObject *Py_UNUSED(ignored))
{
PyObject *pdict, *pResult, *exc;
FILE *fp;
char *filename = "pyrun_sample.py";

pdict = PyDict_New();
if (pdict == NULL) {
assert(PyErr_ExceptionMatches(PyExc_MemoryError));
return NULL;
fn = fileno(fp);
if (closeit && fn > 0) {
PyErr_SetString(PyExc_SystemError, "File was not closed after excution");
goto exit;
}

fp = create_tempscript();
if (fp == NULL) {
return NULL;
else if (!closeit && fn < 0) {
PyErr_SetString(PyExc_SystemError, "Bad file descriptor after excution");
goto exit;
}

pResult = PyRun_FileEx(fp, filename, Py_file_input, NULL, NULL, 1);
exc = PyErr_GetRaisedException();
assert(exc);
assert(PyErr_GivenExceptionMatches(exc, PyExc_SystemError));
PyErr_Clear();
exit:

Py_RETURN_NONE;
return result;
}

static PyMethodDef test_methods[] = {
{"test_pyrun_fileex", test_pyrun_fileex, METH_NOARGS},
{"test_pyrun_fileex_null_locals", test_pyrun_fileex_null_locals, METH_NOARGS},
{"test_pyrun_fileex_null_globals", test_pyrun_fileex_null_globals, METH_NOARGS},
{"test_pyrun_fileex_null_globals_locals", test_pyrun_fileex_null_globals_locals, METH_NOARGS},
{"eval_pyrun_fileexflags", eval_pyrun_fileexflags, METH_VARARGS},
{NULL},
};

Expand Down
4 changes: 4 additions & 0 deletions Modules/_testcapimodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -3951,6 +3951,10 @@ PyInit__testcapi(void)
PyModule_AddIntConstant(m, "the_number_three", 3);
PyModule_AddIntMacro(m, Py_C_RECURSION_LIMIT);

PyModule_AddIntConstant(m, "Py_single_input", Py_single_input);
PyModule_AddIntConstant(m, "Py_file_input", Py_file_input);
PyModule_AddIntConstant(m, "Py_eval_input", Py_eval_input);

testcapistate_t *state = get_testcapi_state(m);
state->error = PyErr_NewException("_testcapi.error", NULL, NULL);
PyModule_AddObject(m, "error", state->error);
Expand Down