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