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