| 1 |
#define PYTESTCAPI_NEED_INTERNAL_API |
| 2 |
#include "parts.h" |
| 3 |
#include "util.h" |
| 4 |
#include "pycore_fileutils.h" |
| 5 |
|
| 6 |
#include <stdio.h> |
| 7 |
#include <errno.h> |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
#undef PyRun_AnyFile |
| 12 |
#undef PyRun_AnyFileEx |
| 13 |
#undef PyRun_AnyFileFlags |
| 14 |
#undef PyRun_File |
| 15 |
#undef PyRun_FileEx |
| 16 |
#undef PyRun_FileFlags |
| 17 |
#undef PyRun_SimpleFile |
| 18 |
#undef PyRun_SimpleFileEx |
| 19 |
#undef PyRun_String |
| 20 |
#undef PyRun_SimpleString |
| 21 |
#undef Py_CompileString |
| 22 |
#undef Py_CompileStringFlags |
| 23 |
#undef PyRun_InteractiveOne |
| 24 |
#undef PyRun_InteractiveLoop |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
static PyObject* |
| 29 |
run_string(PyObject *mod, PyObject *args) |
| 30 |
{ |
| 31 |
const char *str; |
| 32 |
int start; |
| 33 |
PyObject *globals = NULL; |
| 34 |
PyObject *locals = NULL; |
| 35 |
|
| 36 |
if (!PyArg_ParseTuple(args, "yi|OO", &str, &start, &globals, &locals)) { |
| 37 |
return NULL; |
| 38 |
} |
| 39 |
NULLABLE(globals); |
| 40 |
NULLABLE(locals); |
| 41 |
|
| 42 |
return PyRun_String(str, start, globals, locals); |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
static PyObject* |
| 47 |
run_stringflags(PyObject *mod, PyObject *args) |
| 48 |
{ |
| 49 |
const char *str; |
| 50 |
Py_ssize_t size; |
| 51 |
int start; |
| 52 |
PyObject *globals = NULL; |
| 53 |
PyObject *locals = NULL; |
| 54 |
PyCompilerFlags flags = _PyCompilerFlags_INIT; |
| 55 |
PyCompilerFlags *pflags = NULL; |
| 56 |
int cf_flags = 0; |
| 57 |
int cf_feature_version = 0; |
| 58 |
|
| 59 |
if (!PyArg_ParseTuple(args, "z#iO|Oii", |
| 60 |
&str, &size, &start, &globals, &locals, |
| 61 |
&cf_flags, &cf_feature_version)) { |
| 62 |
return NULL; |
| 63 |
} |
| 64 |
NULLABLE(globals); |
| 65 |
NULLABLE(locals); |
| 66 |
if (cf_flags || cf_feature_version) { |
| 67 |
flags.cf_flags = cf_flags; |
| 68 |
flags.cf_feature_version = cf_feature_version; |
| 69 |
pflags = &flags; |
| 70 |
} |
| 71 |
|
| 72 |
return PyRun_StringFlags(str, start, globals, locals, pflags); |
| 73 |
} |
| 74 |
|
| 75 |
static FILE* |
| 76 |
open_file(PyObject *filename, int *closeit) |
| 77 |
{ |
| 78 |
if (PyUnicode_EqualToUTF8(filename, "<stdin>")) { |
| 79 |
if (closeit) { |
| 80 |
|
| 81 |
*closeit = 0; |
| 82 |
} |
| 83 |
return stdin; |
| 84 |
} |
| 85 |
return Py_fopen(filename, "r"); |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
static PyObject* |
| 91 |
run_simplefile(PyObject *mod, PyObject *args) |
| 92 |
{ |
| 93 |
PyObject *open_filename; |
| 94 |
const char *filename; |
| 95 |
|
| 96 |
if (!PyArg_ParseTuple(args, "Oy", &open_filename, &filename)) { |
| 97 |
return NULL; |
| 98 |
} |
| 99 |
|
| 100 |
FILE *fp = open_file(open_filename, NULL); |
| 101 |
if (fp == NULL) { |
| 102 |
return NULL; |
| 103 |
} |
| 104 |
int fd = fileno(fp); |
| 105 |
|
| 106 |
int res = PyRun_SimpleFile(fp, filename); |
| 107 |
|
| 108 |
assert(_Py_IsValidFD(fd)); |
| 109 |
fclose(fp); |
| 110 |
|
| 111 |
assert(!PyErr_Occurred()); |
| 112 |
return PyLong_FromLong(res); |
| 113 |
} |
| 114 |
|
| 115 |
static int |
| 116 |
close_fd(FILE *fp, int fd, int closeit) |
| 117 |
{ |
| 118 |
if (closeit && _Py_IsValidFD(fd)) { |
| 119 |
PyErr_SetString(PyExc_AssertionError, "File was not closed after execution"); |
| 120 |
fclose(fp); |
| 121 |
return -1; |
| 122 |
} |
| 123 |
|
| 124 |
if (!closeit && !_Py_IsValidFD(fd)) { |
| 125 |
PyErr_SetString(PyExc_AssertionError, "Bad file descriptor after execution"); |
| 126 |
return -1; |
| 127 |
} |
| 128 |
|
| 129 |
if (!closeit) { |
| 130 |
fclose(fp); |
| 131 |
} |
| 132 |
return 0; |
| 133 |
} |
| 134 |
|
| 135 |
static int |
| 136 |
close_fd_result(FILE *fp, int fd, int closeit, PyObject *result) |
| 137 |
{ |
| 138 |
if (closeit && result && _Py_IsValidFD(fd)) { |
| 139 |
PyErr_SetString(PyExc_AssertionError, "File was not closed after execution"); |
| 140 |
Py_DECREF(result); |
| 141 |
fclose(fp); |
| 142 |
return -1; |
| 143 |
} |
| 144 |
|
| 145 |
if (!closeit && !_Py_IsValidFD(fd)) { |
| 146 |
PyErr_SetString(PyExc_AssertionError, "Bad file descriptor after execution"); |
| 147 |
Py_XDECREF(result); |
| 148 |
return -1; |
| 149 |
} |
| 150 |
|
| 151 |
if (!closeit) { |
| 152 |
fclose(fp); |
| 153 |
} |
| 154 |
return 0; |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
static PyObject* |
| 159 |
run_simplefileex(PyObject *mod, PyObject *args) |
| 160 |
{ |
| 161 |
PyObject *open_filename; |
| 162 |
const char *filename; |
| 163 |
int closeit = 0; |
| 164 |
|
| 165 |
if (!PyArg_ParseTuple(args, "Oy|i", |
| 166 |
&open_filename, &filename, &closeit)) { |
| 167 |
return NULL; |
| 168 |
} |
| 169 |
|
| 170 |
FILE *fp = open_file(open_filename, &closeit); |
| 171 |
if (fp == NULL) { |
| 172 |
return NULL; |
| 173 |
} |
| 174 |
int fd = fileno(fp); |
| 175 |
|
| 176 |
int res = PyRun_SimpleFileEx(fp, filename, closeit); |
| 177 |
|
| 178 |
if (close_fd(fp, fd, closeit) < 0) { |
| 179 |
return NULL; |
| 180 |
} |
| 181 |
|
| 182 |
assert(!PyErr_Occurred()); |
| 183 |
return PyLong_FromLong(res); |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
static PyObject* |
| 188 |
run_simplefileexflags(PyObject *mod, PyObject *args) |
| 189 |
{ |
| 190 |
PyObject *open_filename; |
| 191 |
const char *filename; |
| 192 |
int closeit = 0; |
| 193 |
PyCompilerFlags flags = _PyCompilerFlags_INIT; |
| 194 |
PyCompilerFlags *pflags = NULL; |
| 195 |
int cf_flags = 0; |
| 196 |
|
| 197 |
if (!PyArg_ParseTuple(args, "Oy|ii", |
| 198 |
&open_filename, &filename, &closeit, &cf_flags)) { |
| 199 |
return NULL; |
| 200 |
} |
| 201 |
|
| 202 |
FILE *fp = open_file(open_filename, &closeit); |
| 203 |
if (fp == NULL) { |
| 204 |
return NULL; |
| 205 |
} |
| 206 |
int fd = fileno(fp); |
| 207 |
|
| 208 |
if (cf_flags) { |
| 209 |
flags.cf_flags = cf_flags; |
| 210 |
pflags = &flags; |
| 211 |
} |
| 212 |
|
| 213 |
int res = PyRun_SimpleFileExFlags(fp, filename, closeit, pflags); |
| 214 |
|
| 215 |
if (close_fd(fp, fd, closeit) < 0) { |
| 216 |
return NULL; |
| 217 |
} |
| 218 |
|
| 219 |
assert(!PyErr_Occurred()); |
| 220 |
return PyLong_FromLong(res); |
| 221 |
} |
| 222 |
|
| 223 |
|
| 224 |
static PyObject* |
| 225 |
run_anyfile(PyObject *mod, PyObject *args) |
| 226 |
{ |
| 227 |
PyObject *open_filename; |
| 228 |
const char *filename; |
| 229 |
|
| 230 |
if (!PyArg_ParseTuple(args, "Oy", &open_filename, &filename)) { |
| 231 |
return NULL; |
| 232 |
} |
| 233 |
|
| 234 |
FILE *fp = open_file(open_filename, NULL); |
| 235 |
if (fp == NULL) { |
| 236 |
return NULL; |
| 237 |
} |
| 238 |
int fd = fileno(fp); |
| 239 |
|
| 240 |
int res = PyRun_AnyFile(fp, filename); |
| 241 |
|
| 242 |
assert(_Py_IsValidFD(fd)); |
| 243 |
fclose(fp); |
| 244 |
|
| 245 |
assert(!PyErr_Occurred()); |
| 246 |
return PyLong_FromLong(res); |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
static PyObject* |
| 251 |
run_anyfileflags(PyObject *mod, PyObject *args) |
| 252 |
{ |
| 253 |
PyObject *open_filename; |
| 254 |
const char *filename; |
| 255 |
PyCompilerFlags flags = _PyCompilerFlags_INIT; |
| 256 |
PyCompilerFlags *pflags = NULL; |
| 257 |
int cf_flags = 0; |
| 258 |
|
| 259 |
if (!PyArg_ParseTuple(args, "Oy|i", |
| 260 |
&open_filename, &filename, &cf_flags)) { |
| 261 |
return NULL; |
| 262 |
} |
| 263 |
|
| 264 |
FILE *fp = open_file(open_filename, NULL); |
| 265 |
if (fp == NULL) { |
| 266 |
return NULL; |
| 267 |
} |
| 268 |
int fd = fileno(fp); |
| 269 |
|
| 270 |
if (cf_flags) { |
| 271 |
flags.cf_flags = cf_flags; |
| 272 |
pflags = &flags; |
| 273 |
} |
| 274 |
|
| 275 |
int res = PyRun_AnyFileFlags(fp, filename, pflags); |
| 276 |
|
| 277 |
assert(_Py_IsValidFD(fd)); |
| 278 |
fclose(fp); |
| 279 |
|
| 280 |
assert(!PyErr_Occurred()); |
| 281 |
return PyLong_FromLong(res); |
| 282 |
} |
| 283 |
|
| 284 |
|
| 285 |
static PyObject* |
| 286 |
run_anyfileex(PyObject *mod, PyObject *args) |
| 287 |
{ |
| 288 |
PyObject *open_filename; |
| 289 |
const char *filename; |
| 290 |
int closeit = 0; |
| 291 |
|
| 292 |
if (!PyArg_ParseTuple(args, "Oy|i", |
| 293 |
&open_filename, &filename, &closeit)) { |
| 294 |
return NULL; |
| 295 |
} |
| 296 |
|
| 297 |
FILE *fp = open_file(open_filename, &closeit); |
| 298 |
if (fp == NULL) { |
| 299 |
return NULL; |
| 300 |
} |
| 301 |
int fd = fileno(fp); |
| 302 |
|
| 303 |
int res = PyRun_AnyFileEx(fp, filename, closeit); |
| 304 |
|
| 305 |
if (close_fd(fp, fd, closeit) < 0) { |
| 306 |
return NULL; |
| 307 |
} |
| 308 |
|
| 309 |
assert(!PyErr_Occurred()); |
| 310 |
return PyLong_FromLong(res); |
| 311 |
} |
| 312 |
|
| 313 |
|
| 314 |
static PyObject* |
| 315 |
run_anyfileexflags(PyObject *mod, PyObject *args) |
| 316 |
{ |
| 317 |
PyObject *open_filename; |
| 318 |
const char *filename; |
| 319 |
int closeit = 0; |
| 320 |
PyCompilerFlags flags = _PyCompilerFlags_INIT; |
| 321 |
PyCompilerFlags *pflags = NULL; |
| 322 |
int cf_flags = 0; |
| 323 |
|
| 324 |
if (!PyArg_ParseTuple(args, "Oy|ii", |
| 325 |
&open_filename, &filename, &closeit, &cf_flags)) { |
| 326 |
return NULL; |
| 327 |
} |
| 328 |
|
| 329 |
FILE *fp = open_file(open_filename, &closeit); |
| 330 |
if (fp == NULL) { |
| 331 |
return NULL; |
| 332 |
} |
| 333 |
int fd = fileno(fp); |
| 334 |
|
| 335 |
if (cf_flags) { |
| 336 |
flags.cf_flags = cf_flags; |
| 337 |
pflags = &flags; |
| 338 |
} |
| 339 |
|
| 340 |
int res = PyRun_AnyFileExFlags(fp, filename, closeit, pflags); |
| 341 |
|
| 342 |
if (close_fd(fp, fd, closeit) < 0) { |
| 343 |
return NULL; |
| 344 |
} |
| 345 |
|
| 346 |
assert(!PyErr_Occurred()); |
| 347 |
return PyLong_FromLong(res); |
| 348 |
} |
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
static PyObject* |
| 353 |
run_interactiveone(PyObject *mod, PyObject *args) |
| 354 |
{ |
| 355 |
PyObject *open_filename; |
| 356 |
char *filename; |
| 357 |
|
| 358 |
if (!PyArg_ParseTuple(args, "Oy", &open_filename, &filename)) { |
| 359 |
return NULL; |
| 360 |
} |
| 361 |
|
| 362 |
FILE *fp = open_file(open_filename, NULL); |
| 363 |
if (fp == NULL) { |
| 364 |
return NULL; |
| 365 |
} |
| 366 |
int fd = fileno(fp); |
| 367 |
|
| 368 |
int res = PyRun_InteractiveOne(fp, filename); |
| 369 |
assert(!PyErr_Occurred()); |
| 370 |
|
| 371 |
assert(_Py_IsValidFD(fd)); |
| 372 |
fclose(fp); |
| 373 |
|
| 374 |
return PyLong_FromLong(res); |
| 375 |
} |
| 376 |
|
| 377 |
|
| 378 |
static PyObject* |
| 379 |
run_interactiveoneflags(PyObject *mod, PyObject *args) |
| 380 |
{ |
| 381 |
PyObject *open_filename; |
| 382 |
char *filename; |
| 383 |
PyCompilerFlags flags = _PyCompilerFlags_INIT; |
| 384 |
PyCompilerFlags *pflags = NULL; |
| 385 |
int cf_flags = 0; |
| 386 |
|
| 387 |
if (!PyArg_ParseTuple(args, "Oy|i", |
| 388 |
&open_filename, &filename, &cf_flags)) { |
| 389 |
return NULL; |
| 390 |
} |
| 391 |
|
| 392 |
FILE *fp = open_file(open_filename, NULL); |
| 393 |
if (fp == NULL) { |
| 394 |
return NULL; |
| 395 |
} |
| 396 |
int fd = fileno(fp); |
| 397 |
|
| 398 |
if (cf_flags) { |
| 399 |
flags.cf_flags = cf_flags; |
| 400 |
pflags = &flags; |
| 401 |
} |
| 402 |
|
| 403 |
int res = PyRun_InteractiveOneFlags(fp, filename, pflags); |
| 404 |
assert(!PyErr_Occurred()); |
| 405 |
|
| 406 |
assert(_Py_IsValidFD(fd)); |
| 407 |
fclose(fp); |
| 408 |
|
| 409 |
return PyLong_FromLong(res); |
| 410 |
} |
| 411 |
|
| 412 |
|
| 413 |
static PyObject* |
| 414 |
run_interactiveoneobject(PyObject *mod, PyObject *args) |
| 415 |
{ |
| 416 |
PyObject *open_filename; |
| 417 |
PyObject *filename; |
| 418 |
PyCompilerFlags flags = _PyCompilerFlags_INIT; |
| 419 |
PyCompilerFlags *pflags = NULL; |
| 420 |
int cf_flags = 0; |
| 421 |
|
| 422 |
if (!PyArg_ParseTuple(args, "OO|i", |
| 423 |
&open_filename, &filename, &cf_flags)) { |
| 424 |
return NULL; |
| 425 |
} |
| 426 |
|
| 427 |
FILE *fp = open_file(open_filename, NULL); |
| 428 |
if (fp == NULL) { |
| 429 |
return NULL; |
| 430 |
} |
| 431 |
int fd = fileno(fp); |
| 432 |
|
| 433 |
if (cf_flags) { |
| 434 |
flags.cf_flags = cf_flags; |
| 435 |
pflags = &flags; |
| 436 |
} |
| 437 |
|
| 438 |
int res = PyRun_InteractiveOneObject(fp, filename, pflags); |
| 439 |
assert(!PyErr_Occurred()); |
| 440 |
|
| 441 |
assert(_Py_IsValidFD(fd)); |
| 442 |
fclose(fp); |
| 443 |
|
| 444 |
return PyLong_FromLong(res); |
| 445 |
} |
| 446 |
|
| 447 |
|
| 448 |
static PyObject* |
| 449 |
run_file(PyObject *mod, PyObject *args) |
| 450 |
{ |
| 451 |
PyObject *result = NULL; |
| 452 |
const char *filename = NULL; |
| 453 |
Py_ssize_t filename_size; |
| 454 |
int start; |
| 455 |
PyObject *globals = NULL; |
| 456 |
PyObject *locals = NULL; |
| 457 |
|
| 458 |
if (!PyArg_ParseTuple(args, "z#iO|O", |
| 459 |
&filename, &filename_size, &start, |
| 460 |
&globals, &locals)) { |
| 461 |
return NULL; |
| 462 |
} |
| 463 |
NULLABLE(globals); |
| 464 |
NULLABLE(locals); |
| 465 |
|
| 466 |
FILE *fp = fopen(filename, "r"); |
| 467 |
if (fp == NULL) { |
| 468 |
PyErr_SetFromErrnoWithFilename(PyExc_OSError, filename); |
| 469 |
return NULL; |
| 470 |
} |
| 471 |
int fd = fileno(fp); |
| 472 |
|
| 473 |
result = PyRun_File(fp, filename, start, globals, locals); |
| 474 |
|
| 475 |
assert(_Py_IsValidFD(fd)); |
| 476 |
fclose(fp); |
| 477 |
|
| 478 |
return result; |
| 479 |
} |
| 480 |
|
| 481 |
|
| 482 |
static PyObject* |
| 483 |
run_fileex(PyObject *mod, PyObject *args) |
| 484 |
{ |
| 485 |
const char *filename = NULL; |
| 486 |
Py_ssize_t filename_size; |
| 487 |
int start; |
| 488 |
PyObject *globals = NULL; |
| 489 |
PyObject *locals = NULL; |
| 490 |
int closeit = 0; |
| 491 |
|
| 492 |
if (!PyArg_ParseTuple(args, "z#iO|Oi", |
| 493 |
&filename, &filename_size, &start, &globals, &locals, |
| 494 |
&closeit)) { |
| 495 |
return NULL; |
| 496 |
} |
| 497 |
NULLABLE(globals); |
| 498 |
NULLABLE(locals); |
| 499 |
|
| 500 |
FILE *fp = fopen(filename, "r"); |
| 501 |
if (fp == NULL) { |
| 502 |
PyErr_SetFromErrnoWithFilename(PyExc_OSError, filename); |
| 503 |
return NULL; |
| 504 |
} |
| 505 |
int fd = fileno(fp); |
| 506 |
|
| 507 |
PyObject *result = PyRun_FileEx(fp, filename, start, globals, locals, |
| 508 |
closeit); |
| 509 |
|
| 510 |
if (close_fd_result(fp, fd, closeit, result) < 0) { |
| 511 |
return NULL; |
| 512 |
} |
| 513 |
|
| 514 |
return result; |
| 515 |
} |
| 516 |
|
| 517 |
|
| 518 |
static PyObject* |
| 519 |
run_fileflags(PyObject *mod, PyObject *args) |
| 520 |
{ |
| 521 |
const char *filename = NULL; |
| 522 |
Py_ssize_t filename_size; |
| 523 |
int start; |
| 524 |
PyObject *globals = NULL; |
| 525 |
PyObject *locals = NULL; |
| 526 |
PyCompilerFlags flags = _PyCompilerFlags_INIT; |
| 527 |
PyCompilerFlags *pflags = NULL; |
| 528 |
int cf_flags = 0; |
| 529 |
int cf_feature_version = 0; |
| 530 |
|
| 531 |
if (!PyArg_ParseTuple(args, "z#iO|Oii", |
| 532 |
&filename, &filename_size, &start, &globals, &locals, |
| 533 |
&cf_flags, &cf_feature_version)) { |
| 534 |
return NULL; |
| 535 |
} |
| 536 |
NULLABLE(globals); |
| 537 |
NULLABLE(locals); |
| 538 |
if (cf_flags || cf_feature_version) { |
| 539 |
flags.cf_flags = cf_flags; |
| 540 |
flags.cf_feature_version = cf_feature_version; |
| 541 |
pflags = &flags; |
| 542 |
} |
| 543 |
|
| 544 |
FILE *fp = fopen(filename, "r"); |
| 545 |
if (fp == NULL) { |
| 546 |
PyErr_SetFromErrnoWithFilename(PyExc_OSError, filename); |
| 547 |
return NULL; |
| 548 |
} |
| 549 |
int fd = fileno(fp); |
| 550 |
|
| 551 |
PyObject *result = PyRun_FileFlags(fp, filename, start, globals, locals, |
| 552 |
pflags); |
| 553 |
|
| 554 |
assert(_Py_IsValidFD(fd)); |
| 555 |
fclose(fp); |
| 556 |
|
| 557 |
return result; |
| 558 |
} |
| 559 |
|
| 560 |
static PyObject* |
| 561 |
run_fileexflags(PyObject *mod, PyObject *args) |
| 562 |
{ |
| 563 |
const char *filename = NULL; |
| 564 |
Py_ssize_t filename_size; |
| 565 |
int start; |
| 566 |
PyObject *globals = NULL; |
| 567 |
PyObject *locals = NULL; |
| 568 |
int closeit = 0; |
| 569 |
PyCompilerFlags flags = _PyCompilerFlags_INIT; |
| 570 |
PyCompilerFlags *pflags = NULL; |
| 571 |
int cf_flags = 0; |
| 572 |
int cf_feature_version = 0; |
| 573 |
|
| 574 |
if (!PyArg_ParseTuple(args, "z#iO|Oiii", |
| 575 |
&filename, &filename_size, &start, &globals, &locals, |
| 576 |
&closeit, &cf_flags, &cf_feature_version)) { |
| 577 |
return NULL; |
| 578 |
} |
| 579 |
NULLABLE(globals); |
| 580 |
NULLABLE(locals); |
| 581 |
if (cf_flags || cf_feature_version) { |
| 582 |
flags.cf_flags = cf_flags; |
| 583 |
flags.cf_feature_version = cf_feature_version; |
| 584 |
pflags = &flags; |
| 585 |
} |
| 586 |
|
| 587 |
FILE *fp = fopen(filename, "r"); |
| 588 |
if (fp == NULL) { |
| 589 |
PyErr_SetFromErrnoWithFilename(PyExc_OSError, filename); |
| 590 |
return NULL; |
| 591 |
} |
| 592 |
int fd = fileno(fp); |
| 593 |
|
| 594 |
PyObject *result = PyRun_FileExFlags(fp, filename, start, globals, locals, |
| 595 |
closeit, pflags); |
| 596 |
|
| 597 |
if (close_fd_result(fp, fd, closeit, result) < 0) { |
| 598 |
return NULL; |
| 599 |
} |
| 600 |
|
| 601 |
return result; |
| 602 |
} |
| 603 |
|
| 604 |
static PyObject* |
| 605 |
run_simplestring(PyObject *mod, PyObject *args) |
| 606 |
{ |
| 607 |
const char *str; |
| 608 |
if (!PyArg_ParseTuple(args, "y", &str)) { |
| 609 |
return NULL; |
| 610 |
} |
| 611 |
|
| 612 |
int res = PyRun_SimpleString(str); |
| 613 |
assert(!PyErr_Occurred()); |
| 614 |
return PyLong_FromLong(res); |
| 615 |
} |
| 616 |
|
| 617 |
static PyObject* |
| 618 |
run_simplestringflags(PyObject *mod, PyObject *args) |
| 619 |
{ |
| 620 |
const char *str; |
| 621 |
PyCompilerFlags flags = _PyCompilerFlags_INIT; |
| 622 |
PyCompilerFlags *pflags = NULL; |
| 623 |
int cf_flags = 0; |
| 624 |
|
| 625 |
if (!PyArg_ParseTuple(args, "y|i", &str, &flags)) { |
| 626 |
return NULL; |
| 627 |
} |
| 628 |
|
| 629 |
if (cf_flags) { |
| 630 |
flags.cf_flags = cf_flags; |
| 631 |
pflags = &flags; |
| 632 |
} |
| 633 |
|
| 634 |
int res = PyRun_SimpleStringFlags(str, pflags); |
| 635 |
assert(!PyErr_Occurred()); |
| 636 |
return PyLong_FromLong(res); |
| 637 |
} |
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
static PyObject* |
| 642 |
run_interactiveloop(PyObject *mod, PyObject *args) |
| 643 |
{ |
| 644 |
PyObject *open_filename; |
| 645 |
char *filename; |
| 646 |
|
| 647 |
if (!PyArg_ParseTuple(args, "Oy|i", &open_filename, &filename)) { |
| 648 |
return NULL; |
| 649 |
} |
| 650 |
|
| 651 |
FILE *fp = open_file(open_filename, NULL); |
| 652 |
if (fp == NULL) { |
| 653 |
return NULL; |
| 654 |
} |
| 655 |
int fd = fileno(fp); |
| 656 |
|
| 657 |
int res = PyRun_InteractiveLoop(fp, filename); |
| 658 |
assert(!PyErr_Occurred()); |
| 659 |
|
| 660 |
assert(_Py_IsValidFD(fd)); |
| 661 |
fclose(fp); |
| 662 |
|
| 663 |
return PyLong_FromLong(res); |
| 664 |
} |
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
static PyObject* |
| 669 |
run_interactiveloopflags(PyObject *mod, PyObject *args) |
| 670 |
{ |
| 671 |
PyObject *open_filename; |
| 672 |
char *filename; |
| 673 |
PyCompilerFlags flags = _PyCompilerFlags_INIT; |
| 674 |
PyCompilerFlags *pflags = NULL; |
| 675 |
int cf_flags = 0; |
| 676 |
|
| 677 |
if (!PyArg_ParseTuple(args, "Oy|i", |
| 678 |
&open_filename, &filename, &cf_flags)) { |
| 679 |
return NULL; |
| 680 |
} |
| 681 |
|
| 682 |
FILE *fp = open_file(open_filename, NULL); |
| 683 |
if (fp == NULL) { |
| 684 |
return NULL; |
| 685 |
} |
| 686 |
int fd = fileno(fp); |
| 687 |
|
| 688 |
if (cf_flags) { |
| 689 |
flags.cf_flags = cf_flags; |
| 690 |
pflags = &flags; |
| 691 |
} |
| 692 |
|
| 693 |
int res = PyRun_InteractiveLoopFlags(fp, filename, pflags); |
| 694 |
assert(!PyErr_Occurred()); |
| 695 |
|
| 696 |
assert(_Py_IsValidFD(fd)); |
| 697 |
fclose(fp); |
| 698 |
|
| 699 |
return PyLong_FromLong(res); |
| 700 |
} |
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
static PyObject* |
| 705 |
run_compilestringflags(PyObject *mod, PyObject *args) |
| 706 |
{ |
| 707 |
const char *str; |
| 708 |
const char *filename; |
| 709 |
int start; |
| 710 |
PyCompilerFlags flags = _PyCompilerFlags_INIT; |
| 711 |
PyCompilerFlags *pflags = NULL; |
| 712 |
int cf_flags = 0; |
| 713 |
int cf_feature_version = 0; |
| 714 |
|
| 715 |
if (!PyArg_ParseTuple(args, "yyi|ii", &str, &filename, &start, |
| 716 |
&cf_flags, &cf_feature_version)) { |
| 717 |
return NULL; |
| 718 |
} |
| 719 |
if (cf_flags || cf_feature_version) { |
| 720 |
flags.cf_flags = cf_flags; |
| 721 |
flags.cf_feature_version = cf_feature_version; |
| 722 |
pflags = &flags; |
| 723 |
} |
| 724 |
|
| 725 |
return Py_CompileStringFlags(str, filename, start, pflags); |
| 726 |
} |
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
static PyObject* |
| 731 |
run_compilestringexflags(PyObject *mod, PyObject *args) |
| 732 |
{ |
| 733 |
const char *str; |
| 734 |
const char *filename; |
| 735 |
int start; |
| 736 |
PyCompilerFlags flags = _PyCompilerFlags_INIT; |
| 737 |
PyCompilerFlags *pflags = NULL; |
| 738 |
int cf_flags = 0; |
| 739 |
int cf_feature_version = 0; |
| 740 |
int optimize = -1; |
| 741 |
|
| 742 |
if (!PyArg_ParseTuple(args, "yyi|iii", &str, &filename, &start, |
| 743 |
&cf_flags, &cf_feature_version, &optimize)) { |
| 744 |
return NULL; |
| 745 |
} |
| 746 |
if (cf_flags || cf_feature_version) { |
| 747 |
flags.cf_flags = cf_flags; |
| 748 |
flags.cf_feature_version = cf_feature_version; |
| 749 |
pflags = &flags; |
| 750 |
} |
| 751 |
|
| 752 |
return Py_CompileStringExFlags(str, filename, start, pflags, optimize); |
| 753 |
} |
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
static PyObject* |
| 758 |
run_compilestringobject(PyObject *mod, PyObject *args) |
| 759 |
{ |
| 760 |
const char *str; |
| 761 |
PyObject *filename; |
| 762 |
int start; |
| 763 |
PyCompilerFlags flags = _PyCompilerFlags_INIT; |
| 764 |
PyCompilerFlags *pflags = NULL; |
| 765 |
int cf_flags = 0; |
| 766 |
int cf_feature_version = 0; |
| 767 |
int optimize = -1; |
| 768 |
|
| 769 |
if (!PyArg_ParseTuple(args, "yOi|iii", &str, &filename, &start, |
| 770 |
&cf_flags, &cf_feature_version, &optimize)) { |
| 771 |
return NULL; |
| 772 |
} |
| 773 |
if (cf_flags || cf_feature_version) { |
| 774 |
flags.cf_flags = cf_flags; |
| 775 |
flags.cf_feature_version = cf_feature_version; |
| 776 |
pflags = &flags; |
| 777 |
} |
| 778 |
|
| 779 |
return Py_CompileStringObject(str, filename, start, pflags, optimize); |
| 780 |
} |
| 781 |
|
| 782 |
|
| 783 |
static PyMethodDef test_methods[] = { |
| 784 |
{"run_string", run_string, METH_VARARGS}, |
| 785 |
{"run_stringflags", run_stringflags, METH_VARARGS}, |
| 786 |
{"run_simplefile", run_simplefile, METH_VARARGS}, |
| 787 |
{"run_simplefileex", run_simplefileex, METH_VARARGS}, |
| 788 |
{"run_simplefileexflags", run_simplefileexflags, METH_VARARGS}, |
| 789 |
{"run_anyfile", run_anyfile, METH_VARARGS}, |
| 790 |
{"run_anyfileflags", run_anyfileflags, METH_VARARGS}, |
| 791 |
{"run_anyfileex", run_anyfileex, METH_VARARGS}, |
| 792 |
{"run_anyfileexflags", run_anyfileexflags, METH_VARARGS}, |
| 793 |
{"run_interactiveone", run_interactiveone, METH_VARARGS}, |
| 794 |
{"run_interactiveoneflags", run_interactiveoneflags, METH_VARARGS}, |
| 795 |
{"run_interactiveoneobject", run_interactiveoneobject, METH_VARARGS}, |
| 796 |
{"run_file", run_file, METH_VARARGS}, |
| 797 |
{"run_fileex", run_fileex, METH_VARARGS}, |
| 798 |
{"run_fileflags", run_fileflags, METH_VARARGS}, |
| 799 |
{"run_fileexflags", run_fileexflags, METH_VARARGS}, |
| 800 |
{"run_simplestring", run_simplestring, METH_VARARGS}, |
| 801 |
{"run_simplestringflags", run_simplestringflags, METH_VARARGS}, |
| 802 |
{"run_interactiveloop", run_interactiveloop, METH_VARARGS}, |
| 803 |
{"run_interactiveloopflags", run_interactiveloopflags, METH_VARARGS}, |
| 804 |
{"run_compilestringflags", run_compilestringflags, METH_VARARGS}, |
| 805 |
{"run_compilestringexflags", run_compilestringexflags, METH_VARARGS}, |
| 806 |
{"run_compilestringobject", run_compilestringobject, METH_VARARGS}, |
| 807 |
{NULL}, |
| 808 |
}; |
| 809 |
|
| 810 |
int |
| 811 |
_PyTestCapi_Init_Run(PyObject *mod) |
| 812 |
{ |
| 813 |
if (PyModule_AddFunctions(mod, test_methods) < 0) { |
| 814 |
return -1; |
| 815 |
} |
| 816 |
if (PyModule_AddIntMacro(mod, PyCF_ONLY_AST) < 0) { |
| 817 |
return -1; |
| 818 |
} |
| 819 |
if (PyModule_AddIntMacro(mod, PyCF_IGNORE_COOKIE) < 0) { |
| 820 |
return -1; |
| 821 |
} |
| 822 |
return 0; |
| 823 |
} |