Mercurial > cpython-lru_cache-weakref
comparison Python/pythonrun.c @ 36836:01bc8ee3d2ff legacy-trunk
Tabify
| author | Neal Norwitz <nnorwitz@gmail.com> |
|---|---|
| date | Sat, 04 Mar 2006 19:01:22 +0000 |
| parents | 9109b1a0d265 |
| children | 3a3aed49b245 |
comparison
equal
deleted
inserted
replaced
| 36835:9109b1a0d265 | 36836:01bc8ee3d2ff |
|---|---|
| 347 * XXX is easy to provoke that way. I've also seen, e.g., | 347 * XXX is easy to provoke that way. I've also seen, e.g., |
| 348 * XXX Exception exceptions.ImportError: 'No module named sha' | 348 * XXX Exception exceptions.ImportError: 'No module named sha' |
| 349 * XXX in <function callback at 0x008F5718> ignored | 349 * XXX in <function callback at 0x008F5718> ignored |
| 350 * XXX but I'm unclear on exactly how that one happens. In any case, | 350 * XXX but I'm unclear on exactly how that one happens. In any case, |
| 351 * XXX I haven't seen a real-life report of either of these. | 351 * XXX I haven't seen a real-life report of either of these. |
| 352 */ | 352 */ |
| 353 PyGC_Collect(); | 353 PyGC_Collect(); |
| 354 | 354 |
| 355 /* Destroy all modules */ | 355 /* Destroy all modules */ |
| 356 PyImport_Cleanup(); | 356 PyImport_Cleanup(); |
| 357 | 357 |
| 688 | 688 |
| 689 /* compute parser flags based on compiler flags */ | 689 /* compute parser flags based on compiler flags */ |
| 690 #define PARSER_FLAGS(flags) \ | 690 #define PARSER_FLAGS(flags) \ |
| 691 ((flags) ? ((((flags)->cf_flags & PyCF_DONT_IMPLY_DEDENT) ? \ | 691 ((flags) ? ((((flags)->cf_flags & PyCF_DONT_IMPLY_DEDENT) ? \ |
| 692 PyPARSE_DONT_IMPLY_DEDENT : 0) \ | 692 PyPARSE_DONT_IMPLY_DEDENT : 0) \ |
| 693 | ((flags)->cf_flags & CO_FUTURE_WITH_STATEMENT ? \ | 693 | ((flags)->cf_flags & CO_FUTURE_WITH_STATEMENT ? \ |
| 694 PyPARSE_WITH_IS_KEYWORD : 0)) : 0) | 694 PyPARSE_WITH_IS_KEYWORD : 0)) : 0) |
| 695 | 695 |
| 696 int | 696 int |
| 697 PyRun_InteractiveOneFlags(FILE *fp, const char *filename, PyCompilerFlags *flags) | 697 PyRun_InteractiveOneFlags(FILE *fp, const char *filename, PyCompilerFlags *flags) |
| 698 { | 698 { |
| 699 PyObject *m, *d, *v, *w; | 699 PyObject *m, *d, *v, *w; |
| 700 mod_ty mod; | 700 mod_ty mod; |
| 701 PyArena *arena; | 701 PyArena *arena; |
| 702 char *ps1 = "", *ps2 = ""; | 702 char *ps1 = "", *ps2 = ""; |
| 703 int errcode = 0; | 703 int errcode = 0; |
| 704 | 704 |
| 705 v = PySys_GetObject("ps1"); | 705 v = PySys_GetObject("ps1"); |
| 706 if (v != NULL) { | 706 if (v != NULL) { |
| 716 if (w == NULL) | 716 if (w == NULL) |
| 717 PyErr_Clear(); | 717 PyErr_Clear(); |
| 718 else if (PyString_Check(w)) | 718 else if (PyString_Check(w)) |
| 719 ps2 = PyString_AsString(w); | 719 ps2 = PyString_AsString(w); |
| 720 } | 720 } |
| 721 arena = PyArena_New(); | 721 arena = PyArena_New(); |
| 722 mod = PyParser_ASTFromFile(fp, filename, | 722 mod = PyParser_ASTFromFile(fp, filename, |
| 723 Py_single_input, ps1, ps2, | 723 Py_single_input, ps1, ps2, |
| 724 flags, &errcode, arena); | 724 flags, &errcode, arena); |
| 725 Py_XDECREF(v); | 725 Py_XDECREF(v); |
| 726 Py_XDECREF(w); | 726 Py_XDECREF(w); |
| 727 if (mod == NULL) { | 727 if (mod == NULL) { |
| 728 PyArena_Free(arena); | 728 PyArena_Free(arena); |
| 729 if (errcode == E_EOF) { | 729 if (errcode == E_EOF) { |
| 730 PyErr_Clear(); | 730 PyErr_Clear(); |
| 731 return E_EOF; | 731 return E_EOF; |
| 732 } | 732 } |
| 733 PyErr_Print(); | 733 PyErr_Print(); |
| 734 return -1; | 734 return -1; |
| 735 } | 735 } |
| 736 m = PyImport_AddModule("__main__"); | 736 m = PyImport_AddModule("__main__"); |
| 737 if (m == NULL) { | 737 if (m == NULL) { |
| 738 PyArena_Free(arena); | 738 PyArena_Free(arena); |
| 739 return -1; | 739 return -1; |
| 740 } | 740 } |
| 741 d = PyModule_GetDict(m); | 741 d = PyModule_GetDict(m); |
| 742 v = run_mod(mod, filename, d, d, flags, arena); | 742 v = run_mod(mod, filename, d, d, flags, arena); |
| 743 PyArena_Free(arena); | 743 PyArena_Free(arena); |
| 744 if (v == NULL) { | 744 if (v == NULL) { |
| 745 PyErr_Print(); | 745 PyErr_Print(); |
| 746 return -1; | 746 return -1; |
| 747 } | 747 } |
| 748 Py_DECREF(v); | 748 Py_DECREF(v); |
| 866 PyObject *v; | 866 PyObject *v; |
| 867 | 867 |
| 868 /* old style errors */ | 868 /* old style errors */ |
| 869 if (PyTuple_Check(err)) | 869 if (PyTuple_Check(err)) |
| 870 return PyArg_ParseTuple(err, "O(ziiz)", message, filename, | 870 return PyArg_ParseTuple(err, "O(ziiz)", message, filename, |
| 871 lineno, offset, text); | 871 lineno, offset, text); |
| 872 | 872 |
| 873 /* new style errors. `err' is an instance */ | 873 /* new style errors. `err' is an instance */ |
| 874 | 874 |
| 875 if (! (v = PyObject_GetAttrString(err, "msg"))) | 875 if (! (v = PyObject_GetAttrString(err, "msg"))) |
| 876 goto finally; | 876 goto finally; |
| 963 } | 963 } |
| 964 | 964 |
| 965 static void | 965 static void |
| 966 handle_system_exit(void) | 966 handle_system_exit(void) |
| 967 { | 967 { |
| 968 PyObject *exception, *value, *tb; | 968 PyObject *exception, *value, *tb; |
| 969 int exitcode = 0; | 969 int exitcode = 0; |
| 970 | 970 |
| 971 PyErr_Fetch(&exception, &value, &tb); | 971 PyErr_Fetch(&exception, &value, &tb); |
| 972 if (Py_FlushLine()) | 972 if (Py_FlushLine()) |
| 973 PyErr_Clear(); | 973 PyErr_Clear(); |
| 974 fflush(stdout); | 974 fflush(stdout); |
| 992 PyObject_Print(value, stderr, Py_PRINT_RAW); | 992 PyObject_Print(value, stderr, Py_PRINT_RAW); |
| 993 PySys_WriteStderr("\n"); | 993 PySys_WriteStderr("\n"); |
| 994 exitcode = 1; | 994 exitcode = 1; |
| 995 } | 995 } |
| 996 done: | 996 done: |
| 997 /* Restore and clear the exception info, in order to properly decref | 997 /* Restore and clear the exception info, in order to properly decref |
| 998 * the exception, value, and traceback. If we just exit instead, | 998 * the exception, value, and traceback. If we just exit instead, |
| 999 * these leak, which confuses PYTHONDUMPREFS output, and may prevent | 999 * these leak, which confuses PYTHONDUMPREFS output, and may prevent |
| 1000 * some finalizers from running. | 1000 * some finalizers from running. |
| 1001 */ | 1001 */ |
| 1002 PyErr_Restore(exception, value, tb); | 1002 PyErr_Restore(exception, value, tb); |
| 1003 PyErr_Clear(); | 1003 PyErr_Clear(); |
| 1004 Py_Exit(exitcode); | 1004 Py_Exit(exitcode); |
| 1005 /* NOTREACHED */ | 1005 /* NOTREACHED */ |
| 1006 } | 1006 } |
| 1025 PySys_SetObject("last_traceback", tb); | 1025 PySys_SetObject("last_traceback", tb); |
| 1026 } | 1026 } |
| 1027 hook = PySys_GetObject("excepthook"); | 1027 hook = PySys_GetObject("excepthook"); |
| 1028 if (hook) { | 1028 if (hook) { |
| 1029 PyObject *args = PyTuple_Pack(3, | 1029 PyObject *args = PyTuple_Pack(3, |
| 1030 exception, v ? v : Py_None, tb ? tb : Py_None); | 1030 exception, v ? v : Py_None, tb ? tb : Py_None); |
| 1031 PyObject *result = PyEval_CallObject(hook, args); | 1031 PyObject *result = PyEval_CallObject(hook, args); |
| 1032 if (result == NULL) { | 1032 if (result == NULL) { |
| 1033 PyObject *exception2, *v2, *tb2; | 1033 PyObject *exception2, *v2, *tb2; |
| 1034 if (PyErr_ExceptionMatches(PyExc_SystemExit)) { | 1034 if (PyErr_ExceptionMatches(PyExc_SystemExit)) { |
| 1035 handle_system_exit(); | 1035 handle_system_exit(); |
| 1156 PyObject * | 1156 PyObject * |
| 1157 PyRun_StringFlags(const char *str, int start, PyObject *globals, | 1157 PyRun_StringFlags(const char *str, int start, PyObject *globals, |
| 1158 PyObject *locals, PyCompilerFlags *flags) | 1158 PyObject *locals, PyCompilerFlags *flags) |
| 1159 { | 1159 { |
| 1160 PyObject *ret = NULL; | 1160 PyObject *ret = NULL; |
| 1161 PyArena *arena = PyArena_New(); | 1161 PyArena *arena = PyArena_New(); |
| 1162 mod_ty mod = PyParser_ASTFromString(str, "<string>", start, flags, | 1162 mod_ty mod = PyParser_ASTFromString(str, "<string>", start, flags, |
| 1163 arena); | 1163 arena); |
| 1164 if (mod != NULL) | 1164 if (mod != NULL) |
| 1165 ret = run_mod(mod, "<string>", globals, locals, flags, arena); | 1165 ret = run_mod(mod, "<string>", globals, locals, flags, arena); |
| 1166 PyArena_Free(arena); | 1166 PyArena_Free(arena); |
| 1167 return ret; | 1167 return ret; |
| 1168 } | 1168 } |
| 1169 | 1169 |
| 1170 PyObject * | 1170 PyObject * |
| 1171 PyRun_FileExFlags(FILE *fp, const char *filename, int start, PyObject *globals, | 1171 PyRun_FileExFlags(FILE *fp, const char *filename, int start, PyObject *globals, |
| 1172 PyObject *locals, int closeit, PyCompilerFlags *flags) | 1172 PyObject *locals, int closeit, PyCompilerFlags *flags) |
| 1173 { | 1173 { |
| 1174 PyObject *ret; | 1174 PyObject *ret; |
| 1175 PyArena *arena = PyArena_New(); | 1175 PyArena *arena = PyArena_New(); |
| 1176 mod_ty mod = PyParser_ASTFromFile(fp, filename, start, 0, 0, | 1176 mod_ty mod = PyParser_ASTFromFile(fp, filename, start, 0, 0, |
| 1177 flags, NULL, arena); | 1177 flags, NULL, arena); |
| 1178 if (mod == NULL) { | 1178 if (mod == NULL) { |
| 1179 PyArena_Free(arena); | 1179 PyArena_Free(arena); |
| 1180 return NULL; | 1180 return NULL; |
| 1181 } | 1181 } |
| 1182 if (closeit) | 1182 if (closeit) |
| 1183 fclose(fp); | 1183 fclose(fp); |
| 1184 ret = run_mod(mod, filename, globals, locals, flags, arena); | 1184 ret = run_mod(mod, filename, globals, locals, flags, arena); |
| 1185 PyArena_Free(arena); | 1185 PyArena_Free(arena); |
| 1186 return ret; | 1186 return ret; |
| 1187 } | 1187 } |
| 1188 | 1188 |
| 1189 static PyObject * | 1189 static PyObject * |
| 1190 run_mod(mod_ty mod, const char *filename, PyObject *globals, PyObject *locals, | 1190 run_mod(mod_ty mod, const char *filename, PyObject *globals, PyObject *locals, |
| 1235 PyObject * | 1235 PyObject * |
| 1236 Py_CompileStringFlags(const char *str, const char *filename, int start, | 1236 Py_CompileStringFlags(const char *str, const char *filename, int start, |
| 1237 PyCompilerFlags *flags) | 1237 PyCompilerFlags *flags) |
| 1238 { | 1238 { |
| 1239 PyCodeObject *co; | 1239 PyCodeObject *co; |
| 1240 PyArena *arena = PyArena_New(); | 1240 PyArena *arena = PyArena_New(); |
| 1241 mod_ty mod = PyParser_ASTFromString(str, filename, start, flags, arena); | 1241 mod_ty mod = PyParser_ASTFromString(str, filename, start, flags, arena); |
| 1242 if (mod == NULL) { | 1242 if (mod == NULL) { |
| 1243 PyArena_Free(arena); | 1243 PyArena_Free(arena); |
| 1244 return NULL; | 1244 return NULL; |
| 1245 } | 1245 } |
| 1246 if (flags && (flags->cf_flags & PyCF_ONLY_AST)) { | 1246 if (flags && (flags->cf_flags & PyCF_ONLY_AST)) { |
| 1247 PyObject *result = PyAST_mod2obj(mod); | 1247 PyObject *result = PyAST_mod2obj(mod); |
| 1248 PyArena_Free(arena); | 1248 PyArena_Free(arena); |
| 1249 return result; | 1249 return result; |
| 1250 } | 1250 } |
| 1251 co = PyAST_Compile(mod, filename, flags, arena); | 1251 co = PyAST_Compile(mod, filename, flags, arena); |
| 1252 PyArena_Free(arena); | 1252 PyArena_Free(arena); |
| 1253 return (PyObject *)co; | 1253 return (PyObject *)co; |
| 1254 } | 1254 } |
| 1255 | 1255 |
| 1256 struct symtable * | 1256 struct symtable * |
| 1257 Py_SymtableString(const char *str, const char *filename, int start) | 1257 Py_SymtableString(const char *str, const char *filename, int start) |
| 1258 { | 1258 { |
| 1259 struct symtable *st; | 1259 struct symtable *st; |
| 1260 PyArena *arena = PyArena_New(); | 1260 PyArena *arena = PyArena_New(); |
| 1261 mod_ty mod = PyParser_ASTFromString(str, filename, start, NULL, arena); | 1261 mod_ty mod = PyParser_ASTFromString(str, filename, start, NULL, arena); |
| 1262 if (mod == NULL) { | 1262 if (mod == NULL) { |
| 1263 PyArena_Free(arena); | 1263 PyArena_Free(arena); |
| 1264 return NULL; | 1264 return NULL; |
| 1265 } | 1265 } |
| 1266 st = PySymtable_Build(mod, filename, 0); | 1266 st = PySymtable_Build(mod, filename, 0); |
| 1267 PyArena_Free(arena); | 1267 PyArena_Free(arena); |
| 1268 return st; | 1268 return st; |
| 1269 } | 1269 } |
| 1270 | 1270 |
| 1271 /* Preferred access to parser is through AST. */ | 1271 /* Preferred access to parser is through AST. */ |
| 1272 mod_ty | 1272 mod_ty |
| 1290 } | 1290 } |
| 1291 | 1291 |
| 1292 mod_ty | 1292 mod_ty |
| 1293 PyParser_ASTFromFile(FILE *fp, const char *filename, int start, char *ps1, | 1293 PyParser_ASTFromFile(FILE *fp, const char *filename, int start, char *ps1, |
| 1294 char *ps2, PyCompilerFlags *flags, int *errcode, | 1294 char *ps2, PyCompilerFlags *flags, int *errcode, |
| 1295 PyArena *arena) | 1295 PyArena *arena) |
| 1296 { | 1296 { |
| 1297 mod_ty mod; | 1297 mod_ty mod; |
| 1298 perrdetail err; | 1298 perrdetail err; |
| 1299 node *n = PyParser_ParseFileFlags(fp, filename, &_PyParser_Grammar, | 1299 node *n = PyParser_ParseFileFlags(fp, filename, &_PyParser_Grammar, |
| 1300 start, ps1, ps2, &err, PARSER_FLAGS(flags)); | 1300 start, ps1, ps2, &err, PARSER_FLAGS(flags)); |
| 1446 msg = "unknown parsing error"; | 1446 msg = "unknown parsing error"; |
| 1447 break; | 1447 break; |
| 1448 } | 1448 } |
| 1449 v = Py_BuildValue("(ziiz)", err->filename, | 1449 v = Py_BuildValue("(ziiz)", err->filename, |
| 1450 err->lineno, err->offset, err->text); | 1450 err->lineno, err->offset, err->text); |
| 1451 if (err->text != NULL) { | 1451 if (err->text != NULL) { |
| 1452 PyMem_DEL(err->text); | 1452 PyMem_DEL(err->text); |
| 1453 err->text = NULL; | 1453 err->text = NULL; |
| 1454 } | 1454 } |
| 1455 w = NULL; | 1455 w = NULL; |
| 1456 if (v != NULL) | 1456 if (v != NULL) |
