Mercurial > cpython-lru_cache-weakref
comparison Python/pythonrun.c @ 17125:2054cea38754 legacy-trunk
Extend support for from __future__ import nested_scopes
If a module has a future statement enabling nested scopes, they are
also enable for the exec statement and the functions compile() and
execfile() if they occur in the module.
If Python is run with the -i option, which enters interactive mode
after executing a script, and the script it runs enables nested
scopes, they are also enabled in interactive mode.
XXX The use of -i with -c "from __future__ import nested_scopes" is
not supported. What's the point?
To support these changes, many function variants have been added to
pythonrun.c. All the variants names end with Flags and they take an
extra PyCompilerFlags * argument. It is possible that this complexity
will be eliminated in a future version of the interpreter in which
nested scopes are not optional.
| author | Jeremy Hylton <jeremy@alum.mit.edu> |
|---|---|
| date | Thu, 22 Mar 2001 02:47:58 +0000 |
| parents | b0456d3a0f1f |
| children | b44e77a12978 |
comparison
equal
deleted
inserted
replaced
| 17124:a8301d4ed2b1 | 17125:2054cea38754 |
|---|---|
| 38 static void initsite(void); | 38 static void initsite(void); |
| 39 static PyObject *run_err_node(node *, char *, PyObject *, PyObject *, | 39 static PyObject *run_err_node(node *, char *, PyObject *, PyObject *, |
| 40 PyCompilerFlags *); | 40 PyCompilerFlags *); |
| 41 static PyObject *run_node(node *, char *, PyObject *, PyObject *, | 41 static PyObject *run_node(node *, char *, PyObject *, PyObject *, |
| 42 PyCompilerFlags *); | 42 PyCompilerFlags *); |
| 43 static PyObject *run_pyc_file(FILE *, char *, PyObject *, PyObject *); | 43 static PyObject *run_pyc_file(FILE *, char *, PyObject *, PyObject *, |
| 44 PyCompilerFlags *); | |
| 44 static void err_input(perrdetail *); | 45 static void err_input(perrdetail *); |
| 45 static void initsigs(void); | 46 static void initsigs(void); |
| 46 static void call_sys_exitfunc(void); | 47 static void call_sys_exitfunc(void); |
| 47 static void call_ll_exitfuncs(void); | 48 static void call_ll_exitfuncs(void); |
| 48 | 49 |
| 445 /* Parse input from a file and execute it */ | 446 /* Parse input from a file and execute it */ |
| 446 | 447 |
| 447 int | 448 int |
| 448 PyRun_AnyFile(FILE *fp, char *filename) | 449 PyRun_AnyFile(FILE *fp, char *filename) |
| 449 { | 450 { |
| 450 return PyRun_AnyFileEx(fp, filename, 0); | 451 return PyRun_AnyFileExFlags(fp, filename, 0, NULL); |
| 452 } | |
| 453 | |
| 454 int | |
| 455 PyRun_AnyFileFlags(FILE *fp, char *filename, PyCompilerFlags *flags) | |
| 456 { | |
| 457 return PyRun_AnyFileExFlags(fp, filename, 0, flags); | |
| 451 } | 458 } |
| 452 | 459 |
| 453 int | 460 int |
| 454 PyRun_AnyFileEx(FILE *fp, char *filename, int closeit) | 461 PyRun_AnyFileEx(FILE *fp, char *filename, int closeit) |
| 462 { | |
| 463 return PyRun_AnyFileExFlags(fp, filename, closeit, NULL); | |
| 464 } | |
| 465 | |
| 466 int | |
| 467 PyRun_AnyFileExFlags(FILE *fp, char *filename, int closeit, | |
| 468 PyCompilerFlags *flags) | |
| 455 { | 469 { |
| 456 if (filename == NULL) | 470 if (filename == NULL) |
| 457 filename = "???"; | 471 filename = "???"; |
| 458 if (Py_FdIsInteractive(fp, filename)) { | 472 if (Py_FdIsInteractive(fp, filename)) { |
| 459 int err = PyRun_InteractiveLoop(fp, filename); | 473 int err = PyRun_InteractiveLoopFlags(fp, filename, flags); |
| 460 if (closeit) | 474 if (closeit) |
| 461 fclose(fp); | 475 fclose(fp); |
| 462 return err; | 476 return err; |
| 463 } | 477 } |
| 464 else | 478 else |
| 465 return PyRun_SimpleFileEx(fp, filename, closeit); | 479 return PyRun_SimpleFileExFlags(fp, filename, closeit, flags); |
| 466 } | 480 } |
| 467 | 481 |
| 468 int | 482 int |
| 469 PyRun_InteractiveLoop(FILE *fp, char *filename) | 483 PyRun_InteractiveLoop(FILE *fp, char *filename) |
| 484 { | |
| 485 return PyRun_InteractiveLoopFlags(fp, filename, NULL); | |
| 486 } | |
| 487 | |
| 488 int | |
| 489 PyRun_InteractiveLoopFlags(FILE *fp, char *filename, PyCompilerFlags *flags) | |
| 470 { | 490 { |
| 471 PyObject *v; | 491 PyObject *v; |
| 472 int ret; | 492 int ret; |
| 473 PyCompilerFlags flags; | 493 PyCompilerFlags local_flags; |
| 474 | 494 |
| 475 flags.cf_nested_scopes = 0; | 495 if (flags == NULL) { |
| 496 flags = &local_flags; | |
| 497 local_flags.cf_nested_scopes = 0; | |
| 498 } | |
| 476 v = PySys_GetObject("ps1"); | 499 v = PySys_GetObject("ps1"); |
| 477 if (v == NULL) { | 500 if (v == NULL) { |
| 478 PySys_SetObject("ps1", v = PyString_FromString(">>> ")); | 501 PySys_SetObject("ps1", v = PyString_FromString(">>> ")); |
| 479 Py_XDECREF(v); | 502 Py_XDECREF(v); |
| 480 } | 503 } |
| 482 if (v == NULL) { | 505 if (v == NULL) { |
| 483 PySys_SetObject("ps2", v = PyString_FromString("... ")); | 506 PySys_SetObject("ps2", v = PyString_FromString("... ")); |
| 484 Py_XDECREF(v); | 507 Py_XDECREF(v); |
| 485 } | 508 } |
| 486 for (;;) { | 509 for (;;) { |
| 487 ret = PyRun_InteractiveOneFlags(fp, filename, &flags); | 510 ret = PyRun_InteractiveOneFlags(fp, filename, flags); |
| 488 #ifdef Py_REF_DEBUG | 511 #ifdef Py_REF_DEBUG |
| 489 fprintf(stderr, "[%ld refs]\n", _Py_RefTotal); | 512 fprintf(stderr, "[%ld refs]\n", _Py_RefTotal); |
| 490 #endif | 513 #endif |
| 491 if (ret == E_EOF) | 514 if (ret == E_EOF) |
| 492 return 0; | 515 return 0; |
| 609 } | 632 } |
| 610 | 633 |
| 611 int | 634 int |
| 612 PyRun_SimpleFileEx(FILE *fp, char *filename, int closeit) | 635 PyRun_SimpleFileEx(FILE *fp, char *filename, int closeit) |
| 613 { | 636 { |
| 637 return PyRun_SimpleFileExFlags(fp, filename, closeit, NULL); | |
| 638 } | |
| 639 | |
| 640 int | |
| 641 PyRun_SimpleFileExFlags(FILE *fp, char *filename, int closeit, | |
| 642 PyCompilerFlags *flags) | |
| 643 { | |
| 614 PyObject *m, *d, *v; | 644 PyObject *m, *d, *v; |
| 615 char *ext; | 645 char *ext; |
| 616 | 646 |
| 617 m = PyImport_AddModule("__main__"); | 647 m = PyImport_AddModule("__main__"); |
| 618 if (m == NULL) | 648 if (m == NULL) |
| 628 return -1; | 658 return -1; |
| 629 } | 659 } |
| 630 /* Turn on optimization if a .pyo file is given */ | 660 /* Turn on optimization if a .pyo file is given */ |
| 631 if (strcmp(ext, ".pyo") == 0) | 661 if (strcmp(ext, ".pyo") == 0) |
| 632 Py_OptimizeFlag = 1; | 662 Py_OptimizeFlag = 1; |
| 633 v = run_pyc_file(fp, filename, d, d); | 663 v = run_pyc_file(fp, filename, d, d, flags); |
| 634 } else { | 664 } else { |
| 635 v = PyRun_FileEx(fp, filename, Py_file_input, d, d, closeit); | 665 v = PyRun_FileExFlags(fp, filename, Py_file_input, d, d, |
| 666 closeit, flags); | |
| 636 } | 667 } |
| 637 if (v == NULL) { | 668 if (v == NULL) { |
| 638 PyErr_Print(); | 669 PyErr_Print(); |
| 639 return -1; | 670 return -1; |
| 640 } | 671 } |
| 924 return PyRun_FileEx(fp, filename, start, globals, locals, 0); | 955 return PyRun_FileEx(fp, filename, start, globals, locals, 0); |
| 925 } | 956 } |
| 926 | 957 |
| 927 PyObject * | 958 PyObject * |
| 928 PyRun_FileEx(FILE *fp, char *filename, int start, PyObject *globals, | 959 PyRun_FileEx(FILE *fp, char *filename, int start, PyObject *globals, |
| 929 PyObject *locals, int closeit) | 960 PyObject *locals, int closeit) |
| 930 { | 961 { |
| 931 node *n = PyParser_SimpleParseFile(fp, filename, start); | 962 node *n = PyParser_SimpleParseFile(fp, filename, start); |
| 932 if (closeit) | 963 if (closeit) |
| 933 fclose(fp); | 964 fclose(fp); |
| 934 return run_err_node(n, filename, globals, locals, NULL); | 965 return run_err_node(n, filename, globals, locals, NULL); |
| 935 } | 966 } |
| 936 | 967 |
| 968 PyObject * | |
| 969 PyRun_StringFlags(char *str, int start, PyObject *globals, PyObject *locals, | |
| 970 PyCompilerFlags *flags) | |
| 971 { | |
| 972 return run_err_node(PyParser_SimpleParseString(str, start), | |
| 973 "<string>", globals, locals, flags); | |
| 974 } | |
| 975 | |
| 976 PyObject * | |
| 977 PyRun_FileFlags(FILE *fp, char *filename, int start, PyObject *globals, | |
| 978 PyObject *locals, PyCompilerFlags *flags) | |
| 979 { | |
| 980 return PyRun_FileExFlags(fp, filename, start, globals, locals, 0, | |
| 981 flags); | |
| 982 } | |
| 983 | |
| 984 PyObject * | |
| 985 PyRun_FileExFlags(FILE *fp, char *filename, int start, PyObject *globals, | |
| 986 PyObject *locals, int closeit, PyCompilerFlags *flags) | |
| 987 { | |
| 988 node *n = PyParser_SimpleParseFile(fp, filename, start); | |
| 989 if (closeit) | |
| 990 fclose(fp); | |
| 991 return run_err_node(n, filename, globals, locals, flags); | |
| 992 } | |
| 993 | |
| 937 static PyObject * | 994 static PyObject * |
| 938 run_err_node(node *n, char *filename, PyObject *globals, PyObject *locals, | 995 run_err_node(node *n, char *filename, PyObject *globals, PyObject *locals, |
| 939 PyCompilerFlags *flags) | 996 PyCompilerFlags *flags) |
| 940 { | 997 { |
| 941 if (n == NULL) | 998 if (n == NULL) |
| 947 run_node(node *n, char *filename, PyObject *globals, PyObject *locals, | 1004 run_node(node *n, char *filename, PyObject *globals, PyObject *locals, |
| 948 PyCompilerFlags *flags) | 1005 PyCompilerFlags *flags) |
| 949 { | 1006 { |
| 950 PyCodeObject *co; | 1007 PyCodeObject *co; |
| 951 PyObject *v; | 1008 PyObject *v; |
| 952 /* XXX pass sess->ss_nested_scopes to PyNode_Compile */ | |
| 953 co = PyNode_CompileFlags(n, filename, flags); | 1009 co = PyNode_CompileFlags(n, filename, flags); |
| 954 PyNode_Free(n); | 1010 PyNode_Free(n); |
| 955 if (co == NULL) | 1011 if (co == NULL) |
| 956 return NULL; | 1012 return NULL; |
| 957 v = PyEval_EvalCode(co, globals, locals); | 1013 v = PyEval_EvalCode(co, globals, locals); |
| 958 Py_DECREF(co); | 1014 Py_DECREF(co); |
| 959 return v; | 1015 return v; |
| 960 } | 1016 } |
| 961 | 1017 |
| 962 static PyObject * | 1018 static PyObject * |
| 963 run_pyc_file(FILE *fp, char *filename, PyObject *globals, PyObject *locals) | 1019 run_pyc_file(FILE *fp, char *filename, PyObject *globals, PyObject *locals, |
| 1020 PyCompilerFlags *flags) | |
| 964 { | 1021 { |
| 965 PyCodeObject *co; | 1022 PyCodeObject *co; |
| 966 PyObject *v; | 1023 PyObject *v; |
| 967 long magic; | 1024 long magic; |
| 968 long PyImport_GetMagicNumber(void); | 1025 long PyImport_GetMagicNumber(void); |
| 982 "Bad code object in .pyc file"); | 1039 "Bad code object in .pyc file"); |
| 983 return NULL; | 1040 return NULL; |
| 984 } | 1041 } |
| 985 co = (PyCodeObject *)v; | 1042 co = (PyCodeObject *)v; |
| 986 v = PyEval_EvalCode(co, globals, locals); | 1043 v = PyEval_EvalCode(co, globals, locals); |
| 1044 if (v && flags) { | |
| 1045 if (co->co_flags & CO_NESTED) | |
| 1046 flags->cf_nested_scopes = 1; | |
| 1047 fprintf(stderr, "run_pyc_file: nested_scopes: %d\n", | |
| 1048 flags->cf_nested_scopes); | |
| 1049 } | |
| 987 Py_DECREF(co); | 1050 Py_DECREF(co); |
| 988 return v; | 1051 return v; |
| 989 } | 1052 } |
| 990 | 1053 |
| 991 PyObject * | 1054 PyObject * |
| 992 Py_CompileString(char *str, char *filename, int start) | 1055 Py_CompileString(char *str, char *filename, int start) |
| 1056 { | |
| 1057 return Py_CompileStringFlags(str, filename, start, NULL); | |
| 1058 } | |
| 1059 | |
| 1060 PyObject * | |
| 1061 Py_CompileStringFlags(char *str, char *filename, int start, | |
| 1062 PyCompilerFlags *flags) | |
| 993 { | 1063 { |
| 994 node *n; | 1064 node *n; |
| 995 PyCodeObject *co; | 1065 PyCodeObject *co; |
| 996 n = PyParser_SimpleParseString(str, start); | 1066 n = PyParser_SimpleParseString(str, start); |
| 997 if (n == NULL) | 1067 if (n == NULL) |
