2024-04-30 17:32:55 -03:00
|
|
|
#define PYTESTCAPI_NEED_INTERNAL_API
|
2024-04-17 17:45:35 -03:00
|
|
|
#include "parts.h"
|
|
|
|
#include "util.h"
|
2024-04-30 17:32:55 -03:00
|
|
|
#include "pycore_fileutils.h" // _Py_IsValidFD()
|
2024-04-17 17:45:35 -03:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
run_stringflags(PyObject *mod, PyObject *pos_args)
|
|
|
|
{
|
|
|
|
const char *str;
|
|
|
|
Py_ssize_t size;
|
|
|
|
int start;
|
|
|
|
PyObject *globals = NULL;
|
|
|
|
PyObject *locals = NULL;
|
|
|
|
PyCompilerFlags flags = _PyCompilerFlags_INIT;
|
|
|
|
PyCompilerFlags *pflags = NULL;
|
|
|
|
int cf_flags = 0;
|
|
|
|
int cf_feature_version = 0;
|
|
|
|
|
|
|
|
if (!PyArg_ParseTuple(pos_args, "z#iO|Oii",
|
|
|
|
&str, &size, &start, &globals, &locals,
|
|
|
|
&cf_flags, &cf_feature_version)) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
return PyRun_StringFlags(str, start, globals, locals, pflags);
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
run_fileexflags(PyObject *mod, PyObject *pos_args)
|
|
|
|
{
|
|
|
|
PyObject *result = NULL;
|
|
|
|
const char *filename = NULL;
|
|
|
|
Py_ssize_t filename_size;
|
|
|
|
int start;
|
|
|
|
PyObject *globals = NULL;
|
|
|
|
PyObject *locals = NULL;
|
|
|
|
int closeit = 0;
|
|
|
|
PyCompilerFlags flags = _PyCompilerFlags_INIT;
|
|
|
|
PyCompilerFlags *pflags = NULL;
|
|
|
|
int cf_flags = 0;
|
|
|
|
int cf_feature_version = 0;
|
|
|
|
|
|
|
|
FILE *fp = NULL;
|
|
|
|
|
|
|
|
if (!PyArg_ParseTuple(pos_args, "z#iO|Oiii",
|
|
|
|
&filename, &filename_size, &start, &globals, &locals,
|
|
|
|
&closeit, &cf_flags, &cf_feature_version)) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
fp = fopen(filename, "r");
|
|
|
|
if (fp == NULL) {
|
|
|
|
PyErr_SetFromErrnoWithFilename(PyExc_OSError, filename);
|
|
|
|
return NULL;
|
|
|
|
}
|
2024-04-30 17:32:55 -03:00
|
|
|
int fd = fileno(fp);
|
2024-04-17 17:45:35 -03:00
|
|
|
|
|
|
|
result = PyRun_FileExFlags(fp, filename, start, globals, locals, closeit, pflags);
|
|
|
|
|
2024-04-30 17:32:55 -03:00
|
|
|
if (closeit && result && _Py_IsValidFD(fd)) {
|
2024-04-17 17:45:35 -03:00
|
|
|
PyErr_SetString(PyExc_AssertionError, "File was not closed after excution");
|
|
|
|
Py_DECREF(result);
|
|
|
|
fclose(fp);
|
|
|
|
return NULL;
|
|
|
|
}
|
2024-04-30 17:32:55 -03:00
|
|
|
|
|
|
|
if (!closeit && !_Py_IsValidFD(fd)) {
|
2024-04-17 17:45:35 -03:00
|
|
|
PyErr_SetString(PyExc_AssertionError, "Bad file descriptor after excution");
|
|
|
|
Py_XDECREF(result);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!closeit) {
|
|
|
|
fclose(fp); /* don't need open file any more*/
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyMethodDef test_methods[] = {
|
|
|
|
{"run_stringflags", run_stringflags, METH_VARARGS},
|
|
|
|
{"run_fileexflags", run_fileexflags, METH_VARARGS},
|
|
|
|
{NULL},
|
|
|
|
};
|
|
|
|
|
|
|
|
int
|
|
|
|
_PyTestCapi_Init_Run(PyObject *mod)
|
|
|
|
{
|
|
|
|
if (PyModule_AddFunctions(mod, test_methods) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|