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