1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
|
#define PYTESTCAPI_NEED_INTERNAL_API
#include "parts.h"
#include "util.h"
#include "pycore_fileutils.h" // _Py_IsValidFD()
#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;
}
int fd = fileno(fp);
result = PyRun_FileExFlags(fp, filename, start, globals, locals, closeit, pflags);
if (closeit && result && _Py_IsValidFD(fd)) {
PyErr_SetString(PyExc_AssertionError, "File was not closed after excution");
Py_DECREF(result);
fclose(fp);
return NULL;
}
if (!closeit && !_Py_IsValidFD(fd)) {
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;
}
|