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