comparison Python/pythonrun.c @ 16916:b0456d3a0f1f legacy-trunk

Useful future statement support for the interactive interpreter (Also remove warning about module-level global decl, because we can't distinguish from code passed to exec.) Define PyCompilerFlags type contains a single element, cf_nested_scopes, that is true if a nested scopes future statement has been entered at the interactive prompt. New API functions: PyNode_CompileFlags() PyRun_InteractiveOneFlags() -- same as their non Flags counterparts except that the take an optional PyCompilerFlags pointer compile.c: In jcompile() use PyCompilerFlags argument. If cf_nested_scopes is true, compile code with nested scopes. If it is false, but the code has a valid future nested scopes statement, set it to true. pythonrun.c: Create a new PyCompilerFlags object in PyRun_InteractiveLoop() and thread it through to PyRun_InteractiveOneFlags().
author Jeremy Hylton <jeremy@alum.mit.edu>
date Thu, 01 Mar 2001 22:59:14 +0000
parents 09dcf5ebcd80
children 2054cea38754
comparison
equal deleted inserted replaced
16915:493410222719 16916:b0456d3a0f1f
34 extern grammar _PyParser_Grammar; /* From graminit.c */ 34 extern grammar _PyParser_Grammar; /* From graminit.c */
35 35
36 /* Forward */ 36 /* Forward */
37 static void initmain(void); 37 static void initmain(void);
38 static void initsite(void); 38 static void initsite(void);
39 static PyObject *run_err_node(node *n, char *filename, 39 static PyObject *run_err_node(node *, char *, PyObject *, PyObject *,
40 PyObject *globals, PyObject *locals); 40 PyCompilerFlags *);
41 static PyObject *run_node(node *n, char *filename, 41 static PyObject *run_node(node *, char *, PyObject *, PyObject *,
42 PyObject *globals, PyObject *locals); 42 PyCompilerFlags *);
43 static PyObject *run_pyc_file(FILE *fp, char *filename, 43 static PyObject *run_pyc_file(FILE *, char *, PyObject *, PyObject *);
44 PyObject *globals, PyObject *locals);
45 static void err_input(perrdetail *); 44 static void err_input(perrdetail *);
46 static void initsigs(void); 45 static void initsigs(void);
47 static void call_sys_exitfunc(void); 46 static void call_sys_exitfunc(void);
48 static void call_ll_exitfuncs(void); 47 static void call_ll_exitfuncs(void);
49 48
53 52
54 extern void _PyUnicode_Init(void); 53 extern void _PyUnicode_Init(void);
55 extern void _PyUnicode_Fini(void); 54 extern void _PyUnicode_Fini(void);
56 extern void _PyCodecRegistry_Init(void); 55 extern void _PyCodecRegistry_Init(void);
57 extern void _PyCodecRegistry_Fini(void); 56 extern void _PyCodecRegistry_Fini(void);
58
59 57
60 int Py_DebugFlag; /* Needed by parser.c */ 58 int Py_DebugFlag; /* Needed by parser.c */
61 int Py_VerboseFlag; /* Needed by import.c */ 59 int Py_VerboseFlag; /* Needed by import.c */
62 int Py_InteractiveFlag; /* Needed by Py_FdIsInteractive() below */ 60 int Py_InteractiveFlag; /* Needed by Py_FdIsInteractive() below */
63 int Py_NoSiteFlag; /* Suppress 'import site' */ 61 int Py_NoSiteFlag; /* Suppress 'import site' */
470 int 468 int
471 PyRun_InteractiveLoop(FILE *fp, char *filename) 469 PyRun_InteractiveLoop(FILE *fp, char *filename)
472 { 470 {
473 PyObject *v; 471 PyObject *v;
474 int ret; 472 int ret;
473 PyCompilerFlags flags;
474
475 flags.cf_nested_scopes = 0;
475 v = PySys_GetObject("ps1"); 476 v = PySys_GetObject("ps1");
476 if (v == NULL) { 477 if (v == NULL) {
477 PySys_SetObject("ps1", v = PyString_FromString(">>> ")); 478 PySys_SetObject("ps1", v = PyString_FromString(">>> "));
478 Py_XDECREF(v); 479 Py_XDECREF(v);
479 } 480 }
481 if (v == NULL) { 482 if (v == NULL) {
482 PySys_SetObject("ps2", v = PyString_FromString("... ")); 483 PySys_SetObject("ps2", v = PyString_FromString("... "));
483 Py_XDECREF(v); 484 Py_XDECREF(v);
484 } 485 }
485 for (;;) { 486 for (;;) {
486 ret = PyRun_InteractiveOne(fp, filename); 487 ret = PyRun_InteractiveOneFlags(fp, filename, &flags);
487 #ifdef Py_REF_DEBUG 488 #ifdef Py_REF_DEBUG
488 fprintf(stderr, "[%ld refs]\n", _Py_RefTotal); 489 fprintf(stderr, "[%ld refs]\n", _Py_RefTotal);
489 #endif 490 #endif
490 if (ret == E_EOF) 491 if (ret == E_EOF)
491 return 0; 492 return 0;
496 } 497 }
497 } 498 }
498 499
499 int 500 int
500 PyRun_InteractiveOne(FILE *fp, char *filename) 501 PyRun_InteractiveOne(FILE *fp, char *filename)
502 {
503 return PyRun_InteractiveOneFlags(fp, filename, NULL);
504 }
505
506 int
507 PyRun_InteractiveOneFlags(FILE *fp, char *filename, PyCompilerFlags *flags)
501 { 508 {
502 PyObject *m, *d, *v, *w; 509 PyObject *m, *d, *v, *w;
503 node *n; 510 node *n;
504 perrdetail err; 511 perrdetail err;
505 char *ps1 = "", *ps2 = ""; 512 char *ps1 = "", *ps2 = "";
535 } 542 }
536 m = PyImport_AddModule("__main__"); 543 m = PyImport_AddModule("__main__");
537 if (m == NULL) 544 if (m == NULL)
538 return -1; 545 return -1;
539 d = PyModule_GetDict(m); 546 d = PyModule_GetDict(m);
540 v = run_node(n, filename, d, d); 547 v = run_node(n, filename, d, d, flags);
541 if (v == NULL) { 548 if (v == NULL) {
542 PyErr_Print(); 549 PyErr_Print();
543 return -1; 550 return -1;
544 } 551 }
545 Py_DECREF(v); 552 Py_DECREF(v);
905 912
906 PyObject * 913 PyObject *
907 PyRun_String(char *str, int start, PyObject *globals, PyObject *locals) 914 PyRun_String(char *str, int start, PyObject *globals, PyObject *locals)
908 { 915 {
909 return run_err_node(PyParser_SimpleParseString(str, start), 916 return run_err_node(PyParser_SimpleParseString(str, start),
910 "<string>", globals, locals); 917 "<string>", globals, locals, NULL);
911 } 918 }
912 919
913 PyObject * 920 PyObject *
914 PyRun_File(FILE *fp, char *filename, int start, PyObject *globals, 921 PyRun_File(FILE *fp, char *filename, int start, PyObject *globals,
915 PyObject *locals) 922 PyObject *locals)
922 PyObject *locals, int closeit) 929 PyObject *locals, int closeit)
923 { 930 {
924 node *n = PyParser_SimpleParseFile(fp, filename, start); 931 node *n = PyParser_SimpleParseFile(fp, filename, start);
925 if (closeit) 932 if (closeit)
926 fclose(fp); 933 fclose(fp);
927 return run_err_node(n, filename, globals, locals); 934 return run_err_node(n, filename, globals, locals, NULL);
928 } 935 }
929 936
930 static PyObject * 937 static PyObject *
931 run_err_node(node *n, char *filename, PyObject *globals, PyObject *locals) 938 run_err_node(node *n, char *filename, PyObject *globals, PyObject *locals,
939 PyCompilerFlags *flags)
932 { 940 {
933 if (n == NULL) 941 if (n == NULL)
934 return NULL; 942 return NULL;
935 return run_node(n, filename, globals, locals); 943 return run_node(n, filename, globals, locals, flags);
936 } 944 }
937 945
938 static PyObject * 946 static PyObject *
939 run_node(node *n, char *filename, PyObject *globals, PyObject *locals) 947 run_node(node *n, char *filename, PyObject *globals, PyObject *locals,
948 PyCompilerFlags *flags)
940 { 949 {
941 PyCodeObject *co; 950 PyCodeObject *co;
942 PyObject *v; 951 PyObject *v;
943 co = PyNode_Compile(n, filename); 952 /* XXX pass sess->ss_nested_scopes to PyNode_Compile */
953 co = PyNode_CompileFlags(n, filename, flags);
944 PyNode_Free(n); 954 PyNode_Free(n);
945 if (co == NULL) 955 if (co == NULL)
946 return NULL; 956 return NULL;
947 v = PyEval_EvalCode(co, globals, locals); 957 v = PyEval_EvalCode(co, globals, locals);
948 Py_DECREF(co); 958 Py_DECREF(co);