blob: 0497ae6b0f14d1c385bf3c1b085d5179a7e62429 [file] [log] [blame]
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001
2/* Python interpreter top-level routines, including init/exit */
3
Guido van Rossum82598051997-03-05 00:20:32 +00004#include "Python.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +00005
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006#include "Python-ast.h"
Kristján Valur Jónsson67387fb2007-04-25 00:17:39 +00007#undef Yield /* undefine macro conflicting with winbase.h */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00008#include "grammar.h"
9#include "node.h"
Fred Drake85f36392000-07-11 17:53:00 +000010#include "token.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +000011#include "parsetok.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +000012#include "errcode.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000013#include "code.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +000014#include "compile.h"
Jeremy Hylton4b38da62001-02-02 18:19:15 +000015#include "symtable.h"
Neal Norwitzadb69fc2005-12-17 20:54:49 +000016#include "pyarena.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000017#include "ast.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +000018#include "eval.h"
Guido van Rossumfdef2711994-09-14 13:31:04 +000019#include "marshal.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +000020
Martin v. Löwis0e8bd7e2006-06-10 12:23:46 +000021#ifdef HAVE_SIGNAL_H
Guido van Rossuma9e7dc11992-10-18 18:53:57 +000022#include <signal.h>
Martin v. Löwis0e8bd7e2006-06-10 12:23:46 +000023#endif
Guido van Rossuma9e7dc11992-10-18 18:53:57 +000024
Martin v. Löwis5344c992009-01-02 20:32:55 +000025#include "malloc.h" /* for alloca */
26
Martin v. Löwis73d538b2003-03-05 15:13:47 +000027#ifdef HAVE_LANGINFO_H
28#include <locale.h>
29#include <langinfo.h>
30#endif
31
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000032#ifdef MS_WINDOWS
Guido van Rossuma44823b1995-03-14 15:01:17 +000033#undef BYTE
34#include "windows.h"
35#endif
36
Neal Norwitz4281cef2006-03-04 19:58:13 +000037#ifndef Py_REF_DEBUG
Tim Peters62e97f02006-03-28 21:44:32 +000038#define PRINT_TOTAL_REFS()
Neal Norwitz4281cef2006-03-04 19:58:13 +000039#else /* Py_REF_DEBUG */
Tim Peters62e97f02006-03-28 21:44:32 +000040#define PRINT_TOTAL_REFS() fprintf(stderr, \
41 "[%" PY_FORMAT_SIZE_T "d refs]\n", \
Armin Rigoe1709372006-04-12 17:06:05 +000042 _Py_GetRefTotal())
Neal Norwitz4281cef2006-03-04 19:58:13 +000043#endif
44
Anthony Baxterac6bd462006-04-13 02:06:09 +000045#ifdef __cplusplus
46extern "C" {
47#endif
48
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000049extern char *Py_GetPath(void);
Guido van Rossum1984f1e1992-08-04 12:41:02 +000050
Guido van Rossum82598051997-03-05 00:20:32 +000051extern grammar _PyParser_Grammar; /* From graminit.c */
Guido van Rossum1984f1e1992-08-04 12:41:02 +000052
Guido van Rossumb73cc041993-11-01 16:28:59 +000053/* Forward */
Tim Petersdbd9ba62000-07-09 03:09:57 +000054static void initmain(void);
55static void initsite(void);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000056static PyObject *run_mod(mod_ty, const char *, PyObject *, PyObject *,
Neal Norwitzadb69fc2005-12-17 20:54:49 +000057 PyCompilerFlags *, PyArena *);
Martin v. Löwis95292d62002-12-11 14:04:59 +000058static PyObject *run_pyc_file(FILE *, const char *, PyObject *, PyObject *,
Jeremy Hyltonbc320242001-03-22 02:47:58 +000059 PyCompilerFlags *);
Tim Petersdbd9ba62000-07-09 03:09:57 +000060static void err_input(perrdetail *);
61static void initsigs(void);
62static void call_sys_exitfunc(void);
63static void call_ll_exitfuncs(void);
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000064extern void _PyUnicode_Init(void);
65extern void _PyUnicode_Fini(void);
Guido van Rossumc94044c2000-03-10 23:03:54 +000066
Mark Hammond8d98d2c2003-04-19 15:41:53 +000067#ifdef WITH_THREAD
68extern void _PyGILState_Init(PyInterpreterState *, PyThreadState *);
69extern void _PyGILState_Fini(void);
70#endif /* WITH_THREAD */
71
Guido van Rossum82598051997-03-05 00:20:32 +000072int Py_DebugFlag; /* Needed by parser.c */
73int Py_VerboseFlag; /* Needed by import.c */
Guido van Rossum7433b121997-02-14 19:45:36 +000074int Py_InteractiveFlag; /* Needed by Py_FdIsInteractive() below */
Georg Brandl49aafc92007-03-07 00:34:46 +000075int Py_InspectFlag; /* Needed to determine whether to exit at SystemError */
Guido van Rossumdcc0c131997-08-29 22:32:42 +000076int Py_NoSiteFlag; /* Suppress 'import site' */
Christian Heimes1a6387e2008-03-26 12:49:49 +000077int Py_BytesWarningFlag; /* Warn on str(bytes) and str(buffer) */
Georg Brandl2da0fce2008-01-07 17:09:35 +000078int Py_DontWriteBytecodeFlag; /* Suppress writing bytecode files (*.py[co]) */
Barry Warsaw3ce09642000-05-02 19:18:59 +000079int Py_UseClassExceptionsFlag = 1; /* Needed by bltinmodule.c: deprecated */
Guido van Rossuma61691e1998-02-06 22:27:24 +000080int Py_FrozenFlag; /* Needed by getpath.c */
Guido van Rossumb16d1972000-05-01 17:55:15 +000081int Py_UnicodeFlag = 0; /* Needed by compile.c */
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +000082int Py_IgnoreEnvironmentFlag; /* e.g. PYTHONPATH, PYTHONHOME */
Tim Peters3caca232001-12-06 06:23:26 +000083/* _XXX Py_QnewFlag should go away in 2.3. It's true iff -Qnew is passed,
84 on the command line, and is used in 2.2 by ceval.c to make all "/" divisions
85 true divisions (which they will be in 2.3). */
86int _Py_QnewFlag = 0;
Christian Heimesaf748c32008-05-06 22:41:46 +000087int Py_NoUserSiteDirectory = 0; /* for -s and site.py */
Guido van Rossum1984f1e1992-08-04 12:41:02 +000088
Brett Cannone9746892008-04-12 23:44:07 +000089/* PyModule_GetWarningsModule is no longer necessary as of 2.6
90since _warnings is builtin. This API should not be used. */
91PyObject *
92PyModule_GetWarningsModule(void)
Mark Hammondedd07732003-07-15 23:03:55 +000093{
Brett Cannone9746892008-04-12 23:44:07 +000094 return PyImport_ImportModule("warnings");
Mark Hammondedd07732003-07-15 23:03:55 +000095}
Mark Hammonda43fd0c2003-02-19 00:33:33 +000096
Guido van Rossum25ce5661997-08-02 03:10:38 +000097static int initialized = 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +000098
Thomas Wouters7e474022000-07-16 12:04:32 +000099/* API to access the initialized flag -- useful for esoteric use */
Guido van Rossume3c0d5e1997-08-22 04:20:13 +0000100
101int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000102Py_IsInitialized(void)
Guido van Rossume3c0d5e1997-08-22 04:20:13 +0000103{
104 return initialized;
105}
106
Guido van Rossum25ce5661997-08-02 03:10:38 +0000107/* Global initializations. Can be undone by Py_Finalize(). Don't
108 call this twice without an intervening Py_Finalize() call. When
109 initializations fail, a fatal error is issued and the function does
110 not return. On return, the first thread and interpreter state have
111 been created.
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000112
Guido van Rossum25ce5661997-08-02 03:10:38 +0000113 Locking: you must hold the interpreter lock while calling this.
114 (If the lock has not yet been initialized, that's equivalent to
115 having the lock, but you cannot use multiple threads.)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000116
Guido van Rossum25ce5661997-08-02 03:10:38 +0000117*/
Guido van Rossuma027efa1997-05-05 20:56:21 +0000118
Guido van Rossum9abaf4d2001-10-12 22:17:56 +0000119static int
120add_flag(int flag, const char *envs)
121{
122 int env = atoi(envs);
123 if (flag < env)
124 flag = env;
125 if (flag < 1)
126 flag = 1;
127 return flag;
128}
129
Guido van Rossuma027efa1997-05-05 20:56:21 +0000130void
Martin v. Löwis336e85f2004-08-19 11:31:58 +0000131Py_InitializeEx(int install_sigs)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000132{
Guido van Rossuma027efa1997-05-05 20:56:21 +0000133 PyInterpreterState *interp;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000134 PyThreadState *tstate;
135 PyObject *bimod, *sysmod;
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000136 char *p;
Neal Norwitz18aa3882008-08-24 05:04:52 +0000137 char *icodeset = NULL; /* On Windows, input codeset may theoretically
138 differ from output codeset. */
Martin v. Löwis99815892008-06-01 07:20:46 +0000139 char *codeset = NULL;
140 char *errors = NULL;
141 int free_codeset = 0;
142 int overridden = 0;
Martin v. Löwisb12d8572008-06-01 08:06:17 +0000143 PyObject *sys_stream, *sys_isatty;
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000144#if defined(Py_USING_UNICODE) && defined(HAVE_LANGINFO_H) && defined(CODESET)
Martin v. Löwis99815892008-06-01 07:20:46 +0000145 char *saved_locale, *loc_codeset;
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000146#endif
Martin v. Löwis99815892008-06-01 07:20:46 +0000147#ifdef MS_WINDOWS
148 char ibuf[128];
149 char buf[128];
150#endif
Guido van Rossum70d893a2001-08-16 08:21:42 +0000151 extern void _Py_ReadyTypes(void);
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000152
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000153 if (initialized)
Guido van Rossumaa615051997-08-20 22:40:18 +0000154 return;
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000155 initialized = 1;
Tim Petersd08e3822003-04-17 15:24:21 +0000156
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000157 if ((p = Py_GETENV("PYTHONDEBUG")) && *p != '\0')
Guido van Rossum9abaf4d2001-10-12 22:17:56 +0000158 Py_DebugFlag = add_flag(Py_DebugFlag, p);
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000159 if ((p = Py_GETENV("PYTHONVERBOSE")) && *p != '\0')
Guido van Rossum9abaf4d2001-10-12 22:17:56 +0000160 Py_VerboseFlag = add_flag(Py_VerboseFlag, p);
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000161 if ((p = Py_GETENV("PYTHONOPTIMIZE")) && *p != '\0')
Guido van Rossum9abaf4d2001-10-12 22:17:56 +0000162 Py_OptimizeFlag = add_flag(Py_OptimizeFlag, p);
Georg Brandl2da0fce2008-01-07 17:09:35 +0000163 if ((p = Py_GETENV("PYTHONDONTWRITEBYTECODE")) && *p != '\0')
164 Py_DontWriteBytecodeFlag = add_flag(Py_DontWriteBytecodeFlag, p);
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000165
Guido van Rossuma027efa1997-05-05 20:56:21 +0000166 interp = PyInterpreterState_New();
167 if (interp == NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000168 Py_FatalError("Py_Initialize: can't make first interpreter");
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000169
Guido van Rossuma027efa1997-05-05 20:56:21 +0000170 tstate = PyThreadState_New(interp);
171 if (tstate == NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000172 Py_FatalError("Py_Initialize: can't make first thread");
Guido van Rossuma027efa1997-05-05 20:56:21 +0000173 (void) PyThreadState_Swap(tstate);
174
Guido van Rossum70d893a2001-08-16 08:21:42 +0000175 _Py_ReadyTypes();
Guido van Rossum528b7eb2001-08-07 17:24:28 +0000176
Neal Norwitzb2501f42002-12-31 03:42:13 +0000177 if (!_PyFrame_Init())
Neal Norwitzc91ed402002-12-30 22:29:22 +0000178 Py_FatalError("Py_Initialize: can't init frames");
179
Neal Norwitzb2501f42002-12-31 03:42:13 +0000180 if (!_PyInt_Init())
Neal Norwitzc91ed402002-12-30 22:29:22 +0000181 Py_FatalError("Py_Initialize: can't init ints");
182
Christian Heimes3497f942008-05-26 12:29:14 +0000183 if (!PyByteArray_Init())
Christian Heimes1a6387e2008-03-26 12:49:49 +0000184 Py_FatalError("Py_Initialize: can't init bytearray");
185
Michael W. Hudsonba283e22005-05-27 15:23:20 +0000186 _PyFloat_Init();
187
Guido van Rossum25ce5661997-08-02 03:10:38 +0000188 interp->modules = PyDict_New();
189 if (interp->modules == NULL)
190 Py_FatalError("Py_Initialize: can't make modules dictionary");
Collin Winter276887b2007-03-12 16:11:39 +0000191 interp->modules_reloading = PyDict_New();
192 if (interp->modules_reloading == NULL)
193 Py_FatalError("Py_Initialize: can't make modules_reloading dictionary");
Guido van Rossuma027efa1997-05-05 20:56:21 +0000194
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000195#ifdef Py_USING_UNICODE
Guido van Rossumc94044c2000-03-10 23:03:54 +0000196 /* Init Unicode implementation; relies on the codec registry */
197 _PyUnicode_Init();
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000198#endif
Guido van Rossumc94044c2000-03-10 23:03:54 +0000199
Barry Warsawf242aa02000-05-25 23:09:49 +0000200 bimod = _PyBuiltin_Init();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000201 if (bimod == NULL)
202 Py_FatalError("Py_Initialize: can't initialize __builtin__");
Guido van Rossum4a1f39a1997-11-04 19:36:18 +0000203 interp->builtins = PyModule_GetDict(bimod);
Neal Norwitz0f7dbf72006-08-12 03:17:41 +0000204 if (interp->builtins == NULL)
205 Py_FatalError("Py_Initialize: can't initialize builtins dict");
Guido van Rossum4a1f39a1997-11-04 19:36:18 +0000206 Py_INCREF(interp->builtins);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000207
208 sysmod = _PySys_Init();
209 if (sysmod == NULL)
210 Py_FatalError("Py_Initialize: can't initialize sys");
211 interp->sysdict = PyModule_GetDict(sysmod);
Neal Norwitz0f7dbf72006-08-12 03:17:41 +0000212 if (interp->sysdict == NULL)
213 Py_FatalError("Py_Initialize: can't initialize sys dict");
Guido van Rossum25ce5661997-08-02 03:10:38 +0000214 Py_INCREF(interp->sysdict);
215 _PyImport_FixupExtension("sys", "sys");
Guido van Rossuma027efa1997-05-05 20:56:21 +0000216 PySys_SetPath(Py_GetPath());
Guido van Rossum25ce5661997-08-02 03:10:38 +0000217 PyDict_SetItemString(interp->sysdict, "modules",
218 interp->modules);
219
Guido van Rossum7c85ab81999-07-08 17:26:56 +0000220 _PyImport_Init();
221
Barry Warsawf242aa02000-05-25 23:09:49 +0000222 /* initialize builtin exceptions */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000223 _PyExc_Init();
Barry Warsaw5821bc52001-08-13 23:04:56 +0000224 _PyImport_FixupExtension("exceptions", "exceptions");
Barry Warsawf242aa02000-05-25 23:09:49 +0000225
Barry Warsaw035574d1997-08-29 22:07:17 +0000226 /* phase 2 of builtins */
Barry Warsaw963b8711997-09-18 16:42:02 +0000227 _PyImport_FixupExtension("__builtin__", "__builtin__");
Barry Warsaw035574d1997-08-29 22:07:17 +0000228
Just van Rossum52e14d62002-12-30 22:08:05 +0000229 _PyImportHooks_Init();
230
Martin v. Löwis336e85f2004-08-19 11:31:58 +0000231 if (install_sigs)
232 initsigs(); /* Signal handling stuff, including initintr() */
Brett Cannone9746892008-04-12 23:44:07 +0000233
Georg Brandl3c0fd562008-07-05 10:07:18 +0000234 /* Initialize warnings. */
235 _PyWarnings_Init();
236 if (PySys_HasWarnOptions()) {
237 PyObject *warnings_module = PyImport_ImportModule("warnings");
238 if (!warnings_module)
239 PyErr_Clear();
240 Py_XDECREF(warnings_module);
241 }
Guido van Rossum25ce5661997-08-02 03:10:38 +0000242
243 initmain(); /* Module __main__ */
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000244 if (!Py_NoSiteFlag)
245 initsite(); /* Module site */
Just van Rossum5bfba3a2003-02-25 20:25:12 +0000246
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000247 /* auto-thread-state API, if available */
248#ifdef WITH_THREAD
249 _PyGILState_Init(interp, tstate);
250#endif /* WITH_THREAD */
251
Martin v. Löwis99815892008-06-01 07:20:46 +0000252 if ((p = Py_GETENV("PYTHONIOENCODING")) && *p != '\0') {
253 p = icodeset = codeset = strdup(p);
254 free_codeset = 1;
255 errors = strchr(p, ':');
256 if (errors) {
257 *errors = '\0';
258 errors++;
259 }
260 overridden = 1;
261 }
262
Martin v. Löwis73d538b2003-03-05 15:13:47 +0000263#if defined(Py_USING_UNICODE) && defined(HAVE_LANGINFO_H) && defined(CODESET)
264 /* On Unix, set the file system encoding according to the
265 user's preference, if the CODESET names a well-known
266 Python codec, and Py_FileSystemDefaultEncoding isn't
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000267 initialized by other means. Also set the encoding of
Martin v. Löwis99815892008-06-01 07:20:46 +0000268 stdin and stdout if these are terminals, unless overridden. */
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000269
Martin v. Löwis99815892008-06-01 07:20:46 +0000270 if (!overridden || !Py_FileSystemDefaultEncoding) {
271 saved_locale = strdup(setlocale(LC_CTYPE, NULL));
272 setlocale(LC_CTYPE, "");
273 loc_codeset = nl_langinfo(CODESET);
274 if (loc_codeset && *loc_codeset) {
275 PyObject *enc = PyCodec_Encoder(loc_codeset);
276 if (enc) {
277 loc_codeset = strdup(loc_codeset);
278 Py_DECREF(enc);
279 } else {
280 loc_codeset = NULL;
281 PyErr_Clear();
282 }
283 } else
284 loc_codeset = NULL;
285 setlocale(LC_CTYPE, saved_locale);
286 free(saved_locale);
287
288 if (!overridden) {
289 codeset = icodeset = loc_codeset;
290 free_codeset = 1;
Martin v. Löwis73d538b2003-03-05 15:13:47 +0000291 }
Martin v. Löwis99815892008-06-01 07:20:46 +0000292
293 /* Initialize Py_FileSystemDefaultEncoding from
294 locale even if PYTHONIOENCODING is set. */
295 if (!Py_FileSystemDefaultEncoding) {
296 Py_FileSystemDefaultEncoding = loc_codeset;
297 if (!overridden)
298 free_codeset = 0;
299 }
300 }
301#endif
302
303#ifdef MS_WINDOWS
304 if (!overridden) {
305 icodeset = ibuf;
Martin v. Löwis6495c8d2008-06-01 08:19:02 +0000306 codeset = buf;
Martin v. Löwis99815892008-06-01 07:20:46 +0000307 sprintf(ibuf, "cp%d", GetConsoleCP());
308 sprintf(buf, "cp%d", GetConsoleOutputCP());
309 }
310#endif
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000311
312 if (codeset) {
Michael W. Hudson68debc92003-08-11 12:20:24 +0000313 sys_stream = PySys_GetObject("stdin");
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000314 sys_isatty = PyObject_CallMethod(sys_stream, "isatty", "");
315 if (!sys_isatty)
316 PyErr_Clear();
Martin v. Löwis99815892008-06-01 07:20:46 +0000317 if ((overridden ||
318 (sys_isatty && PyObject_IsTrue(sys_isatty))) &&
Thomas Woutersafea5292007-01-23 13:42:00 +0000319 PyFile_Check(sys_stream)) {
Martin v. Löwis99815892008-06-01 07:20:46 +0000320 if (!PyFile_SetEncodingAndErrors(sys_stream, icodeset, errors))
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000321 Py_FatalError("Cannot set codeset of stdin");
322 }
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000323 Py_XDECREF(sys_isatty);
324
325 sys_stream = PySys_GetObject("stdout");
326 sys_isatty = PyObject_CallMethod(sys_stream, "isatty", "");
327 if (!sys_isatty)
328 PyErr_Clear();
Martin v. Löwis99815892008-06-01 07:20:46 +0000329 if ((overridden ||
330 (sys_isatty && PyObject_IsTrue(sys_isatty))) &&
Thomas Woutersafea5292007-01-23 13:42:00 +0000331 PyFile_Check(sys_stream)) {
Martin v. Löwis99815892008-06-01 07:20:46 +0000332 if (!PyFile_SetEncodingAndErrors(sys_stream, codeset, errors))
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000333 Py_FatalError("Cannot set codeset of stdout");
334 }
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000335 Py_XDECREF(sys_isatty);
336
Martin v. Löwisea62d252006-04-03 10:56:49 +0000337 sys_stream = PySys_GetObject("stderr");
338 sys_isatty = PyObject_CallMethod(sys_stream, "isatty", "");
339 if (!sys_isatty)
340 PyErr_Clear();
Martin v. Löwis99815892008-06-01 07:20:46 +0000341 if((overridden ||
342 (sys_isatty && PyObject_IsTrue(sys_isatty))) &&
Thomas Woutersafea5292007-01-23 13:42:00 +0000343 PyFile_Check(sys_stream)) {
Martin v. Löwis99815892008-06-01 07:20:46 +0000344 if (!PyFile_SetEncodingAndErrors(sys_stream, codeset, errors))
Martin v. Löwisea62d252006-04-03 10:56:49 +0000345 Py_FatalError("Cannot set codeset of stderr");
346 }
347 Py_XDECREF(sys_isatty);
348
Martin v. Löwis99815892008-06-01 07:20:46 +0000349 if (free_codeset)
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000350 free(codeset);
Martin v. Löwis73d538b2003-03-05 15:13:47 +0000351 }
Guido van Rossum25ce5661997-08-02 03:10:38 +0000352}
353
Martin v. Löwis336e85f2004-08-19 11:31:58 +0000354void
355Py_Initialize(void)
356{
357 Py_InitializeEx(1);
358}
359
360
Guido van Rossum2edcf0d1998-12-15 16:12:00 +0000361#ifdef COUNT_ALLOCS
Martin v. Löwis45294a92006-04-18 06:24:08 +0000362extern void dump_counts(FILE*);
Guido van Rossum2edcf0d1998-12-15 16:12:00 +0000363#endif
364
Guido van Rossum25ce5661997-08-02 03:10:38 +0000365/* Undo the effect of Py_Initialize().
366
367 Beware: if multiple interpreter and/or thread states exist, these
368 are not wiped out; only the current thread and interpreter state
369 are deleted. But since everything else is deleted, those other
370 interpreter and thread states should no longer be used.
371
372 (XXX We should do better, e.g. wipe out all interpreters and
373 threads.)
374
375 Locking: as above.
376
377*/
378
379void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000380Py_Finalize(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000381{
382 PyInterpreterState *interp;
383 PyThreadState *tstate;
384
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000385 if (!initialized)
Guido van Rossumaa615051997-08-20 22:40:18 +0000386 return;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000387
Tim Peters384fd102001-01-21 03:40:37 +0000388 /* The interpreter is still entirely intact at this point, and the
389 * exit funcs may be relying on that. In particular, if some thread
390 * or exit func is still waiting to do an import, the import machinery
391 * expects Py_IsInitialized() to return true. So don't say the
392 * interpreter is uninitialized until after the exit funcs have run.
393 * Note that Threading.py uses an exit func to do a join on all the
394 * threads created thru it, so this also protects pending imports in
395 * the threads created via Threading.
396 */
Guido van Rossum4cc462e1998-01-19 22:00:38 +0000397 call_sys_exitfunc();
Tim Peters384fd102001-01-21 03:40:37 +0000398 initialized = 0;
Guido van Rossum4cc462e1998-01-19 22:00:38 +0000399
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000400 /* Get current thread state and interpreter pointer */
Nicholas Bastine5662ae2004-03-24 22:22:12 +0000401 tstate = PyThreadState_GET();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000402 interp = tstate->interp;
403
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000404 /* Disable signal handling */
405 PyOS_FiniInterrupts();
406
Christian Heimes908caac2008-01-27 23:34:59 +0000407 /* Clear type lookup cache */
408 PyType_ClearCache();
409
Guido van Rossume13ddc92003-04-17 17:29:22 +0000410 /* Collect garbage. This may call finalizers; it's nice to call these
Tim Peters1d7323e2003-12-01 21:35:27 +0000411 * before all modules are destroyed.
412 * XXX If a __del__ or weakref callback is triggered here, and tries to
413 * XXX import a module, bad things can happen, because Python no
414 * XXX longer believes it's initialized.
415 * XXX Fatal Python error: Interpreter not initialized (version mismatch?)
416 * XXX is easy to provoke that way. I've also seen, e.g.,
417 * XXX Exception exceptions.ImportError: 'No module named sha'
418 * XXX in <function callback at 0x008F5718> ignored
419 * XXX but I'm unclear on exactly how that one happens. In any case,
420 * XXX I haven't seen a real-life report of either of these.
Neal Norwitz9589ee22006-03-04 19:01:22 +0000421 */
Guido van Rossume13ddc92003-04-17 17:29:22 +0000422 PyGC_Collect();
Martin v. Löwis45294a92006-04-18 06:24:08 +0000423#ifdef COUNT_ALLOCS
424 /* With COUNT_ALLOCS, it helps to run GC multiple times:
425 each collection might release some types from the type
426 list, so they become garbage. */
427 while (PyGC_Collect() > 0)
428 /* nothing */;
429#endif
Guido van Rossume13ddc92003-04-17 17:29:22 +0000430
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000431 /* Destroy all modules */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000432 PyImport_Cleanup();
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000433
Guido van Rossume13ddc92003-04-17 17:29:22 +0000434 /* Collect final garbage. This disposes of cycles created by
Tim Peters1d7323e2003-12-01 21:35:27 +0000435 * new-style class definitions, for example.
436 * XXX This is disabled because it caused too many problems. If
437 * XXX a __del__ or weakref callback triggers here, Python code has
438 * XXX a hard time running, because even the sys module has been
439 * XXX cleared out (sys.stdout is gone, sys.excepthook is gone, etc).
440 * XXX One symptom is a sequence of information-free messages
441 * XXX coming from threads (if a __del__ or callback is invoked,
442 * XXX other threads can execute too, and any exception they encounter
443 * XXX triggers a comedy of errors as subsystem after subsystem
444 * XXX fails to find what it *expects* to find in sys to help report
445 * XXX the exception and consequent unexpected failures). I've also
446 * XXX seen segfaults then, after adding print statements to the
447 * XXX Python code getting called.
448 */
449#if 0
Guido van Rossume13ddc92003-04-17 17:29:22 +0000450 PyGC_Collect();
Tim Peters1d7323e2003-12-01 21:35:27 +0000451#endif
Guido van Rossume13ddc92003-04-17 17:29:22 +0000452
Guido van Rossum1707aad1997-12-08 23:43:45 +0000453 /* Destroy the database used by _PyImport_{Fixup,Find}Extension */
454 _PyImport_Fini();
455
456 /* Debugging stuff */
457#ifdef COUNT_ALLOCS
Martin v. Löwis45294a92006-04-18 06:24:08 +0000458 dump_counts(stdout);
Guido van Rossum1707aad1997-12-08 23:43:45 +0000459#endif
460
Tim Peters62e97f02006-03-28 21:44:32 +0000461 PRINT_TOTAL_REFS();
Guido van Rossum1707aad1997-12-08 23:43:45 +0000462
Tim Peters9cf25ce2003-04-17 15:21:01 +0000463#ifdef Py_TRACE_REFS
464 /* Display all objects still alive -- this can invoke arbitrary
465 * __repr__ overrides, so requires a mostly-intact interpreter.
466 * Alas, a lot of stuff may still be alive now that will be cleaned
467 * up later.
468 */
Tim Peters269b2a62003-04-17 19:52:29 +0000469 if (Py_GETENV("PYTHONDUMPREFS"))
Tim Peters9cf25ce2003-04-17 15:21:01 +0000470 _Py_PrintReferences(stderr);
Tim Peters9cf25ce2003-04-17 15:21:01 +0000471#endif /* Py_TRACE_REFS */
472
Guido van Rossumd922fa42003-04-15 14:10:09 +0000473 /* Clear interpreter state */
Barry Warsawf242aa02000-05-25 23:09:49 +0000474 PyInterpreterState_Clear(interp);
Guido van Rossumd922fa42003-04-15 14:10:09 +0000475
Anthony Baxter12b6f6c2005-03-29 13:36:16 +0000476 /* Now we decref the exception classes. After this point nothing
477 can raise an exception. That's okay, because each Fini() method
478 below has been checked to make sure no exceptions are ever
479 raised.
480 */
481
482 _PyExc_Fini();
483
Amaury Forgeot d'Arc025c3472007-11-29 23:35:25 +0000484 /* Cleanup auto-thread-state */
485#ifdef WITH_THREAD
486 _PyGILState_Fini();
487#endif /* WITH_THREAD */
488
Guido van Rossumd922fa42003-04-15 14:10:09 +0000489 /* Delete current thread */
Barry Warsawf242aa02000-05-25 23:09:49 +0000490 PyThreadState_Swap(NULL);
491 PyInterpreterState_Delete(interp);
492
Guido van Rossumd922fa42003-04-15 14:10:09 +0000493 /* Sundry finalizers */
Guido van Rossumcc283f51997-08-05 02:22:03 +0000494 PyMethod_Fini();
495 PyFrame_Fini();
496 PyCFunction_Fini();
497 PyTuple_Fini();
Raymond Hettingerfb09f0e2004-10-07 03:58:07 +0000498 PyList_Fini();
Raymond Hettingerd7946662005-08-01 21:39:29 +0000499 PySet_Fini();
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000500 PyString_Fini();
Christian Heimes3497f942008-05-26 12:29:14 +0000501 PyByteArray_Fini();
Guido van Rossumcc283f51997-08-05 02:22:03 +0000502 PyInt_Fini();
503 PyFloat_Fini();
Christian Heimesf75dbef2008-02-08 00:11:31 +0000504 PyDict_Fini();
Guido van Rossumcc283f51997-08-05 02:22:03 +0000505
Marc-André Lemburg95de5c12002-04-08 08:19:36 +0000506#ifdef Py_USING_UNICODE
507 /* Cleanup Unicode implementation */
508 _PyUnicode_Fini();
509#endif
510
Guido van Rossumcc283f51997-08-05 02:22:03 +0000511 /* XXX Still allocated:
512 - various static ad-hoc pointers to interned strings
513 - int and float free list blocks
514 - whatever various modules and libraries allocate
515 */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000516
517 PyGrammar_RemoveAccelerators(&_PyParser_Grammar);
Guido van Rossumcc283f51997-08-05 02:22:03 +0000518
Tim Peters269b2a62003-04-17 19:52:29 +0000519#ifdef Py_TRACE_REFS
520 /* Display addresses (& refcnts) of all objects still alive.
521 * An address can be used to find the repr of the object, printed
522 * above by _Py_PrintReferences.
523 */
524 if (Py_GETENV("PYTHONDUMPREFS"))
525 _Py_PrintReferenceAddresses(stderr);
526#endif /* Py_TRACE_REFS */
Tim Peters0e871182002-04-13 08:29:14 +0000527#ifdef PYMALLOC_DEBUG
528 if (Py_GETENV("PYTHONMALLOCSTATS"))
529 _PyObject_DebugMallocStats();
530#endif
531
Guido van Rossumcc283f51997-08-05 02:22:03 +0000532 call_ll_exitfuncs();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000533}
534
535/* Create and initialize a new interpreter and thread, and return the
536 new thread. This requires that Py_Initialize() has been called
537 first.
538
539 Unsuccessful initialization yields a NULL pointer. Note that *no*
540 exception information is available even in this case -- the
541 exception information is held in the thread, and there is no
542 thread.
543
544 Locking: as above.
545
546*/
547
548PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000549Py_NewInterpreter(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000550{
551 PyInterpreterState *interp;
552 PyThreadState *tstate, *save_tstate;
553 PyObject *bimod, *sysmod;
554
555 if (!initialized)
556 Py_FatalError("Py_NewInterpreter: call Py_Initialize first");
557
558 interp = PyInterpreterState_New();
559 if (interp == NULL)
560 return NULL;
561
562 tstate = PyThreadState_New(interp);
563 if (tstate == NULL) {
564 PyInterpreterState_Delete(interp);
565 return NULL;
566 }
567
568 save_tstate = PyThreadState_Swap(tstate);
569
570 /* XXX The following is lax in error checking */
571
572 interp->modules = PyDict_New();
Collin Winter276887b2007-03-12 16:11:39 +0000573 interp->modules_reloading = PyDict_New();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000574
575 bimod = _PyImport_FindExtension("__builtin__", "__builtin__");
576 if (bimod != NULL) {
Guido van Rossum4a1f39a1997-11-04 19:36:18 +0000577 interp->builtins = PyModule_GetDict(bimod);
Neal Norwitz0c6ae5b2006-08-21 20:16:24 +0000578 if (interp->builtins == NULL)
579 goto handle_error;
Guido van Rossum4a1f39a1997-11-04 19:36:18 +0000580 Py_INCREF(interp->builtins);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000581 }
582 sysmod = _PyImport_FindExtension("sys", "sys");
583 if (bimod != NULL && sysmod != NULL) {
584 interp->sysdict = PyModule_GetDict(sysmod);
Neal Norwitz0c6ae5b2006-08-21 20:16:24 +0000585 if (interp->sysdict == NULL)
586 goto handle_error;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000587 Py_INCREF(interp->sysdict);
588 PySys_SetPath(Py_GetPath());
589 PyDict_SetItemString(interp->sysdict, "modules",
590 interp->modules);
Martin v. Löwisd7ceb222003-01-22 09:00:38 +0000591 _PyImportHooks_Init();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000592 initmain();
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000593 if (!Py_NoSiteFlag)
594 initsite();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000595 }
596
597 if (!PyErr_Occurred())
598 return tstate;
599
Neal Norwitz0c6ae5b2006-08-21 20:16:24 +0000600handle_error:
Guido van Rossum25ce5661997-08-02 03:10:38 +0000601 /* Oops, it didn't work. Undo it all. */
602
603 PyErr_Print();
604 PyThreadState_Clear(tstate);
605 PyThreadState_Swap(save_tstate);
606 PyThreadState_Delete(tstate);
607 PyInterpreterState_Delete(interp);
608
609 return NULL;
610}
611
612/* Delete an interpreter and its last thread. This requires that the
613 given thread state is current, that the thread has no remaining
614 frames, and that it is its interpreter's only remaining thread.
615 It is a fatal error to violate these constraints.
616
617 (Py_Finalize() doesn't have these constraints -- it zaps
618 everything, regardless.)
619
620 Locking: as above.
621
622*/
623
624void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000625Py_EndInterpreter(PyThreadState *tstate)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000626{
627 PyInterpreterState *interp = tstate->interp;
628
Nicholas Bastine5662ae2004-03-24 22:22:12 +0000629 if (tstate != PyThreadState_GET())
Guido van Rossum25ce5661997-08-02 03:10:38 +0000630 Py_FatalError("Py_EndInterpreter: thread is not current");
631 if (tstate->frame != NULL)
632 Py_FatalError("Py_EndInterpreter: thread still has a frame");
633 if (tstate != interp->tstate_head || tstate->next != NULL)
634 Py_FatalError("Py_EndInterpreter: not the last thread");
635
636 PyImport_Cleanup();
637 PyInterpreterState_Clear(interp);
638 PyThreadState_Swap(NULL);
639 PyInterpreterState_Delete(interp);
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000640}
641
642static char *progname = "python";
643
644void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000645Py_SetProgramName(char *pn)
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000646{
647 if (pn && *pn)
648 progname = pn;
649}
650
651char *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000652Py_GetProgramName(void)
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000653{
654 return progname;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000655}
656
Guido van Rossuma61691e1998-02-06 22:27:24 +0000657static char *default_home = NULL;
658
659void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000660Py_SetPythonHome(char *home)
Guido van Rossuma61691e1998-02-06 22:27:24 +0000661{
662 default_home = home;
663}
664
665char *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000666Py_GetPythonHome(void)
Guido van Rossuma61691e1998-02-06 22:27:24 +0000667{
668 char *home = default_home;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000669 if (home == NULL && !Py_IgnoreEnvironmentFlag)
670 home = Py_GETENV("PYTHONHOME");
Guido van Rossuma61691e1998-02-06 22:27:24 +0000671 return home;
672}
673
Guido van Rossum6135a871995-01-09 17:53:26 +0000674/* Create __main__ module */
675
676static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000677initmain(void)
Guido van Rossum6135a871995-01-09 17:53:26 +0000678{
Guido van Rossum82598051997-03-05 00:20:32 +0000679 PyObject *m, *d;
680 m = PyImport_AddModule("__main__");
Guido van Rossum6135a871995-01-09 17:53:26 +0000681 if (m == NULL)
Guido van Rossum82598051997-03-05 00:20:32 +0000682 Py_FatalError("can't create __main__ module");
683 d = PyModule_GetDict(m);
684 if (PyDict_GetItemString(d, "__builtins__") == NULL) {
Guido van Rossum858cb731997-11-19 16:15:37 +0000685 PyObject *bimod = PyImport_ImportModule("__builtin__");
686 if (bimod == NULL ||
687 PyDict_SetItemString(d, "__builtins__", bimod) != 0)
Guido van Rossum82598051997-03-05 00:20:32 +0000688 Py_FatalError("can't add __builtins__ to __main__");
Barry Warsaw3d05b1a1999-01-29 21:30:22 +0000689 Py_DECREF(bimod);
Guido van Rossum6135a871995-01-09 17:53:26 +0000690 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000691}
692
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000693/* Import the site module (not into __main__ though) */
694
695static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000696initsite(void)
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000697{
698 PyObject *m, *f;
699 m = PyImport_ImportModule("site");
700 if (m == NULL) {
701 f = PySys_GetObject("stderr");
702 if (Py_VerboseFlag) {
703 PyFile_WriteString(
704 "'import site' failed; traceback:\n", f);
705 PyErr_Print();
706 }
707 else {
708 PyFile_WriteString(
709 "'import site' failed; use -v for traceback\n", f);
710 PyErr_Clear();
711 }
712 }
713 else {
714 Py_DECREF(m);
715 }
716}
717
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000718/* Parse input from a file and execute it */
719
720int
Tim Peters5e9d6cf2006-05-28 10:41:29 +0000721PyRun_AnyFileExFlags(FILE *fp, const char *filename, int closeit,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000722 PyCompilerFlags *flags)
723{
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000724 if (filename == NULL)
725 filename = "???";
Guido van Rossum0df002c2000-08-27 19:21:52 +0000726 if (Py_FdIsInteractive(fp, filename)) {
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000727 int err = PyRun_InteractiveLoopFlags(fp, filename, flags);
Guido van Rossum0df002c2000-08-27 19:21:52 +0000728 if (closeit)
729 fclose(fp);
730 return err;
731 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000732 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000733 return PyRun_SimpleFileExFlags(fp, filename, closeit, flags);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000734}
735
736int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000737PyRun_InteractiveLoopFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000738{
Guido van Rossum82598051997-03-05 00:20:32 +0000739 PyObject *v;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000740 int ret;
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000741 PyCompilerFlags local_flags;
Jeremy Hylton9f324e92001-03-01 22:59:14 +0000742
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000743 if (flags == NULL) {
744 flags = &local_flags;
Tim Peters5ba58662001-07-16 02:29:45 +0000745 local_flags.cf_flags = 0;
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000746 }
Guido van Rossum82598051997-03-05 00:20:32 +0000747 v = PySys_GetObject("ps1");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000748 if (v == NULL) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000749 PySys_SetObject("ps1", v = PyString_FromString(">>> "));
Guido van Rossum82598051997-03-05 00:20:32 +0000750 Py_XDECREF(v);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000751 }
Guido van Rossum82598051997-03-05 00:20:32 +0000752 v = PySys_GetObject("ps2");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000753 if (v == NULL) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000754 PySys_SetObject("ps2", v = PyString_FromString("... "));
Guido van Rossum82598051997-03-05 00:20:32 +0000755 Py_XDECREF(v);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000756 }
757 for (;;) {
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000758 ret = PyRun_InteractiveOneFlags(fp, filename, flags);
Tim Peters62e97f02006-03-28 21:44:32 +0000759 PRINT_TOTAL_REFS();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000760 if (ret == E_EOF)
761 return 0;
762 /*
763 if (ret == E_NOMEM)
764 return -1;
765 */
766 }
767}
768
Eric Smith7c478942008-03-18 23:45:49 +0000769#if 0
Neil Schemenauerc24ea082002-03-22 23:53:36 +0000770/* compute parser flags based on compiler flags */
Neil Schemenauerc24ea082002-03-22 23:53:36 +0000771#define PARSER_FLAGS(flags) \
Thomas Wouters34aa7ba2006-02-28 19:02:24 +0000772 ((flags) ? ((((flags)->cf_flags & PyCF_DONT_IMPLY_DEDENT) ? \
Neal Norwitzca460d92006-09-06 06:28:06 +0000773 PyPARSE_DONT_IMPLY_DEDENT : 0)) : 0)
Eric Smith7c478942008-03-18 23:45:49 +0000774#endif
775#if 1
Neal Norwitzca460d92006-09-06 06:28:06 +0000776/* Keep an example of flags with future keyword support. */
777#define PARSER_FLAGS(flags) \
778 ((flags) ? ((((flags)->cf_flags & PyCF_DONT_IMPLY_DEDENT) ? \
Thomas Wouters34aa7ba2006-02-28 19:02:24 +0000779 PyPARSE_DONT_IMPLY_DEDENT : 0) \
Christian Heimes3c608332008-03-26 22:01:37 +0000780 | (((flags)->cf_flags & CO_FUTURE_PRINT_FUNCTION) ? \
781 PyPARSE_PRINT_IS_FUNCTION : 0) \
782 | (((flags)->cf_flags & CO_FUTURE_UNICODE_LITERALS) ? \
783 PyPARSE_UNICODE_LITERALS : 0) \
784 ) : 0)
Neal Norwitzca460d92006-09-06 06:28:06 +0000785#endif
Neil Schemenauerc24ea082002-03-22 23:53:36 +0000786
Jeremy Hylton9f324e92001-03-01 22:59:14 +0000787int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000788PyRun_InteractiveOneFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
Jeremy Hylton9f324e92001-03-01 22:59:14 +0000789{
Guido van Rossum82598051997-03-05 00:20:32 +0000790 PyObject *m, *d, *v, *w;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000791 mod_ty mod;
Neal Norwitz9589ee22006-03-04 19:01:22 +0000792 PyArena *arena;
Guido van Rossumddc3fb51997-11-25 20:58:13 +0000793 char *ps1 = "", *ps2 = "";
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000794 int errcode = 0;
Tim Petersfe2127d2001-07-16 05:37:24 +0000795
Guido van Rossum82598051997-03-05 00:20:32 +0000796 v = PySys_GetObject("ps1");
Guido van Rossumddc3fb51997-11-25 20:58:13 +0000797 if (v != NULL) {
798 v = PyObject_Str(v);
799 if (v == NULL)
800 PyErr_Clear();
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000801 else if (PyString_Check(v))
802 ps1 = PyString_AsString(v);
Guido van Rossumddc3fb51997-11-25 20:58:13 +0000803 }
Guido van Rossum82598051997-03-05 00:20:32 +0000804 w = PySys_GetObject("ps2");
Guido van Rossumddc3fb51997-11-25 20:58:13 +0000805 if (w != NULL) {
806 w = PyObject_Str(w);
807 if (w == NULL)
808 PyErr_Clear();
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000809 else if (PyString_Check(w))
810 ps2 = PyString_AsString(w);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000811 }
Neal Norwitz9589ee22006-03-04 19:01:22 +0000812 arena = PyArena_New();
Neal Norwitzb59d08c2006-07-22 16:20:49 +0000813 if (arena == NULL) {
814 Py_XDECREF(v);
815 Py_XDECREF(w);
816 return -1;
817 }
Tim Peters5e9d6cf2006-05-28 10:41:29 +0000818 mod = PyParser_ASTFromFile(fp, filename,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000819 Py_single_input, ps1, ps2,
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000820 flags, &errcode, arena);
Guido van Rossum82598051997-03-05 00:20:32 +0000821 Py_XDECREF(v);
822 Py_XDECREF(w);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000823 if (mod == NULL) {
Neal Norwitz9589ee22006-03-04 19:01:22 +0000824 PyArena_Free(arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000825 if (errcode == E_EOF) {
826 PyErr_Clear();
Guido van Rossuma110aa61994-08-29 12:50:44 +0000827 return E_EOF;
828 }
Guido van Rossum82598051997-03-05 00:20:32 +0000829 PyErr_Print();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000830 return -1;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000831 }
Guido van Rossum82598051997-03-05 00:20:32 +0000832 m = PyImport_AddModule("__main__");
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000833 if (m == NULL) {
Neal Norwitz9589ee22006-03-04 19:01:22 +0000834 PyArena_Free(arena);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000835 return -1;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000836 }
Guido van Rossum82598051997-03-05 00:20:32 +0000837 d = PyModule_GetDict(m);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000838 v = run_mod(mod, filename, d, d, flags, arena);
Neal Norwitz9589ee22006-03-04 19:01:22 +0000839 PyArena_Free(arena);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000840 if (v == NULL) {
Guido van Rossum82598051997-03-05 00:20:32 +0000841 PyErr_Print();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000842 return -1;
843 }
Guido van Rossum82598051997-03-05 00:20:32 +0000844 Py_DECREF(v);
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000845 if (Py_FlushLine())
846 PyErr_Clear();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000847 return 0;
848}
849
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000850/* Check whether a file maybe a pyc file: Look at the extension,
851 the file type, and, if we may close it, at the first few bytes. */
852
853static int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000854maybe_pyc_file(FILE *fp, const char* filename, const char* ext, int closeit)
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000855{
856 if (strcmp(ext, ".pyc") == 0 || strcmp(ext, ".pyo") == 0)
857 return 1;
858
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000859 /* Only look into the file if we are allowed to close it, since
860 it then should also be seekable. */
861 if (closeit) {
862 /* Read only two bytes of the magic. If the file was opened in
863 text mode, the bytes 3 and 4 of the magic (\r\n) might not
864 be read as they are on disk. */
865 unsigned int halfmagic = PyImport_GetMagicNumber() & 0xFFFF;
866 unsigned char buf[2];
Tim Peters3e876562001-02-11 04:35:39 +0000867 /* Mess: In case of -x, the stream is NOT at its start now,
868 and ungetc() was used to push back the first newline,
Tim Peters6f5a4ef2001-02-17 22:02:34 +0000869 which makes the current stream position formally undefined,
870 and a x-platform nightmare.
871 Unfortunately, we have no direct way to know whether -x
872 was specified. So we use a terrible hack: if the current
873 stream position is not 0, we assume -x was specified, and
874 give up. Bug 132850 on SourceForge spells out the
875 hopelessness of trying anything else (fseek and ftell
876 don't work predictably x-platform for text-mode files).
Tim Peters3e876562001-02-11 04:35:39 +0000877 */
Tim Peters3e876562001-02-11 04:35:39 +0000878 int ispyc = 0;
Tim Peters6f5a4ef2001-02-17 22:02:34 +0000879 if (ftell(fp) == 0) {
880 if (fread(buf, 1, 2, fp) == 2 &&
Tim Petersd08e3822003-04-17 15:24:21 +0000881 ((unsigned int)buf[1]<<8 | buf[0]) == halfmagic)
Tim Peters6f5a4ef2001-02-17 22:02:34 +0000882 ispyc = 1;
883 rewind(fp);
884 }
Tim Peters3e876562001-02-11 04:35:39 +0000885 return ispyc;
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000886 }
887 return 0;
Tim Petersd08e3822003-04-17 15:24:21 +0000888}
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000889
Guido van Rossum0df002c2000-08-27 19:21:52 +0000890int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000891PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000892 PyCompilerFlags *flags)
893{
Guido van Rossum82598051997-03-05 00:20:32 +0000894 PyObject *m, *d, *v;
Martin v. Löwis95292d62002-12-11 14:04:59 +0000895 const char *ext;
Georg Brandlaa2321b2007-03-07 00:40:28 +0000896 int set_file_name = 0, ret;
Guido van Rossumfdef2711994-09-14 13:31:04 +0000897
Guido van Rossum82598051997-03-05 00:20:32 +0000898 m = PyImport_AddModule("__main__");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000899 if (m == NULL)
900 return -1;
Guido van Rossum82598051997-03-05 00:20:32 +0000901 d = PyModule_GetDict(m);
Fred Drake8ed02042002-10-17 21:24:58 +0000902 if (PyDict_GetItemString(d, "__file__") == NULL) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000903 PyObject *f = PyString_FromString(filename);
Fred Drake8ed02042002-10-17 21:24:58 +0000904 if (f == NULL)
905 return -1;
906 if (PyDict_SetItemString(d, "__file__", f) < 0) {
907 Py_DECREF(f);
908 return -1;
909 }
Georg Brandlaa2321b2007-03-07 00:40:28 +0000910 set_file_name = 1;
Fred Drake8ed02042002-10-17 21:24:58 +0000911 Py_DECREF(f);
912 }
Guido van Rossumfdef2711994-09-14 13:31:04 +0000913 ext = filename + strlen(filename) - 4;
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000914 if (maybe_pyc_file(fp, filename, ext, closeit)) {
Guido van Rossumfdef2711994-09-14 13:31:04 +0000915 /* Try to run a pyc file. First, re-open in binary */
Guido van Rossum0df002c2000-08-27 19:21:52 +0000916 if (closeit)
917 fclose(fp);
Fred Drake8ed02042002-10-17 21:24:58 +0000918 if ((fp = fopen(filename, "rb")) == NULL) {
Guido van Rossumfdef2711994-09-14 13:31:04 +0000919 fprintf(stderr, "python: Can't reopen .pyc file\n");
Georg Brandlaa2321b2007-03-07 00:40:28 +0000920 ret = -1;
921 goto done;
Guido van Rossumfdef2711994-09-14 13:31:04 +0000922 }
Guido van Rossum2a7f58d1997-04-02 05:28:38 +0000923 /* Turn on optimization if a .pyo file is given */
924 if (strcmp(ext, ".pyo") == 0)
925 Py_OptimizeFlag = 1;
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000926 v = run_pyc_file(fp, filename, d, d, flags);
Guido van Rossumfdef2711994-09-14 13:31:04 +0000927 } else {
Tim Petersd08e3822003-04-17 15:24:21 +0000928 v = PyRun_FileExFlags(fp, filename, Py_file_input, d, d,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000929 closeit, flags);
Guido van Rossumfdef2711994-09-14 13:31:04 +0000930 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000931 if (v == NULL) {
Guido van Rossum82598051997-03-05 00:20:32 +0000932 PyErr_Print();
Georg Brandlaa2321b2007-03-07 00:40:28 +0000933 ret = -1;
934 goto done;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000935 }
Guido van Rossum82598051997-03-05 00:20:32 +0000936 Py_DECREF(v);
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000937 if (Py_FlushLine())
938 PyErr_Clear();
Georg Brandlaa2321b2007-03-07 00:40:28 +0000939 ret = 0;
940 done:
941 if (set_file_name && PyDict_DelItemString(d, "__file__"))
942 PyErr_Clear();
943 return ret;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000944}
945
946int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000947PyRun_SimpleStringFlags(const char *command, PyCompilerFlags *flags)
Guido van Rossum393661d2001-08-31 17:40:15 +0000948{
Guido van Rossum82598051997-03-05 00:20:32 +0000949 PyObject *m, *d, *v;
950 m = PyImport_AddModule("__main__");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000951 if (m == NULL)
952 return -1;
Guido van Rossum82598051997-03-05 00:20:32 +0000953 d = PyModule_GetDict(m);
Guido van Rossum393661d2001-08-31 17:40:15 +0000954 v = PyRun_StringFlags(command, Py_file_input, d, d, flags);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000955 if (v == NULL) {
Guido van Rossum82598051997-03-05 00:20:32 +0000956 PyErr_Print();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000957 return -1;
958 }
Guido van Rossum82598051997-03-05 00:20:32 +0000959 Py_DECREF(v);
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000960 if (Py_FlushLine())
961 PyErr_Clear();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000962 return 0;
963}
964
Barry Warsaw035574d1997-08-29 22:07:17 +0000965static int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000966parse_syntax_error(PyObject *err, PyObject **message, const char **filename,
967 int *lineno, int *offset, const char **text)
Barry Warsaw035574d1997-08-29 22:07:17 +0000968{
969 long hold;
970 PyObject *v;
971
972 /* old style errors */
973 if (PyTuple_Check(err))
Neal Norwitz78293352002-04-01 01:41:20 +0000974 return PyArg_ParseTuple(err, "O(ziiz)", message, filename,
Neal Norwitz9589ee22006-03-04 19:01:22 +0000975 lineno, offset, text);
Barry Warsaw035574d1997-08-29 22:07:17 +0000976
977 /* new style errors. `err' is an instance */
978
979 if (! (v = PyObject_GetAttrString(err, "msg")))
980 goto finally;
981 *message = v;
982
983 if (!(v = PyObject_GetAttrString(err, "filename")))
984 goto finally;
985 if (v == Py_None)
986 *filename = NULL;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000987 else if (! (*filename = PyString_AsString(v)))
Barry Warsaw035574d1997-08-29 22:07:17 +0000988 goto finally;
989
990 Py_DECREF(v);
991 if (!(v = PyObject_GetAttrString(err, "lineno")))
992 goto finally;
993 hold = PyInt_AsLong(v);
994 Py_DECREF(v);
995 v = NULL;
996 if (hold < 0 && PyErr_Occurred())
997 goto finally;
998 *lineno = (int)hold;
999
1000 if (!(v = PyObject_GetAttrString(err, "offset")))
1001 goto finally;
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001002 if (v == Py_None) {
1003 *offset = -1;
1004 Py_DECREF(v);
1005 v = NULL;
1006 } else {
1007 hold = PyInt_AsLong(v);
1008 Py_DECREF(v);
1009 v = NULL;
1010 if (hold < 0 && PyErr_Occurred())
1011 goto finally;
1012 *offset = (int)hold;
1013 }
Barry Warsaw035574d1997-08-29 22:07:17 +00001014
1015 if (!(v = PyObject_GetAttrString(err, "text")))
1016 goto finally;
1017 if (v == Py_None)
1018 *text = NULL;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001019 else if (! (*text = PyString_AsString(v)))
Barry Warsaw035574d1997-08-29 22:07:17 +00001020 goto finally;
1021 Py_DECREF(v);
1022 return 1;
1023
1024finally:
1025 Py_XDECREF(v);
1026 return 0;
1027}
1028
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001029void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001030PyErr_Print(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001031{
Guido van Rossuma61691e1998-02-06 22:27:24 +00001032 PyErr_PrintEx(1);
1033}
1034
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001035static void
Martin v. Löwis95292d62002-12-11 14:04:59 +00001036print_error_text(PyObject *f, int offset, const char *text)
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001037{
1038 char *nl;
1039 if (offset >= 0) {
1040 if (offset > 0 && offset == (int)strlen(text))
1041 offset--;
1042 for (;;) {
1043 nl = strchr(text, '\n');
1044 if (nl == NULL || nl-text >= offset)
1045 break;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001046 offset -= (int)(nl+1-text);
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001047 text = nl+1;
1048 }
1049 while (*text == ' ' || *text == '\t') {
1050 text++;
1051 offset--;
1052 }
1053 }
1054 PyFile_WriteString(" ", f);
1055 PyFile_WriteString(text, f);
1056 if (*text == '\0' || text[strlen(text)-1] != '\n')
1057 PyFile_WriteString("\n", f);
1058 if (offset == -1)
1059 return;
1060 PyFile_WriteString(" ", f);
1061 offset--;
1062 while (offset > 0) {
1063 PyFile_WriteString(" ", f);
1064 offset--;
1065 }
1066 PyFile_WriteString("^\n", f);
1067}
1068
Guido van Rossum66e8e862001-03-23 17:54:43 +00001069static void
1070handle_system_exit(void)
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001071{
Neal Norwitz9589ee22006-03-04 19:01:22 +00001072 PyObject *exception, *value, *tb;
1073 int exitcode = 0;
Tim Peterscf615b52003-04-19 18:47:02 +00001074
Georg Brandl49aafc92007-03-07 00:34:46 +00001075 if (Py_InspectFlag)
1076 /* Don't exit if -i flag was given. This flag is set to 0
1077 * when entering interactive mode for inspecting. */
1078 return;
1079
Guido van Rossum66e8e862001-03-23 17:54:43 +00001080 PyErr_Fetch(&exception, &value, &tb);
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001081 if (Py_FlushLine())
1082 PyErr_Clear();
1083 fflush(stdout);
1084 if (value == NULL || value == Py_None)
Tim Peterscf615b52003-04-19 18:47:02 +00001085 goto done;
Brett Cannonbf364092006-03-01 04:25:17 +00001086 if (PyExceptionInstance_Check(value)) {
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001087 /* The error code should be in the `code' attribute. */
1088 PyObject *code = PyObject_GetAttrString(value, "code");
1089 if (code) {
1090 Py_DECREF(value);
1091 value = code;
1092 if (value == Py_None)
Tim Peterscf615b52003-04-19 18:47:02 +00001093 goto done;
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001094 }
1095 /* If we failed to dig out the 'code' attribute,
1096 just let the else clause below print the error. */
1097 }
1098 if (PyInt_Check(value))
Tim Peterscf615b52003-04-19 18:47:02 +00001099 exitcode = (int)PyInt_AsLong(value);
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001100 else {
1101 PyObject_Print(value, stderr, Py_PRINT_RAW);
1102 PySys_WriteStderr("\n");
Tim Peterscf615b52003-04-19 18:47:02 +00001103 exitcode = 1;
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001104 }
Tim Peterscf615b52003-04-19 18:47:02 +00001105 done:
Neal Norwitz9589ee22006-03-04 19:01:22 +00001106 /* Restore and clear the exception info, in order to properly decref
1107 * the exception, value, and traceback. If we just exit instead,
1108 * these leak, which confuses PYTHONDUMPREFS output, and may prevent
1109 * some finalizers from running.
1110 */
Tim Peterscf615b52003-04-19 18:47:02 +00001111 PyErr_Restore(exception, value, tb);
1112 PyErr_Clear();
1113 Py_Exit(exitcode);
1114 /* NOTREACHED */
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001115}
1116
1117void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001118PyErr_PrintEx(int set_sys_last_vars)
Guido van Rossuma61691e1998-02-06 22:27:24 +00001119{
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001120 PyObject *exception, *v, *tb, *hook;
Guido van Rossum66e8e862001-03-23 17:54:43 +00001121
1122 if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
1123 handle_system_exit();
1124 }
Guido van Rossum82598051997-03-05 00:20:32 +00001125 PyErr_Fetch(&exception, &v, &tb);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001126 if (exception == NULL)
1127 return;
Barry Warsaw035574d1997-08-29 22:07:17 +00001128 PyErr_NormalizeException(&exception, &v, &tb);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001129 if (exception == NULL)
Guido van Rossum296b4751997-05-23 00:19:20 +00001130 return;
Georg Brandl3c0fd562008-07-05 10:07:18 +00001131 /* Now we know v != NULL too */
Guido van Rossuma61691e1998-02-06 22:27:24 +00001132 if (set_sys_last_vars) {
1133 PySys_SetObject("last_type", exception);
1134 PySys_SetObject("last_value", v);
1135 PySys_SetObject("last_traceback", tb);
1136 }
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001137 hook = PySys_GetObject("excepthook");
1138 if (hook) {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001139 PyObject *args = PyTuple_Pack(3,
Guido van Rossum9d785502006-03-07 18:31:44 +00001140 exception, v, tb ? tb : Py_None);
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001141 PyObject *result = PyEval_CallObject(hook, args);
1142 if (result == NULL) {
1143 PyObject *exception2, *v2, *tb2;
Guido van Rossum66e8e862001-03-23 17:54:43 +00001144 if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
1145 handle_system_exit();
1146 }
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001147 PyErr_Fetch(&exception2, &v2, &tb2);
1148 PyErr_NormalizeException(&exception2, &v2, &tb2);
Neal Norwitza5e4f222006-07-17 00:59:04 +00001149 /* It should not be possible for exception2 or v2
1150 to be NULL. However PyErr_Display() can't
1151 tolerate NULLs, so just be safe. */
1152 if (exception2 == NULL) {
1153 exception2 = Py_None;
1154 Py_INCREF(exception2);
1155 }
1156 if (v2 == NULL) {
1157 v2 = Py_None;
1158 Py_INCREF(v2);
1159 }
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001160 if (Py_FlushLine())
1161 PyErr_Clear();
1162 fflush(stdout);
1163 PySys_WriteStderr("Error in sys.excepthook:\n");
1164 PyErr_Display(exception2, v2, tb2);
1165 PySys_WriteStderr("\nOriginal exception was:\n");
1166 PyErr_Display(exception, v, tb);
Neal Norwitza5e4f222006-07-17 00:59:04 +00001167 Py_DECREF(exception2);
1168 Py_DECREF(v2);
Jeremy Hylton07028582001-12-07 15:35:35 +00001169 Py_XDECREF(tb2);
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001170 }
1171 Py_XDECREF(result);
1172 Py_XDECREF(args);
1173 } else {
1174 PySys_WriteStderr("sys.excepthook is missing\n");
1175 PyErr_Display(exception, v, tb);
1176 }
1177 Py_XDECREF(exception);
1178 Py_XDECREF(v);
1179 Py_XDECREF(tb);
1180}
1181
Richard Jones7b9558d2006-05-27 12:29:24 +00001182void
1183PyErr_Display(PyObject *exception, PyObject *value, PyObject *tb)
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001184{
1185 int err = 0;
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001186 PyObject *f = PySys_GetObject("stderr");
Armin Rigo5d2c6832004-03-22 20:16:58 +00001187 Py_INCREF(value);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001188 if (f == NULL)
1189 fprintf(stderr, "lost sys.stderr\n");
1190 else {
Guido van Rossum0829c751998-02-28 04:31:39 +00001191 if (Py_FlushLine())
1192 PyErr_Clear();
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001193 fflush(stdout);
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001194 if (tb && tb != Py_None)
1195 err = PyTraceBack_Print(tb, f);
Barry Warsaw36b8f941997-08-26 18:09:48 +00001196 if (err == 0 &&
Armin Rigo5d2c6832004-03-22 20:16:58 +00001197 PyObject_HasAttrString(value, "print_file_and_line"))
Barry Warsaw36b8f941997-08-26 18:09:48 +00001198 {
Guido van Rossum82598051997-03-05 00:20:32 +00001199 PyObject *message;
Martin v. Löwis95292d62002-12-11 14:04:59 +00001200 const char *filename, *text;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001201 int lineno, offset;
Armin Rigo5d2c6832004-03-22 20:16:58 +00001202 if (!parse_syntax_error(value, &message, &filename,
Barry Warsaw035574d1997-08-29 22:07:17 +00001203 &lineno, &offset, &text))
Guido van Rossum82598051997-03-05 00:20:32 +00001204 PyErr_Clear();
Guido van Rossuma110aa61994-08-29 12:50:44 +00001205 else {
1206 char buf[10];
Guido van Rossum82598051997-03-05 00:20:32 +00001207 PyFile_WriteString(" File \"", f);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001208 if (filename == NULL)
Guido van Rossum82598051997-03-05 00:20:32 +00001209 PyFile_WriteString("<string>", f);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001210 else
Guido van Rossum82598051997-03-05 00:20:32 +00001211 PyFile_WriteString(filename, f);
1212 PyFile_WriteString("\", line ", f);
Jeremy Hylton518ab1c2001-11-28 20:42:20 +00001213 PyOS_snprintf(buf, sizeof(buf), "%d", lineno);
Guido van Rossum82598051997-03-05 00:20:32 +00001214 PyFile_WriteString(buf, f);
1215 PyFile_WriteString("\n", f);
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001216 if (text != NULL)
1217 print_error_text(f, offset, text);
Armin Rigo5d2c6832004-03-22 20:16:58 +00001218 Py_DECREF(value);
1219 value = message;
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001220 /* Can't be bothered to check all those
1221 PyFile_WriteString() calls */
1222 if (PyErr_Occurred())
1223 err = -1;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001224 }
1225 }
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001226 if (err) {
1227 /* Don't do anything else */
1228 }
Brett Cannonbf364092006-03-01 04:25:17 +00001229 else if (PyExceptionClass_Check(exception)) {
Richard Jones7b9558d2006-05-27 12:29:24 +00001230 PyObject* moduleName;
Tim Peters5e9d6cf2006-05-28 10:41:29 +00001231 char* className = PyExceptionClass_Name(exception);
1232 if (className != NULL) {
1233 char *dot = strrchr(className, '.');
1234 if (dot != NULL)
1235 className = dot+1;
1236 }
Barry Warsaw2f5f6a21997-09-16 21:42:03 +00001237
Tim Peters5e9d6cf2006-05-28 10:41:29 +00001238 moduleName = PyObject_GetAttrString(exception, "__module__");
Barry Warsaw2f5f6a21997-09-16 21:42:03 +00001239 if (moduleName == NULL)
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001240 err = PyFile_WriteString("<unknown>", f);
Barry Warsaw2f5f6a21997-09-16 21:42:03 +00001241 else {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001242 char* modstr = PyString_AsString(moduleName);
Tim Petersd08e3822003-04-17 15:24:21 +00001243 if (modstr && strcmp(modstr, "exceptions"))
Barry Warsaw2f5f6a21997-09-16 21:42:03 +00001244 {
1245 err = PyFile_WriteString(modstr, f);
1246 err += PyFile_WriteString(".", f);
1247 }
Richard Jones7b9558d2006-05-27 12:29:24 +00001248 Py_DECREF(moduleName);
Barry Warsaw2f5f6a21997-09-16 21:42:03 +00001249 }
1250 if (err == 0) {
1251 if (className == NULL)
1252 err = PyFile_WriteString("<unknown>", f);
1253 else
Brett Cannonbf364092006-03-01 04:25:17 +00001254 err = PyFile_WriteString(className, f);
Barry Warsaw2f5f6a21997-09-16 21:42:03 +00001255 }
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001256 }
1257 else
1258 err = PyFile_WriteObject(exception, f, Py_PRINT_RAW);
Brett Cannon2e63b732006-03-02 18:34:57 +00001259 if (err == 0 && (value != Py_None)) {
1260 PyObject *s = PyObject_Str(value);
1261 /* only print colon if the str() of the
1262 object is not the empty string
1263 */
1264 if (s == NULL)
1265 err = -1;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001266 else if (!PyString_Check(s) ||
1267 PyString_GET_SIZE(s) != 0)
Brett Cannon2e63b732006-03-02 18:34:57 +00001268 err = PyFile_WriteString(": ", f);
1269 if (err == 0)
1270 err = PyFile_WriteObject(s, f, Py_PRINT_RAW);
1271 Py_XDECREF(s);
Guido van Rossum262e1241995-02-07 15:30:45 +00001272 }
Georg Brandl7b9c5552007-03-12 14:30:05 +00001273 /* try to write a newline in any case */
1274 err += PyFile_WriteString("\n", f);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001275 }
Armin Rigo5d2c6832004-03-22 20:16:58 +00001276 Py_DECREF(value);
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001277 /* If an error happened here, don't show it.
1278 XXX This is wrong, but too many callers rely on this behavior. */
1279 if (err != 0)
1280 PyErr_Clear();
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001281}
1282
Guido van Rossum82598051997-03-05 00:20:32 +00001283PyObject *
Tim Peters5e9d6cf2006-05-28 10:41:29 +00001284PyRun_StringFlags(const char *str, int start, PyObject *globals,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001285 PyObject *locals, PyCompilerFlags *flags)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001286{
Neal Norwitze92fba02006-03-04 18:52:26 +00001287 PyObject *ret = NULL;
Neal Norwitzd12bd012006-07-21 07:59:47 +00001288 mod_ty mod;
Neal Norwitzb59d08c2006-07-22 16:20:49 +00001289 PyArena *arena = PyArena_New();
Neal Norwitzd12bd012006-07-21 07:59:47 +00001290 if (arena == NULL)
1291 return NULL;
1292
1293 mod = PyParser_ASTFromString(str, "<string>", start, flags, arena);
Neal Norwitze92fba02006-03-04 18:52:26 +00001294 if (mod != NULL)
1295 ret = run_mod(mod, "<string>", globals, locals, flags, arena);
Neal Norwitz9589ee22006-03-04 19:01:22 +00001296 PyArena_Free(arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001297 return ret;
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001298}
1299
1300PyObject *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001301PyRun_FileExFlags(FILE *fp, const char *filename, int start, PyObject *globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001302 PyObject *locals, int closeit, PyCompilerFlags *flags)
1303{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001304 PyObject *ret;
Neal Norwitzd12bd012006-07-21 07:59:47 +00001305 mod_ty mod;
Neal Norwitzb59d08c2006-07-22 16:20:49 +00001306 PyArena *arena = PyArena_New();
Neal Norwitzd12bd012006-07-21 07:59:47 +00001307 if (arena == NULL)
1308 return NULL;
1309
1310 mod = PyParser_ASTFromFile(fp, filename, start, 0, 0,
1311 flags, NULL, arena);
Georg Brandl098cd692007-03-06 12:17:50 +00001312 if (closeit)
1313 fclose(fp);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001314 if (mod == NULL) {
Neal Norwitz9589ee22006-03-04 19:01:22 +00001315 PyArena_Free(arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001316 return NULL;
Neal Norwitz9589ee22006-03-04 19:01:22 +00001317 }
Neal Norwitze92fba02006-03-04 18:52:26 +00001318 ret = run_mod(mod, filename, globals, locals, flags, arena);
Neal Norwitz9589ee22006-03-04 19:01:22 +00001319 PyArena_Free(arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001320 return ret;
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001321}
1322
Guido van Rossum82598051997-03-05 00:20:32 +00001323static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001324run_mod(mod_ty mod, const char *filename, PyObject *globals, PyObject *locals,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001325 PyCompilerFlags *flags, PyArena *arena)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001326{
Guido van Rossum82598051997-03-05 00:20:32 +00001327 PyCodeObject *co;
1328 PyObject *v;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001329 co = PyAST_Compile(mod, filename, flags, arena);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001330 if (co == NULL)
1331 return NULL;
Guido van Rossum82598051997-03-05 00:20:32 +00001332 v = PyEval_EvalCode(co, globals, locals);
1333 Py_DECREF(co);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001334 return v;
1335}
1336
Guido van Rossum82598051997-03-05 00:20:32 +00001337static PyObject *
Tim Peters5e9d6cf2006-05-28 10:41:29 +00001338run_pyc_file(FILE *fp, const char *filename, PyObject *globals,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001339 PyObject *locals, PyCompilerFlags *flags)
Guido van Rossumfdef2711994-09-14 13:31:04 +00001340{
Guido van Rossum82598051997-03-05 00:20:32 +00001341 PyCodeObject *co;
1342 PyObject *v;
Guido van Rossumfdef2711994-09-14 13:31:04 +00001343 long magic;
Fred Drakee8de31c2000-08-31 05:38:39 +00001344 long PyImport_GetMagicNumber(void);
Guido van Rossumfdef2711994-09-14 13:31:04 +00001345
Guido van Rossum82598051997-03-05 00:20:32 +00001346 magic = PyMarshal_ReadLongFromFile(fp);
1347 if (magic != PyImport_GetMagicNumber()) {
1348 PyErr_SetString(PyExc_RuntimeError,
Guido van Rossumfdef2711994-09-14 13:31:04 +00001349 "Bad magic number in .pyc file");
1350 return NULL;
1351 }
Guido van Rossum82598051997-03-05 00:20:32 +00001352 (void) PyMarshal_ReadLongFromFile(fp);
Tim Petersd9b9ac82001-01-28 00:27:39 +00001353 v = PyMarshal_ReadLastObjectFromFile(fp);
Guido van Rossumfdef2711994-09-14 13:31:04 +00001354 fclose(fp);
Guido van Rossum82598051997-03-05 00:20:32 +00001355 if (v == NULL || !PyCode_Check(v)) {
1356 Py_XDECREF(v);
1357 PyErr_SetString(PyExc_RuntimeError,
Guido van Rossumfdef2711994-09-14 13:31:04 +00001358 "Bad code object in .pyc file");
1359 return NULL;
1360 }
Guido van Rossum82598051997-03-05 00:20:32 +00001361 co = (PyCodeObject *)v;
1362 v = PyEval_EvalCode(co, globals, locals);
Jeremy Hyltonb857ba22001-08-10 21:41:33 +00001363 if (v && flags)
1364 flags->cf_flags |= (co->co_flags & PyCF_MASK);
Guido van Rossum82598051997-03-05 00:20:32 +00001365 Py_DECREF(co);
Guido van Rossumfdef2711994-09-14 13:31:04 +00001366 return v;
1367}
1368
Guido van Rossum82598051997-03-05 00:20:32 +00001369PyObject *
Tim Petersd08e3822003-04-17 15:24:21 +00001370Py_CompileStringFlags(const char *str, const char *filename, int start,
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001371 PyCompilerFlags *flags)
1372{
Guido van Rossum82598051997-03-05 00:20:32 +00001373 PyCodeObject *co;
Neal Norwitzb59d08c2006-07-22 16:20:49 +00001374 mod_ty mod;
Neal Norwitz9589ee22006-03-04 19:01:22 +00001375 PyArena *arena = PyArena_New();
Neal Norwitzb59d08c2006-07-22 16:20:49 +00001376 if (arena == NULL)
1377 return NULL;
1378
1379 mod = PyParser_ASTFromString(str, filename, start, flags, arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001380 if (mod == NULL) {
Neal Norwitz9589ee22006-03-04 19:01:22 +00001381 PyArena_Free(arena);
Guido van Rossum5b722181993-03-30 17:46:03 +00001382 return NULL;
Neal Norwitz9589ee22006-03-04 19:01:22 +00001383 }
Martin v. Löwis2b366e42006-02-26 22:12:35 +00001384 if (flags && (flags->cf_flags & PyCF_ONLY_AST)) {
Martin v. Löwisbd260da2006-02-26 19:42:26 +00001385 PyObject *result = PyAST_mod2obj(mod);
1386 PyArena_Free(arena);
1387 return result;
1388 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001389 co = PyAST_Compile(mod, filename, flags, arena);
Neal Norwitz9589ee22006-03-04 19:01:22 +00001390 PyArena_Free(arena);
Guido van Rossum82598051997-03-05 00:20:32 +00001391 return (PyObject *)co;
Guido van Rossum5b722181993-03-30 17:46:03 +00001392}
1393
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001394struct symtable *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001395Py_SymtableString(const char *str, const char *filename, int start)
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001396{
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001397 struct symtable *st;
Neal Norwitzb59d08c2006-07-22 16:20:49 +00001398 mod_ty mod;
Christian Heimes3c608332008-03-26 22:01:37 +00001399 PyCompilerFlags flags;
Neal Norwitz9589ee22006-03-04 19:01:22 +00001400 PyArena *arena = PyArena_New();
Neal Norwitzb59d08c2006-07-22 16:20:49 +00001401 if (arena == NULL)
1402 return NULL;
1403
Christian Heimes7f23d862008-03-26 22:51:58 +00001404 flags.cf_flags = 0;
1405
Christian Heimes3c608332008-03-26 22:01:37 +00001406 mod = PyParser_ASTFromString(str, filename, start, &flags, arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001407 if (mod == NULL) {
Neal Norwitz9589ee22006-03-04 19:01:22 +00001408 PyArena_Free(arena);
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001409 return NULL;
Neal Norwitz9589ee22006-03-04 19:01:22 +00001410 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001411 st = PySymtable_Build(mod, filename, 0);
Neal Norwitz9589ee22006-03-04 19:01:22 +00001412 PyArena_Free(arena);
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001413 return st;
1414}
1415
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001416/* Preferred access to parser is through AST. */
1417mod_ty
Tim Peters5e9d6cf2006-05-28 10:41:29 +00001418PyParser_ASTFromString(const char *s, const char *filename, int start,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001419 PyCompilerFlags *flags, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001420{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001421 mod_ty mod;
Benjamin Petersondcee09d2008-10-31 02:16:05 +00001422 PyCompilerFlags localflags;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001423 perrdetail err;
Amaury Forgeot d'Arcdf70e052008-03-26 23:07:43 +00001424 int iflags = PARSER_FLAGS(flags);
Christian Heimes3c608332008-03-26 22:01:37 +00001425
1426 node *n = PyParser_ParseStringFlagsFilenameEx(s, filename,
Tim Peters5e9d6cf2006-05-28 10:41:29 +00001427 &_PyParser_Grammar, start, &err,
Christian Heimes3c608332008-03-26 22:01:37 +00001428 &iflags);
Benjamin Petersondcee09d2008-10-31 02:16:05 +00001429 if (flags == NULL) {
1430 localflags.cf_flags = 0;
1431 flags = &localflags;
1432 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001433 if (n) {
Benjamin Petersondcee09d2008-10-31 02:16:05 +00001434 flags->cf_flags |= iflags & PyCF_MASK;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001435 mod = PyAST_FromNode(n, flags, filename, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001436 PyNode_Free(n);
1437 return mod;
1438 }
1439 else {
1440 err_input(&err);
1441 return NULL;
1442 }
1443}
1444
1445mod_ty
Tim Peters5e9d6cf2006-05-28 10:41:29 +00001446PyParser_ASTFromFile(FILE *fp, const char *filename, int start, char *ps1,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001447 char *ps2, PyCompilerFlags *flags, int *errcode,
Neal Norwitz9589ee22006-03-04 19:01:22 +00001448 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001449{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001450 mod_ty mod;
Benjamin Petersondcee09d2008-10-31 02:16:05 +00001451 PyCompilerFlags localflags;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001452 perrdetail err;
Amaury Forgeot d'Arcdf70e052008-03-26 23:07:43 +00001453 int iflags = PARSER_FLAGS(flags);
Christian Heimes3c608332008-03-26 22:01:37 +00001454
Christian Heimes3c608332008-03-26 22:01:37 +00001455 node *n = PyParser_ParseFileFlagsEx(fp, filename, &_PyParser_Grammar,
1456 start, ps1, ps2, &err, &iflags);
Benjamin Petersondcee09d2008-10-31 02:16:05 +00001457 if (flags == NULL) {
1458 localflags.cf_flags = 0;
1459 flags = &localflags;
1460 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001461 if (n) {
Benjamin Petersondcee09d2008-10-31 02:16:05 +00001462 flags->cf_flags |= iflags & PyCF_MASK;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001463 mod = PyAST_FromNode(n, flags, filename, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001464 PyNode_Free(n);
1465 return mod;
1466 }
1467 else {
1468 err_input(&err);
1469 if (errcode)
1470 *errcode = err.error;
1471 return NULL;
1472 }
1473}
1474
Guido van Rossuma110aa61994-08-29 12:50:44 +00001475/* Simplified interface to parsefile -- return node or set exception */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001476
Guido van Rossuma110aa61994-08-29 12:50:44 +00001477node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001478PyParser_SimpleParseFileFlags(FILE *fp, const char *filename, int start, int flags)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001479{
Guido van Rossuma110aa61994-08-29 12:50:44 +00001480 perrdetail err;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001481 node *n = PyParser_ParseFileFlags(fp, filename, &_PyParser_Grammar,
1482 start, NULL, NULL, &err, flags);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001483 if (n == NULL)
1484 err_input(&err);
Tim Peters5e9d6cf2006-05-28 10:41:29 +00001485
Guido van Rossuma110aa61994-08-29 12:50:44 +00001486 return n;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001487}
1488
Guido van Rossuma110aa61994-08-29 12:50:44 +00001489/* Simplified interface to parsestring -- return node or set exception */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001490
Guido van Rossuma110aa61994-08-29 12:50:44 +00001491node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001492PyParser_SimpleParseStringFlags(const char *str, int start, int flags)
Tim Petersfe2127d2001-07-16 05:37:24 +00001493{
Tim Petersfe2127d2001-07-16 05:37:24 +00001494 perrdetail err;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001495 node *n = PyParser_ParseStringFlags(str, &_PyParser_Grammar,
1496 start, &err, flags);
Tim Petersfe2127d2001-07-16 05:37:24 +00001497 if (n == NULL)
1498 err_input(&err);
1499 return n;
1500}
1501
1502node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001503PyParser_SimpleParseStringFlagsFilename(const char *str, const char *filename,
Thomas Heller6b17abf2002-07-09 09:23:27 +00001504 int start, int flags)
1505{
Thomas Heller6b17abf2002-07-09 09:23:27 +00001506 perrdetail err;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001507 node *n = PyParser_ParseStringFlagsFilename(str, filename,
1508 &_PyParser_Grammar, start, &err, flags);
Thomas Heller6b17abf2002-07-09 09:23:27 +00001509 if (n == NULL)
1510 err_input(&err);
1511 return n;
1512}
1513
1514node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001515PyParser_SimpleParseStringFilename(const char *str, const char *filename, int start)
Thomas Heller6b17abf2002-07-09 09:23:27 +00001516{
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001517 return PyParser_SimpleParseStringFlagsFilename(str, filename, start, 0);
Thomas Heller6b17abf2002-07-09 09:23:27 +00001518}
1519
Guido van Rossum66ebd912003-04-17 16:02:26 +00001520/* May want to move a more generalized form of this to parsetok.c or
1521 even parser modules. */
1522
1523void
1524PyParser_SetError(perrdetail *err)
1525{
1526 err_input(err);
1527}
1528
Guido van Rossuma110aa61994-08-29 12:50:44 +00001529/* Set the error appropriate to the given input error code (see errcode.h) */
1530
1531static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001532err_input(perrdetail *err)
Guido van Rossuma110aa61994-08-29 12:50:44 +00001533{
Fred Drake85f36392000-07-11 17:53:00 +00001534 PyObject *v, *w, *errtype;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001535 PyObject* u = NULL;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001536 char *msg = NULL;
Fred Drake85f36392000-07-11 17:53:00 +00001537 errtype = PyExc_SyntaxError;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001538 switch (err->error) {
1539 case E_SYNTAX:
Fred Drake85f36392000-07-11 17:53:00 +00001540 errtype = PyExc_IndentationError;
1541 if (err->expected == INDENT)
1542 msg = "expected an indented block";
1543 else if (err->token == INDENT)
1544 msg = "unexpected indent";
1545 else if (err->token == DEDENT)
1546 msg = "unexpected unindent";
1547 else {
1548 errtype = PyExc_SyntaxError;
1549 msg = "invalid syntax";
1550 }
Guido van Rossuma110aa61994-08-29 12:50:44 +00001551 break;
1552 case E_TOKEN:
1553 msg = "invalid token";
Guido van Rossuma110aa61994-08-29 12:50:44 +00001554 break;
Skip Montanaro118ec702002-08-15 01:20:16 +00001555 case E_EOFS:
Georg Brandlb52a74b2008-05-11 15:07:39 +00001556 msg = "EOF while scanning triple-quoted string literal";
Skip Montanaro118ec702002-08-15 01:20:16 +00001557 break;
1558 case E_EOLS:
Georg Brandlb52a74b2008-05-11 15:07:39 +00001559 msg = "EOL while scanning string literal";
Skip Montanaro118ec702002-08-15 01:20:16 +00001560 break;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001561 case E_INTR:
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001562 if (!PyErr_Occurred())
1563 PyErr_SetNone(PyExc_KeyboardInterrupt);
Georg Brandl1ad108d2008-07-19 10:08:55 +00001564 goto cleanup;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001565 case E_NOMEM:
Guido van Rossum82598051997-03-05 00:20:32 +00001566 PyErr_NoMemory();
Georg Brandl1ad108d2008-07-19 10:08:55 +00001567 goto cleanup;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001568 case E_EOF:
1569 msg = "unexpected EOF while parsing";
1570 break;
Fred Drake85f36392000-07-11 17:53:00 +00001571 case E_TABSPACE:
1572 errtype = PyExc_TabError;
Guido van Rossum560e8ad1998-04-10 19:43:42 +00001573 msg = "inconsistent use of tabs and spaces in indentation";
1574 break;
Jeremy Hylton94988062000-06-20 19:10:44 +00001575 case E_OVERFLOW:
1576 msg = "expression too long";
1577 break;
Fred Drake85f36392000-07-11 17:53:00 +00001578 case E_DEDENT:
1579 errtype = PyExc_IndentationError;
1580 msg = "unindent does not match any outer indentation level";
1581 break;
1582 case E_TOODEEP:
1583 errtype = PyExc_IndentationError;
1584 msg = "too many levels of indentation";
1585 break;
Martin v. Löwisd35edda2005-08-24 08:39:24 +00001586 case E_DECODE: {
1587 PyObject *type, *value, *tb;
1588 PyErr_Fetch(&type, &value, &tb);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001589 if (value != NULL) {
Martin v. Löwisd35edda2005-08-24 08:39:24 +00001590 u = PyObject_Str(value);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001591 if (u != NULL) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001592 msg = PyString_AsString(u);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001593 }
1594 }
Martin v. Löwisc2632a52004-07-21 05:35:02 +00001595 if (msg == NULL)
1596 msg = "unknown decode error";
Neal Norwitzdb83eb32005-12-18 05:29:30 +00001597 Py_XDECREF(type);
1598 Py_XDECREF(value);
Neal Norwitz40d37812005-10-02 01:48:49 +00001599 Py_XDECREF(tb);
Martin v. Löwisc2632a52004-07-21 05:35:02 +00001600 break;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001601 }
Martin v. Löwis4bf108d2005-03-03 11:45:45 +00001602 case E_LINECONT:
1603 msg = "unexpected character after line continuation character";
1604 break;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001605 default:
1606 fprintf(stderr, "error=%d\n", err->error);
1607 msg = "unknown parsing error";
1608 break;
1609 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001610 v = Py_BuildValue("(ziiz)", err->filename,
1611 err->lineno, err->offset, err->text);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001612 w = NULL;
1613 if (v != NULL)
1614 w = Py_BuildValue("(sO)", msg, v);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001615 Py_XDECREF(u);
Guido van Rossum41318302001-03-23 04:01:07 +00001616 Py_XDECREF(v);
Fred Drake85f36392000-07-11 17:53:00 +00001617 PyErr_SetObject(errtype, w);
Guido van Rossum82598051997-03-05 00:20:32 +00001618 Py_XDECREF(w);
Georg Brandl1ad108d2008-07-19 10:08:55 +00001619cleanup:
1620 if (err->text != NULL) {
1621 PyObject_FREE(err->text);
1622 err->text = NULL;
1623 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001624}
1625
1626/* Print fatal error message and abort */
1627
1628void
Tim Peters7c321a82002-07-09 02:57:01 +00001629Py_FatalError(const char *msg)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001630{
Guido van Rossum83dd6c31994-09-29 09:38:33 +00001631 fprintf(stderr, "Fatal Python error: %s\n", msg);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001632#ifdef MS_WINDOWS
Martin v. Löwis5344c992009-01-02 20:32:55 +00001633 {
1634 size_t len = strlen(msg);
1635 WCHAR* buffer;
1636 size_t i;
1637
1638 /* Convert the message to wchar_t. This uses a simple one-to-one
1639 conversion, assuming that the this error message actually uses ASCII
1640 only. If this ceases to be true, we will have to convert. */
1641 buffer = alloca( (len+1) * (sizeof *buffer));
1642 for( i=0; i<=len; ++i)
1643 buffer[i] = msg[i];
1644 OutputDebugStringW(L"Fatal Python error: ");
1645 OutputDebugStringW(buffer);
1646 OutputDebugStringW(L"\n");
1647 }
Guido van Rossum0ba35361998-08-13 13:33:16 +00001648#ifdef _DEBUG
1649 DebugBreak();
Guido van Rossuma44823b1995-03-14 15:01:17 +00001650#endif
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001651#endif /* MS_WINDOWS */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001652 abort();
1653}
1654
1655/* Clean up and exit */
1656
Guido van Rossuma110aa61994-08-29 12:50:44 +00001657#ifdef WITH_THREAD
Guido van Rossum49b56061998-10-01 20:42:43 +00001658#include "pythread.h"
Guido van Rossumf9f2e821992-08-17 08:59:08 +00001659#endif
1660
Guido van Rossum2dcfc961998-10-01 16:01:57 +00001661#define NEXITFUNCS 32
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001662static void (*exitfuncs[NEXITFUNCS])(void);
Guido van Rossum1662dd51994-09-07 14:38:28 +00001663static int nexitfuncs = 0;
1664
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001665int Py_AtExit(void (*func)(void))
Guido van Rossum1662dd51994-09-07 14:38:28 +00001666{
1667 if (nexitfuncs >= NEXITFUNCS)
1668 return -1;
1669 exitfuncs[nexitfuncs++] = func;
1670 return 0;
1671}
1672
Guido van Rossumcc283f51997-08-05 02:22:03 +00001673static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001674call_sys_exitfunc(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001675{
Guido van Rossum82598051997-03-05 00:20:32 +00001676 PyObject *exitfunc = PySys_GetObject("exitfunc");
Guido van Rossum59bff391992-09-03 20:28:00 +00001677
1678 if (exitfunc) {
Fred Drake6a12d8d2001-03-23 17:34:02 +00001679 PyObject *res;
Guido van Rossum82598051997-03-05 00:20:32 +00001680 Py_INCREF(exitfunc);
1681 PySys_SetObject("exitfunc", (PyObject *)NULL);
1682 res = PyEval_CallObject(exitfunc, (PyObject *)NULL);
Guido van Rossum59bff391992-09-03 20:28:00 +00001683 if (res == NULL) {
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001684 if (!PyErr_ExceptionMatches(PyExc_SystemExit)) {
1685 PySys_WriteStderr("Error in sys.exitfunc:\n");
1686 }
Guido van Rossum82598051997-03-05 00:20:32 +00001687 PyErr_Print();
Guido van Rossum59bff391992-09-03 20:28:00 +00001688 }
Guido van Rossum82598051997-03-05 00:20:32 +00001689 Py_DECREF(exitfunc);
Guido van Rossum59bff391992-09-03 20:28:00 +00001690 }
1691
Guido van Rossum0829c751998-02-28 04:31:39 +00001692 if (Py_FlushLine())
1693 PyErr_Clear();
Guido van Rossumcc283f51997-08-05 02:22:03 +00001694}
Guido van Rossum1662dd51994-09-07 14:38:28 +00001695
Guido van Rossumcc283f51997-08-05 02:22:03 +00001696static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001697call_ll_exitfuncs(void)
Guido van Rossumcc283f51997-08-05 02:22:03 +00001698{
Guido van Rossum1662dd51994-09-07 14:38:28 +00001699 while (nexitfuncs > 0)
1700 (*exitfuncs[--nexitfuncs])();
Guido van Rossum25ce5661997-08-02 03:10:38 +00001701
1702 fflush(stdout);
1703 fflush(stderr);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001704}
1705
1706void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001707Py_Exit(int sts)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001708{
Guido van Rossumcc283f51997-08-05 02:22:03 +00001709 Py_Finalize();
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001710
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001711 exit(sts);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001712}
1713
Guido van Rossumf1dc5661993-07-05 10:31:29 +00001714static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001715initsigs(void)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001716{
Guido van Rossuma110aa61994-08-29 12:50:44 +00001717#ifdef SIGPIPE
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001718 PyOS_setsig(SIGPIPE, SIG_IGN);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001719#endif
Guido van Rossum70d893a2001-08-16 08:21:42 +00001720#ifdef SIGXFZ
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001721 PyOS_setsig(SIGXFZ, SIG_IGN);
Guido van Rossum70d893a2001-08-16 08:21:42 +00001722#endif
Jeremy Hylton1b0bf9b2002-04-23 20:31:01 +00001723#ifdef SIGXFSZ
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001724 PyOS_setsig(SIGXFSZ, SIG_IGN);
Jeremy Hylton1b0bf9b2002-04-23 20:31:01 +00001725#endif
Guido van Rossum82598051997-03-05 00:20:32 +00001726 PyOS_InitInterrupts(); /* May imply initsignal() */
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001727}
1728
Guido van Rossum7433b121997-02-14 19:45:36 +00001729
1730/*
1731 * The file descriptor fd is considered ``interactive'' if either
1732 * a) isatty(fd) is TRUE, or
1733 * b) the -i flag was given, and the filename associated with
1734 * the descriptor is NULL or "<stdin>" or "???".
1735 */
1736int
Martin v. Löwis95292d62002-12-11 14:04:59 +00001737Py_FdIsInteractive(FILE *fp, const char *filename)
Guido van Rossum7433b121997-02-14 19:45:36 +00001738{
1739 if (isatty((int)fileno(fp)))
1740 return 1;
1741 if (!Py_InteractiveFlag)
1742 return 0;
1743 return (filename == NULL) ||
1744 (strcmp(filename, "<stdin>") == 0) ||
1745 (strcmp(filename, "???") == 0);
1746}
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001747
1748
Tim Petersd08e3822003-04-17 15:24:21 +00001749#if defined(USE_STACKCHECK)
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001750#if defined(WIN32) && defined(_MSC_VER)
1751
1752/* Stack checking for Microsoft C */
1753
1754#include <malloc.h>
1755#include <excpt.h>
1756
Fred Drakee8de31c2000-08-31 05:38:39 +00001757/*
1758 * Return non-zero when we run out of memory on the stack; zero otherwise.
1759 */
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001760int
Fred Drake399739f2000-08-31 05:52:44 +00001761PyOS_CheckStack(void)
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001762{
1763 __try {
Tim Peters92e4dd82002-10-05 01:47:34 +00001764 /* alloca throws a stack overflow exception if there's
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001765 not enough space left on the stack */
Tim Peters92e4dd82002-10-05 01:47:34 +00001766 alloca(PYOS_STACK_MARGIN * sizeof(void*));
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001767 return 0;
Kristján Valur Jónsson52999352008-02-18 17:40:47 +00001768 } __except (GetExceptionCode() == STATUS_STACK_OVERFLOW ?
1769 EXCEPTION_EXECUTE_HANDLER :
1770 EXCEPTION_CONTINUE_SEARCH) {
1771 int errcode = _resetstkoflw();
Amaury Forgeot d'Arcdc619012008-11-22 20:01:18 +00001772 if (errcode == 0)
Kristján Valur Jónsson52999352008-02-18 17:40:47 +00001773 {
1774 Py_FatalError("Could not reset the stack!");
1775 }
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001776 }
1777 return 1;
1778}
1779
1780#endif /* WIN32 && _MSC_VER */
1781
1782/* Alternate implementations can be added here... */
1783
1784#endif /* USE_STACKCHECK */
Guido van Rossum6f256182000-09-16 16:32:19 +00001785
1786
1787/* Wrappers around sigaction() or signal(). */
1788
1789PyOS_sighandler_t
1790PyOS_getsig(int sig)
1791{
1792#ifdef HAVE_SIGACTION
1793 struct sigaction context;
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001794 if (sigaction(sig, NULL, &context) == -1)
1795 return SIG_ERR;
Guido van Rossum6f256182000-09-16 16:32:19 +00001796 return context.sa_handler;
1797#else
1798 PyOS_sighandler_t handler;
Martin v. Löwisb45b3152005-11-28 17:34:23 +00001799/* Special signal handling for the secure CRT in Visual Studio 2005 */
1800#if defined(_MSC_VER) && _MSC_VER >= 1400
1801 switch (sig) {
1802 /* Only these signals are valid */
1803 case SIGINT:
1804 case SIGILL:
1805 case SIGFPE:
1806 case SIGSEGV:
1807 case SIGTERM:
1808 case SIGBREAK:
1809 case SIGABRT:
1810 break;
1811 /* Don't call signal() with other values or it will assert */
1812 default:
1813 return SIG_ERR;
1814 }
1815#endif /* _MSC_VER && _MSC_VER >= 1400 */
Guido van Rossum6f256182000-09-16 16:32:19 +00001816 handler = signal(sig, SIG_IGN);
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001817 if (handler != SIG_ERR)
1818 signal(sig, handler);
Guido van Rossum6f256182000-09-16 16:32:19 +00001819 return handler;
1820#endif
1821}
1822
1823PyOS_sighandler_t
1824PyOS_setsig(int sig, PyOS_sighandler_t handler)
1825{
1826#ifdef HAVE_SIGACTION
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001827 struct sigaction context, ocontext;
Guido van Rossum6f256182000-09-16 16:32:19 +00001828 context.sa_handler = handler;
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001829 sigemptyset(&context.sa_mask);
1830 context.sa_flags = 0;
1831 if (sigaction(sig, &context, &ocontext) == -1)
1832 return SIG_ERR;
1833 return ocontext.sa_handler;
Guido van Rossum6f256182000-09-16 16:32:19 +00001834#else
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001835 PyOS_sighandler_t oldhandler;
1836 oldhandler = signal(sig, handler);
1837#ifdef HAVE_SIGINTERRUPT
1838 siginterrupt(sig, 1);
1839#endif
1840 return oldhandler;
Guido van Rossum6f256182000-09-16 16:32:19 +00001841#endif
1842}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001843
1844/* Deprecated C API functions still provided for binary compatiblity */
1845
1846#undef PyParser_SimpleParseFile
Thomas Heller1b046642006-04-18 18:51:06 +00001847PyAPI_FUNC(node *)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001848PyParser_SimpleParseFile(FILE *fp, const char *filename, int start)
1849{
1850 return PyParser_SimpleParseFileFlags(fp, filename, start, 0);
1851}
1852
Thomas Heller1b046642006-04-18 18:51:06 +00001853#undef PyParser_SimpleParseString
1854PyAPI_FUNC(node *)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001855PyParser_SimpleParseString(const char *str, int start)
1856{
1857 return PyParser_SimpleParseStringFlags(str, start, 0);
1858}
Anthony Baxterac6bd462006-04-13 02:06:09 +00001859
Thomas Heller1b046642006-04-18 18:51:06 +00001860#undef PyRun_AnyFile
1861PyAPI_FUNC(int)
1862PyRun_AnyFile(FILE *fp, const char *name)
1863{
1864 return PyRun_AnyFileExFlags(fp, name, 0, NULL);
1865}
1866
1867#undef PyRun_AnyFileEx
1868PyAPI_FUNC(int)
1869PyRun_AnyFileEx(FILE *fp, const char *name, int closeit)
1870{
1871 return PyRun_AnyFileExFlags(fp, name, closeit, NULL);
1872}
1873
1874#undef PyRun_AnyFileFlags
1875PyAPI_FUNC(int)
1876PyRun_AnyFileFlags(FILE *fp, const char *name, PyCompilerFlags *flags)
1877{
1878 return PyRun_AnyFileExFlags(fp, name, 0, flags);
1879}
1880
1881#undef PyRun_File
1882PyAPI_FUNC(PyObject *)
1883PyRun_File(FILE *fp, const char *p, int s, PyObject *g, PyObject *l)
1884{
Georg Brandl3c0fd562008-07-05 10:07:18 +00001885 return PyRun_FileExFlags(fp, p, s, g, l, 0, NULL);
Thomas Heller1b046642006-04-18 18:51:06 +00001886}
1887
1888#undef PyRun_FileEx
1889PyAPI_FUNC(PyObject *)
1890PyRun_FileEx(FILE *fp, const char *p, int s, PyObject *g, PyObject *l, int c)
1891{
Georg Brandl3c0fd562008-07-05 10:07:18 +00001892 return PyRun_FileExFlags(fp, p, s, g, l, c, NULL);
Thomas Heller1b046642006-04-18 18:51:06 +00001893}
1894
1895#undef PyRun_FileFlags
1896PyAPI_FUNC(PyObject *)
1897PyRun_FileFlags(FILE *fp, const char *p, int s, PyObject *g, PyObject *l,
1898 PyCompilerFlags *flags)
1899{
Georg Brandl3c0fd562008-07-05 10:07:18 +00001900 return PyRun_FileExFlags(fp, p, s, g, l, 0, flags);
Thomas Heller1b046642006-04-18 18:51:06 +00001901}
1902
1903#undef PyRun_SimpleFile
1904PyAPI_FUNC(int)
1905PyRun_SimpleFile(FILE *f, const char *p)
1906{
1907 return PyRun_SimpleFileExFlags(f, p, 0, NULL);
1908}
1909
1910#undef PyRun_SimpleFileEx
1911PyAPI_FUNC(int)
1912PyRun_SimpleFileEx(FILE *f, const char *p, int c)
1913{
1914 return PyRun_SimpleFileExFlags(f, p, c, NULL);
1915}
1916
1917
1918#undef PyRun_String
1919PyAPI_FUNC(PyObject *)
1920PyRun_String(const char *str, int s, PyObject *g, PyObject *l)
1921{
1922 return PyRun_StringFlags(str, s, g, l, NULL);
1923}
1924
1925#undef PyRun_SimpleString
1926PyAPI_FUNC(int)
1927PyRun_SimpleString(const char *s)
1928{
1929 return PyRun_SimpleStringFlags(s, NULL);
1930}
1931
1932#undef Py_CompileString
1933PyAPI_FUNC(PyObject *)
1934Py_CompileString(const char *str, const char *p, int s)
1935{
1936 return Py_CompileStringFlags(str, p, s, NULL);
1937}
1938
1939#undef PyRun_InteractiveOne
1940PyAPI_FUNC(int)
1941PyRun_InteractiveOne(FILE *f, const char *p)
1942{
1943 return PyRun_InteractiveOneFlags(f, p, NULL);
1944}
1945
1946#undef PyRun_InteractiveLoop
1947PyAPI_FUNC(int)
1948PyRun_InteractiveLoop(FILE *f, const char *p)
1949{
1950 return PyRun_InteractiveLoopFlags(f, p, NULL);
1951}
1952
Anthony Baxterac6bd462006-04-13 02:06:09 +00001953#ifdef __cplusplus
1954}
1955#endif
1956