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