Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 1 | |
| 2 | /* Python interpreter top-level routines, including init/exit */ |
| 3 | |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 4 | #include "Python.h" |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 5 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6 | #include "Python-ast.h" |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 7 | #undef Yield /* undefine macro conflicting with winbase.h */ |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 8 | #include "grammar.h" |
| 9 | #include "node.h" |
Fred Drake | 85f3639 | 2000-07-11 17:53:00 +0000 | [diff] [blame] | 10 | #include "token.h" |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 11 | #include "parsetok.h" |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 12 | #include "errcode.h" |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 13 | #include "code.h" |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 14 | #include "symtable.h" |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 15 | #include "ast.h" |
Guido van Rossum | fdef271 | 1994-09-14 13:31:04 +0000 | [diff] [blame] | 16 | #include "marshal.h" |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 17 | #include "osdefs.h" |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 18 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 19 | #ifdef HAVE_SIGNAL_H |
Guido van Rossum | a9e7dc1 | 1992-10-18 18:53:57 +0000 | [diff] [blame] | 20 | #include <signal.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 21 | #endif |
Guido van Rossum | a9e7dc1 | 1992-10-18 18:53:57 +0000 | [diff] [blame] | 22 | |
Benjamin Peterson | 80a50ac | 2009-01-02 21:24:04 +0000 | [diff] [blame] | 23 | #ifdef MS_WINDOWS |
Martin v. Löwis | 5c88d81 | 2009-01-02 20:47:48 +0000 | [diff] [blame] | 24 | #include "malloc.h" /* for alloca */ |
Benjamin Peterson | 80a50ac | 2009-01-02 21:24:04 +0000 | [diff] [blame] | 25 | #endif |
Martin v. Löwis | 5c88d81 | 2009-01-02 20:47:48 +0000 | [diff] [blame] | 26 | |
Martin v. Löwis | 73d538b | 2003-03-05 15:13:47 +0000 | [diff] [blame] | 27 | #ifdef HAVE_LANGINFO_H |
| 28 | #include <locale.h> |
| 29 | #include <langinfo.h> |
| 30 | #endif |
| 31 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 32 | #ifdef MS_WINDOWS |
Guido van Rossum | a44823b | 1995-03-14 15:01:17 +0000 | [diff] [blame] | 33 | #undef BYTE |
| 34 | #include "windows.h" |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 35 | #define PATH_MAX MAXPATHLEN |
Guido van Rossum | a44823b | 1995-03-14 15:01:17 +0000 | [diff] [blame] | 36 | #endif |
| 37 | |
Neal Norwitz | 4281cef | 2006-03-04 19:58:13 +0000 | [diff] [blame] | 38 | #ifndef Py_REF_DEBUG |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 39 | #define PRINT_TOTAL_REFS() |
Neal Norwitz | 4281cef | 2006-03-04 19:58:13 +0000 | [diff] [blame] | 40 | #else /* Py_REF_DEBUG */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 41 | #define PRINT_TOTAL_REFS() fprintf(stderr, \ |
| 42 | "[%" PY_FORMAT_SIZE_T "d refs]\n", \ |
| 43 | _Py_GetRefTotal()) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 44 | #endif |
| 45 | |
| 46 | #ifdef __cplusplus |
| 47 | extern "C" { |
Neal Norwitz | 4281cef | 2006-03-04 19:58:13 +0000 | [diff] [blame] | 48 | #endif |
| 49 | |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 50 | extern wchar_t *Py_GetPath(void); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 51 | |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 52 | extern grammar _PyParser_Grammar; /* From graminit.c */ |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 53 | |
Guido van Rossum | b73cc04 | 1993-11-01 16:28:59 +0000 | [diff] [blame] | 54 | /* Forward */ |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 55 | static void initmain(void); |
Victor Stinner | 793b531 | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 56 | static int initfsencoding(PyInterpreterState *interp); |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 57 | static void initsite(void); |
Guido van Rossum | ce3a72a | 2007-10-19 23:16:50 +0000 | [diff] [blame] | 58 | static int initstdio(void); |
Amaury Forgeot d'Arc | 7fedbe5 | 2008-04-10 21:03:09 +0000 | [diff] [blame] | 59 | static void flush_io(void); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 60 | static PyObject *run_mod(mod_ty, const char *, PyObject *, PyObject *, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 61 | PyCompilerFlags *, PyArena *); |
Martin v. Löwis | 95292d6 | 2002-12-11 14:04:59 +0000 | [diff] [blame] | 62 | static PyObject *run_pyc_file(FILE *, const char *, PyObject *, PyObject *, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 63 | PyCompilerFlags *); |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 64 | static void err_input(perrdetail *); |
Victor Stinner | 7f2fee3 | 2011-04-05 00:39:01 +0200 | [diff] [blame] | 65 | static void err_free(perrdetail *); |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 66 | static void initsigs(void); |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 67 | static void call_py_exitfuncs(void); |
Antoine Pitrou | 011bd62 | 2009-10-20 21:52:47 +0000 | [diff] [blame] | 68 | static void wait_for_thread_shutdown(void); |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 69 | static void call_ll_exitfuncs(void); |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 70 | extern int _PyUnicode_Init(void); |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 71 | extern void _PyUnicode_Fini(void); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 72 | extern int _PyLong_Init(void); |
| 73 | extern void PyLong_Fini(void); |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 74 | extern int _PyFaulthandler_Init(void); |
| 75 | extern void _PyFaulthandler_Fini(void); |
Guido van Rossum | c94044c | 2000-03-10 23:03:54 +0000 | [diff] [blame] | 76 | |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 77 | #ifdef WITH_THREAD |
| 78 | extern void _PyGILState_Init(PyInterpreterState *, PyThreadState *); |
| 79 | extern void _PyGILState_Fini(void); |
| 80 | #endif /* WITH_THREAD */ |
| 81 | |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 82 | int Py_DebugFlag; /* Needed by parser.c */ |
| 83 | int Py_VerboseFlag; /* Needed by import.c */ |
Georg Brandl | 8aa7e99 | 2010-12-28 18:30:18 +0000 | [diff] [blame] | 84 | int Py_QuietFlag; /* Needed by sysmodule.c */ |
Guido van Rossum | 7433b12 | 1997-02-14 19:45:36 +0000 | [diff] [blame] | 85 | int Py_InteractiveFlag; /* Needed by Py_FdIsInteractive() below */ |
Georg Brandl | 0b2489e | 2011-05-15 08:49:12 +0200 | [diff] [blame] | 86 | int Py_InspectFlag; /* Needed to determine whether to exit at SystemExit */ |
Guido van Rossum | dcc0c13 | 1997-08-29 22:32:42 +0000 | [diff] [blame] | 87 | int Py_NoSiteFlag; /* Suppress 'import site' */ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 88 | int Py_BytesWarningFlag; /* Warn on str(bytes) and str(buffer) */ |
Christian Heimes | 790c823 | 2008-01-07 21:14:23 +0000 | [diff] [blame] | 89 | int Py_DontWriteBytecodeFlag; /* Suppress writing bytecode files (*.py[co]) */ |
Barry Warsaw | 3ce0964 | 2000-05-02 19:18:59 +0000 | [diff] [blame] | 90 | int Py_UseClassExceptionsFlag = 1; /* Needed by bltinmodule.c: deprecated */ |
Guido van Rossum | a61691e | 1998-02-06 22:27:24 +0000 | [diff] [blame] | 91 | int Py_FrozenFlag; /* Needed by getpath.c */ |
Neil Schemenauer | 7d4bb9f | 2001-07-23 16:30:27 +0000 | [diff] [blame] | 92 | int Py_IgnoreEnvironmentFlag; /* e.g. PYTHONPATH, PYTHONHOME */ |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 93 | int Py_NoUserSiteDirectory = 0; /* for -s and site.py */ |
Antoine Pitrou | 0560843 | 2009-01-09 18:53:14 +0000 | [diff] [blame] | 94 | int Py_UnbufferedStdioFlag = 0; /* Unbuffered binary std{in,out,err} */ |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 95 | |
Antoine Pitrou | 0d5e52d | 2011-05-04 20:02:30 +0200 | [diff] [blame] | 96 | PyThreadState *_Py_Finalizing = NULL; |
| 97 | |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 98 | /* PyModule_GetWarningsModule is no longer necessary as of 2.6 |
| 99 | since _warnings is builtin. This API should not be used. */ |
| 100 | PyObject * |
| 101 | PyModule_GetWarningsModule(void) |
Mark Hammond | edd0773 | 2003-07-15 23:03:55 +0000 | [diff] [blame] | 102 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 103 | return PyImport_ImportModule("warnings"); |
Mark Hammond | edd0773 | 2003-07-15 23:03:55 +0000 | [diff] [blame] | 104 | } |
Mark Hammond | a43fd0c | 2003-02-19 00:33:33 +0000 | [diff] [blame] | 105 | |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 106 | static int initialized = 0; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 107 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 108 | /* API to access the initialized flag -- useful for esoteric use */ |
Guido van Rossum | e3c0d5e | 1997-08-22 04:20:13 +0000 | [diff] [blame] | 109 | |
| 110 | int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 111 | Py_IsInitialized(void) |
Guido van Rossum | e3c0d5e | 1997-08-22 04:20:13 +0000 | [diff] [blame] | 112 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 113 | return initialized; |
Guido van Rossum | e3c0d5e | 1997-08-22 04:20:13 +0000 | [diff] [blame] | 114 | } |
| 115 | |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 116 | /* Global initializations. Can be undone by Py_Finalize(). Don't |
| 117 | call this twice without an intervening Py_Finalize() call. When |
| 118 | initializations fail, a fatal error is issued and the function does |
| 119 | not return. On return, the first thread and interpreter state have |
| 120 | been created. |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 121 | |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 122 | Locking: you must hold the interpreter lock while calling this. |
| 123 | (If the lock has not yet been initialized, that's equivalent to |
| 124 | having the lock, but you cannot use multiple threads.) |
Guido van Rossum | a9e7dc1 | 1992-10-18 18:53:57 +0000 | [diff] [blame] | 125 | |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 126 | */ |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 127 | |
Guido van Rossum | 9abaf4d | 2001-10-12 22:17:56 +0000 | [diff] [blame] | 128 | static int |
| 129 | add_flag(int flag, const char *envs) |
| 130 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 131 | int env = atoi(envs); |
| 132 | if (flag < env) |
| 133 | flag = env; |
| 134 | if (flag < 1) |
| 135 | flag = 1; |
| 136 | return flag; |
Guido van Rossum | 9abaf4d | 2001-10-12 22:17:56 +0000 | [diff] [blame] | 137 | } |
| 138 | |
Christian Heimes | 5833a2f | 2008-10-30 21:40:04 +0000 | [diff] [blame] | 139 | static char* |
Victor Stinner | 94908bb | 2010-08-18 21:23:25 +0000 | [diff] [blame] | 140 | get_codec_name(const char *encoding) |
Christian Heimes | 5833a2f | 2008-10-30 21:40:04 +0000 | [diff] [blame] | 141 | { |
Victor Stinner | 94908bb | 2010-08-18 21:23:25 +0000 | [diff] [blame] | 142 | char *name_utf8, *name_str; |
Victor Stinner | 386fe71 | 2010-05-19 00:34:15 +0000 | [diff] [blame] | 143 | PyObject *codec, *name = NULL; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 144 | _Py_IDENTIFIER(name); |
Christian Heimes | 5833a2f | 2008-10-30 21:40:04 +0000 | [diff] [blame] | 145 | |
Victor Stinner | 94908bb | 2010-08-18 21:23:25 +0000 | [diff] [blame] | 146 | codec = _PyCodec_Lookup(encoding); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 147 | if (!codec) |
| 148 | goto error; |
Christian Heimes | 5833a2f | 2008-10-30 21:40:04 +0000 | [diff] [blame] | 149 | |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 150 | name = _PyObject_GetAttrId(codec, &PyId_name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 151 | Py_CLEAR(codec); |
| 152 | if (!name) |
| 153 | goto error; |
Christian Heimes | 5833a2f | 2008-10-30 21:40:04 +0000 | [diff] [blame] | 154 | |
Victor Stinner | 94908bb | 2010-08-18 21:23:25 +0000 | [diff] [blame] | 155 | name_utf8 = _PyUnicode_AsString(name); |
Victor Stinner | 4ca2809 | 2011-03-20 23:09:03 +0100 | [diff] [blame] | 156 | if (name_utf8 == NULL) |
Victor Stinner | 386fe71 | 2010-05-19 00:34:15 +0000 | [diff] [blame] | 157 | goto error; |
Victor Stinner | 94908bb | 2010-08-18 21:23:25 +0000 | [diff] [blame] | 158 | name_str = strdup(name_utf8); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 159 | Py_DECREF(name); |
Victor Stinner | 94908bb | 2010-08-18 21:23:25 +0000 | [diff] [blame] | 160 | if (name_str == NULL) { |
| 161 | PyErr_NoMemory(); |
| 162 | return NULL; |
| 163 | } |
| 164 | return name_str; |
Christian Heimes | 5833a2f | 2008-10-30 21:40:04 +0000 | [diff] [blame] | 165 | |
| 166 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 167 | Py_XDECREF(codec); |
Victor Stinner | 386fe71 | 2010-05-19 00:34:15 +0000 | [diff] [blame] | 168 | Py_XDECREF(name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 169 | return NULL; |
Christian Heimes | 5833a2f | 2008-10-30 21:40:04 +0000 | [diff] [blame] | 170 | } |
Victor Stinner | 94908bb | 2010-08-18 21:23:25 +0000 | [diff] [blame] | 171 | |
Victor Stinner | 94908bb | 2010-08-18 21:23:25 +0000 | [diff] [blame] | 172 | static char* |
Victor Stinner | d64e8a7 | 2011-07-04 13:48:30 +0200 | [diff] [blame] | 173 | get_locale_encoding(void) |
Victor Stinner | 94908bb | 2010-08-18 21:23:25 +0000 | [diff] [blame] | 174 | { |
Victor Stinner | d64e8a7 | 2011-07-04 13:48:30 +0200 | [diff] [blame] | 175 | #ifdef MS_WINDOWS |
| 176 | char codepage[100]; |
| 177 | PyOS_snprintf(codepage, sizeof(codepage), "cp%d", GetACP()); |
| 178 | return get_codec_name(codepage); |
| 179 | #elif defined(HAVE_LANGINFO_H) && defined(CODESET) |
Victor Stinner | 94908bb | 2010-08-18 21:23:25 +0000 | [diff] [blame] | 180 | char* codeset = nl_langinfo(CODESET); |
| 181 | if (!codeset || codeset[0] == '\0') { |
| 182 | PyErr_SetString(PyExc_ValueError, "CODESET is not set or empty"); |
| 183 | return NULL; |
| 184 | } |
| 185 | return get_codec_name(codeset); |
Victor Stinner | d64e8a7 | 2011-07-04 13:48:30 +0200 | [diff] [blame] | 186 | #else |
| 187 | PyErr_SetNone(PyExc_NotImplementedError); |
| 188 | return NULL; |
Christian Heimes | 5833a2f | 2008-10-30 21:40:04 +0000 | [diff] [blame] | 189 | #endif |
Victor Stinner | d64e8a7 | 2011-07-04 13:48:30 +0200 | [diff] [blame] | 190 | } |
Christian Heimes | 5833a2f | 2008-10-30 21:40:04 +0000 | [diff] [blame] | 191 | |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 192 | void |
Martin v. Löwis | 336e85f | 2004-08-19 11:31:58 +0000 | [diff] [blame] | 193 | Py_InitializeEx(int install_sigs) |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 194 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 195 | PyInterpreterState *interp; |
| 196 | PyThreadState *tstate; |
| 197 | PyObject *bimod, *sysmod, *pstderr; |
| 198 | char *p; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 199 | extern void _Py_ReadyTypes(void); |
Guido van Rossum | ad6dfda | 1997-07-19 19:17:22 +0000 | [diff] [blame] | 200 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 201 | if (initialized) |
| 202 | return; |
| 203 | initialized = 1; |
Antoine Pitrou | 0d5e52d | 2011-05-04 20:02:30 +0200 | [diff] [blame] | 204 | _Py_Finalizing = NULL; |
Tim Peters | d08e382 | 2003-04-17 15:24:21 +0000 | [diff] [blame] | 205 | |
Andrew M. Kuchling | b2ceb3a | 2010-02-22 23:26:10 +0000 | [diff] [blame] | 206 | #if defined(HAVE_LANGINFO_H) && defined(HAVE_SETLOCALE) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 207 | /* Set up the LC_CTYPE locale, so we can obtain |
| 208 | the locale's charset without having to switch |
| 209 | locales. */ |
| 210 | setlocale(LC_CTYPE, ""); |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 211 | #endif |
| 212 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 213 | if ((p = Py_GETENV("PYTHONDEBUG")) && *p != '\0') |
| 214 | Py_DebugFlag = add_flag(Py_DebugFlag, p); |
| 215 | if ((p = Py_GETENV("PYTHONVERBOSE")) && *p != '\0') |
| 216 | Py_VerboseFlag = add_flag(Py_VerboseFlag, p); |
| 217 | if ((p = Py_GETENV("PYTHONOPTIMIZE")) && *p != '\0') |
| 218 | Py_OptimizeFlag = add_flag(Py_OptimizeFlag, p); |
| 219 | if ((p = Py_GETENV("PYTHONDONTWRITEBYTECODE")) && *p != '\0') |
| 220 | Py_DontWriteBytecodeFlag = add_flag(Py_DontWriteBytecodeFlag, p); |
Guido van Rossum | ad6dfda | 1997-07-19 19:17:22 +0000 | [diff] [blame] | 221 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 222 | interp = PyInterpreterState_New(); |
| 223 | if (interp == NULL) |
| 224 | Py_FatalError("Py_Initialize: can't make first interpreter"); |
Guido van Rossum | ad6dfda | 1997-07-19 19:17:22 +0000 | [diff] [blame] | 225 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 226 | tstate = PyThreadState_New(interp); |
| 227 | if (tstate == NULL) |
| 228 | Py_FatalError("Py_Initialize: can't make first thread"); |
| 229 | (void) PyThreadState_Swap(tstate); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 230 | |
Victor Stinner | 6961bd6 | 2010-08-17 22:26:51 +0000 | [diff] [blame] | 231 | #ifdef WITH_THREAD |
Antoine Pitrou | b0b384b | 2010-09-20 20:13:48 +0000 | [diff] [blame] | 232 | /* We can't call _PyEval_FiniThreads() in Py_Finalize because |
| 233 | destroying the GIL might fail when it is being referenced from |
| 234 | another running thread (see issue #9901). |
| 235 | Instead we destroy the previously created GIL here, which ensures |
| 236 | that we can call Py_Initialize / Py_Finalize multiple times. */ |
| 237 | _PyEval_FiniThreads(); |
| 238 | |
| 239 | /* Auto-thread-state API */ |
Victor Stinner | 6961bd6 | 2010-08-17 22:26:51 +0000 | [diff] [blame] | 240 | _PyGILState_Init(interp, tstate); |
| 241 | #endif /* WITH_THREAD */ |
| 242 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 243 | _Py_ReadyTypes(); |
Guido van Rossum | 528b7eb | 2001-08-07 17:24:28 +0000 | [diff] [blame] | 244 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 245 | if (!_PyFrame_Init()) |
| 246 | Py_FatalError("Py_Initialize: can't init frames"); |
Neal Norwitz | c91ed40 | 2002-12-30 22:29:22 +0000 | [diff] [blame] | 247 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 248 | if (!_PyLong_Init()) |
| 249 | Py_FatalError("Py_Initialize: can't init longs"); |
Neal Norwitz | c91ed40 | 2002-12-30 22:29:22 +0000 | [diff] [blame] | 250 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 251 | if (!PyByteArray_Init()) |
| 252 | Py_FatalError("Py_Initialize: can't init bytearray"); |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 253 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 254 | _PyFloat_Init(); |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 255 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 256 | interp->modules = PyDict_New(); |
| 257 | if (interp->modules == NULL) |
| 258 | Py_FatalError("Py_Initialize: can't make modules dictionary"); |
| 259 | interp->modules_reloading = PyDict_New(); |
| 260 | if (interp->modules_reloading == NULL) |
| 261 | Py_FatalError("Py_Initialize: can't make modules_reloading dictionary"); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 262 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 263 | /* Init Unicode implementation; relies on the codec registry */ |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 264 | if (_PyUnicode_Init() < 0) |
| 265 | Py_FatalError("Py_Initialize: can't initialize unicode"); |
Guido van Rossum | c94044c | 2000-03-10 23:03:54 +0000 | [diff] [blame] | 266 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 267 | bimod = _PyBuiltin_Init(); |
| 268 | if (bimod == NULL) |
| 269 | Py_FatalError("Py_Initialize: can't initialize builtins modules"); |
Victor Stinner | 49d3f25 | 2010-10-17 01:24:53 +0000 | [diff] [blame] | 270 | _PyImport_FixupBuiltin(bimod, "builtins"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 271 | interp->builtins = PyModule_GetDict(bimod); |
| 272 | if (interp->builtins == NULL) |
| 273 | Py_FatalError("Py_Initialize: can't initialize builtins dict"); |
| 274 | Py_INCREF(interp->builtins); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 275 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 276 | /* initialize builtin exceptions */ |
| 277 | _PyExc_Init(); |
Christian Heimes | 9a68f8c | 2007-11-14 00:16:07 +0000 | [diff] [blame] | 278 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 279 | sysmod = _PySys_Init(); |
| 280 | if (sysmod == NULL) |
| 281 | Py_FatalError("Py_Initialize: can't initialize sys"); |
| 282 | interp->sysdict = PyModule_GetDict(sysmod); |
| 283 | if (interp->sysdict == NULL) |
| 284 | Py_FatalError("Py_Initialize: can't initialize sys dict"); |
| 285 | Py_INCREF(interp->sysdict); |
Victor Stinner | 49d3f25 | 2010-10-17 01:24:53 +0000 | [diff] [blame] | 286 | _PyImport_FixupBuiltin(sysmod, "sys"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 287 | PySys_SetPath(Py_GetPath()); |
| 288 | PyDict_SetItemString(interp->sysdict, "modules", |
| 289 | interp->modules); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 290 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 291 | /* Set up a preliminary stderr printer until we have enough |
| 292 | infrastructure for the io module in place. */ |
| 293 | pstderr = PyFile_NewStdPrinter(fileno(stderr)); |
| 294 | if (pstderr == NULL) |
| 295 | Py_FatalError("Py_Initialize: can't set preliminary stderr"); |
| 296 | PySys_SetObject("stderr", pstderr); |
| 297 | PySys_SetObject("__stderr__", pstderr); |
Hirokazu Yamamoto | daf83ac | 2010-10-30 15:08:15 +0000 | [diff] [blame] | 298 | Py_DECREF(pstderr); |
Guido van Rossum | 826d897 | 2007-10-30 18:34:07 +0000 | [diff] [blame] | 299 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 300 | _PyImport_Init(); |
Guido van Rossum | 7c85ab8 | 1999-07-08 17:26:56 +0000 | [diff] [blame] | 301 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 302 | _PyImportHooks_Init(); |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 303 | |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 304 | /* initialize the faulthandler module */ |
| 305 | if (_PyFaulthandler_Init()) |
| 306 | Py_FatalError("Py_Initialize: can't initialize faulthandler"); |
| 307 | |
Victor Stinner | 7d79b8b | 2010-05-19 20:40:50 +0000 | [diff] [blame] | 308 | /* Initialize _warnings. */ |
| 309 | _PyWarnings_Init(); |
| 310 | |
Alexander Belopolsky | 6fc4ade | 2010-08-05 17:34:27 +0000 | [diff] [blame] | 311 | _PyTime_Init(); |
| 312 | |
Victor Stinner | 793b531 | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 313 | if (initfsencoding(interp) < 0) |
| 314 | Py_FatalError("Py_Initialize: unable to load the file system codec"); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 315 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 316 | if (install_sigs) |
| 317 | initsigs(); /* Signal handling stuff, including initintr() */ |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 318 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 319 | initmain(); /* Module __main__ */ |
| 320 | if (initstdio() < 0) |
| 321 | Py_FatalError( |
| 322 | "Py_Initialize: can't initialize sys standard streams"); |
| 323 | |
Antoine Pitrou | cf9f980 | 2010-11-10 13:55:25 +0000 | [diff] [blame] | 324 | /* Initialize warnings. */ |
| 325 | if (PySys_HasWarnOptions()) { |
| 326 | PyObject *warnings_module = PyImport_ImportModule("warnings"); |
| 327 | if (warnings_module == NULL) { |
| 328 | fprintf(stderr, "'import warnings' failed; traceback:\n"); |
| 329 | PyErr_Print(); |
| 330 | } |
| 331 | Py_XDECREF(warnings_module); |
| 332 | } |
| 333 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 334 | if (!Py_NoSiteFlag) |
| 335 | initsite(); /* Module site */ |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 336 | } |
| 337 | |
Martin v. Löwis | 336e85f | 2004-08-19 11:31:58 +0000 | [diff] [blame] | 338 | void |
| 339 | Py_Initialize(void) |
| 340 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 341 | Py_InitializeEx(1); |
Martin v. Löwis | 336e85f | 2004-08-19 11:31:58 +0000 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | |
Guido van Rossum | 2edcf0d | 1998-12-15 16:12:00 +0000 | [diff] [blame] | 345 | #ifdef COUNT_ALLOCS |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 346 | extern void dump_counts(FILE*); |
Guido van Rossum | 2edcf0d | 1998-12-15 16:12:00 +0000 | [diff] [blame] | 347 | #endif |
| 348 | |
Guido van Rossum | e8432ac | 2007-07-09 15:04:50 +0000 | [diff] [blame] | 349 | /* Flush stdout and stderr */ |
| 350 | |
Antoine Pitrou | d7c8fbf | 2011-11-26 21:59:36 +0100 | [diff] [blame] | 351 | static int |
| 352 | file_is_closed(PyObject *fobj) |
| 353 | { |
| 354 | int r; |
| 355 | PyObject *tmp = PyObject_GetAttrString(fobj, "closed"); |
| 356 | if (tmp == NULL) { |
| 357 | PyErr_Clear(); |
| 358 | return 0; |
| 359 | } |
| 360 | r = PyObject_IsTrue(tmp); |
| 361 | Py_DECREF(tmp); |
| 362 | if (r < 0) |
| 363 | PyErr_Clear(); |
| 364 | return r > 0; |
| 365 | } |
| 366 | |
Neal Norwitz | 2bad970 | 2007-08-27 06:19:22 +0000 | [diff] [blame] | 367 | static void |
Guido van Rossum | 1bd2122 | 2007-07-10 20:14:13 +0000 | [diff] [blame] | 368 | flush_std_files(void) |
Guido van Rossum | e8432ac | 2007-07-09 15:04:50 +0000 | [diff] [blame] | 369 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 370 | PyObject *fout = PySys_GetObject("stdout"); |
| 371 | PyObject *ferr = PySys_GetObject("stderr"); |
| 372 | PyObject *tmp; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 373 | _Py_IDENTIFIER(flush); |
Guido van Rossum | e8432ac | 2007-07-09 15:04:50 +0000 | [diff] [blame] | 374 | |
Antoine Pitrou | d7c8fbf | 2011-11-26 21:59:36 +0100 | [diff] [blame] | 375 | if (fout != NULL && fout != Py_None && !file_is_closed(fout)) { |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 376 | tmp = _PyObject_CallMethodId(fout, &PyId_flush, ""); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 377 | if (tmp == NULL) |
Antoine Pitrou | bddc9fe | 2010-08-08 20:46:42 +0000 | [diff] [blame] | 378 | PyErr_WriteUnraisable(fout); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 379 | else |
| 380 | Py_DECREF(tmp); |
| 381 | } |
Guido van Rossum | e8432ac | 2007-07-09 15:04:50 +0000 | [diff] [blame] | 382 | |
Antoine Pitrou | d7c8fbf | 2011-11-26 21:59:36 +0100 | [diff] [blame] | 383 | if (ferr != NULL && ferr != Py_None && !file_is_closed(ferr)) { |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 384 | tmp = _PyObject_CallMethodId(ferr, &PyId_flush, ""); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 385 | if (tmp == NULL) |
| 386 | PyErr_Clear(); |
| 387 | else |
| 388 | Py_DECREF(tmp); |
| 389 | } |
Guido van Rossum | e8432ac | 2007-07-09 15:04:50 +0000 | [diff] [blame] | 390 | } |
| 391 | |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 392 | /* Undo the effect of Py_Initialize(). |
| 393 | |
| 394 | Beware: if multiple interpreter and/or thread states exist, these |
| 395 | are not wiped out; only the current thread and interpreter state |
| 396 | are deleted. But since everything else is deleted, those other |
| 397 | interpreter and thread states should no longer be used. |
| 398 | |
| 399 | (XXX We should do better, e.g. wipe out all interpreters and |
| 400 | threads.) |
| 401 | |
| 402 | Locking: as above. |
| 403 | |
| 404 | */ |
| 405 | |
| 406 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 407 | Py_Finalize(void) |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 408 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 409 | PyInterpreterState *interp; |
| 410 | PyThreadState *tstate; |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 411 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 412 | if (!initialized) |
| 413 | return; |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 414 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 415 | wait_for_thread_shutdown(); |
Antoine Pitrou | 011bd62 | 2009-10-20 21:52:47 +0000 | [diff] [blame] | 416 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 417 | /* The interpreter is still entirely intact at this point, and the |
| 418 | * exit funcs may be relying on that. In particular, if some thread |
| 419 | * or exit func is still waiting to do an import, the import machinery |
| 420 | * expects Py_IsInitialized() to return true. So don't say the |
| 421 | * interpreter is uninitialized until after the exit funcs have run. |
| 422 | * Note that Threading.py uses an exit func to do a join on all the |
| 423 | * threads created thru it, so this also protects pending imports in |
| 424 | * the threads created via Threading. |
| 425 | */ |
| 426 | call_py_exitfuncs(); |
Guido van Rossum | e8432ac | 2007-07-09 15:04:50 +0000 | [diff] [blame] | 427 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 428 | /* Get current thread state and interpreter pointer */ |
| 429 | tstate = PyThreadState_GET(); |
| 430 | interp = tstate->interp; |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 431 | |
Antoine Pitrou | 0d5e52d | 2011-05-04 20:02:30 +0200 | [diff] [blame] | 432 | /* Remaining threads (e.g. daemon threads) will automatically exit |
| 433 | after taking the GIL (in PyEval_RestoreThread()). */ |
| 434 | _Py_Finalizing = tstate; |
| 435 | initialized = 0; |
| 436 | |
| 437 | /* Flush stdout+stderr */ |
| 438 | flush_std_files(); |
| 439 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 440 | /* Disable signal handling */ |
| 441 | PyOS_FiniInterrupts(); |
Guido van Rossum | 3a44e1b | 1997-11-03 21:58:47 +0000 | [diff] [blame] | 442 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 443 | /* Clear type lookup cache */ |
| 444 | PyType_ClearCache(); |
Christian Heimes | 2685563 | 2008-01-27 23:50:43 +0000 | [diff] [blame] | 445 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 446 | /* Collect garbage. This may call finalizers; it's nice to call these |
| 447 | * before all modules are destroyed. |
| 448 | * XXX If a __del__ or weakref callback is triggered here, and tries to |
| 449 | * XXX import a module, bad things can happen, because Python no |
| 450 | * XXX longer believes it's initialized. |
| 451 | * XXX Fatal Python error: Interpreter not initialized (version mismatch?) |
| 452 | * XXX is easy to provoke that way. I've also seen, e.g., |
| 453 | * XXX Exception exceptions.ImportError: 'No module named sha' |
| 454 | * XXX in <function callback at 0x008F5718> ignored |
| 455 | * XXX but I'm unclear on exactly how that one happens. In any case, |
| 456 | * XXX I haven't seen a real-life report of either of these. |
| 457 | */ |
| 458 | PyGC_Collect(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 459 | #ifdef COUNT_ALLOCS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 460 | /* With COUNT_ALLOCS, it helps to run GC multiple times: |
| 461 | each collection might release some types from the type |
| 462 | list, so they become garbage. */ |
| 463 | while (PyGC_Collect() > 0) |
| 464 | /* nothing */; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 465 | #endif |
Antoine Pitrou | 696e035 | 2010-08-08 22:18:46 +0000 | [diff] [blame] | 466 | /* We run this while most interpreter state is still alive, so that |
| 467 | debug information can be printed out */ |
| 468 | _PyGC_Fini(); |
Guido van Rossum | e13ddc9 | 2003-04-17 17:29:22 +0000 | [diff] [blame] | 469 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 470 | /* Destroy all modules */ |
| 471 | PyImport_Cleanup(); |
Guido van Rossum | 3a44e1b | 1997-11-03 21:58:47 +0000 | [diff] [blame] | 472 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 473 | /* Flush stdout+stderr (again, in case more was printed) */ |
| 474 | flush_std_files(); |
Guido van Rossum | e8432ac | 2007-07-09 15:04:50 +0000 | [diff] [blame] | 475 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 476 | /* Collect final garbage. This disposes of cycles created by |
Florent Xicluna | aa6c1d2 | 2011-12-12 18:54:29 +0100 | [diff] [blame] | 477 | * class definitions, for example. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 478 | * XXX This is disabled because it caused too many problems. If |
| 479 | * XXX a __del__ or weakref callback triggers here, Python code has |
| 480 | * XXX a hard time running, because even the sys module has been |
| 481 | * XXX cleared out (sys.stdout is gone, sys.excepthook is gone, etc). |
| 482 | * XXX One symptom is a sequence of information-free messages |
| 483 | * XXX coming from threads (if a __del__ or callback is invoked, |
| 484 | * XXX other threads can execute too, and any exception they encounter |
| 485 | * XXX triggers a comedy of errors as subsystem after subsystem |
| 486 | * XXX fails to find what it *expects* to find in sys to help report |
| 487 | * XXX the exception and consequent unexpected failures). I've also |
| 488 | * XXX seen segfaults then, after adding print statements to the |
| 489 | * XXX Python code getting called. |
| 490 | */ |
Tim Peters | 1d7323e | 2003-12-01 21:35:27 +0000 | [diff] [blame] | 491 | #if 0 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 492 | PyGC_Collect(); |
Tim Peters | 1d7323e | 2003-12-01 21:35:27 +0000 | [diff] [blame] | 493 | #endif |
Guido van Rossum | e13ddc9 | 2003-04-17 17:29:22 +0000 | [diff] [blame] | 494 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 495 | /* Destroy the database used by _PyImport_{Fixup,Find}Extension */ |
| 496 | _PyImport_Fini(); |
Guido van Rossum | 1707aad | 1997-12-08 23:43:45 +0000 | [diff] [blame] | 497 | |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 498 | /* unload faulthandler module */ |
| 499 | _PyFaulthandler_Fini(); |
| 500 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 501 | /* Debugging stuff */ |
Guido van Rossum | 1707aad | 1997-12-08 23:43:45 +0000 | [diff] [blame] | 502 | #ifdef COUNT_ALLOCS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 503 | dump_counts(stdout); |
Guido van Rossum | 1707aad | 1997-12-08 23:43:45 +0000 | [diff] [blame] | 504 | #endif |
| 505 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 506 | PRINT_TOTAL_REFS(); |
Guido van Rossum | 1707aad | 1997-12-08 23:43:45 +0000 | [diff] [blame] | 507 | |
Tim Peters | 9cf25ce | 2003-04-17 15:21:01 +0000 | [diff] [blame] | 508 | #ifdef Py_TRACE_REFS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 509 | /* Display all objects still alive -- this can invoke arbitrary |
| 510 | * __repr__ overrides, so requires a mostly-intact interpreter. |
| 511 | * Alas, a lot of stuff may still be alive now that will be cleaned |
| 512 | * up later. |
| 513 | */ |
| 514 | if (Py_GETENV("PYTHONDUMPREFS")) |
| 515 | _Py_PrintReferences(stderr); |
Tim Peters | 9cf25ce | 2003-04-17 15:21:01 +0000 | [diff] [blame] | 516 | #endif /* Py_TRACE_REFS */ |
| 517 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 518 | /* Clear interpreter state */ |
| 519 | PyInterpreterState_Clear(interp); |
Guido van Rossum | d922fa4 | 2003-04-15 14:10:09 +0000 | [diff] [blame] | 520 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 521 | /* Now we decref the exception classes. After this point nothing |
| 522 | can raise an exception. That's okay, because each Fini() method |
| 523 | below has been checked to make sure no exceptions are ever |
| 524 | raised. |
| 525 | */ |
Anthony Baxter | 12b6f6c | 2005-03-29 13:36:16 +0000 | [diff] [blame] | 526 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 527 | _PyExc_Fini(); |
Anthony Baxter | 12b6f6c | 2005-03-29 13:36:16 +0000 | [diff] [blame] | 528 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 529 | /* Cleanup auto-thread-state */ |
Christian Heimes | 7d2ff88 | 2007-11-30 14:35:04 +0000 | [diff] [blame] | 530 | #ifdef WITH_THREAD |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 531 | _PyGILState_Fini(); |
Christian Heimes | 7d2ff88 | 2007-11-30 14:35:04 +0000 | [diff] [blame] | 532 | #endif /* WITH_THREAD */ |
| 533 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 534 | /* Delete current thread */ |
| 535 | PyThreadState_Swap(NULL); |
| 536 | PyInterpreterState_Delete(interp); |
Barry Warsaw | f242aa0 | 2000-05-25 23:09:49 +0000 | [diff] [blame] | 537 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 538 | /* Sundry finalizers */ |
| 539 | PyMethod_Fini(); |
| 540 | PyFrame_Fini(); |
| 541 | PyCFunction_Fini(); |
| 542 | PyTuple_Fini(); |
| 543 | PyList_Fini(); |
| 544 | PySet_Fini(); |
| 545 | PyBytes_Fini(); |
| 546 | PyByteArray_Fini(); |
| 547 | PyLong_Fini(); |
| 548 | PyFloat_Fini(); |
| 549 | PyDict_Fini(); |
Antoine Pitrou | f34a0cd | 2011-11-18 20:14:34 +0100 | [diff] [blame] | 550 | PySlice_Fini(); |
Guido van Rossum | cc283f5 | 1997-08-05 02:22:03 +0000 | [diff] [blame] | 551 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 552 | /* Cleanup Unicode implementation */ |
| 553 | _PyUnicode_Fini(); |
Marc-André Lemburg | 95de5c1 | 2002-04-08 08:19:36 +0000 | [diff] [blame] | 554 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 555 | /* reset file system default encoding */ |
Victor Stinner | b744ba1 | 2010-05-15 12:27:16 +0000 | [diff] [blame] | 556 | if (!Py_HasFileSystemDefaultEncoding && Py_FileSystemDefaultEncoding) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 557 | free((char*)Py_FileSystemDefaultEncoding); |
| 558 | Py_FileSystemDefaultEncoding = NULL; |
| 559 | } |
Christian Heimes | c896700 | 2007-11-30 10:18:26 +0000 | [diff] [blame] | 560 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 561 | /* XXX Still allocated: |
| 562 | - various static ad-hoc pointers to interned strings |
| 563 | - int and float free list blocks |
| 564 | - whatever various modules and libraries allocate |
| 565 | */ |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 566 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 567 | PyGrammar_RemoveAccelerators(&_PyParser_Grammar); |
Guido van Rossum | cc283f5 | 1997-08-05 02:22:03 +0000 | [diff] [blame] | 568 | |
Tim Peters | 269b2a6 | 2003-04-17 19:52:29 +0000 | [diff] [blame] | 569 | #ifdef Py_TRACE_REFS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 570 | /* Display addresses (& refcnts) of all objects still alive. |
| 571 | * An address can be used to find the repr of the object, printed |
| 572 | * above by _Py_PrintReferences. |
| 573 | */ |
| 574 | if (Py_GETENV("PYTHONDUMPREFS")) |
| 575 | _Py_PrintReferenceAddresses(stderr); |
Tim Peters | 269b2a6 | 2003-04-17 19:52:29 +0000 | [diff] [blame] | 576 | #endif /* Py_TRACE_REFS */ |
Tim Peters | 0e87118 | 2002-04-13 08:29:14 +0000 | [diff] [blame] | 577 | #ifdef PYMALLOC_DEBUG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 578 | if (Py_GETENV("PYTHONMALLOCSTATS")) |
| 579 | _PyObject_DebugMallocStats(); |
Tim Peters | 0e87118 | 2002-04-13 08:29:14 +0000 | [diff] [blame] | 580 | #endif |
| 581 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 582 | call_ll_exitfuncs(); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 583 | } |
| 584 | |
| 585 | /* Create and initialize a new interpreter and thread, and return the |
| 586 | new thread. This requires that Py_Initialize() has been called |
| 587 | first. |
| 588 | |
| 589 | Unsuccessful initialization yields a NULL pointer. Note that *no* |
| 590 | exception information is available even in this case -- the |
| 591 | exception information is held in the thread, and there is no |
| 592 | thread. |
| 593 | |
| 594 | Locking: as above. |
| 595 | |
| 596 | */ |
| 597 | |
| 598 | PyThreadState * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 599 | Py_NewInterpreter(void) |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 600 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 601 | PyInterpreterState *interp; |
| 602 | PyThreadState *tstate, *save_tstate; |
| 603 | PyObject *bimod, *sysmod; |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 604 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 605 | if (!initialized) |
| 606 | Py_FatalError("Py_NewInterpreter: call Py_Initialize first"); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 607 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 608 | interp = PyInterpreterState_New(); |
| 609 | if (interp == NULL) |
| 610 | return NULL; |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 611 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 612 | tstate = PyThreadState_New(interp); |
| 613 | if (tstate == NULL) { |
| 614 | PyInterpreterState_Delete(interp); |
| 615 | return NULL; |
| 616 | } |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 617 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 618 | save_tstate = PyThreadState_Swap(tstate); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 619 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 620 | /* XXX The following is lax in error checking */ |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 621 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 622 | interp->modules = PyDict_New(); |
| 623 | interp->modules_reloading = PyDict_New(); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 624 | |
Victor Stinner | 49d3f25 | 2010-10-17 01:24:53 +0000 | [diff] [blame] | 625 | bimod = _PyImport_FindBuiltin("builtins"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 626 | if (bimod != NULL) { |
| 627 | interp->builtins = PyModule_GetDict(bimod); |
| 628 | if (interp->builtins == NULL) |
| 629 | goto handle_error; |
| 630 | Py_INCREF(interp->builtins); |
| 631 | } |
Christian Heimes | 6a27efa | 2008-10-30 21:48:26 +0000 | [diff] [blame] | 632 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 633 | /* initialize builtin exceptions */ |
| 634 | _PyExc_Init(); |
Christian Heimes | 6a27efa | 2008-10-30 21:48:26 +0000 | [diff] [blame] | 635 | |
Victor Stinner | 49d3f25 | 2010-10-17 01:24:53 +0000 | [diff] [blame] | 636 | sysmod = _PyImport_FindBuiltin("sys"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 637 | if (bimod != NULL && sysmod != NULL) { |
| 638 | PyObject *pstderr; |
| 639 | interp->sysdict = PyModule_GetDict(sysmod); |
| 640 | if (interp->sysdict == NULL) |
| 641 | goto handle_error; |
| 642 | Py_INCREF(interp->sysdict); |
| 643 | PySys_SetPath(Py_GetPath()); |
| 644 | PyDict_SetItemString(interp->sysdict, "modules", |
| 645 | interp->modules); |
| 646 | /* Set up a preliminary stderr printer until we have enough |
| 647 | infrastructure for the io module in place. */ |
| 648 | pstderr = PyFile_NewStdPrinter(fileno(stderr)); |
| 649 | if (pstderr == NULL) |
| 650 | Py_FatalError("Py_Initialize: can't set preliminary stderr"); |
| 651 | PySys_SetObject("stderr", pstderr); |
| 652 | PySys_SetObject("__stderr__", pstderr); |
Hirokazu Yamamoto | daf83ac | 2010-10-30 15:08:15 +0000 | [diff] [blame] | 653 | Py_DECREF(pstderr); |
Christian Heimes | 6a27efa | 2008-10-30 21:48:26 +0000 | [diff] [blame] | 654 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 655 | _PyImportHooks_Init(); |
Victor Stinner | 793b531 | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 656 | |
| 657 | if (initfsencoding(interp) < 0) |
| 658 | goto handle_error; |
| 659 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 660 | if (initstdio() < 0) |
| 661 | Py_FatalError( |
| 662 | "Py_Initialize: can't initialize sys standard streams"); |
| 663 | initmain(); |
| 664 | if (!Py_NoSiteFlag) |
| 665 | initsite(); |
| 666 | } |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 667 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 668 | if (!PyErr_Occurred()) |
| 669 | return tstate; |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 670 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 671 | handle_error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 672 | /* Oops, it didn't work. Undo it all. */ |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 673 | |
Victor Stinner | c40a350 | 2011-04-27 00:20:27 +0200 | [diff] [blame] | 674 | PyErr_PrintEx(0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 675 | PyThreadState_Clear(tstate); |
| 676 | PyThreadState_Swap(save_tstate); |
| 677 | PyThreadState_Delete(tstate); |
| 678 | PyInterpreterState_Delete(interp); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 679 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 680 | return NULL; |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 681 | } |
| 682 | |
| 683 | /* Delete an interpreter and its last thread. This requires that the |
| 684 | given thread state is current, that the thread has no remaining |
| 685 | frames, and that it is its interpreter's only remaining thread. |
| 686 | It is a fatal error to violate these constraints. |
| 687 | |
| 688 | (Py_Finalize() doesn't have these constraints -- it zaps |
| 689 | everything, regardless.) |
| 690 | |
| 691 | Locking: as above. |
| 692 | |
| 693 | */ |
| 694 | |
| 695 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 696 | Py_EndInterpreter(PyThreadState *tstate) |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 697 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 698 | PyInterpreterState *interp = tstate->interp; |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 699 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 700 | if (tstate != PyThreadState_GET()) |
| 701 | Py_FatalError("Py_EndInterpreter: thread is not current"); |
| 702 | if (tstate->frame != NULL) |
| 703 | Py_FatalError("Py_EndInterpreter: thread still has a frame"); |
| 704 | if (tstate != interp->tstate_head || tstate->next != NULL) |
| 705 | Py_FatalError("Py_EndInterpreter: not the last thread"); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 706 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 707 | PyImport_Cleanup(); |
| 708 | PyInterpreterState_Clear(interp); |
| 709 | PyThreadState_Swap(NULL); |
| 710 | PyInterpreterState_Delete(interp); |
Guido van Rossum | ad6dfda | 1997-07-19 19:17:22 +0000 | [diff] [blame] | 711 | } |
| 712 | |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 713 | static wchar_t *progname = L"python"; |
Guido van Rossum | ad6dfda | 1997-07-19 19:17:22 +0000 | [diff] [blame] | 714 | |
| 715 | void |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 716 | Py_SetProgramName(wchar_t *pn) |
Guido van Rossum | ad6dfda | 1997-07-19 19:17:22 +0000 | [diff] [blame] | 717 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 718 | if (pn && *pn) |
| 719 | progname = pn; |
Guido van Rossum | ad6dfda | 1997-07-19 19:17:22 +0000 | [diff] [blame] | 720 | } |
| 721 | |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 722 | wchar_t * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 723 | Py_GetProgramName(void) |
Guido van Rossum | ad6dfda | 1997-07-19 19:17:22 +0000 | [diff] [blame] | 724 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 725 | return progname; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 726 | } |
| 727 | |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 728 | static wchar_t *default_home = NULL; |
| 729 | static wchar_t env_home[PATH_MAX+1]; |
Guido van Rossum | a61691e | 1998-02-06 22:27:24 +0000 | [diff] [blame] | 730 | |
| 731 | void |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 732 | Py_SetPythonHome(wchar_t *home) |
Guido van Rossum | a61691e | 1998-02-06 22:27:24 +0000 | [diff] [blame] | 733 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 734 | default_home = home; |
Guido van Rossum | a61691e | 1998-02-06 22:27:24 +0000 | [diff] [blame] | 735 | } |
| 736 | |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 737 | wchar_t * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 738 | Py_GetPythonHome(void) |
Guido van Rossum | a61691e | 1998-02-06 22:27:24 +0000 | [diff] [blame] | 739 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 740 | wchar_t *home = default_home; |
| 741 | if (home == NULL && !Py_IgnoreEnvironmentFlag) { |
| 742 | char* chome = Py_GETENV("PYTHONHOME"); |
| 743 | if (chome) { |
| 744 | size_t r = mbstowcs(env_home, chome, PATH_MAX+1); |
| 745 | if (r != (size_t)-1 && r <= PATH_MAX) |
| 746 | home = env_home; |
| 747 | } |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 748 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 749 | } |
| 750 | return home; |
Guido van Rossum | a61691e | 1998-02-06 22:27:24 +0000 | [diff] [blame] | 751 | } |
| 752 | |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 753 | /* Create __main__ module */ |
| 754 | |
| 755 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 756 | initmain(void) |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 757 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 758 | PyObject *m, *d; |
| 759 | m = PyImport_AddModule("__main__"); |
| 760 | if (m == NULL) |
| 761 | Py_FatalError("can't create __main__ module"); |
| 762 | d = PyModule_GetDict(m); |
| 763 | if (PyDict_GetItemString(d, "__builtins__") == NULL) { |
| 764 | PyObject *bimod = PyImport_ImportModule("builtins"); |
| 765 | if (bimod == NULL || |
| 766 | PyDict_SetItemString(d, "__builtins__", bimod) != 0) |
| 767 | Py_FatalError("can't add __builtins__ to __main__"); |
| 768 | Py_DECREF(bimod); |
| 769 | } |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 770 | } |
| 771 | |
Victor Stinner | 793b531 | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 772 | static int |
| 773 | initfsencoding(PyInterpreterState *interp) |
Victor Stinner | b744ba1 | 2010-05-15 12:27:16 +0000 | [diff] [blame] | 774 | { |
| 775 | PyObject *codec; |
Victor Stinner | b744ba1 | 2010-05-15 12:27:16 +0000 | [diff] [blame] | 776 | |
Victor Stinner | d64e8a7 | 2011-07-04 13:48:30 +0200 | [diff] [blame] | 777 | if (Py_FileSystemDefaultEncoding == NULL) |
| 778 | { |
| 779 | Py_FileSystemDefaultEncoding = get_locale_encoding(); |
| 780 | if (Py_FileSystemDefaultEncoding == NULL) |
Victor Stinner | e474309 | 2010-10-19 00:05:51 +0000 | [diff] [blame] | 781 | Py_FatalError("Py_Initialize: Unable to get the locale encoding"); |
Victor Stinner | b744ba1 | 2010-05-15 12:27:16 +0000 | [diff] [blame] | 782 | |
Victor Stinner | e474309 | 2010-10-19 00:05:51 +0000 | [diff] [blame] | 783 | Py_HasFileSystemDefaultEncoding = 0; |
Victor Stinner | 793b531 | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 784 | interp->fscodec_initialized = 1; |
| 785 | return 0; |
Victor Stinner | 7f84ab5 | 2010-06-11 00:36:33 +0000 | [diff] [blame] | 786 | } |
Victor Stinner | b744ba1 | 2010-05-15 12:27:16 +0000 | [diff] [blame] | 787 | |
| 788 | /* the encoding is mbcs, utf-8 or ascii */ |
| 789 | codec = _PyCodec_Lookup(Py_FileSystemDefaultEncoding); |
| 790 | if (!codec) { |
| 791 | /* Such error can only occurs in critical situations: no more |
| 792 | * memory, import a module of the standard library failed, |
| 793 | * etc. */ |
Victor Stinner | 793b531 | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 794 | return -1; |
Victor Stinner | b744ba1 | 2010-05-15 12:27:16 +0000 | [diff] [blame] | 795 | } |
Victor Stinner | 793b531 | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 796 | Py_DECREF(codec); |
| 797 | interp->fscodec_initialized = 1; |
| 798 | return 0; |
Victor Stinner | b744ba1 | 2010-05-15 12:27:16 +0000 | [diff] [blame] | 799 | } |
| 800 | |
Guido van Rossum | dcc0c13 | 1997-08-29 22:32:42 +0000 | [diff] [blame] | 801 | /* Import the site module (not into __main__ though) */ |
| 802 | |
| 803 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 804 | initsite(void) |
Guido van Rossum | dcc0c13 | 1997-08-29 22:32:42 +0000 | [diff] [blame] | 805 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 806 | PyObject *m; |
| 807 | m = PyImport_ImportModule("site"); |
| 808 | if (m == NULL) { |
| 809 | PyErr_Print(); |
| 810 | Py_Finalize(); |
| 811 | exit(1); |
| 812 | } |
| 813 | else { |
| 814 | Py_DECREF(m); |
| 815 | } |
Guido van Rossum | dcc0c13 | 1997-08-29 22:32:42 +0000 | [diff] [blame] | 816 | } |
| 817 | |
Antoine Pitrou | 0560843 | 2009-01-09 18:53:14 +0000 | [diff] [blame] | 818 | static PyObject* |
| 819 | create_stdio(PyObject* io, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 820 | int fd, int write_mode, char* name, |
| 821 | char* encoding, char* errors) |
Antoine Pitrou | 0560843 | 2009-01-09 18:53:14 +0000 | [diff] [blame] | 822 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 823 | PyObject *buf = NULL, *stream = NULL, *text = NULL, *raw = NULL, *res; |
| 824 | const char* mode; |
Victor Stinner | c0f1a1a | 2011-02-23 12:07:37 +0000 | [diff] [blame] | 825 | const char* newline; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 826 | PyObject *line_buffering; |
| 827 | int buffering, isatty; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 828 | _Py_IDENTIFIER(open); |
| 829 | _Py_IDENTIFIER(isatty); |
| 830 | _Py_IDENTIFIER(TextIOWrapper); |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 831 | _Py_IDENTIFIER(name); |
| 832 | _Py_IDENTIFIER(mode); |
Antoine Pitrou | 0560843 | 2009-01-09 18:53:14 +0000 | [diff] [blame] | 833 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 834 | /* stdin is always opened in buffered mode, first because it shouldn't |
| 835 | make a difference in common use cases, second because TextIOWrapper |
| 836 | depends on the presence of a read1() method which only exists on |
| 837 | buffered streams. |
| 838 | */ |
| 839 | if (Py_UnbufferedStdioFlag && write_mode) |
| 840 | buffering = 0; |
| 841 | else |
| 842 | buffering = -1; |
| 843 | if (write_mode) |
| 844 | mode = "wb"; |
| 845 | else |
| 846 | mode = "rb"; |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 847 | buf = _PyObject_CallMethodId(io, &PyId_open, "isiOOOi", |
| 848 | fd, mode, buffering, |
| 849 | Py_None, Py_None, Py_None, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 850 | if (buf == NULL) |
| 851 | goto error; |
Antoine Pitrou | 0560843 | 2009-01-09 18:53:14 +0000 | [diff] [blame] | 852 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 853 | if (buffering) { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 854 | _Py_IDENTIFIER(raw); |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 855 | raw = _PyObject_GetAttrId(buf, &PyId_raw); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 856 | if (raw == NULL) |
| 857 | goto error; |
| 858 | } |
| 859 | else { |
| 860 | raw = buf; |
| 861 | Py_INCREF(raw); |
| 862 | } |
Antoine Pitrou | 0560843 | 2009-01-09 18:53:14 +0000 | [diff] [blame] | 863 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 864 | text = PyUnicode_FromString(name); |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 865 | if (text == NULL || _PyObject_SetAttrId(raw, &PyId_name, text) < 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 866 | goto error; |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 867 | res = _PyObject_CallMethodId(raw, &PyId_isatty, ""); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 868 | if (res == NULL) |
| 869 | goto error; |
| 870 | isatty = PyObject_IsTrue(res); |
| 871 | Py_DECREF(res); |
| 872 | if (isatty == -1) |
| 873 | goto error; |
| 874 | if (isatty || Py_UnbufferedStdioFlag) |
| 875 | line_buffering = Py_True; |
| 876 | else |
| 877 | line_buffering = Py_False; |
Antoine Pitrou | 9169641 | 2009-01-09 22:12:30 +0000 | [diff] [blame] | 878 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 879 | Py_CLEAR(raw); |
| 880 | Py_CLEAR(text); |
Antoine Pitrou | 9169641 | 2009-01-09 22:12:30 +0000 | [diff] [blame] | 881 | |
Victor Stinner | c0f1a1a | 2011-02-23 12:07:37 +0000 | [diff] [blame] | 882 | newline = "\n"; |
| 883 | #ifdef MS_WINDOWS |
| 884 | if (!write_mode) { |
| 885 | /* translate \r\n to \n for sys.stdin on Windows */ |
| 886 | newline = NULL; |
| 887 | } |
| 888 | #endif |
| 889 | |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 890 | stream = _PyObject_CallMethodId(io, &PyId_TextIOWrapper, "OsssO", |
| 891 | buf, encoding, errors, |
| 892 | newline, line_buffering); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 893 | Py_CLEAR(buf); |
| 894 | if (stream == NULL) |
| 895 | goto error; |
Antoine Pitrou | 0560843 | 2009-01-09 18:53:14 +0000 | [diff] [blame] | 896 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 897 | if (write_mode) |
| 898 | mode = "w"; |
| 899 | else |
| 900 | mode = "r"; |
| 901 | text = PyUnicode_FromString(mode); |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 902 | if (!text || _PyObject_SetAttrId(stream, &PyId_mode, text) < 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 903 | goto error; |
| 904 | Py_CLEAR(text); |
| 905 | return stream; |
Antoine Pitrou | 0560843 | 2009-01-09 18:53:14 +0000 | [diff] [blame] | 906 | |
| 907 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 908 | Py_XDECREF(buf); |
| 909 | Py_XDECREF(stream); |
| 910 | Py_XDECREF(text); |
| 911 | Py_XDECREF(raw); |
| 912 | return NULL; |
Antoine Pitrou | 0560843 | 2009-01-09 18:53:14 +0000 | [diff] [blame] | 913 | } |
| 914 | |
Antoine Pitrou | 11942a5 | 2011-11-28 19:08:36 +0100 | [diff] [blame] | 915 | static int |
| 916 | is_valid_fd(int fd) |
| 917 | { |
| 918 | int dummy_fd; |
| 919 | if (fd < 0 || !_PyVerify_fd(fd)) |
| 920 | return 0; |
| 921 | dummy_fd = dup(fd); |
| 922 | if (dummy_fd < 0) |
| 923 | return 0; |
| 924 | close(dummy_fd); |
| 925 | return 1; |
| 926 | } |
| 927 | |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 928 | /* Initialize sys.stdin, stdout, stderr and builtins.open */ |
Guido van Rossum | ce3a72a | 2007-10-19 23:16:50 +0000 | [diff] [blame] | 929 | static int |
| 930 | initstdio(void) |
| 931 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 932 | PyObject *iomod = NULL, *wrapper; |
| 933 | PyObject *bimod = NULL; |
| 934 | PyObject *m; |
| 935 | PyObject *std = NULL; |
| 936 | int status = 0, fd; |
| 937 | PyObject * encoding_attr; |
| 938 | char *encoding = NULL, *errors; |
Guido van Rossum | ce3a72a | 2007-10-19 23:16:50 +0000 | [diff] [blame] | 939 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 940 | /* Hack to avoid a nasty recursion issue when Python is invoked |
| 941 | in verbose mode: pre-import the Latin-1 and UTF-8 codecs */ |
| 942 | if ((m = PyImport_ImportModule("encodings.utf_8")) == NULL) { |
| 943 | goto error; |
| 944 | } |
| 945 | Py_DECREF(m); |
Guido van Rossum | ce3a72a | 2007-10-19 23:16:50 +0000 | [diff] [blame] | 946 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 947 | if (!(m = PyImport_ImportModule("encodings.latin_1"))) { |
| 948 | goto error; |
| 949 | } |
| 950 | Py_DECREF(m); |
Guido van Rossum | ce3a72a | 2007-10-19 23:16:50 +0000 | [diff] [blame] | 951 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 952 | if (!(bimod = PyImport_ImportModule("builtins"))) { |
| 953 | goto error; |
| 954 | } |
Guido van Rossum | ce3a72a | 2007-10-19 23:16:50 +0000 | [diff] [blame] | 955 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 956 | if (!(iomod = PyImport_ImportModule("io"))) { |
| 957 | goto error; |
| 958 | } |
| 959 | if (!(wrapper = PyObject_GetAttrString(iomod, "OpenWrapper"))) { |
| 960 | goto error; |
| 961 | } |
Guido van Rossum | ce3a72a | 2007-10-19 23:16:50 +0000 | [diff] [blame] | 962 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 963 | /* Set builtins.open */ |
| 964 | if (PyObject_SetAttrString(bimod, "open", wrapper) == -1) { |
Antoine Pitrou | 5a96b52 | 2010-11-20 19:50:57 +0000 | [diff] [blame] | 965 | Py_DECREF(wrapper); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 966 | goto error; |
| 967 | } |
Antoine Pitrou | 5a96b52 | 2010-11-20 19:50:57 +0000 | [diff] [blame] | 968 | Py_DECREF(wrapper); |
Guido van Rossum | ce3a72a | 2007-10-19 23:16:50 +0000 | [diff] [blame] | 969 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 970 | encoding = Py_GETENV("PYTHONIOENCODING"); |
| 971 | errors = NULL; |
| 972 | if (encoding) { |
| 973 | encoding = strdup(encoding); |
| 974 | errors = strchr(encoding, ':'); |
| 975 | if (errors) { |
| 976 | *errors = '\0'; |
| 977 | errors++; |
| 978 | } |
| 979 | } |
Martin v. Löwis | 0f59989 | 2008-06-02 11:13:03 +0000 | [diff] [blame] | 980 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 981 | /* Set sys.stdin */ |
| 982 | fd = fileno(stdin); |
| 983 | /* Under some conditions stdin, stdout and stderr may not be connected |
| 984 | * and fileno() may point to an invalid file descriptor. For example |
| 985 | * GUI apps don't have valid standard streams by default. |
| 986 | */ |
Antoine Pitrou | 11942a5 | 2011-11-28 19:08:36 +0100 | [diff] [blame] | 987 | if (!is_valid_fd(fd)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 988 | std = Py_None; |
| 989 | Py_INCREF(std); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 990 | } |
| 991 | else { |
| 992 | std = create_stdio(iomod, fd, 0, "<stdin>", encoding, errors); |
| 993 | if (std == NULL) |
| 994 | goto error; |
| 995 | } /* if (fd < 0) */ |
| 996 | PySys_SetObject("__stdin__", std); |
| 997 | PySys_SetObject("stdin", std); |
| 998 | Py_DECREF(std); |
Guido van Rossum | ce3a72a | 2007-10-19 23:16:50 +0000 | [diff] [blame] | 999 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1000 | /* Set sys.stdout */ |
| 1001 | fd = fileno(stdout); |
Antoine Pitrou | 11942a5 | 2011-11-28 19:08:36 +0100 | [diff] [blame] | 1002 | if (!is_valid_fd(fd)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1003 | std = Py_None; |
| 1004 | Py_INCREF(std); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1005 | } |
| 1006 | else { |
| 1007 | std = create_stdio(iomod, fd, 1, "<stdout>", encoding, errors); |
| 1008 | if (std == NULL) |
| 1009 | goto error; |
| 1010 | } /* if (fd < 0) */ |
| 1011 | PySys_SetObject("__stdout__", std); |
| 1012 | PySys_SetObject("stdout", std); |
| 1013 | Py_DECREF(std); |
Guido van Rossum | ce3a72a | 2007-10-19 23:16:50 +0000 | [diff] [blame] | 1014 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1015 | #if 1 /* Disable this if you have trouble debugging bootstrap stuff */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1016 | /* Set sys.stderr, replaces the preliminary stderr */ |
| 1017 | fd = fileno(stderr); |
Antoine Pitrou | 11942a5 | 2011-11-28 19:08:36 +0100 | [diff] [blame] | 1018 | if (!is_valid_fd(fd)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1019 | std = Py_None; |
| 1020 | Py_INCREF(std); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1021 | } |
| 1022 | else { |
| 1023 | std = create_stdio(iomod, fd, 1, "<stderr>", encoding, "backslashreplace"); |
| 1024 | if (std == NULL) |
| 1025 | goto error; |
| 1026 | } /* if (fd < 0) */ |
Trent Nelson | 39e307e | 2008-03-19 06:45:48 +0000 | [diff] [blame] | 1027 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1028 | /* Same as hack above, pre-import stderr's codec to avoid recursion |
| 1029 | when import.c tries to write to stderr in verbose mode. */ |
| 1030 | encoding_attr = PyObject_GetAttrString(std, "encoding"); |
| 1031 | if (encoding_attr != NULL) { |
| 1032 | const char * encoding; |
| 1033 | encoding = _PyUnicode_AsString(encoding_attr); |
| 1034 | if (encoding != NULL) { |
| 1035 | _PyCodec_Lookup(encoding); |
| 1036 | } |
Hirokazu Yamamoto | daf83ac | 2010-10-30 15:08:15 +0000 | [diff] [blame] | 1037 | Py_DECREF(encoding_attr); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1038 | } |
| 1039 | PyErr_Clear(); /* Not a fatal error if codec isn't available */ |
Trent Nelson | 39e307e | 2008-03-19 06:45:48 +0000 | [diff] [blame] | 1040 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1041 | PySys_SetObject("__stderr__", std); |
| 1042 | PySys_SetObject("stderr", std); |
| 1043 | Py_DECREF(std); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1044 | #endif |
Guido van Rossum | ce3a72a | 2007-10-19 23:16:50 +0000 | [diff] [blame] | 1045 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1046 | if (0) { |
Guido van Rossum | ce3a72a | 2007-10-19 23:16:50 +0000 | [diff] [blame] | 1047 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1048 | status = -1; |
| 1049 | } |
Guido van Rossum | ce3a72a | 2007-10-19 23:16:50 +0000 | [diff] [blame] | 1050 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1051 | if (encoding) |
| 1052 | free(encoding); |
| 1053 | Py_XDECREF(bimod); |
| 1054 | Py_XDECREF(iomod); |
| 1055 | return status; |
Guido van Rossum | ce3a72a | 2007-10-19 23:16:50 +0000 | [diff] [blame] | 1056 | } |
| 1057 | |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 1058 | /* Parse input from a file and execute it */ |
| 1059 | |
| 1060 | int |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1061 | PyRun_AnyFileExFlags(FILE *fp, const char *filename, int closeit, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1062 | PyCompilerFlags *flags) |
Jeremy Hylton | bc32024 | 2001-03-22 02:47:58 +0000 | [diff] [blame] | 1063 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1064 | if (filename == NULL) |
| 1065 | filename = "???"; |
| 1066 | if (Py_FdIsInteractive(fp, filename)) { |
| 1067 | int err = PyRun_InteractiveLoopFlags(fp, filename, flags); |
| 1068 | if (closeit) |
| 1069 | fclose(fp); |
| 1070 | return err; |
| 1071 | } |
| 1072 | else |
| 1073 | return PyRun_SimpleFileExFlags(fp, filename, closeit, flags); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 1074 | } |
| 1075 | |
| 1076 | int |
Martin v. Löwis | 95292d6 | 2002-12-11 14:04:59 +0000 | [diff] [blame] | 1077 | PyRun_InteractiveLoopFlags(FILE *fp, const char *filename, PyCompilerFlags *flags) |
Jeremy Hylton | bc32024 | 2001-03-22 02:47:58 +0000 | [diff] [blame] | 1078 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1079 | PyObject *v; |
| 1080 | int ret; |
| 1081 | PyCompilerFlags local_flags; |
Jeremy Hylton | 9f324e9 | 2001-03-01 22:59:14 +0000 | [diff] [blame] | 1082 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1083 | if (flags == NULL) { |
| 1084 | flags = &local_flags; |
| 1085 | local_flags.cf_flags = 0; |
| 1086 | } |
| 1087 | v = PySys_GetObject("ps1"); |
| 1088 | if (v == NULL) { |
| 1089 | PySys_SetObject("ps1", v = PyUnicode_FromString(">>> ")); |
| 1090 | Py_XDECREF(v); |
| 1091 | } |
| 1092 | v = PySys_GetObject("ps2"); |
| 1093 | if (v == NULL) { |
| 1094 | PySys_SetObject("ps2", v = PyUnicode_FromString("... ")); |
| 1095 | Py_XDECREF(v); |
| 1096 | } |
| 1097 | for (;;) { |
| 1098 | ret = PyRun_InteractiveOneFlags(fp, filename, flags); |
| 1099 | PRINT_TOTAL_REFS(); |
| 1100 | if (ret == E_EOF) |
| 1101 | return 0; |
| 1102 | /* |
| 1103 | if (ret == E_NOMEM) |
| 1104 | return -1; |
| 1105 | */ |
| 1106 | } |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 1107 | } |
| 1108 | |
Neil Schemenauer | c24ea08 | 2002-03-22 23:53:36 +0000 | [diff] [blame] | 1109 | /* compute parser flags based on compiler flags */ |
Benjamin Peterson | f5b5224 | 2009-03-02 23:31:26 +0000 | [diff] [blame] | 1110 | static int PARSER_FLAGS(PyCompilerFlags *flags) |
| 1111 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1112 | int parser_flags = 0; |
| 1113 | if (!flags) |
| 1114 | return 0; |
| 1115 | if (flags->cf_flags & PyCF_DONT_IMPLY_DEDENT) |
| 1116 | parser_flags |= PyPARSE_DONT_IMPLY_DEDENT; |
| 1117 | if (flags->cf_flags & PyCF_IGNORE_COOKIE) |
| 1118 | parser_flags |= PyPARSE_IGNORE_COOKIE; |
| 1119 | if (flags->cf_flags & CO_FUTURE_BARRY_AS_BDFL) |
| 1120 | parser_flags |= PyPARSE_BARRY_AS_BDFL; |
| 1121 | return parser_flags; |
Benjamin Peterson | f5b5224 | 2009-03-02 23:31:26 +0000 | [diff] [blame] | 1122 | } |
Neil Schemenauer | c24ea08 | 2002-03-22 23:53:36 +0000 | [diff] [blame] | 1123 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1124 | #if 0 |
| 1125 | /* Keep an example of flags with future keyword support. */ |
| 1126 | #define PARSER_FLAGS(flags) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1127 | ((flags) ? ((((flags)->cf_flags & PyCF_DONT_IMPLY_DEDENT) ? \ |
| 1128 | PyPARSE_DONT_IMPLY_DEDENT : 0) \ |
| 1129 | | ((flags)->cf_flags & CO_FUTURE_WITH_STATEMENT ? \ |
| 1130 | PyPARSE_WITH_IS_KEYWORD : 0)) : 0) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1131 | #endif |
| 1132 | |
Jeremy Hylton | 9f324e9 | 2001-03-01 22:59:14 +0000 | [diff] [blame] | 1133 | int |
Martin v. Löwis | 95292d6 | 2002-12-11 14:04:59 +0000 | [diff] [blame] | 1134 | PyRun_InteractiveOneFlags(FILE *fp, const char *filename, PyCompilerFlags *flags) |
Jeremy Hylton | 9f324e9 | 2001-03-01 22:59:14 +0000 | [diff] [blame] | 1135 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1136 | PyObject *m, *d, *v, *w, *oenc = NULL; |
| 1137 | mod_ty mod; |
| 1138 | PyArena *arena; |
| 1139 | char *ps1 = "", *ps2 = "", *enc = NULL; |
| 1140 | int errcode = 0; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 1141 | _Py_IDENTIFIER(encoding); |
Tim Peters | fe2127d | 2001-07-16 05:37:24 +0000 | [diff] [blame] | 1142 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1143 | if (fp == stdin) { |
| 1144 | /* Fetch encoding from sys.stdin */ |
| 1145 | v = PySys_GetObject("stdin"); |
| 1146 | if (v == NULL || v == Py_None) |
| 1147 | return -1; |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 1148 | oenc = _PyObject_GetAttrId(v, &PyId_encoding); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1149 | if (!oenc) |
| 1150 | return -1; |
| 1151 | enc = _PyUnicode_AsString(oenc); |
Victor Stinner | 386fe71 | 2010-05-19 00:34:15 +0000 | [diff] [blame] | 1152 | if (enc == NULL) |
| 1153 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1154 | } |
| 1155 | v = PySys_GetObject("ps1"); |
| 1156 | if (v != NULL) { |
| 1157 | v = PyObject_Str(v); |
| 1158 | if (v == NULL) |
| 1159 | PyErr_Clear(); |
Victor Stinner | 386fe71 | 2010-05-19 00:34:15 +0000 | [diff] [blame] | 1160 | else if (PyUnicode_Check(v)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1161 | ps1 = _PyUnicode_AsString(v); |
Victor Stinner | 386fe71 | 2010-05-19 00:34:15 +0000 | [diff] [blame] | 1162 | if (ps1 == NULL) { |
| 1163 | PyErr_Clear(); |
| 1164 | ps1 = ""; |
| 1165 | } |
| 1166 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1167 | } |
| 1168 | w = PySys_GetObject("ps2"); |
| 1169 | if (w != NULL) { |
| 1170 | w = PyObject_Str(w); |
| 1171 | if (w == NULL) |
| 1172 | PyErr_Clear(); |
Victor Stinner | 386fe71 | 2010-05-19 00:34:15 +0000 | [diff] [blame] | 1173 | else if (PyUnicode_Check(w)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1174 | ps2 = _PyUnicode_AsString(w); |
Victor Stinner | 386fe71 | 2010-05-19 00:34:15 +0000 | [diff] [blame] | 1175 | if (ps2 == NULL) { |
| 1176 | PyErr_Clear(); |
| 1177 | ps2 = ""; |
| 1178 | } |
| 1179 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1180 | } |
| 1181 | arena = PyArena_New(); |
| 1182 | if (arena == NULL) { |
| 1183 | Py_XDECREF(v); |
| 1184 | Py_XDECREF(w); |
| 1185 | Py_XDECREF(oenc); |
| 1186 | return -1; |
| 1187 | } |
| 1188 | mod = PyParser_ASTFromFile(fp, filename, enc, |
| 1189 | Py_single_input, ps1, ps2, |
| 1190 | flags, &errcode, arena); |
| 1191 | Py_XDECREF(v); |
| 1192 | Py_XDECREF(w); |
| 1193 | Py_XDECREF(oenc); |
| 1194 | if (mod == NULL) { |
| 1195 | PyArena_Free(arena); |
| 1196 | if (errcode == E_EOF) { |
| 1197 | PyErr_Clear(); |
| 1198 | return E_EOF; |
| 1199 | } |
| 1200 | PyErr_Print(); |
| 1201 | return -1; |
| 1202 | } |
| 1203 | m = PyImport_AddModule("__main__"); |
| 1204 | if (m == NULL) { |
| 1205 | PyArena_Free(arena); |
| 1206 | return -1; |
| 1207 | } |
| 1208 | d = PyModule_GetDict(m); |
| 1209 | v = run_mod(mod, filename, d, d, flags, arena); |
| 1210 | PyArena_Free(arena); |
| 1211 | flush_io(); |
| 1212 | if (v == NULL) { |
| 1213 | PyErr_Print(); |
| 1214 | return -1; |
| 1215 | } |
| 1216 | Py_DECREF(v); |
| 1217 | return 0; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 1218 | } |
| 1219 | |
Martin v. Löwis | be4c0f5 | 2001-01-04 20:30:56 +0000 | [diff] [blame] | 1220 | /* Check whether a file maybe a pyc file: Look at the extension, |
| 1221 | the file type, and, if we may close it, at the first few bytes. */ |
| 1222 | |
| 1223 | static int |
Martin v. Löwis | 95292d6 | 2002-12-11 14:04:59 +0000 | [diff] [blame] | 1224 | maybe_pyc_file(FILE *fp, const char* filename, const char* ext, int closeit) |
Martin v. Löwis | be4c0f5 | 2001-01-04 20:30:56 +0000 | [diff] [blame] | 1225 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1226 | if (strcmp(ext, ".pyc") == 0 || strcmp(ext, ".pyo") == 0) |
| 1227 | return 1; |
Martin v. Löwis | be4c0f5 | 2001-01-04 20:30:56 +0000 | [diff] [blame] | 1228 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1229 | /* Only look into the file if we are allowed to close it, since |
| 1230 | it then should also be seekable. */ |
| 1231 | if (closeit) { |
| 1232 | /* Read only two bytes of the magic. If the file was opened in |
| 1233 | text mode, the bytes 3 and 4 of the magic (\r\n) might not |
| 1234 | be read as they are on disk. */ |
| 1235 | unsigned int halfmagic = PyImport_GetMagicNumber() & 0xFFFF; |
| 1236 | unsigned char buf[2]; |
| 1237 | /* Mess: In case of -x, the stream is NOT at its start now, |
| 1238 | and ungetc() was used to push back the first newline, |
| 1239 | which makes the current stream position formally undefined, |
| 1240 | and a x-platform nightmare. |
| 1241 | Unfortunately, we have no direct way to know whether -x |
| 1242 | was specified. So we use a terrible hack: if the current |
| 1243 | stream position is not 0, we assume -x was specified, and |
| 1244 | give up. Bug 132850 on SourceForge spells out the |
| 1245 | hopelessness of trying anything else (fseek and ftell |
| 1246 | don't work predictably x-platform for text-mode files). |
| 1247 | */ |
| 1248 | int ispyc = 0; |
| 1249 | if (ftell(fp) == 0) { |
| 1250 | if (fread(buf, 1, 2, fp) == 2 && |
| 1251 | ((unsigned int)buf[1]<<8 | buf[0]) == halfmagic) |
| 1252 | ispyc = 1; |
| 1253 | rewind(fp); |
| 1254 | } |
| 1255 | return ispyc; |
| 1256 | } |
| 1257 | return 0; |
Tim Peters | d08e382 | 2003-04-17 15:24:21 +0000 | [diff] [blame] | 1258 | } |
Martin v. Löwis | be4c0f5 | 2001-01-04 20:30:56 +0000 | [diff] [blame] | 1259 | |
Guido van Rossum | 0df002c | 2000-08-27 19:21:52 +0000 | [diff] [blame] | 1260 | int |
Martin v. Löwis | 95292d6 | 2002-12-11 14:04:59 +0000 | [diff] [blame] | 1261 | PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1262 | PyCompilerFlags *flags) |
Jeremy Hylton | bc32024 | 2001-03-22 02:47:58 +0000 | [diff] [blame] | 1263 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1264 | PyObject *m, *d, *v; |
| 1265 | const char *ext; |
Victor Stinner | 0fcab4a | 2011-01-04 12:59:15 +0000 | [diff] [blame] | 1266 | int set_file_name = 0, ret; |
| 1267 | size_t len; |
Guido van Rossum | fdef271 | 1994-09-14 13:31:04 +0000 | [diff] [blame] | 1268 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1269 | m = PyImport_AddModule("__main__"); |
| 1270 | if (m == NULL) |
| 1271 | return -1; |
| 1272 | d = PyModule_GetDict(m); |
| 1273 | if (PyDict_GetItemString(d, "__file__") == NULL) { |
| 1274 | PyObject *f; |
Victor Stinner | 4c7c8c3 | 2010-10-16 13:14:10 +0000 | [diff] [blame] | 1275 | f = PyUnicode_DecodeFSDefault(filename); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1276 | if (f == NULL) |
| 1277 | return -1; |
| 1278 | if (PyDict_SetItemString(d, "__file__", f) < 0) { |
| 1279 | Py_DECREF(f); |
| 1280 | return -1; |
| 1281 | } |
Barry Warsaw | 916048d | 2011-09-20 14:45:44 -0400 | [diff] [blame] | 1282 | if (PyDict_SetItemString(d, "__cached__", Py_None) < 0) { |
| 1283 | Py_DECREF(f); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1284 | return -1; |
Barry Warsaw | 916048d | 2011-09-20 14:45:44 -0400 | [diff] [blame] | 1285 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1286 | set_file_name = 1; |
| 1287 | Py_DECREF(f); |
| 1288 | } |
| 1289 | len = strlen(filename); |
| 1290 | ext = filename + len - (len > 4 ? 4 : 0); |
| 1291 | if (maybe_pyc_file(fp, filename, ext, closeit)) { |
| 1292 | /* Try to run a pyc file. First, re-open in binary */ |
| 1293 | if (closeit) |
| 1294 | fclose(fp); |
| 1295 | if ((fp = fopen(filename, "rb")) == NULL) { |
| 1296 | fprintf(stderr, "python: Can't reopen .pyc file\n"); |
| 1297 | ret = -1; |
| 1298 | goto done; |
| 1299 | } |
| 1300 | /* Turn on optimization if a .pyo file is given */ |
| 1301 | if (strcmp(ext, ".pyo") == 0) |
| 1302 | Py_OptimizeFlag = 1; |
| 1303 | v = run_pyc_file(fp, filename, d, d, flags); |
| 1304 | } else { |
| 1305 | v = PyRun_FileExFlags(fp, filename, Py_file_input, d, d, |
| 1306 | closeit, flags); |
| 1307 | } |
| 1308 | flush_io(); |
| 1309 | if (v == NULL) { |
| 1310 | PyErr_Print(); |
| 1311 | ret = -1; |
| 1312 | goto done; |
| 1313 | } |
| 1314 | Py_DECREF(v); |
| 1315 | ret = 0; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1316 | done: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1317 | if (set_file_name && PyDict_DelItemString(d, "__file__")) |
| 1318 | PyErr_Clear(); |
| 1319 | return ret; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 1320 | } |
| 1321 | |
| 1322 | int |
Martin v. Löwis | 95292d6 | 2002-12-11 14:04:59 +0000 | [diff] [blame] | 1323 | PyRun_SimpleStringFlags(const char *command, PyCompilerFlags *flags) |
Guido van Rossum | 393661d | 2001-08-31 17:40:15 +0000 | [diff] [blame] | 1324 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1325 | PyObject *m, *d, *v; |
| 1326 | m = PyImport_AddModule("__main__"); |
| 1327 | if (m == NULL) |
| 1328 | return -1; |
| 1329 | d = PyModule_GetDict(m); |
| 1330 | v = PyRun_StringFlags(command, Py_file_input, d, d, flags); |
| 1331 | if (v == NULL) { |
| 1332 | PyErr_Print(); |
| 1333 | return -1; |
| 1334 | } |
| 1335 | Py_DECREF(v); |
| 1336 | return 0; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 1337 | } |
| 1338 | |
Barry Warsaw | 035574d | 1997-08-29 22:07:17 +0000 | [diff] [blame] | 1339 | static int |
Martin v. Löwis | 95292d6 | 2002-12-11 14:04:59 +0000 | [diff] [blame] | 1340 | parse_syntax_error(PyObject *err, PyObject **message, const char **filename, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1341 | int *lineno, int *offset, const char **text) |
Barry Warsaw | 035574d | 1997-08-29 22:07:17 +0000 | [diff] [blame] | 1342 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1343 | long hold; |
| 1344 | PyObject *v; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 1345 | _Py_IDENTIFIER(msg); |
| 1346 | _Py_IDENTIFIER(filename); |
| 1347 | _Py_IDENTIFIER(lineno); |
| 1348 | _Py_IDENTIFIER(offset); |
| 1349 | _Py_IDENTIFIER(text); |
Barry Warsaw | 035574d | 1997-08-29 22:07:17 +0000 | [diff] [blame] | 1350 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1351 | /* new style errors. `err' is an instance */ |
Barry Warsaw | 035574d | 1997-08-29 22:07:17 +0000 | [diff] [blame] | 1352 | |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 1353 | if (! (v = _PyObject_GetAttrId(err, &PyId_msg))) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1354 | goto finally; |
| 1355 | *message = v; |
Barry Warsaw | 035574d | 1997-08-29 22:07:17 +0000 | [diff] [blame] | 1356 | |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 1357 | if (!(v = _PyObject_GetAttrId(err, &PyId_filename))) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1358 | goto finally; |
| 1359 | if (v == Py_None) |
| 1360 | *filename = NULL; |
| 1361 | else if (! (*filename = _PyUnicode_AsString(v))) |
| 1362 | goto finally; |
Barry Warsaw | 035574d | 1997-08-29 22:07:17 +0000 | [diff] [blame] | 1363 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1364 | Py_DECREF(v); |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 1365 | if (!(v = _PyObject_GetAttrId(err, &PyId_lineno))) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1366 | goto finally; |
| 1367 | hold = PyLong_AsLong(v); |
| 1368 | Py_DECREF(v); |
| 1369 | v = NULL; |
| 1370 | if (hold < 0 && PyErr_Occurred()) |
| 1371 | goto finally; |
| 1372 | *lineno = (int)hold; |
Barry Warsaw | 035574d | 1997-08-29 22:07:17 +0000 | [diff] [blame] | 1373 | |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 1374 | if (!(v = _PyObject_GetAttrId(err, &PyId_offset))) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1375 | goto finally; |
| 1376 | if (v == Py_None) { |
| 1377 | *offset = -1; |
| 1378 | Py_DECREF(v); |
| 1379 | v = NULL; |
| 1380 | } else { |
| 1381 | hold = PyLong_AsLong(v); |
| 1382 | Py_DECREF(v); |
| 1383 | v = NULL; |
| 1384 | if (hold < 0 && PyErr_Occurred()) |
| 1385 | goto finally; |
| 1386 | *offset = (int)hold; |
| 1387 | } |
Barry Warsaw | 035574d | 1997-08-29 22:07:17 +0000 | [diff] [blame] | 1388 | |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 1389 | if (!(v = _PyObject_GetAttrId(err, &PyId_text))) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1390 | goto finally; |
| 1391 | if (v == Py_None) |
| 1392 | *text = NULL; |
| 1393 | else if (!PyUnicode_Check(v) || |
| 1394 | !(*text = _PyUnicode_AsString(v))) |
| 1395 | goto finally; |
| 1396 | Py_DECREF(v); |
| 1397 | return 1; |
Barry Warsaw | 035574d | 1997-08-29 22:07:17 +0000 | [diff] [blame] | 1398 | |
| 1399 | finally: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1400 | Py_XDECREF(v); |
| 1401 | return 0; |
Barry Warsaw | 035574d | 1997-08-29 22:07:17 +0000 | [diff] [blame] | 1402 | } |
| 1403 | |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 1404 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1405 | PyErr_Print(void) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 1406 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1407 | PyErr_PrintEx(1); |
Guido van Rossum | a61691e | 1998-02-06 22:27:24 +0000 | [diff] [blame] | 1408 | } |
| 1409 | |
Jeremy Hylton | 9f1b993 | 2001-02-28 07:07:43 +0000 | [diff] [blame] | 1410 | static void |
Martin v. Löwis | 95292d6 | 2002-12-11 14:04:59 +0000 | [diff] [blame] | 1411 | print_error_text(PyObject *f, int offset, const char *text) |
Jeremy Hylton | 9f1b993 | 2001-02-28 07:07:43 +0000 | [diff] [blame] | 1412 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1413 | char *nl; |
| 1414 | if (offset >= 0) { |
Benjamin Peterson | a95e977 | 2010-10-29 03:28:14 +0000 | [diff] [blame] | 1415 | if (offset > 0 && offset == strlen(text) && text[offset - 1] == '\n') |
| 1416 | offset--; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1417 | for (;;) { |
| 1418 | nl = strchr(text, '\n'); |
| 1419 | if (nl == NULL || nl-text >= offset) |
| 1420 | break; |
| 1421 | offset -= (int)(nl+1-text); |
| 1422 | text = nl+1; |
| 1423 | } |
| 1424 | while (*text == ' ' || *text == '\t') { |
| 1425 | text++; |
| 1426 | offset--; |
| 1427 | } |
| 1428 | } |
| 1429 | PyFile_WriteString(" ", f); |
| 1430 | PyFile_WriteString(text, f); |
| 1431 | if (*text == '\0' || text[strlen(text)-1] != '\n') |
| 1432 | PyFile_WriteString("\n", f); |
| 1433 | if (offset == -1) |
| 1434 | return; |
| 1435 | PyFile_WriteString(" ", f); |
Benjamin Peterson | a95e977 | 2010-10-29 03:28:14 +0000 | [diff] [blame] | 1436 | while (--offset > 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1437 | PyFile_WriteString(" ", f); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1438 | PyFile_WriteString("^\n", f); |
Jeremy Hylton | 9f1b993 | 2001-02-28 07:07:43 +0000 | [diff] [blame] | 1439 | } |
| 1440 | |
Guido van Rossum | 66e8e86 | 2001-03-23 17:54:43 +0000 | [diff] [blame] | 1441 | static void |
| 1442 | handle_system_exit(void) |
Ka-Ping Yee | 26fabb0 | 2001-03-23 15:36:41 +0000 | [diff] [blame] | 1443 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1444 | PyObject *exception, *value, *tb; |
| 1445 | int exitcode = 0; |
Tim Peters | cf615b5 | 2003-04-19 18:47:02 +0000 | [diff] [blame] | 1446 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1447 | if (Py_InspectFlag) |
| 1448 | /* Don't exit if -i flag was given. This flag is set to 0 |
| 1449 | * when entering interactive mode for inspecting. */ |
| 1450 | return; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1451 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1452 | PyErr_Fetch(&exception, &value, &tb); |
| 1453 | fflush(stdout); |
| 1454 | if (value == NULL || value == Py_None) |
| 1455 | goto done; |
| 1456 | if (PyExceptionInstance_Check(value)) { |
| 1457 | /* The error code should be in the `code' attribute. */ |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 1458 | _Py_IDENTIFIER(code); |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 1459 | PyObject *code = _PyObject_GetAttrId(value, &PyId_code); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1460 | if (code) { |
| 1461 | Py_DECREF(value); |
| 1462 | value = code; |
| 1463 | if (value == Py_None) |
| 1464 | goto done; |
| 1465 | } |
| 1466 | /* If we failed to dig out the 'code' attribute, |
| 1467 | just let the else clause below print the error. */ |
| 1468 | } |
| 1469 | if (PyLong_Check(value)) |
| 1470 | exitcode = (int)PyLong_AsLong(value); |
| 1471 | else { |
Victor Stinner | e9fb319 | 2010-05-17 08:58:51 +0000 | [diff] [blame] | 1472 | PyObject *sys_stderr = PySys_GetObject("stderr"); |
Victor Stinner | 7126dbc | 2010-05-21 23:45:42 +0000 | [diff] [blame] | 1473 | if (sys_stderr != NULL && sys_stderr != Py_None) { |
| 1474 | PyFile_WriteObject(value, sys_stderr, Py_PRINT_RAW); |
| 1475 | } else { |
| 1476 | PyObject_Print(value, stderr, Py_PRINT_RAW); |
| 1477 | fflush(stderr); |
| 1478 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1479 | PySys_WriteStderr("\n"); |
| 1480 | exitcode = 1; |
| 1481 | } |
Tim Peters | cf615b5 | 2003-04-19 18:47:02 +0000 | [diff] [blame] | 1482 | done: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1483 | /* Restore and clear the exception info, in order to properly decref |
| 1484 | * the exception, value, and traceback. If we just exit instead, |
| 1485 | * these leak, which confuses PYTHONDUMPREFS output, and may prevent |
| 1486 | * some finalizers from running. |
| 1487 | */ |
| 1488 | PyErr_Restore(exception, value, tb); |
| 1489 | PyErr_Clear(); |
| 1490 | Py_Exit(exitcode); |
| 1491 | /* NOTREACHED */ |
Ka-Ping Yee | 26fabb0 | 2001-03-23 15:36:41 +0000 | [diff] [blame] | 1492 | } |
| 1493 | |
| 1494 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1495 | PyErr_PrintEx(int set_sys_last_vars) |
Guido van Rossum | a61691e | 1998-02-06 22:27:24 +0000 | [diff] [blame] | 1496 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1497 | PyObject *exception, *v, *tb, *hook; |
Guido van Rossum | 66e8e86 | 2001-03-23 17:54:43 +0000 | [diff] [blame] | 1498 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1499 | if (PyErr_ExceptionMatches(PyExc_SystemExit)) { |
| 1500 | handle_system_exit(); |
| 1501 | } |
| 1502 | PyErr_Fetch(&exception, &v, &tb); |
| 1503 | if (exception == NULL) |
| 1504 | return; |
| 1505 | PyErr_NormalizeException(&exception, &v, &tb); |
| 1506 | if (tb == NULL) { |
| 1507 | tb = Py_None; |
| 1508 | Py_INCREF(tb); |
| 1509 | } |
| 1510 | PyException_SetTraceback(v, tb); |
| 1511 | if (exception == NULL) |
| 1512 | return; |
| 1513 | /* Now we know v != NULL too */ |
| 1514 | if (set_sys_last_vars) { |
| 1515 | PySys_SetObject("last_type", exception); |
| 1516 | PySys_SetObject("last_value", v); |
| 1517 | PySys_SetObject("last_traceback", tb); |
| 1518 | } |
| 1519 | hook = PySys_GetObject("excepthook"); |
| 1520 | if (hook) { |
| 1521 | PyObject *args = PyTuple_Pack(3, exception, v, tb); |
| 1522 | PyObject *result = PyEval_CallObject(hook, args); |
| 1523 | if (result == NULL) { |
| 1524 | PyObject *exception2, *v2, *tb2; |
| 1525 | if (PyErr_ExceptionMatches(PyExc_SystemExit)) { |
| 1526 | handle_system_exit(); |
| 1527 | } |
| 1528 | PyErr_Fetch(&exception2, &v2, &tb2); |
| 1529 | PyErr_NormalizeException(&exception2, &v2, &tb2); |
| 1530 | /* It should not be possible for exception2 or v2 |
| 1531 | to be NULL. However PyErr_Display() can't |
| 1532 | tolerate NULLs, so just be safe. */ |
| 1533 | if (exception2 == NULL) { |
| 1534 | exception2 = Py_None; |
| 1535 | Py_INCREF(exception2); |
| 1536 | } |
| 1537 | if (v2 == NULL) { |
| 1538 | v2 = Py_None; |
| 1539 | Py_INCREF(v2); |
| 1540 | } |
| 1541 | fflush(stdout); |
| 1542 | PySys_WriteStderr("Error in sys.excepthook:\n"); |
| 1543 | PyErr_Display(exception2, v2, tb2); |
| 1544 | PySys_WriteStderr("\nOriginal exception was:\n"); |
| 1545 | PyErr_Display(exception, v, tb); |
| 1546 | Py_DECREF(exception2); |
| 1547 | Py_DECREF(v2); |
| 1548 | Py_XDECREF(tb2); |
| 1549 | } |
| 1550 | Py_XDECREF(result); |
| 1551 | Py_XDECREF(args); |
| 1552 | } else { |
| 1553 | PySys_WriteStderr("sys.excepthook is missing\n"); |
| 1554 | PyErr_Display(exception, v, tb); |
| 1555 | } |
| 1556 | Py_XDECREF(exception); |
| 1557 | Py_XDECREF(v); |
| 1558 | Py_XDECREF(tb); |
Ka-Ping Yee | b5c5132 | 2001-03-23 02:46:52 +0000 | [diff] [blame] | 1559 | } |
| 1560 | |
Benjamin Peterson | e652821 | 2008-07-15 15:32:09 +0000 | [diff] [blame] | 1561 | static void |
| 1562 | print_exception(PyObject *f, PyObject *value) |
| 1563 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1564 | int err = 0; |
| 1565 | PyObject *type, *tb; |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 1566 | _Py_IDENTIFIER(print_file_and_line); |
Benjamin Peterson | e652821 | 2008-07-15 15:32:09 +0000 | [diff] [blame] | 1567 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1568 | if (!PyExceptionInstance_Check(value)) { |
| 1569 | PyFile_WriteString("TypeError: print_exception(): Exception expected for value, ", f); |
| 1570 | PyFile_WriteString(Py_TYPE(value)->tp_name, f); |
| 1571 | PyFile_WriteString(" found\n", f); |
| 1572 | return; |
| 1573 | } |
Benjamin Peterson | 2658260 | 2008-08-23 20:08:07 +0000 | [diff] [blame] | 1574 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1575 | Py_INCREF(value); |
| 1576 | fflush(stdout); |
| 1577 | type = (PyObject *) Py_TYPE(value); |
| 1578 | tb = PyException_GetTraceback(value); |
| 1579 | if (tb && tb != Py_None) |
| 1580 | err = PyTraceBack_Print(tb, f); |
| 1581 | if (err == 0 && |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 1582 | _PyObject_HasAttrId(value, &PyId_print_file_and_line)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1583 | { |
| 1584 | PyObject *message; |
| 1585 | const char *filename, *text; |
| 1586 | int lineno, offset; |
| 1587 | if (!parse_syntax_error(value, &message, &filename, |
| 1588 | &lineno, &offset, &text)) |
| 1589 | PyErr_Clear(); |
| 1590 | else { |
| 1591 | char buf[10]; |
| 1592 | PyFile_WriteString(" File \"", f); |
| 1593 | if (filename == NULL) |
| 1594 | PyFile_WriteString("<string>", f); |
| 1595 | else |
| 1596 | PyFile_WriteString(filename, f); |
| 1597 | PyFile_WriteString("\", line ", f); |
| 1598 | PyOS_snprintf(buf, sizeof(buf), "%d", lineno); |
| 1599 | PyFile_WriteString(buf, f); |
| 1600 | PyFile_WriteString("\n", f); |
| 1601 | if (text != NULL) |
| 1602 | print_error_text(f, offset, text); |
| 1603 | Py_DECREF(value); |
| 1604 | value = message; |
| 1605 | /* Can't be bothered to check all those |
| 1606 | PyFile_WriteString() calls */ |
| 1607 | if (PyErr_Occurred()) |
| 1608 | err = -1; |
| 1609 | } |
| 1610 | } |
| 1611 | if (err) { |
| 1612 | /* Don't do anything else */ |
| 1613 | } |
| 1614 | else { |
| 1615 | PyObject* moduleName; |
| 1616 | char* className; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 1617 | _Py_IDENTIFIER(__module__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1618 | assert(PyExceptionClass_Check(type)); |
| 1619 | className = PyExceptionClass_Name(type); |
| 1620 | if (className != NULL) { |
| 1621 | char *dot = strrchr(className, '.'); |
| 1622 | if (dot != NULL) |
| 1623 | className = dot+1; |
| 1624 | } |
Benjamin Peterson | e652821 | 2008-07-15 15:32:09 +0000 | [diff] [blame] | 1625 | |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 1626 | moduleName = _PyObject_GetAttrId(type, &PyId___module__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1627 | if (moduleName == NULL || !PyUnicode_Check(moduleName)) |
| 1628 | { |
Victor Stinner | 13b21bd | 2011-05-26 14:25:13 +0200 | [diff] [blame] | 1629 | Py_XDECREF(moduleName); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1630 | err = PyFile_WriteString("<unknown>", f); |
| 1631 | } |
| 1632 | else { |
| 1633 | char* modstr = _PyUnicode_AsString(moduleName); |
| 1634 | if (modstr && strcmp(modstr, "builtins")) |
| 1635 | { |
| 1636 | err = PyFile_WriteString(modstr, f); |
| 1637 | err += PyFile_WriteString(".", f); |
| 1638 | } |
| 1639 | Py_DECREF(moduleName); |
| 1640 | } |
| 1641 | if (err == 0) { |
| 1642 | if (className == NULL) |
| 1643 | err = PyFile_WriteString("<unknown>", f); |
| 1644 | else |
| 1645 | err = PyFile_WriteString(className, f); |
| 1646 | } |
| 1647 | } |
| 1648 | if (err == 0 && (value != Py_None)) { |
| 1649 | PyObject *s = PyObject_Str(value); |
| 1650 | /* only print colon if the str() of the |
| 1651 | object is not the empty string |
| 1652 | */ |
| 1653 | if (s == NULL) |
| 1654 | err = -1; |
| 1655 | else if (!PyUnicode_Check(s) || |
Victor Stinner | e251d6d | 2011-11-20 19:20:00 +0100 | [diff] [blame] | 1656 | PyUnicode_GetLength(s) != 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1657 | err = PyFile_WriteString(": ", f); |
| 1658 | if (err == 0) |
| 1659 | err = PyFile_WriteObject(s, f, Py_PRINT_RAW); |
| 1660 | Py_XDECREF(s); |
| 1661 | } |
| 1662 | /* try to write a newline in any case */ |
| 1663 | err += PyFile_WriteString("\n", f); |
| 1664 | Py_XDECREF(tb); |
| 1665 | Py_DECREF(value); |
| 1666 | /* If an error happened here, don't show it. |
| 1667 | XXX This is wrong, but too many callers rely on this behavior. */ |
| 1668 | if (err != 0) |
| 1669 | PyErr_Clear(); |
Benjamin Peterson | e652821 | 2008-07-15 15:32:09 +0000 | [diff] [blame] | 1670 | } |
| 1671 | |
| 1672 | static const char *cause_message = |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1673 | "\nThe above exception was the direct cause " |
| 1674 | "of the following exception:\n\n"; |
Benjamin Peterson | e652821 | 2008-07-15 15:32:09 +0000 | [diff] [blame] | 1675 | |
| 1676 | static const char *context_message = |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1677 | "\nDuring handling of the above exception, " |
| 1678 | "another exception occurred:\n\n"; |
Benjamin Peterson | e652821 | 2008-07-15 15:32:09 +0000 | [diff] [blame] | 1679 | |
| 1680 | static void |
| 1681 | print_exception_recursive(PyObject *f, PyObject *value, PyObject *seen) |
| 1682 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1683 | int err = 0, res; |
| 1684 | PyObject *cause, *context; |
Benjamin Peterson | e652821 | 2008-07-15 15:32:09 +0000 | [diff] [blame] | 1685 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1686 | if (seen != NULL) { |
| 1687 | /* Exception chaining */ |
| 1688 | if (PySet_Add(seen, value) == -1) |
| 1689 | PyErr_Clear(); |
| 1690 | else if (PyExceptionInstance_Check(value)) { |
| 1691 | cause = PyException_GetCause(value); |
| 1692 | context = PyException_GetContext(value); |
| 1693 | if (cause) { |
| 1694 | res = PySet_Contains(seen, cause); |
| 1695 | if (res == -1) |
| 1696 | PyErr_Clear(); |
| 1697 | if (res == 0) { |
| 1698 | print_exception_recursive( |
| 1699 | f, cause, seen); |
| 1700 | err |= PyFile_WriteString( |
| 1701 | cause_message, f); |
| 1702 | } |
| 1703 | } |
| 1704 | else if (context) { |
| 1705 | res = PySet_Contains(seen, context); |
| 1706 | if (res == -1) |
| 1707 | PyErr_Clear(); |
| 1708 | if (res == 0) { |
| 1709 | print_exception_recursive( |
| 1710 | f, context, seen); |
| 1711 | err |= PyFile_WriteString( |
| 1712 | context_message, f); |
| 1713 | } |
| 1714 | } |
| 1715 | Py_XDECREF(context); |
| 1716 | Py_XDECREF(cause); |
| 1717 | } |
| 1718 | } |
| 1719 | print_exception(f, value); |
| 1720 | if (err != 0) |
| 1721 | PyErr_Clear(); |
Benjamin Peterson | e652821 | 2008-07-15 15:32:09 +0000 | [diff] [blame] | 1722 | } |
| 1723 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1724 | void |
| 1725 | PyErr_Display(PyObject *exception, PyObject *value, PyObject *tb) |
Ka-Ping Yee | b5c5132 | 2001-03-23 02:46:52 +0000 | [diff] [blame] | 1726 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1727 | PyObject *seen; |
| 1728 | PyObject *f = PySys_GetObject("stderr"); |
| 1729 | if (f == Py_None) { |
| 1730 | /* pass */ |
| 1731 | } |
| 1732 | else if (f == NULL) { |
| 1733 | _PyObject_Dump(value); |
| 1734 | fprintf(stderr, "lost sys.stderr\n"); |
| 1735 | } |
| 1736 | else { |
| 1737 | /* We choose to ignore seen being possibly NULL, and report |
| 1738 | at least the main exception (it could be a MemoryError). |
| 1739 | */ |
| 1740 | seen = PySet_New(NULL); |
| 1741 | if (seen == NULL) |
| 1742 | PyErr_Clear(); |
| 1743 | print_exception_recursive(f, value, seen); |
| 1744 | Py_XDECREF(seen); |
| 1745 | } |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 1746 | } |
| 1747 | |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 1748 | PyObject * |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1749 | PyRun_StringFlags(const char *str, int start, PyObject *globals, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1750 | PyObject *locals, PyCompilerFlags *flags) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 1751 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1752 | PyObject *ret = NULL; |
| 1753 | mod_ty mod; |
| 1754 | PyArena *arena = PyArena_New(); |
| 1755 | if (arena == NULL) |
| 1756 | return NULL; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1757 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1758 | mod = PyParser_ASTFromString(str, "<string>", start, flags, arena); |
| 1759 | if (mod != NULL) |
| 1760 | ret = run_mod(mod, "<string>", globals, locals, flags, arena); |
| 1761 | PyArena_Free(arena); |
| 1762 | return ret; |
Jeremy Hylton | bc32024 | 2001-03-22 02:47:58 +0000 | [diff] [blame] | 1763 | } |
| 1764 | |
| 1765 | PyObject * |
Martin v. Löwis | 95292d6 | 2002-12-11 14:04:59 +0000 | [diff] [blame] | 1766 | PyRun_FileExFlags(FILE *fp, const char *filename, int start, PyObject *globals, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1767 | PyObject *locals, int closeit, PyCompilerFlags *flags) |
Jeremy Hylton | bc32024 | 2001-03-22 02:47:58 +0000 | [diff] [blame] | 1768 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1769 | PyObject *ret; |
| 1770 | mod_ty mod; |
| 1771 | PyArena *arena = PyArena_New(); |
| 1772 | if (arena == NULL) |
| 1773 | return NULL; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1774 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1775 | mod = PyParser_ASTFromFile(fp, filename, NULL, start, 0, 0, |
| 1776 | flags, NULL, arena); |
| 1777 | if (closeit) |
| 1778 | fclose(fp); |
| 1779 | if (mod == NULL) { |
| 1780 | PyArena_Free(arena); |
| 1781 | return NULL; |
| 1782 | } |
| 1783 | ret = run_mod(mod, filename, globals, locals, flags, arena); |
| 1784 | PyArena_Free(arena); |
| 1785 | return ret; |
Jeremy Hylton | bc32024 | 2001-03-22 02:47:58 +0000 | [diff] [blame] | 1786 | } |
| 1787 | |
Guido van Rossum | 6c193fa | 2007-12-05 05:14:58 +0000 | [diff] [blame] | 1788 | static void |
| 1789 | flush_io(void) |
| 1790 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1791 | PyObject *f, *r; |
| 1792 | PyObject *type, *value, *traceback; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 1793 | _Py_IDENTIFIER(flush); |
Amaury Forgeot d'Arc | 9ed7735 | 2008-04-04 23:25:27 +0000 | [diff] [blame] | 1794 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1795 | /* Save the current exception */ |
| 1796 | PyErr_Fetch(&type, &value, &traceback); |
Amaury Forgeot d'Arc | 9ed7735 | 2008-04-04 23:25:27 +0000 | [diff] [blame] | 1797 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1798 | f = PySys_GetObject("stderr"); |
| 1799 | if (f != NULL) { |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1800 | r = _PyObject_CallMethodId(f, &PyId_flush, ""); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1801 | if (r) |
| 1802 | Py_DECREF(r); |
| 1803 | else |
| 1804 | PyErr_Clear(); |
| 1805 | } |
| 1806 | f = PySys_GetObject("stdout"); |
| 1807 | if (f != NULL) { |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1808 | r = _PyObject_CallMethodId(f, &PyId_flush, ""); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1809 | if (r) |
| 1810 | Py_DECREF(r); |
| 1811 | else |
| 1812 | PyErr_Clear(); |
| 1813 | } |
Amaury Forgeot d'Arc | 9ed7735 | 2008-04-04 23:25:27 +0000 | [diff] [blame] | 1814 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1815 | PyErr_Restore(type, value, traceback); |
Guido van Rossum | 6c193fa | 2007-12-05 05:14:58 +0000 | [diff] [blame] | 1816 | } |
| 1817 | |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 1818 | static PyObject * |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1819 | run_mod(mod_ty mod, const char *filename, PyObject *globals, PyObject *locals, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1820 | PyCompilerFlags *flags, PyArena *arena) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 1821 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1822 | PyCodeObject *co; |
| 1823 | PyObject *v; |
| 1824 | co = PyAST_Compile(mod, filename, flags, arena); |
| 1825 | if (co == NULL) |
| 1826 | return NULL; |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 1827 | v = PyEval_EvalCode((PyObject*)co, globals, locals); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1828 | Py_DECREF(co); |
| 1829 | return v; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 1830 | } |
| 1831 | |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 1832 | static PyObject * |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1833 | run_pyc_file(FILE *fp, const char *filename, PyObject *globals, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1834 | PyObject *locals, PyCompilerFlags *flags) |
Guido van Rossum | fdef271 | 1994-09-14 13:31:04 +0000 | [diff] [blame] | 1835 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1836 | PyCodeObject *co; |
| 1837 | PyObject *v; |
| 1838 | long magic; |
| 1839 | long PyImport_GetMagicNumber(void); |
Guido van Rossum | fdef271 | 1994-09-14 13:31:04 +0000 | [diff] [blame] | 1840 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1841 | magic = PyMarshal_ReadLongFromFile(fp); |
| 1842 | if (magic != PyImport_GetMagicNumber()) { |
| 1843 | PyErr_SetString(PyExc_RuntimeError, |
| 1844 | "Bad magic number in .pyc file"); |
| 1845 | return NULL; |
| 1846 | } |
Antoine Pitrou | 5136ac0 | 2012-01-13 18:52:16 +0100 | [diff] [blame] | 1847 | /* Skip mtime and size */ |
| 1848 | (void) PyMarshal_ReadLongFromFile(fp); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1849 | (void) PyMarshal_ReadLongFromFile(fp); |
| 1850 | v = PyMarshal_ReadLastObjectFromFile(fp); |
| 1851 | fclose(fp); |
| 1852 | if (v == NULL || !PyCode_Check(v)) { |
| 1853 | Py_XDECREF(v); |
| 1854 | PyErr_SetString(PyExc_RuntimeError, |
| 1855 | "Bad code object in .pyc file"); |
| 1856 | return NULL; |
| 1857 | } |
| 1858 | co = (PyCodeObject *)v; |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 1859 | v = PyEval_EvalCode((PyObject*)co, globals, locals); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1860 | if (v && flags) |
| 1861 | flags->cf_flags |= (co->co_flags & PyCF_MASK); |
| 1862 | Py_DECREF(co); |
| 1863 | return v; |
Guido van Rossum | fdef271 | 1994-09-14 13:31:04 +0000 | [diff] [blame] | 1864 | } |
| 1865 | |
Guido van Rossum | 8259805 | 1997-03-05 00:20:32 +0000 | [diff] [blame] | 1866 | PyObject * |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 1867 | Py_CompileStringExFlags(const char *str, const char *filename, int start, |
| 1868 | PyCompilerFlags *flags, int optimize) |
Jeremy Hylton | bc32024 | 2001-03-22 02:47:58 +0000 | [diff] [blame] | 1869 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1870 | PyCodeObject *co; |
| 1871 | mod_ty mod; |
| 1872 | PyArena *arena = PyArena_New(); |
| 1873 | if (arena == NULL) |
| 1874 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1875 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1876 | mod = PyParser_ASTFromString(str, filename, start, flags, arena); |
| 1877 | if (mod == NULL) { |
| 1878 | PyArena_Free(arena); |
| 1879 | return NULL; |
| 1880 | } |
| 1881 | if (flags && (flags->cf_flags & PyCF_ONLY_AST)) { |
| 1882 | PyObject *result = PyAST_mod2obj(mod); |
| 1883 | PyArena_Free(arena); |
| 1884 | return result; |
| 1885 | } |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 1886 | co = PyAST_CompileEx(mod, filename, flags, optimize, arena); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1887 | PyArena_Free(arena); |
| 1888 | return (PyObject *)co; |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 1889 | } |
| 1890 | |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 1891 | /* For use in Py_LIMITED_API */ |
| 1892 | #undef Py_CompileString |
| 1893 | PyObject * |
| 1894 | PyCompileString(const char *str, const char *filename, int start) |
| 1895 | { |
| 1896 | return Py_CompileStringFlags(str, filename, start, NULL); |
| 1897 | } |
| 1898 | |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 1899 | struct symtable * |
Martin v. Löwis | 95292d6 | 2002-12-11 14:04:59 +0000 | [diff] [blame] | 1900 | Py_SymtableString(const char *str, const char *filename, int start) |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 1901 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1902 | struct symtable *st; |
| 1903 | mod_ty mod; |
| 1904 | PyCompilerFlags flags; |
| 1905 | PyArena *arena = PyArena_New(); |
| 1906 | if (arena == NULL) |
| 1907 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1908 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1909 | flags.cf_flags = 0; |
| 1910 | mod = PyParser_ASTFromString(str, filename, start, &flags, arena); |
| 1911 | if (mod == NULL) { |
| 1912 | PyArena_Free(arena); |
| 1913 | return NULL; |
| 1914 | } |
| 1915 | st = PySymtable_Build(mod, filename, 0); |
| 1916 | PyArena_Free(arena); |
| 1917 | return st; |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 1918 | } |
| 1919 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1920 | /* Preferred access to parser is through AST. */ |
| 1921 | mod_ty |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1922 | PyParser_ASTFromString(const char *s, const char *filename, int start, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1923 | PyCompilerFlags *flags, PyArena *arena) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1924 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1925 | mod_ty mod; |
| 1926 | PyCompilerFlags localflags; |
| 1927 | perrdetail err; |
| 1928 | int iflags = PARSER_FLAGS(flags); |
Christian Heimes | 4d6ec85 | 2008-03-26 22:34:47 +0000 | [diff] [blame] | 1929 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1930 | node *n = PyParser_ParseStringFlagsFilenameEx(s, filename, |
| 1931 | &_PyParser_Grammar, start, &err, |
| 1932 | &iflags); |
| 1933 | if (flags == NULL) { |
| 1934 | localflags.cf_flags = 0; |
| 1935 | flags = &localflags; |
| 1936 | } |
| 1937 | if (n) { |
| 1938 | flags->cf_flags |= iflags & PyCF_MASK; |
| 1939 | mod = PyAST_FromNode(n, flags, filename, arena); |
| 1940 | PyNode_Free(n); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1941 | } |
| 1942 | else { |
| 1943 | err_input(&err); |
Victor Stinner | 7f2fee3 | 2011-04-05 00:39:01 +0200 | [diff] [blame] | 1944 | mod = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1945 | } |
Victor Stinner | 7f2fee3 | 2011-04-05 00:39:01 +0200 | [diff] [blame] | 1946 | err_free(&err); |
| 1947 | return mod; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1948 | } |
| 1949 | |
| 1950 | mod_ty |
Martin v. Löwis | 85bcc66 | 2007-09-04 09:18:06 +0000 | [diff] [blame] | 1951 | PyParser_ASTFromFile(FILE *fp, const char *filename, const char* enc, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1952 | int start, char *ps1, |
| 1953 | char *ps2, PyCompilerFlags *flags, int *errcode, |
| 1954 | PyArena *arena) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1955 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1956 | mod_ty mod; |
| 1957 | PyCompilerFlags localflags; |
| 1958 | perrdetail err; |
| 1959 | int iflags = PARSER_FLAGS(flags); |
Christian Heimes | 4d6ec85 | 2008-03-26 22:34:47 +0000 | [diff] [blame] | 1960 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1961 | node *n = PyParser_ParseFileFlagsEx(fp, filename, enc, |
| 1962 | &_PyParser_Grammar, |
| 1963 | start, ps1, ps2, &err, &iflags); |
| 1964 | if (flags == NULL) { |
| 1965 | localflags.cf_flags = 0; |
| 1966 | flags = &localflags; |
| 1967 | } |
| 1968 | if (n) { |
| 1969 | flags->cf_flags |= iflags & PyCF_MASK; |
| 1970 | mod = PyAST_FromNode(n, flags, filename, arena); |
| 1971 | PyNode_Free(n); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1972 | } |
| 1973 | else { |
| 1974 | err_input(&err); |
| 1975 | if (errcode) |
| 1976 | *errcode = err.error; |
Victor Stinner | 7f2fee3 | 2011-04-05 00:39:01 +0200 | [diff] [blame] | 1977 | mod = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1978 | } |
Victor Stinner | 7f2fee3 | 2011-04-05 00:39:01 +0200 | [diff] [blame] | 1979 | err_free(&err); |
| 1980 | return mod; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1981 | } |
| 1982 | |
Guido van Rossum | a110aa6 | 1994-08-29 12:50:44 +0000 | [diff] [blame] | 1983 | /* Simplified interface to parsefile -- return node or set exception */ |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 1984 | |
Guido van Rossum | a110aa6 | 1994-08-29 12:50:44 +0000 | [diff] [blame] | 1985 | node * |
Martin v. Löwis | 95292d6 | 2002-12-11 14:04:59 +0000 | [diff] [blame] | 1986 | PyParser_SimpleParseFileFlags(FILE *fp, const char *filename, int start, int flags) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 1987 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1988 | perrdetail err; |
| 1989 | node *n = PyParser_ParseFileFlags(fp, filename, NULL, |
| 1990 | &_PyParser_Grammar, |
| 1991 | start, NULL, NULL, &err, flags); |
| 1992 | if (n == NULL) |
| 1993 | err_input(&err); |
Victor Stinner | 7f2fee3 | 2011-04-05 00:39:01 +0200 | [diff] [blame] | 1994 | err_free(&err); |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1995 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1996 | return n; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 1997 | } |
| 1998 | |
Guido van Rossum | a110aa6 | 1994-08-29 12:50:44 +0000 | [diff] [blame] | 1999 | /* Simplified interface to parsestring -- return node or set exception */ |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 2000 | |
Guido van Rossum | a110aa6 | 1994-08-29 12:50:44 +0000 | [diff] [blame] | 2001 | node * |
Martin v. Löwis | 95292d6 | 2002-12-11 14:04:59 +0000 | [diff] [blame] | 2002 | PyParser_SimpleParseStringFlags(const char *str, int start, int flags) |
Tim Peters | fe2127d | 2001-07-16 05:37:24 +0000 | [diff] [blame] | 2003 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2004 | perrdetail err; |
| 2005 | node *n = PyParser_ParseStringFlags(str, &_PyParser_Grammar, |
| 2006 | start, &err, flags); |
| 2007 | if (n == NULL) |
| 2008 | err_input(&err); |
Victor Stinner | 7f2fee3 | 2011-04-05 00:39:01 +0200 | [diff] [blame] | 2009 | err_free(&err); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2010 | return n; |
Tim Peters | fe2127d | 2001-07-16 05:37:24 +0000 | [diff] [blame] | 2011 | } |
| 2012 | |
| 2013 | node * |
Martin v. Löwis | 95292d6 | 2002-12-11 14:04:59 +0000 | [diff] [blame] | 2014 | PyParser_SimpleParseStringFlagsFilename(const char *str, const char *filename, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2015 | int start, int flags) |
Thomas Heller | 6b17abf | 2002-07-09 09:23:27 +0000 | [diff] [blame] | 2016 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2017 | perrdetail err; |
| 2018 | node *n = PyParser_ParseStringFlagsFilename(str, filename, |
| 2019 | &_PyParser_Grammar, start, &err, flags); |
| 2020 | if (n == NULL) |
| 2021 | err_input(&err); |
Victor Stinner | 7f2fee3 | 2011-04-05 00:39:01 +0200 | [diff] [blame] | 2022 | err_free(&err); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2023 | return n; |
Thomas Heller | 6b17abf | 2002-07-09 09:23:27 +0000 | [diff] [blame] | 2024 | } |
| 2025 | |
| 2026 | node * |
Martin v. Löwis | 95292d6 | 2002-12-11 14:04:59 +0000 | [diff] [blame] | 2027 | PyParser_SimpleParseStringFilename(const char *str, const char *filename, int start) |
Thomas Heller | 6b17abf | 2002-07-09 09:23:27 +0000 | [diff] [blame] | 2028 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2029 | return PyParser_SimpleParseStringFlagsFilename(str, filename, start, 0); |
Thomas Heller | 6b17abf | 2002-07-09 09:23:27 +0000 | [diff] [blame] | 2030 | } |
| 2031 | |
Guido van Rossum | 66ebd91 | 2003-04-17 16:02:26 +0000 | [diff] [blame] | 2032 | /* May want to move a more generalized form of this to parsetok.c or |
| 2033 | even parser modules. */ |
| 2034 | |
| 2035 | void |
Victor Stinner | 7f2fee3 | 2011-04-05 00:39:01 +0200 | [diff] [blame] | 2036 | PyParser_ClearError(perrdetail *err) |
| 2037 | { |
| 2038 | err_free(err); |
| 2039 | } |
| 2040 | |
| 2041 | void |
Guido van Rossum | 66ebd91 | 2003-04-17 16:02:26 +0000 | [diff] [blame] | 2042 | PyParser_SetError(perrdetail *err) |
| 2043 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2044 | err_input(err); |
Guido van Rossum | 66ebd91 | 2003-04-17 16:02:26 +0000 | [diff] [blame] | 2045 | } |
| 2046 | |
Victor Stinner | 7f2fee3 | 2011-04-05 00:39:01 +0200 | [diff] [blame] | 2047 | static void |
| 2048 | err_free(perrdetail *err) |
| 2049 | { |
| 2050 | Py_CLEAR(err->filename); |
| 2051 | } |
| 2052 | |
Guido van Rossum | a110aa6 | 1994-08-29 12:50:44 +0000 | [diff] [blame] | 2053 | /* Set the error appropriate to the given input error code (see errcode.h) */ |
| 2054 | |
| 2055 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2056 | err_input(perrdetail *err) |
Guido van Rossum | a110aa6 | 1994-08-29 12:50:44 +0000 | [diff] [blame] | 2057 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2058 | PyObject *v, *w, *errtype, *errtext; |
| 2059 | PyObject *msg_obj = NULL; |
| 2060 | char *msg = NULL; |
Victor Stinner | 4c7c8c3 | 2010-10-16 13:14:10 +0000 | [diff] [blame] | 2061 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2062 | errtype = PyExc_SyntaxError; |
| 2063 | switch (err->error) { |
| 2064 | case E_ERROR: |
| 2065 | return; |
| 2066 | case E_SYNTAX: |
| 2067 | errtype = PyExc_IndentationError; |
| 2068 | if (err->expected == INDENT) |
| 2069 | msg = "expected an indented block"; |
| 2070 | else if (err->token == INDENT) |
| 2071 | msg = "unexpected indent"; |
| 2072 | else if (err->token == DEDENT) |
| 2073 | msg = "unexpected unindent"; |
| 2074 | else { |
| 2075 | errtype = PyExc_SyntaxError; |
| 2076 | msg = "invalid syntax"; |
| 2077 | } |
| 2078 | break; |
| 2079 | case E_TOKEN: |
| 2080 | msg = "invalid token"; |
| 2081 | break; |
| 2082 | case E_EOFS: |
| 2083 | msg = "EOF while scanning triple-quoted string literal"; |
| 2084 | break; |
| 2085 | case E_EOLS: |
| 2086 | msg = "EOL while scanning string literal"; |
| 2087 | break; |
| 2088 | case E_INTR: |
| 2089 | if (!PyErr_Occurred()) |
| 2090 | PyErr_SetNone(PyExc_KeyboardInterrupt); |
| 2091 | goto cleanup; |
| 2092 | case E_NOMEM: |
| 2093 | PyErr_NoMemory(); |
| 2094 | goto cleanup; |
| 2095 | case E_EOF: |
| 2096 | msg = "unexpected EOF while parsing"; |
| 2097 | break; |
| 2098 | case E_TABSPACE: |
| 2099 | errtype = PyExc_TabError; |
| 2100 | msg = "inconsistent use of tabs and spaces in indentation"; |
| 2101 | break; |
| 2102 | case E_OVERFLOW: |
| 2103 | msg = "expression too long"; |
| 2104 | break; |
| 2105 | case E_DEDENT: |
| 2106 | errtype = PyExc_IndentationError; |
| 2107 | msg = "unindent does not match any outer indentation level"; |
| 2108 | break; |
| 2109 | case E_TOODEEP: |
| 2110 | errtype = PyExc_IndentationError; |
| 2111 | msg = "too many levels of indentation"; |
| 2112 | break; |
| 2113 | case E_DECODE: { |
| 2114 | PyObject *type, *value, *tb; |
| 2115 | PyErr_Fetch(&type, &value, &tb); |
| 2116 | msg = "unknown decode error"; |
| 2117 | if (value != NULL) |
| 2118 | msg_obj = PyObject_Str(value); |
| 2119 | Py_XDECREF(type); |
| 2120 | Py_XDECREF(value); |
| 2121 | Py_XDECREF(tb); |
| 2122 | break; |
| 2123 | } |
| 2124 | case E_LINECONT: |
| 2125 | msg = "unexpected character after line continuation character"; |
| 2126 | break; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 2127 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2128 | case E_IDENTIFIER: |
| 2129 | msg = "invalid character in identifier"; |
| 2130 | break; |
| 2131 | default: |
| 2132 | fprintf(stderr, "error=%d\n", err->error); |
| 2133 | msg = "unknown parsing error"; |
| 2134 | break; |
| 2135 | } |
| 2136 | /* err->text may not be UTF-8 in case of decoding errors. |
| 2137 | Explicitly convert to an object. */ |
| 2138 | if (!err->text) { |
| 2139 | errtext = Py_None; |
| 2140 | Py_INCREF(Py_None); |
| 2141 | } else { |
| 2142 | errtext = PyUnicode_DecodeUTF8(err->text, strlen(err->text), |
| 2143 | "replace"); |
| 2144 | } |
Victor Stinner | 7f2fee3 | 2011-04-05 00:39:01 +0200 | [diff] [blame] | 2145 | v = Py_BuildValue("(OiiN)", err->filename, |
| 2146 | err->lineno, err->offset, errtext); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2147 | if (v != NULL) { |
| 2148 | if (msg_obj) |
| 2149 | w = Py_BuildValue("(OO)", msg_obj, v); |
| 2150 | else |
| 2151 | w = Py_BuildValue("(sO)", msg, v); |
| 2152 | } else |
| 2153 | w = NULL; |
| 2154 | Py_XDECREF(v); |
| 2155 | PyErr_SetObject(errtype, w); |
| 2156 | Py_XDECREF(w); |
Georg Brandl | 3dbca81 | 2008-07-23 16:10:53 +0000 | [diff] [blame] | 2157 | cleanup: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2158 | Py_XDECREF(msg_obj); |
| 2159 | if (err->text != NULL) { |
| 2160 | PyObject_FREE(err->text); |
| 2161 | err->text = NULL; |
| 2162 | } |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 2163 | } |
| 2164 | |
| 2165 | /* Print fatal error message and abort */ |
| 2166 | |
| 2167 | void |
Tim Peters | 7c321a8 | 2002-07-09 02:57:01 +0000 | [diff] [blame] | 2168 | Py_FatalError(const char *msg) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 2169 | { |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 2170 | const int fd = fileno(stderr); |
| 2171 | PyThreadState *tstate; |
| 2172 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2173 | fprintf(stderr, "Fatal Python error: %s\n", msg); |
| 2174 | fflush(stderr); /* it helps in Windows debug build */ |
| 2175 | if (PyErr_Occurred()) { |
Victor Stinner | 55a5c78 | 2010-06-08 21:00:13 +0000 | [diff] [blame] | 2176 | PyErr_PrintEx(0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2177 | } |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 2178 | else { |
| 2179 | tstate = _Py_atomic_load_relaxed(&_PyThreadState_Current); |
| 2180 | if (tstate != NULL) { |
| 2181 | fputc('\n', stderr); |
| 2182 | fflush(stderr); |
Victor Stinner | 7bba62f | 2011-05-07 12:43:00 +0200 | [diff] [blame] | 2183 | _Py_DumpTracebackThreads(fd, tstate->interp, tstate); |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 2184 | } |
Victor Stinner | d727e23 | 2011-04-01 12:13:55 +0200 | [diff] [blame] | 2185 | _PyFaulthandler_Fini(); |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 2186 | } |
| 2187 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 2188 | #ifdef MS_WINDOWS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2189 | { |
| 2190 | size_t len = strlen(msg); |
| 2191 | WCHAR* buffer; |
| 2192 | size_t i; |
Martin v. Löwis | 5c88d81 | 2009-01-02 20:47:48 +0000 | [diff] [blame] | 2193 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2194 | /* Convert the message to wchar_t. This uses a simple one-to-one |
| 2195 | conversion, assuming that the this error message actually uses ASCII |
| 2196 | only. If this ceases to be true, we will have to convert. */ |
| 2197 | buffer = alloca( (len+1) * (sizeof *buffer)); |
| 2198 | for( i=0; i<=len; ++i) |
| 2199 | buffer[i] = msg[i]; |
| 2200 | OutputDebugStringW(L"Fatal Python error: "); |
| 2201 | OutputDebugStringW(buffer); |
| 2202 | OutputDebugStringW(L"\n"); |
| 2203 | } |
Guido van Rossum | 0ba3536 | 1998-08-13 13:33:16 +0000 | [diff] [blame] | 2204 | #ifdef _DEBUG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2205 | DebugBreak(); |
Guido van Rossum | a44823b | 1995-03-14 15:01:17 +0000 | [diff] [blame] | 2206 | #endif |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 2207 | #endif /* MS_WINDOWS */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2208 | abort(); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 2209 | } |
| 2210 | |
| 2211 | /* Clean up and exit */ |
| 2212 | |
Guido van Rossum | a110aa6 | 1994-08-29 12:50:44 +0000 | [diff] [blame] | 2213 | #ifdef WITH_THREAD |
Guido van Rossum | 49b5606 | 1998-10-01 20:42:43 +0000 | [diff] [blame] | 2214 | #include "pythread.h" |
Guido van Rossum | f9f2e82 | 1992-08-17 08:59:08 +0000 | [diff] [blame] | 2215 | #endif |
| 2216 | |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 2217 | static void (*pyexitfunc)(void) = NULL; |
| 2218 | /* For the atexit module. */ |
| 2219 | void _Py_PyAtExit(void (*func)(void)) |
| 2220 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2221 | pyexitfunc = func; |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 2222 | } |
| 2223 | |
| 2224 | static void |
| 2225 | call_py_exitfuncs(void) |
| 2226 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2227 | if (pyexitfunc == NULL) |
| 2228 | return; |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 2229 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2230 | (*pyexitfunc)(); |
| 2231 | PyErr_Clear(); |
Collin Winter | 670e692 | 2007-03-21 02:57:17 +0000 | [diff] [blame] | 2232 | } |
| 2233 | |
Antoine Pitrou | 011bd62 | 2009-10-20 21:52:47 +0000 | [diff] [blame] | 2234 | /* Wait until threading._shutdown completes, provided |
| 2235 | the threading module was imported in the first place. |
| 2236 | The shutdown routine will wait until all non-daemon |
| 2237 | "threading" threads have completed. */ |
| 2238 | static void |
| 2239 | wait_for_thread_shutdown(void) |
| 2240 | { |
| 2241 | #ifdef WITH_THREAD |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 2242 | _Py_IDENTIFIER(_shutdown); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2243 | PyObject *result; |
| 2244 | PyThreadState *tstate = PyThreadState_GET(); |
| 2245 | PyObject *threading = PyMapping_GetItemString(tstate->interp->modules, |
| 2246 | "threading"); |
| 2247 | if (threading == NULL) { |
| 2248 | /* threading not imported */ |
| 2249 | PyErr_Clear(); |
| 2250 | return; |
| 2251 | } |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 2252 | result = _PyObject_CallMethodId(threading, &PyId__shutdown, ""); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2253 | if (result == NULL) { |
| 2254 | PyErr_WriteUnraisable(threading); |
| 2255 | } |
| 2256 | else { |
| 2257 | Py_DECREF(result); |
| 2258 | } |
| 2259 | Py_DECREF(threading); |
Antoine Pitrou | 011bd62 | 2009-10-20 21:52:47 +0000 | [diff] [blame] | 2260 | #endif |
| 2261 | } |
| 2262 | |
Guido van Rossum | 2dcfc96 | 1998-10-01 16:01:57 +0000 | [diff] [blame] | 2263 | #define NEXITFUNCS 32 |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2264 | static void (*exitfuncs[NEXITFUNCS])(void); |
Guido van Rossum | 1662dd5 | 1994-09-07 14:38:28 +0000 | [diff] [blame] | 2265 | static int nexitfuncs = 0; |
| 2266 | |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2267 | int Py_AtExit(void (*func)(void)) |
Guido van Rossum | 1662dd5 | 1994-09-07 14:38:28 +0000 | [diff] [blame] | 2268 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2269 | if (nexitfuncs >= NEXITFUNCS) |
| 2270 | return -1; |
| 2271 | exitfuncs[nexitfuncs++] = func; |
| 2272 | return 0; |
Guido van Rossum | 1662dd5 | 1994-09-07 14:38:28 +0000 | [diff] [blame] | 2273 | } |
| 2274 | |
Guido van Rossum | cc283f5 | 1997-08-05 02:22:03 +0000 | [diff] [blame] | 2275 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2276 | call_ll_exitfuncs(void) |
Guido van Rossum | cc283f5 | 1997-08-05 02:22:03 +0000 | [diff] [blame] | 2277 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2278 | while (nexitfuncs > 0) |
| 2279 | (*exitfuncs[--nexitfuncs])(); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 2280 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2281 | fflush(stdout); |
| 2282 | fflush(stderr); |
Guido van Rossum | a9e7dc1 | 1992-10-18 18:53:57 +0000 | [diff] [blame] | 2283 | } |
| 2284 | |
| 2285 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2286 | Py_Exit(int sts) |
Guido van Rossum | a9e7dc1 | 1992-10-18 18:53:57 +0000 | [diff] [blame] | 2287 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2288 | Py_Finalize(); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 2289 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2290 | exit(sts); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 2291 | } |
| 2292 | |
Guido van Rossum | f1dc566 | 1993-07-05 10:31:29 +0000 | [diff] [blame] | 2293 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2294 | initsigs(void) |
Guido van Rossum | a9e7dc1 | 1992-10-18 18:53:57 +0000 | [diff] [blame] | 2295 | { |
Guido van Rossum | a110aa6 | 1994-08-29 12:50:44 +0000 | [diff] [blame] | 2296 | #ifdef SIGPIPE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2297 | PyOS_setsig(SIGPIPE, SIG_IGN); |
Guido van Rossum | a9e7dc1 | 1992-10-18 18:53:57 +0000 | [diff] [blame] | 2298 | #endif |
Guido van Rossum | 70d893a | 2001-08-16 08:21:42 +0000 | [diff] [blame] | 2299 | #ifdef SIGXFZ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2300 | PyOS_setsig(SIGXFZ, SIG_IGN); |
Guido van Rossum | 70d893a | 2001-08-16 08:21:42 +0000 | [diff] [blame] | 2301 | #endif |
Jeremy Hylton | 1b0bf9b | 2002-04-23 20:31:01 +0000 | [diff] [blame] | 2302 | #ifdef SIGXFSZ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2303 | PyOS_setsig(SIGXFSZ, SIG_IGN); |
Jeremy Hylton | 1b0bf9b | 2002-04-23 20:31:01 +0000 | [diff] [blame] | 2304 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2305 | PyOS_InitInterrupts(); /* May imply initsignal() */ |
Guido van Rossum | a9e7dc1 | 1992-10-18 18:53:57 +0000 | [diff] [blame] | 2306 | } |
| 2307 | |
Guido van Rossum | 7433b12 | 1997-02-14 19:45:36 +0000 | [diff] [blame] | 2308 | |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 2309 | /* Restore signals that the interpreter has called SIG_IGN on to SIG_DFL. |
| 2310 | * |
| 2311 | * All of the code in this function must only use async-signal-safe functions, |
| 2312 | * listed at `man 7 signal` or |
| 2313 | * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html. |
| 2314 | */ |
| 2315 | void |
| 2316 | _Py_RestoreSignals(void) |
| 2317 | { |
| 2318 | #ifdef SIGPIPE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2319 | PyOS_setsig(SIGPIPE, SIG_DFL); |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 2320 | #endif |
| 2321 | #ifdef SIGXFZ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2322 | PyOS_setsig(SIGXFZ, SIG_DFL); |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 2323 | #endif |
| 2324 | #ifdef SIGXFSZ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2325 | PyOS_setsig(SIGXFSZ, SIG_DFL); |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 2326 | #endif |
| 2327 | } |
| 2328 | |
| 2329 | |
Guido van Rossum | 7433b12 | 1997-02-14 19:45:36 +0000 | [diff] [blame] | 2330 | /* |
| 2331 | * The file descriptor fd is considered ``interactive'' if either |
| 2332 | * a) isatty(fd) is TRUE, or |
| 2333 | * b) the -i flag was given, and the filename associated with |
| 2334 | * the descriptor is NULL or "<stdin>" or "???". |
| 2335 | */ |
| 2336 | int |
Martin v. Löwis | 95292d6 | 2002-12-11 14:04:59 +0000 | [diff] [blame] | 2337 | Py_FdIsInteractive(FILE *fp, const char *filename) |
Guido van Rossum | 7433b12 | 1997-02-14 19:45:36 +0000 | [diff] [blame] | 2338 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2339 | if (isatty((int)fileno(fp))) |
| 2340 | return 1; |
| 2341 | if (!Py_InteractiveFlag) |
| 2342 | return 0; |
| 2343 | return (filename == NULL) || |
| 2344 | (strcmp(filename, "<stdin>") == 0) || |
| 2345 | (strcmp(filename, "???") == 0); |
Guido van Rossum | 7433b12 | 1997-02-14 19:45:36 +0000 | [diff] [blame] | 2346 | } |
Fredrik Lundh | 2f15b25 | 2000-08-27 19:15:31 +0000 | [diff] [blame] | 2347 | |
| 2348 | |
Tim Peters | d08e382 | 2003-04-17 15:24:21 +0000 | [diff] [blame] | 2349 | #if defined(USE_STACKCHECK) |
Fredrik Lundh | 2f15b25 | 2000-08-27 19:15:31 +0000 | [diff] [blame] | 2350 | #if defined(WIN32) && defined(_MSC_VER) |
| 2351 | |
| 2352 | /* Stack checking for Microsoft C */ |
| 2353 | |
| 2354 | #include <malloc.h> |
| 2355 | #include <excpt.h> |
| 2356 | |
Fred Drake | e8de31c | 2000-08-31 05:38:39 +0000 | [diff] [blame] | 2357 | /* |
| 2358 | * Return non-zero when we run out of memory on the stack; zero otherwise. |
| 2359 | */ |
Fredrik Lundh | 2f15b25 | 2000-08-27 19:15:31 +0000 | [diff] [blame] | 2360 | int |
Fred Drake | 399739f | 2000-08-31 05:52:44 +0000 | [diff] [blame] | 2361 | PyOS_CheckStack(void) |
Fredrik Lundh | 2f15b25 | 2000-08-27 19:15:31 +0000 | [diff] [blame] | 2362 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2363 | __try { |
| 2364 | /* alloca throws a stack overflow exception if there's |
| 2365 | not enough space left on the stack */ |
| 2366 | alloca(PYOS_STACK_MARGIN * sizeof(void*)); |
| 2367 | return 0; |
| 2368 | } __except (GetExceptionCode() == STATUS_STACK_OVERFLOW ? |
| 2369 | EXCEPTION_EXECUTE_HANDLER : |
| 2370 | EXCEPTION_CONTINUE_SEARCH) { |
| 2371 | int errcode = _resetstkoflw(); |
| 2372 | if (errcode == 0) |
| 2373 | { |
| 2374 | Py_FatalError("Could not reset the stack!"); |
| 2375 | } |
| 2376 | } |
| 2377 | return 1; |
Fredrik Lundh | 2f15b25 | 2000-08-27 19:15:31 +0000 | [diff] [blame] | 2378 | } |
| 2379 | |
| 2380 | #endif /* WIN32 && _MSC_VER */ |
| 2381 | |
| 2382 | /* Alternate implementations can be added here... */ |
| 2383 | |
| 2384 | #endif /* USE_STACKCHECK */ |
Guido van Rossum | 6f25618 | 2000-09-16 16:32:19 +0000 | [diff] [blame] | 2385 | |
| 2386 | |
| 2387 | /* Wrappers around sigaction() or signal(). */ |
| 2388 | |
| 2389 | PyOS_sighandler_t |
| 2390 | PyOS_getsig(int sig) |
| 2391 | { |
| 2392 | #ifdef HAVE_SIGACTION |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2393 | struct sigaction context; |
| 2394 | if (sigaction(sig, NULL, &context) == -1) |
| 2395 | return SIG_ERR; |
| 2396 | return context.sa_handler; |
Guido van Rossum | 6f25618 | 2000-09-16 16:32:19 +0000 | [diff] [blame] | 2397 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2398 | PyOS_sighandler_t handler; |
Martin v. Löwis | b45b315 | 2005-11-28 17:34:23 +0000 | [diff] [blame] | 2399 | /* Special signal handling for the secure CRT in Visual Studio 2005 */ |
| 2400 | #if defined(_MSC_VER) && _MSC_VER >= 1400 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2401 | switch (sig) { |
| 2402 | /* Only these signals are valid */ |
| 2403 | case SIGINT: |
| 2404 | case SIGILL: |
| 2405 | case SIGFPE: |
| 2406 | case SIGSEGV: |
| 2407 | case SIGTERM: |
| 2408 | case SIGBREAK: |
| 2409 | case SIGABRT: |
| 2410 | break; |
| 2411 | /* Don't call signal() with other values or it will assert */ |
| 2412 | default: |
| 2413 | return SIG_ERR; |
| 2414 | } |
Martin v. Löwis | b45b315 | 2005-11-28 17:34:23 +0000 | [diff] [blame] | 2415 | #endif /* _MSC_VER && _MSC_VER >= 1400 */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2416 | handler = signal(sig, SIG_IGN); |
| 2417 | if (handler != SIG_ERR) |
| 2418 | signal(sig, handler); |
| 2419 | return handler; |
Guido van Rossum | 6f25618 | 2000-09-16 16:32:19 +0000 | [diff] [blame] | 2420 | #endif |
| 2421 | } |
| 2422 | |
Gregory P. Smith | fb94c5f | 2010-03-14 06:49:55 +0000 | [diff] [blame] | 2423 | /* |
| 2424 | * All of the code in this function must only use async-signal-safe functions, |
| 2425 | * listed at `man 7 signal` or |
| 2426 | * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html. |
| 2427 | */ |
Guido van Rossum | 6f25618 | 2000-09-16 16:32:19 +0000 | [diff] [blame] | 2428 | PyOS_sighandler_t |
| 2429 | PyOS_setsig(int sig, PyOS_sighandler_t handler) |
| 2430 | { |
| 2431 | #ifdef HAVE_SIGACTION |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2432 | /* Some code in Modules/signalmodule.c depends on sigaction() being |
| 2433 | * used here if HAVE_SIGACTION is defined. Fix that if this code |
| 2434 | * changes to invalidate that assumption. |
| 2435 | */ |
| 2436 | struct sigaction context, ocontext; |
| 2437 | context.sa_handler = handler; |
| 2438 | sigemptyset(&context.sa_mask); |
| 2439 | context.sa_flags = 0; |
| 2440 | if (sigaction(sig, &context, &ocontext) == -1) |
| 2441 | return SIG_ERR; |
| 2442 | return ocontext.sa_handler; |
Guido van Rossum | 6f25618 | 2000-09-16 16:32:19 +0000 | [diff] [blame] | 2443 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2444 | PyOS_sighandler_t oldhandler; |
| 2445 | oldhandler = signal(sig, handler); |
Anthony Baxter | 9ceaa72 | 2004-10-13 14:48:50 +0000 | [diff] [blame] | 2446 | #ifdef HAVE_SIGINTERRUPT |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2447 | siginterrupt(sig, 1); |
Anthony Baxter | 9ceaa72 | 2004-10-13 14:48:50 +0000 | [diff] [blame] | 2448 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2449 | return oldhandler; |
Guido van Rossum | 6f25618 | 2000-09-16 16:32:19 +0000 | [diff] [blame] | 2450 | #endif |
| 2451 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2452 | |
| 2453 | /* Deprecated C API functions still provided for binary compatiblity */ |
| 2454 | |
| 2455 | #undef PyParser_SimpleParseFile |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2456 | PyAPI_FUNC(node *) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2457 | PyParser_SimpleParseFile(FILE *fp, const char *filename, int start) |
| 2458 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2459 | return PyParser_SimpleParseFileFlags(fp, filename, start, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2460 | } |
| 2461 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2462 | #undef PyParser_SimpleParseString |
| 2463 | PyAPI_FUNC(node *) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2464 | PyParser_SimpleParseString(const char *str, int start) |
| 2465 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2466 | return PyParser_SimpleParseStringFlags(str, start, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2467 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2468 | |
| 2469 | #undef PyRun_AnyFile |
| 2470 | PyAPI_FUNC(int) |
| 2471 | PyRun_AnyFile(FILE *fp, const char *name) |
| 2472 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2473 | return PyRun_AnyFileExFlags(fp, name, 0, NULL); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2474 | } |
| 2475 | |
| 2476 | #undef PyRun_AnyFileEx |
| 2477 | PyAPI_FUNC(int) |
| 2478 | PyRun_AnyFileEx(FILE *fp, const char *name, int closeit) |
| 2479 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2480 | return PyRun_AnyFileExFlags(fp, name, closeit, NULL); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2481 | } |
| 2482 | |
| 2483 | #undef PyRun_AnyFileFlags |
| 2484 | PyAPI_FUNC(int) |
| 2485 | PyRun_AnyFileFlags(FILE *fp, const char *name, PyCompilerFlags *flags) |
| 2486 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2487 | return PyRun_AnyFileExFlags(fp, name, 0, flags); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2488 | } |
| 2489 | |
| 2490 | #undef PyRun_File |
| 2491 | PyAPI_FUNC(PyObject *) |
| 2492 | PyRun_File(FILE *fp, const char *p, int s, PyObject *g, PyObject *l) |
| 2493 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2494 | return PyRun_FileExFlags(fp, p, s, g, l, 0, NULL); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2495 | } |
| 2496 | |
| 2497 | #undef PyRun_FileEx |
| 2498 | PyAPI_FUNC(PyObject *) |
| 2499 | PyRun_FileEx(FILE *fp, const char *p, int s, PyObject *g, PyObject *l, int c) |
| 2500 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2501 | return PyRun_FileExFlags(fp, p, s, g, l, c, NULL); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2502 | } |
| 2503 | |
| 2504 | #undef PyRun_FileFlags |
| 2505 | PyAPI_FUNC(PyObject *) |
| 2506 | PyRun_FileFlags(FILE *fp, const char *p, int s, PyObject *g, PyObject *l, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2507 | PyCompilerFlags *flags) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2508 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2509 | return PyRun_FileExFlags(fp, p, s, g, l, 0, flags); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2510 | } |
| 2511 | |
| 2512 | #undef PyRun_SimpleFile |
| 2513 | PyAPI_FUNC(int) |
| 2514 | PyRun_SimpleFile(FILE *f, const char *p) |
| 2515 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2516 | return PyRun_SimpleFileExFlags(f, p, 0, NULL); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2517 | } |
| 2518 | |
| 2519 | #undef PyRun_SimpleFileEx |
| 2520 | PyAPI_FUNC(int) |
| 2521 | PyRun_SimpleFileEx(FILE *f, const char *p, int c) |
| 2522 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2523 | return PyRun_SimpleFileExFlags(f, p, c, NULL); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2524 | } |
| 2525 | |
| 2526 | |
| 2527 | #undef PyRun_String |
| 2528 | PyAPI_FUNC(PyObject *) |
| 2529 | PyRun_String(const char *str, int s, PyObject *g, PyObject *l) |
| 2530 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2531 | return PyRun_StringFlags(str, s, g, l, NULL); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2532 | } |
| 2533 | |
| 2534 | #undef PyRun_SimpleString |
| 2535 | PyAPI_FUNC(int) |
| 2536 | PyRun_SimpleString(const char *s) |
| 2537 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2538 | return PyRun_SimpleStringFlags(s, NULL); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2539 | } |
| 2540 | |
| 2541 | #undef Py_CompileString |
| 2542 | PyAPI_FUNC(PyObject *) |
| 2543 | Py_CompileString(const char *str, const char *p, int s) |
| 2544 | { |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 2545 | return Py_CompileStringExFlags(str, p, s, NULL, -1); |
| 2546 | } |
| 2547 | |
| 2548 | #undef Py_CompileStringFlags |
| 2549 | PyAPI_FUNC(PyObject *) |
| 2550 | Py_CompileStringFlags(const char *str, const char *p, int s, |
| 2551 | PyCompilerFlags *flags) |
| 2552 | { |
| 2553 | return Py_CompileStringExFlags(str, p, s, flags, -1); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2554 | } |
| 2555 | |
| 2556 | #undef PyRun_InteractiveOne |
| 2557 | PyAPI_FUNC(int) |
| 2558 | PyRun_InteractiveOne(FILE *f, const char *p) |
| 2559 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2560 | return PyRun_InteractiveOneFlags(f, p, NULL); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2561 | } |
| 2562 | |
| 2563 | #undef PyRun_InteractiveLoop |
| 2564 | PyAPI_FUNC(int) |
| 2565 | PyRun_InteractiveLoop(FILE *f, const char *p) |
| 2566 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2567 | return PyRun_InteractiveLoopFlags(f, p, NULL); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2568 | } |
| 2569 | |
| 2570 | #ifdef __cplusplus |
| 2571 | } |
| 2572 | #endif |