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