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