blob: eedb562b3a6b6aae55cc5c6a477e6ed5ed5516d3 [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
6#include "grammar.h"
7#include "node.h"
Fred Drake85f36392000-07-11 17:53:00 +00008#include "token.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +00009#include "parsetok.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +000010#include "errcode.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +000011#include "compile.h"
Jeremy Hylton4b38da62001-02-02 18:19:15 +000012#include "symtable.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +000013#include "eval.h"
Guido van Rossumfdef2711994-09-14 13:31:04 +000014#include "marshal.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +000015
Guido van Rossuma110aa61994-08-29 12:50:44 +000016#ifdef HAVE_SIGNAL_H
Guido van Rossuma9e7dc11992-10-18 18:53:57 +000017#include <signal.h>
Guido van Rossuma9e7dc11992-10-18 18:53:57 +000018#endif
19
Martin v. Löwis73d538b2003-03-05 15:13:47 +000020#ifdef HAVE_LANGINFO_H
21#include <locale.h>
22#include <langinfo.h>
23#endif
24
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000025#ifdef MS_WINDOWS
Guido van Rossuma44823b1995-03-14 15:01:17 +000026#undef BYTE
27#include "windows.h"
28#endif
29
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000030extern char *Py_GetPath(void);
Guido van Rossum1984f1e1992-08-04 12:41:02 +000031
Guido van Rossum82598051997-03-05 00:20:32 +000032extern grammar _PyParser_Grammar; /* From graminit.c */
Guido van Rossum1984f1e1992-08-04 12:41:02 +000033
Guido van Rossumb73cc041993-11-01 16:28:59 +000034/* Forward */
Tim Petersdbd9ba62000-07-09 03:09:57 +000035static void initmain(void);
36static void initsite(void);
Martin v. Löwis95292d62002-12-11 14:04:59 +000037static PyObject *run_err_node(node *, const char *, PyObject *, PyObject *,
Jeremy Hylton9f324e92001-03-01 22:59:14 +000038 PyCompilerFlags *);
Martin v. Löwis95292d62002-12-11 14:04:59 +000039static PyObject *run_node(node *, const char *, PyObject *, PyObject *,
Jeremy Hylton9f324e92001-03-01 22:59:14 +000040 PyCompilerFlags *);
Martin v. Löwis95292d62002-12-11 14:04:59 +000041static PyObject *run_pyc_file(FILE *, const char *, PyObject *, PyObject *,
Jeremy Hyltonbc320242001-03-22 02:47:58 +000042 PyCompilerFlags *);
Tim Petersdbd9ba62000-07-09 03:09:57 +000043static void err_input(perrdetail *);
44static void initsigs(void);
45static void call_sys_exitfunc(void);
46static void call_ll_exitfuncs(void);
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000047extern void _PyUnicode_Init(void);
48extern void _PyUnicode_Fini(void);
Guido van Rossumc94044c2000-03-10 23:03:54 +000049
Mark Hammond8d98d2c2003-04-19 15:41:53 +000050#ifdef WITH_THREAD
51extern void _PyGILState_Init(PyInterpreterState *, PyThreadState *);
52extern void _PyGILState_Fini(void);
53#endif /* WITH_THREAD */
54
Guido van Rossum82598051997-03-05 00:20:32 +000055int Py_DebugFlag; /* Needed by parser.c */
56int Py_VerboseFlag; /* Needed by import.c */
Guido van Rossum7433b121997-02-14 19:45:36 +000057int Py_InteractiveFlag; /* Needed by Py_FdIsInteractive() below */
Guido van Rossumdcc0c131997-08-29 22:32:42 +000058int Py_NoSiteFlag; /* Suppress 'import site' */
Barry Warsaw3ce09642000-05-02 19:18:59 +000059int Py_UseClassExceptionsFlag = 1; /* Needed by bltinmodule.c: deprecated */
Guido van Rossuma61691e1998-02-06 22:27:24 +000060int Py_FrozenFlag; /* Needed by getpath.c */
Guido van Rossumb16d1972000-05-01 17:55:15 +000061int Py_UnicodeFlag = 0; /* Needed by compile.c */
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +000062int Py_IgnoreEnvironmentFlag; /* e.g. PYTHONPATH, PYTHONHOME */
Tim Peters3caca232001-12-06 06:23:26 +000063/* _XXX Py_QnewFlag should go away in 2.3. It's true iff -Qnew is passed,
64 on the command line, and is used in 2.2 by ceval.c to make all "/" divisions
65 true divisions (which they will be in 2.3). */
66int _Py_QnewFlag = 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +000067
Mark Hammonda43fd0c2003-02-19 00:33:33 +000068/* Reference to 'warnings' module, to avoid importing it
Mark Hammondedd07732003-07-15 23:03:55 +000069 on the fly when the import lock may be held. See 683658/771097
Mark Hammonda43fd0c2003-02-19 00:33:33 +000070*/
Mark Hammondedd07732003-07-15 23:03:55 +000071static PyObject *warnings_module = NULL;
72
73/* Returns a borrowed reference to the 'warnings' module, or NULL.
74 If the module is returned, it is guaranteed to have been obtained
75 without acquiring the import lock
76*/
Martin v. Löwisa2c17c52003-08-09 09:47:11 +000077PyObject *PyModule_GetWarningsModule(void)
Mark Hammondedd07732003-07-15 23:03:55 +000078{
79 PyObject *typ, *val, *tb;
80 PyObject *all_modules;
81 /* If we managed to get the module at init time, just use it */
82 if (warnings_module)
83 return warnings_module;
84 /* If it wasn't available at init time, it may be available
85 now in sys.modules (common scenario is frozen apps: import
86 at init time fails, but the frozen init code sets up sys.path
87 correctly, then does an implicit import of warnings for us
88 */
89 /* Save and restore any exceptions */
90 PyErr_Fetch(&typ, &val, &tb);
91
Mark Hammond5f4e8ca2003-07-16 01:54:38 +000092 all_modules = PySys_GetObject("modules");
Mark Hammondedd07732003-07-15 23:03:55 +000093 if (all_modules) {
94 warnings_module = PyDict_GetItemString(all_modules, "warnings");
95 /* We keep a ref in the global */
96 Py_XINCREF(warnings_module);
97 }
98 PyErr_Restore(typ, val, tb);
99 return warnings_module;
100}
Mark Hammonda43fd0c2003-02-19 00:33:33 +0000101
Guido van Rossum25ce5661997-08-02 03:10:38 +0000102static int initialized = 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000103
Thomas Wouters7e474022000-07-16 12:04:32 +0000104/* API to access the initialized flag -- useful for esoteric use */
Guido van Rossume3c0d5e1997-08-22 04:20:13 +0000105
106int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000107Py_IsInitialized(void)
Guido van Rossume3c0d5e1997-08-22 04:20:13 +0000108{
109 return initialized;
110}
111
Guido van Rossum25ce5661997-08-02 03:10:38 +0000112/* Global initializations. Can be undone by Py_Finalize(). Don't
113 call this twice without an intervening Py_Finalize() call. When
114 initializations fail, a fatal error is issued and the function does
115 not return. On return, the first thread and interpreter state have
116 been created.
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000117
Guido van Rossum25ce5661997-08-02 03:10:38 +0000118 Locking: you must hold the interpreter lock while calling this.
119 (If the lock has not yet been initialized, that's equivalent to
120 having the lock, but you cannot use multiple threads.)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000121
Guido van Rossum25ce5661997-08-02 03:10:38 +0000122*/
Guido van Rossuma027efa1997-05-05 20:56:21 +0000123
Guido van Rossum9abaf4d2001-10-12 22:17:56 +0000124static int
125add_flag(int flag, const char *envs)
126{
127 int env = atoi(envs);
128 if (flag < env)
129 flag = env;
130 if (flag < 1)
131 flag = 1;
132 return flag;
133}
134
Guido van Rossuma027efa1997-05-05 20:56:21 +0000135void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000136Py_Initialize(void)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000137{
Guido van Rossuma027efa1997-05-05 20:56:21 +0000138 PyInterpreterState *interp;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000139 PyThreadState *tstate;
140 PyObject *bimod, *sysmod;
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000141 char *p;
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000142#if defined(Py_USING_UNICODE) && defined(HAVE_LANGINFO_H) && defined(CODESET)
143 char *codeset;
144 char *saved_locale;
145 PyObject *sys_stream, *sys_isatty;
146#endif
Guido van Rossum70d893a2001-08-16 08:21:42 +0000147 extern void _Py_ReadyTypes(void);
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000148
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000149 if (initialized)
Guido van Rossumaa615051997-08-20 22:40:18 +0000150 return;
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000151 initialized = 1;
Tim Petersd08e3822003-04-17 15:24:21 +0000152
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000153 if ((p = Py_GETENV("PYTHONDEBUG")) && *p != '\0')
Guido van Rossum9abaf4d2001-10-12 22:17:56 +0000154 Py_DebugFlag = add_flag(Py_DebugFlag, p);
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000155 if ((p = Py_GETENV("PYTHONVERBOSE")) && *p != '\0')
Guido van Rossum9abaf4d2001-10-12 22:17:56 +0000156 Py_VerboseFlag = add_flag(Py_VerboseFlag, p);
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000157 if ((p = Py_GETENV("PYTHONOPTIMIZE")) && *p != '\0')
Guido van Rossum9abaf4d2001-10-12 22:17:56 +0000158 Py_OptimizeFlag = add_flag(Py_OptimizeFlag, p);
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000159
Guido van Rossuma027efa1997-05-05 20:56:21 +0000160 interp = PyInterpreterState_New();
161 if (interp == NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000162 Py_FatalError("Py_Initialize: can't make first interpreter");
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000163
Guido van Rossuma027efa1997-05-05 20:56:21 +0000164 tstate = PyThreadState_New(interp);
165 if (tstate == NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000166 Py_FatalError("Py_Initialize: can't make first thread");
Guido van Rossuma027efa1997-05-05 20:56:21 +0000167 (void) PyThreadState_Swap(tstate);
168
Guido van Rossum70d893a2001-08-16 08:21:42 +0000169 _Py_ReadyTypes();
Guido van Rossum528b7eb2001-08-07 17:24:28 +0000170
Neal Norwitzb2501f42002-12-31 03:42:13 +0000171 if (!_PyFrame_Init())
Neal Norwitzc91ed402002-12-30 22:29:22 +0000172 Py_FatalError("Py_Initialize: can't init frames");
173
Neal Norwitzb2501f42002-12-31 03:42:13 +0000174 if (!_PyInt_Init())
Neal Norwitzc91ed402002-12-30 22:29:22 +0000175 Py_FatalError("Py_Initialize: can't init ints");
176
Guido van Rossum25ce5661997-08-02 03:10:38 +0000177 interp->modules = PyDict_New();
178 if (interp->modules == NULL)
179 Py_FatalError("Py_Initialize: can't make modules dictionary");
Guido van Rossuma027efa1997-05-05 20:56:21 +0000180
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000181#ifdef Py_USING_UNICODE
Guido van Rossumc94044c2000-03-10 23:03:54 +0000182 /* Init Unicode implementation; relies on the codec registry */
183 _PyUnicode_Init();
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000184#endif
Guido van Rossumc94044c2000-03-10 23:03:54 +0000185
Barry Warsawf242aa02000-05-25 23:09:49 +0000186 bimod = _PyBuiltin_Init();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000187 if (bimod == NULL)
188 Py_FatalError("Py_Initialize: can't initialize __builtin__");
Guido van Rossum4a1f39a1997-11-04 19:36:18 +0000189 interp->builtins = PyModule_GetDict(bimod);
190 Py_INCREF(interp->builtins);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000191
192 sysmod = _PySys_Init();
193 if (sysmod == NULL)
194 Py_FatalError("Py_Initialize: can't initialize sys");
195 interp->sysdict = PyModule_GetDict(sysmod);
196 Py_INCREF(interp->sysdict);
197 _PyImport_FixupExtension("sys", "sys");
Guido van Rossuma027efa1997-05-05 20:56:21 +0000198 PySys_SetPath(Py_GetPath());
Guido van Rossum25ce5661997-08-02 03:10:38 +0000199 PyDict_SetItemString(interp->sysdict, "modules",
200 interp->modules);
201
Guido van Rossum7c85ab81999-07-08 17:26:56 +0000202 _PyImport_Init();
203
Barry Warsawf242aa02000-05-25 23:09:49 +0000204 /* initialize builtin exceptions */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000205 _PyExc_Init();
Barry Warsaw5821bc52001-08-13 23:04:56 +0000206 _PyImport_FixupExtension("exceptions", "exceptions");
Barry Warsawf242aa02000-05-25 23:09:49 +0000207
Barry Warsaw035574d1997-08-29 22:07:17 +0000208 /* phase 2 of builtins */
Barry Warsaw963b8711997-09-18 16:42:02 +0000209 _PyImport_FixupExtension("__builtin__", "__builtin__");
Barry Warsaw035574d1997-08-29 22:07:17 +0000210
Just van Rossum52e14d62002-12-30 22:08:05 +0000211 _PyImportHooks_Init();
212
Guido van Rossum25ce5661997-08-02 03:10:38 +0000213 initsigs(); /* Signal handling stuff, including initintr() */
214
215 initmain(); /* Module __main__ */
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000216 if (!Py_NoSiteFlag)
217 initsite(); /* Module site */
Just van Rossum5bfba3a2003-02-25 20:25:12 +0000218
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000219 /* auto-thread-state API, if available */
220#ifdef WITH_THREAD
221 _PyGILState_Init(interp, tstate);
222#endif /* WITH_THREAD */
223
Mark Hammondedd07732003-07-15 23:03:55 +0000224 warnings_module = PyImport_ImportModule("warnings");
225 if (!warnings_module)
226 PyErr_Clear();
Martin v. Löwis73d538b2003-03-05 15:13:47 +0000227
228#if defined(Py_USING_UNICODE) && defined(HAVE_LANGINFO_H) && defined(CODESET)
229 /* On Unix, set the file system encoding according to the
230 user's preference, if the CODESET names a well-known
231 Python codec, and Py_FileSystemDefaultEncoding isn't
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000232 initialized by other means. Also set the encoding of
233 stdin and stdout if these are terminals. */
234
Martin v. Löwisf56d0152003-11-13 07:43:21 +0000235 saved_locale = strdup(setlocale(LC_CTYPE, NULL));
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000236 setlocale(LC_CTYPE, "");
237 codeset = nl_langinfo(CODESET);
238 if (codeset && *codeset) {
239 PyObject *enc = PyCodec_Encoder(codeset);
240 if (enc) {
241 codeset = strdup(codeset);
242 Py_DECREF(enc);
243 } else {
244 codeset = NULL;
245 PyErr_Clear();
Martin v. Löwis73d538b2003-03-05 15:13:47 +0000246 }
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000247 } else
248 codeset = NULL;
249 setlocale(LC_CTYPE, saved_locale);
Martin v. Löwisf56d0152003-11-13 07:43:21 +0000250 free(saved_locale);
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000251
252 if (codeset) {
Michael W. Hudson68debc92003-08-11 12:20:24 +0000253 sys_stream = PySys_GetObject("stdin");
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000254 sys_isatty = PyObject_CallMethod(sys_stream, "isatty", "");
255 if (!sys_isatty)
256 PyErr_Clear();
257 if(sys_isatty && PyObject_IsTrue(sys_isatty)) {
258 if (!PyFile_SetEncoding(sys_stream, codeset))
259 Py_FatalError("Cannot set codeset of stdin");
260 }
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000261 Py_XDECREF(sys_isatty);
262
263 sys_stream = PySys_GetObject("stdout");
264 sys_isatty = PyObject_CallMethod(sys_stream, "isatty", "");
265 if (!sys_isatty)
266 PyErr_Clear();
267 if(sys_isatty && PyObject_IsTrue(sys_isatty)) {
268 if (!PyFile_SetEncoding(sys_stream, codeset))
269 Py_FatalError("Cannot set codeset of stdout");
270 }
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000271 Py_XDECREF(sys_isatty);
272
273 if (!Py_FileSystemDefaultEncoding)
274 Py_FileSystemDefaultEncoding = codeset;
275 else
276 free(codeset);
Martin v. Löwis73d538b2003-03-05 15:13:47 +0000277 }
278#endif
Guido van Rossum25ce5661997-08-02 03:10:38 +0000279}
280
Guido van Rossum2edcf0d1998-12-15 16:12:00 +0000281#ifdef COUNT_ALLOCS
Tim Petersdbd9ba62000-07-09 03:09:57 +0000282extern void dump_counts(void);
Guido van Rossum2edcf0d1998-12-15 16:12:00 +0000283#endif
284
Guido van Rossum25ce5661997-08-02 03:10:38 +0000285/* Undo the effect of Py_Initialize().
286
287 Beware: if multiple interpreter and/or thread states exist, these
288 are not wiped out; only the current thread and interpreter state
289 are deleted. But since everything else is deleted, those other
290 interpreter and thread states should no longer be used.
291
292 (XXX We should do better, e.g. wipe out all interpreters and
293 threads.)
294
295 Locking: as above.
296
297*/
298
299void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000300Py_Finalize(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000301{
302 PyInterpreterState *interp;
303 PyThreadState *tstate;
304
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000305 if (!initialized)
Guido van Rossumaa615051997-08-20 22:40:18 +0000306 return;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000307
Tim Peters384fd102001-01-21 03:40:37 +0000308 /* The interpreter is still entirely intact at this point, and the
309 * exit funcs may be relying on that. In particular, if some thread
310 * or exit func is still waiting to do an import, the import machinery
311 * expects Py_IsInitialized() to return true. So don't say the
312 * interpreter is uninitialized until after the exit funcs have run.
313 * Note that Threading.py uses an exit func to do a join on all the
314 * threads created thru it, so this also protects pending imports in
315 * the threads created via Threading.
316 */
Guido van Rossum4cc462e1998-01-19 22:00:38 +0000317 call_sys_exitfunc();
Tim Peters384fd102001-01-21 03:40:37 +0000318 initialized = 0;
Guido van Rossum4cc462e1998-01-19 22:00:38 +0000319
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000320 /* Get current thread state and interpreter pointer */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000321 tstate = PyThreadState_Get();
322 interp = tstate->interp;
323
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000324 /* Disable signal handling */
325 PyOS_FiniInterrupts();
326
Mark Hammonda43fd0c2003-02-19 00:33:33 +0000327 /* drop module references we saved */
Mark Hammondedd07732003-07-15 23:03:55 +0000328 Py_XDECREF(warnings_module);
329 warnings_module = NULL;
Mark Hammonda43fd0c2003-02-19 00:33:33 +0000330
Guido van Rossume13ddc92003-04-17 17:29:22 +0000331 /* Collect garbage. This may call finalizers; it's nice to call these
Tim Peters1d7323e2003-12-01 21:35:27 +0000332 * before all modules are destroyed.
333 * XXX If a __del__ or weakref callback is triggered here, and tries to
334 * XXX import a module, bad things can happen, because Python no
335 * XXX longer believes it's initialized.
336 * XXX Fatal Python error: Interpreter not initialized (version mismatch?)
337 * XXX is easy to provoke that way. I've also seen, e.g.,
338 * XXX Exception exceptions.ImportError: 'No module named sha'
339 * XXX in <function callback at 0x008F5718> ignored
340 * XXX but I'm unclear on exactly how that one happens. In any case,
341 * XXX I haven't seen a real-life report of either of these.
342 */
Guido van Rossume13ddc92003-04-17 17:29:22 +0000343 PyGC_Collect();
344
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000345 /* Destroy all modules */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000346 PyImport_Cleanup();
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000347
Guido van Rossume13ddc92003-04-17 17:29:22 +0000348 /* Collect final garbage. This disposes of cycles created by
Tim Peters1d7323e2003-12-01 21:35:27 +0000349 * new-style class definitions, for example.
350 * XXX This is disabled because it caused too many problems. If
351 * XXX a __del__ or weakref callback triggers here, Python code has
352 * XXX a hard time running, because even the sys module has been
353 * XXX cleared out (sys.stdout is gone, sys.excepthook is gone, etc).
354 * XXX One symptom is a sequence of information-free messages
355 * XXX coming from threads (if a __del__ or callback is invoked,
356 * XXX other threads can execute too, and any exception they encounter
357 * XXX triggers a comedy of errors as subsystem after subsystem
358 * XXX fails to find what it *expects* to find in sys to help report
359 * XXX the exception and consequent unexpected failures). I've also
360 * XXX seen segfaults then, after adding print statements to the
361 * XXX Python code getting called.
362 */
363#if 0
Guido van Rossume13ddc92003-04-17 17:29:22 +0000364 PyGC_Collect();
Tim Peters1d7323e2003-12-01 21:35:27 +0000365#endif
Guido van Rossume13ddc92003-04-17 17:29:22 +0000366
Guido van Rossum1707aad1997-12-08 23:43:45 +0000367 /* Destroy the database used by _PyImport_{Fixup,Find}Extension */
368 _PyImport_Fini();
369
370 /* Debugging stuff */
371#ifdef COUNT_ALLOCS
372 dump_counts();
373#endif
374
375#ifdef Py_REF_DEBUG
376 fprintf(stderr, "[%ld refs]\n", _Py_RefTotal);
377#endif
378
Tim Peters9cf25ce2003-04-17 15:21:01 +0000379#ifdef Py_TRACE_REFS
380 /* Display all objects still alive -- this can invoke arbitrary
381 * __repr__ overrides, so requires a mostly-intact interpreter.
382 * Alas, a lot of stuff may still be alive now that will be cleaned
383 * up later.
384 */
Tim Peters269b2a62003-04-17 19:52:29 +0000385 if (Py_GETENV("PYTHONDUMPREFS"))
Tim Peters9cf25ce2003-04-17 15:21:01 +0000386 _Py_PrintReferences(stderr);
Tim Peters9cf25ce2003-04-17 15:21:01 +0000387#endif /* Py_TRACE_REFS */
388
Barry Warsaw035574d1997-08-29 22:07:17 +0000389 /* Now we decref the exception classes. After this point nothing
390 can raise an exception. That's okay, because each Fini() method
391 below has been checked to make sure no exceptions are ever
392 raised.
393 */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000394 _PyExc_Fini();
Barry Warsawf242aa02000-05-25 23:09:49 +0000395
Mark Hammond6cb90292003-04-22 11:18:00 +0000396 /* Cleanup auto-thread-state */
397#ifdef WITH_THREAD
398 _PyGILState_Fini();
399#endif /* WITH_THREAD */
400
Guido van Rossumd922fa42003-04-15 14:10:09 +0000401 /* Clear interpreter state */
Barry Warsawf242aa02000-05-25 23:09:49 +0000402 PyInterpreterState_Clear(interp);
Guido van Rossumd922fa42003-04-15 14:10:09 +0000403
Guido van Rossumd922fa42003-04-15 14:10:09 +0000404 /* Delete current thread */
Barry Warsawf242aa02000-05-25 23:09:49 +0000405 PyThreadState_Swap(NULL);
406 PyInterpreterState_Delete(interp);
407
Guido van Rossumd922fa42003-04-15 14:10:09 +0000408 /* Sundry finalizers */
Guido van Rossumcc283f51997-08-05 02:22:03 +0000409 PyMethod_Fini();
410 PyFrame_Fini();
411 PyCFunction_Fini();
412 PyTuple_Fini();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000413 PyString_Fini();
Guido van Rossumcc283f51997-08-05 02:22:03 +0000414 PyInt_Fini();
415 PyFloat_Fini();
416
Marc-André Lemburg95de5c12002-04-08 08:19:36 +0000417#ifdef Py_USING_UNICODE
418 /* Cleanup Unicode implementation */
419 _PyUnicode_Fini();
420#endif
421
Guido van Rossumcc283f51997-08-05 02:22:03 +0000422 /* XXX Still allocated:
423 - various static ad-hoc pointers to interned strings
424 - int and float free list blocks
425 - whatever various modules and libraries allocate
426 */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000427
428 PyGrammar_RemoveAccelerators(&_PyParser_Grammar);
Guido van Rossumcc283f51997-08-05 02:22:03 +0000429
Tim Peters269b2a62003-04-17 19:52:29 +0000430#ifdef Py_TRACE_REFS
431 /* Display addresses (& refcnts) of all objects still alive.
432 * An address can be used to find the repr of the object, printed
433 * above by _Py_PrintReferences.
434 */
435 if (Py_GETENV("PYTHONDUMPREFS"))
436 _Py_PrintReferenceAddresses(stderr);
437#endif /* Py_TRACE_REFS */
Tim Peters0e871182002-04-13 08:29:14 +0000438#ifdef PYMALLOC_DEBUG
439 if (Py_GETENV("PYTHONMALLOCSTATS"))
440 _PyObject_DebugMallocStats();
441#endif
442
Guido van Rossumcc283f51997-08-05 02:22:03 +0000443 call_ll_exitfuncs();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000444}
445
446/* Create and initialize a new interpreter and thread, and return the
447 new thread. This requires that Py_Initialize() has been called
448 first.
449
450 Unsuccessful initialization yields a NULL pointer. Note that *no*
451 exception information is available even in this case -- the
452 exception information is held in the thread, and there is no
453 thread.
454
455 Locking: as above.
456
457*/
458
459PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000460Py_NewInterpreter(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000461{
462 PyInterpreterState *interp;
463 PyThreadState *tstate, *save_tstate;
464 PyObject *bimod, *sysmod;
465
466 if (!initialized)
467 Py_FatalError("Py_NewInterpreter: call Py_Initialize first");
468
469 interp = PyInterpreterState_New();
470 if (interp == NULL)
471 return NULL;
472
473 tstate = PyThreadState_New(interp);
474 if (tstate == NULL) {
475 PyInterpreterState_Delete(interp);
476 return NULL;
477 }
478
479 save_tstate = PyThreadState_Swap(tstate);
480
481 /* XXX The following is lax in error checking */
482
483 interp->modules = PyDict_New();
484
485 bimod = _PyImport_FindExtension("__builtin__", "__builtin__");
486 if (bimod != NULL) {
Guido van Rossum4a1f39a1997-11-04 19:36:18 +0000487 interp->builtins = PyModule_GetDict(bimod);
488 Py_INCREF(interp->builtins);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000489 }
490 sysmod = _PyImport_FindExtension("sys", "sys");
491 if (bimod != NULL && sysmod != NULL) {
492 interp->sysdict = PyModule_GetDict(sysmod);
493 Py_INCREF(interp->sysdict);
494 PySys_SetPath(Py_GetPath());
495 PyDict_SetItemString(interp->sysdict, "modules",
496 interp->modules);
Martin v. Löwisd7ceb222003-01-22 09:00:38 +0000497 _PyImportHooks_Init();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000498 initmain();
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000499 if (!Py_NoSiteFlag)
500 initsite();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000501 }
502
503 if (!PyErr_Occurred())
504 return tstate;
505
506 /* Oops, it didn't work. Undo it all. */
507
508 PyErr_Print();
509 PyThreadState_Clear(tstate);
510 PyThreadState_Swap(save_tstate);
511 PyThreadState_Delete(tstate);
512 PyInterpreterState_Delete(interp);
513
514 return NULL;
515}
516
517/* Delete an interpreter and its last thread. This requires that the
518 given thread state is current, that the thread has no remaining
519 frames, and that it is its interpreter's only remaining thread.
520 It is a fatal error to violate these constraints.
521
522 (Py_Finalize() doesn't have these constraints -- it zaps
523 everything, regardless.)
524
525 Locking: as above.
526
527*/
528
529void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000530Py_EndInterpreter(PyThreadState *tstate)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000531{
532 PyInterpreterState *interp = tstate->interp;
533
534 if (tstate != PyThreadState_Get())
535 Py_FatalError("Py_EndInterpreter: thread is not current");
536 if (tstate->frame != NULL)
537 Py_FatalError("Py_EndInterpreter: thread still has a frame");
538 if (tstate != interp->tstate_head || tstate->next != NULL)
539 Py_FatalError("Py_EndInterpreter: not the last thread");
540
541 PyImport_Cleanup();
542 PyInterpreterState_Clear(interp);
543 PyThreadState_Swap(NULL);
544 PyInterpreterState_Delete(interp);
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000545}
546
547static char *progname = "python";
548
549void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000550Py_SetProgramName(char *pn)
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000551{
552 if (pn && *pn)
553 progname = pn;
554}
555
556char *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000557Py_GetProgramName(void)
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000558{
559 return progname;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000560}
561
Guido van Rossuma61691e1998-02-06 22:27:24 +0000562static char *default_home = NULL;
563
564void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000565Py_SetPythonHome(char *home)
Guido van Rossuma61691e1998-02-06 22:27:24 +0000566{
567 default_home = home;
568}
569
570char *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000571Py_GetPythonHome(void)
Guido van Rossuma61691e1998-02-06 22:27:24 +0000572{
573 char *home = default_home;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000574 if (home == NULL && !Py_IgnoreEnvironmentFlag)
575 home = Py_GETENV("PYTHONHOME");
Guido van Rossuma61691e1998-02-06 22:27:24 +0000576 return home;
577}
578
Guido van Rossum6135a871995-01-09 17:53:26 +0000579/* Create __main__ module */
580
581static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000582initmain(void)
Guido van Rossum6135a871995-01-09 17:53:26 +0000583{
Guido van Rossum82598051997-03-05 00:20:32 +0000584 PyObject *m, *d;
585 m = PyImport_AddModule("__main__");
Guido van Rossum6135a871995-01-09 17:53:26 +0000586 if (m == NULL)
Guido van Rossum82598051997-03-05 00:20:32 +0000587 Py_FatalError("can't create __main__ module");
588 d = PyModule_GetDict(m);
589 if (PyDict_GetItemString(d, "__builtins__") == NULL) {
Guido van Rossum858cb731997-11-19 16:15:37 +0000590 PyObject *bimod = PyImport_ImportModule("__builtin__");
591 if (bimod == NULL ||
592 PyDict_SetItemString(d, "__builtins__", bimod) != 0)
Guido van Rossum82598051997-03-05 00:20:32 +0000593 Py_FatalError("can't add __builtins__ to __main__");
Barry Warsaw3d05b1a1999-01-29 21:30:22 +0000594 Py_DECREF(bimod);
Guido van Rossum6135a871995-01-09 17:53:26 +0000595 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000596}
597
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000598/* Import the site module (not into __main__ though) */
599
600static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000601initsite(void)
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000602{
603 PyObject *m, *f;
604 m = PyImport_ImportModule("site");
605 if (m == NULL) {
606 f = PySys_GetObject("stderr");
607 if (Py_VerboseFlag) {
608 PyFile_WriteString(
609 "'import site' failed; traceback:\n", f);
610 PyErr_Print();
611 }
612 else {
613 PyFile_WriteString(
614 "'import site' failed; use -v for traceback\n", f);
615 PyErr_Clear();
616 }
617 }
618 else {
619 Py_DECREF(m);
620 }
621}
622
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000623/* Parse input from a file and execute it */
624
625int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000626PyRun_AnyFile(FILE *fp, const char *filename)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000627{
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000628 return PyRun_AnyFileExFlags(fp, filename, 0, NULL);
629}
630
631int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000632PyRun_AnyFileFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000633{
634 return PyRun_AnyFileExFlags(fp, filename, 0, flags);
Guido van Rossum0df002c2000-08-27 19:21:52 +0000635}
636
637int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000638PyRun_AnyFileEx(FILE *fp, const char *filename, int closeit)
Guido van Rossum0df002c2000-08-27 19:21:52 +0000639{
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000640 return PyRun_AnyFileExFlags(fp, filename, closeit, NULL);
641}
642
643int
Tim Petersd08e3822003-04-17 15:24:21 +0000644PyRun_AnyFileExFlags(FILE *fp, const char *filename, int closeit,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000645 PyCompilerFlags *flags)
646{
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000647 if (filename == NULL)
648 filename = "???";
Guido van Rossum0df002c2000-08-27 19:21:52 +0000649 if (Py_FdIsInteractive(fp, filename)) {
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000650 int err = PyRun_InteractiveLoopFlags(fp, filename, flags);
Guido van Rossum0df002c2000-08-27 19:21:52 +0000651 if (closeit)
652 fclose(fp);
653 return err;
654 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000655 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000656 return PyRun_SimpleFileExFlags(fp, filename, closeit, flags);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000657}
658
659int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000660PyRun_InteractiveLoop(FILE *fp, const char *filename)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000661{
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000662 return PyRun_InteractiveLoopFlags(fp, filename, NULL);
663}
664
665int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000666PyRun_InteractiveLoopFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000667{
Guido van Rossum82598051997-03-05 00:20:32 +0000668 PyObject *v;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000669 int ret;
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000670 PyCompilerFlags local_flags;
Jeremy Hylton9f324e92001-03-01 22:59:14 +0000671
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000672 if (flags == NULL) {
673 flags = &local_flags;
Tim Peters5ba58662001-07-16 02:29:45 +0000674 local_flags.cf_flags = 0;
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000675 }
Guido van Rossum82598051997-03-05 00:20:32 +0000676 v = PySys_GetObject("ps1");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000677 if (v == NULL) {
Guido van Rossum82598051997-03-05 00:20:32 +0000678 PySys_SetObject("ps1", v = PyString_FromString(">>> "));
679 Py_XDECREF(v);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000680 }
Guido van Rossum82598051997-03-05 00:20:32 +0000681 v = PySys_GetObject("ps2");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000682 if (v == NULL) {
Guido van Rossum82598051997-03-05 00:20:32 +0000683 PySys_SetObject("ps2", v = PyString_FromString("... "));
684 Py_XDECREF(v);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000685 }
686 for (;;) {
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000687 ret = PyRun_InteractiveOneFlags(fp, filename, flags);
Guido van Rossumaae0d321996-05-22 16:35:33 +0000688#ifdef Py_REF_DEBUG
Guido van Rossum6f9e4331995-03-29 16:57:48 +0000689 fprintf(stderr, "[%ld refs]\n", _Py_RefTotal);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000690#endif
691 if (ret == E_EOF)
692 return 0;
693 /*
694 if (ret == E_NOMEM)
695 return -1;
696 */
697 }
698}
699
700int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000701PyRun_InteractiveOne(FILE *fp, const char *filename)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000702{
Jeremy Hylton9f324e92001-03-01 22:59:14 +0000703 return PyRun_InteractiveOneFlags(fp, filename, NULL);
704}
705
Neil Schemenauerc24ea082002-03-22 23:53:36 +0000706/* compute parser flags based on compiler flags */
Neil Schemenauerc24ea082002-03-22 23:53:36 +0000707#define PARSER_FLAGS(flags) \
Guido van Rossum4b499dd32003-02-13 22:07:59 +0000708 (((flags) && (flags)->cf_flags & PyCF_DONT_IMPLY_DEDENT) ? \
709 PyPARSE_DONT_IMPLY_DEDENT : 0)
Neil Schemenauerc24ea082002-03-22 23:53:36 +0000710
Jeremy Hylton9f324e92001-03-01 22:59:14 +0000711int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000712PyRun_InteractiveOneFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
Jeremy Hylton9f324e92001-03-01 22:59:14 +0000713{
Guido van Rossum82598051997-03-05 00:20:32 +0000714 PyObject *m, *d, *v, *w;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000715 node *n;
Guido van Rossuma110aa61994-08-29 12:50:44 +0000716 perrdetail err;
Guido van Rossumddc3fb51997-11-25 20:58:13 +0000717 char *ps1 = "", *ps2 = "";
Tim Petersfe2127d2001-07-16 05:37:24 +0000718
Guido van Rossum82598051997-03-05 00:20:32 +0000719 v = PySys_GetObject("ps1");
Guido van Rossumddc3fb51997-11-25 20:58:13 +0000720 if (v != NULL) {
721 v = PyObject_Str(v);
722 if (v == NULL)
723 PyErr_Clear();
724 else if (PyString_Check(v))
725 ps1 = PyString_AsString(v);
726 }
Guido van Rossum82598051997-03-05 00:20:32 +0000727 w = PySys_GetObject("ps2");
Guido van Rossumddc3fb51997-11-25 20:58:13 +0000728 if (w != NULL) {
729 w = PyObject_Str(w);
730 if (w == NULL)
731 PyErr_Clear();
732 else if (PyString_Check(w))
733 ps2 = PyString_AsString(w);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000734 }
Tim Petersfe2127d2001-07-16 05:37:24 +0000735 n = PyParser_ParseFileFlags(fp, filename, &_PyParser_Grammar,
736 Py_single_input, ps1, ps2, &err,
Neil Schemenauerc24ea082002-03-22 23:53:36 +0000737 PARSER_FLAGS(flags));
Guido van Rossum82598051997-03-05 00:20:32 +0000738 Py_XDECREF(v);
739 Py_XDECREF(w);
Guido van Rossuma110aa61994-08-29 12:50:44 +0000740 if (n == NULL) {
741 if (err.error == E_EOF) {
742 if (err.text)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000743 PyMem_DEL(err.text);
Guido van Rossuma110aa61994-08-29 12:50:44 +0000744 return E_EOF;
745 }
746 err_input(&err);
Guido van Rossum82598051997-03-05 00:20:32 +0000747 PyErr_Print();
Guido van Rossuma110aa61994-08-29 12:50:44 +0000748 return err.error;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000749 }
Guido van Rossum82598051997-03-05 00:20:32 +0000750 m = PyImport_AddModule("__main__");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000751 if (m == NULL)
752 return -1;
Guido van Rossum82598051997-03-05 00:20:32 +0000753 d = PyModule_GetDict(m);
Jeremy Hylton9f324e92001-03-01 22:59:14 +0000754 v = run_node(n, filename, d, d, flags);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000755 if (v == NULL) {
Guido van Rossum82598051997-03-05 00:20:32 +0000756 PyErr_Print();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000757 return -1;
758 }
Guido van Rossum82598051997-03-05 00:20:32 +0000759 Py_DECREF(v);
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000760 if (Py_FlushLine())
761 PyErr_Clear();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000762 return 0;
763}
764
765int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000766PyRun_SimpleFile(FILE *fp, const char *filename)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000767{
Guido van Rossum0df002c2000-08-27 19:21:52 +0000768 return PyRun_SimpleFileEx(fp, filename, 0);
769}
770
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000771/* Check whether a file maybe a pyc file: Look at the extension,
772 the file type, and, if we may close it, at the first few bytes. */
773
774static int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000775maybe_pyc_file(FILE *fp, const char* filename, const char* ext, int closeit)
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000776{
777 if (strcmp(ext, ".pyc") == 0 || strcmp(ext, ".pyo") == 0)
778 return 1;
779
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000780 /* Only look into the file if we are allowed to close it, since
781 it then should also be seekable. */
782 if (closeit) {
783 /* Read only two bytes of the magic. If the file was opened in
784 text mode, the bytes 3 and 4 of the magic (\r\n) might not
785 be read as they are on disk. */
786 unsigned int halfmagic = PyImport_GetMagicNumber() & 0xFFFF;
787 unsigned char buf[2];
Tim Peters3e876562001-02-11 04:35:39 +0000788 /* Mess: In case of -x, the stream is NOT at its start now,
789 and ungetc() was used to push back the first newline,
Tim Peters6f5a4ef2001-02-17 22:02:34 +0000790 which makes the current stream position formally undefined,
791 and a x-platform nightmare.
792 Unfortunately, we have no direct way to know whether -x
793 was specified. So we use a terrible hack: if the current
794 stream position is not 0, we assume -x was specified, and
795 give up. Bug 132850 on SourceForge spells out the
796 hopelessness of trying anything else (fseek and ftell
797 don't work predictably x-platform for text-mode files).
Tim Peters3e876562001-02-11 04:35:39 +0000798 */
Tim Peters3e876562001-02-11 04:35:39 +0000799 int ispyc = 0;
Tim Peters6f5a4ef2001-02-17 22:02:34 +0000800 if (ftell(fp) == 0) {
801 if (fread(buf, 1, 2, fp) == 2 &&
Tim Petersd08e3822003-04-17 15:24:21 +0000802 ((unsigned int)buf[1]<<8 | buf[0]) == halfmagic)
Tim Peters6f5a4ef2001-02-17 22:02:34 +0000803 ispyc = 1;
804 rewind(fp);
805 }
Tim Peters3e876562001-02-11 04:35:39 +0000806 return ispyc;
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000807 }
808 return 0;
Tim Petersd08e3822003-04-17 15:24:21 +0000809}
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000810
Guido van Rossum0df002c2000-08-27 19:21:52 +0000811int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000812PyRun_SimpleFileEx(FILE *fp, const char *filename, int closeit)
Guido van Rossum0df002c2000-08-27 19:21:52 +0000813{
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000814 return PyRun_SimpleFileExFlags(fp, filename, closeit, NULL);
815}
816
817int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000818PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000819 PyCompilerFlags *flags)
820{
Guido van Rossum82598051997-03-05 00:20:32 +0000821 PyObject *m, *d, *v;
Martin v. Löwis95292d62002-12-11 14:04:59 +0000822 const char *ext;
Guido van Rossumfdef2711994-09-14 13:31:04 +0000823
Guido van Rossum82598051997-03-05 00:20:32 +0000824 m = PyImport_AddModule("__main__");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000825 if (m == NULL)
826 return -1;
Guido van Rossum82598051997-03-05 00:20:32 +0000827 d = PyModule_GetDict(m);
Fred Drake8ed02042002-10-17 21:24:58 +0000828 if (PyDict_GetItemString(d, "__file__") == NULL) {
829 PyObject *f = PyString_FromString(filename);
830 if (f == NULL)
831 return -1;
832 if (PyDict_SetItemString(d, "__file__", f) < 0) {
833 Py_DECREF(f);
834 return -1;
835 }
836 Py_DECREF(f);
837 }
Guido van Rossumfdef2711994-09-14 13:31:04 +0000838 ext = filename + strlen(filename) - 4;
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000839 if (maybe_pyc_file(fp, filename, ext, closeit)) {
Guido van Rossumfdef2711994-09-14 13:31:04 +0000840 /* Try to run a pyc file. First, re-open in binary */
Guido van Rossum0df002c2000-08-27 19:21:52 +0000841 if (closeit)
842 fclose(fp);
Fred Drake8ed02042002-10-17 21:24:58 +0000843 if ((fp = fopen(filename, "rb")) == NULL) {
Guido van Rossumfdef2711994-09-14 13:31:04 +0000844 fprintf(stderr, "python: Can't reopen .pyc file\n");
845 return -1;
846 }
Guido van Rossum2a7f58d1997-04-02 05:28:38 +0000847 /* Turn on optimization if a .pyo file is given */
848 if (strcmp(ext, ".pyo") == 0)
849 Py_OptimizeFlag = 1;
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000850 v = run_pyc_file(fp, filename, d, d, flags);
Guido van Rossumfdef2711994-09-14 13:31:04 +0000851 } else {
Tim Petersd08e3822003-04-17 15:24:21 +0000852 v = PyRun_FileExFlags(fp, filename, Py_file_input, d, d,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000853 closeit, flags);
Guido van Rossumfdef2711994-09-14 13:31:04 +0000854 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000855 if (v == NULL) {
Guido van Rossum82598051997-03-05 00:20:32 +0000856 PyErr_Print();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000857 return -1;
858 }
Guido van Rossum82598051997-03-05 00:20:32 +0000859 Py_DECREF(v);
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000860 if (Py_FlushLine())
861 PyErr_Clear();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000862 return 0;
863}
864
865int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000866PyRun_SimpleString(const char *command)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000867{
Guido van Rossum393661d2001-08-31 17:40:15 +0000868 return PyRun_SimpleStringFlags(command, NULL);
869}
870
871int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000872PyRun_SimpleStringFlags(const char *command, PyCompilerFlags *flags)
Guido van Rossum393661d2001-08-31 17:40:15 +0000873{
Guido van Rossum82598051997-03-05 00:20:32 +0000874 PyObject *m, *d, *v;
875 m = PyImport_AddModule("__main__");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000876 if (m == NULL)
877 return -1;
Guido van Rossum82598051997-03-05 00:20:32 +0000878 d = PyModule_GetDict(m);
Guido van Rossum393661d2001-08-31 17:40:15 +0000879 v = PyRun_StringFlags(command, Py_file_input, d, d, flags);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000880 if (v == NULL) {
Guido van Rossum82598051997-03-05 00:20:32 +0000881 PyErr_Print();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000882 return -1;
883 }
Guido van Rossum82598051997-03-05 00:20:32 +0000884 Py_DECREF(v);
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000885 if (Py_FlushLine())
886 PyErr_Clear();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000887 return 0;
888}
889
Barry Warsaw035574d1997-08-29 22:07:17 +0000890static int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000891parse_syntax_error(PyObject *err, PyObject **message, const char **filename,
892 int *lineno, int *offset, const char **text)
Barry Warsaw035574d1997-08-29 22:07:17 +0000893{
894 long hold;
895 PyObject *v;
896
897 /* old style errors */
898 if (PyTuple_Check(err))
Neal Norwitz78293352002-04-01 01:41:20 +0000899 return PyArg_ParseTuple(err, "O(ziiz)", message, filename,
900 lineno, offset, text);
Barry Warsaw035574d1997-08-29 22:07:17 +0000901
902 /* new style errors. `err' is an instance */
903
904 if (! (v = PyObject_GetAttrString(err, "msg")))
905 goto finally;
906 *message = v;
907
908 if (!(v = PyObject_GetAttrString(err, "filename")))
909 goto finally;
910 if (v == Py_None)
911 *filename = NULL;
912 else if (! (*filename = PyString_AsString(v)))
913 goto finally;
914
915 Py_DECREF(v);
916 if (!(v = PyObject_GetAttrString(err, "lineno")))
917 goto finally;
918 hold = PyInt_AsLong(v);
919 Py_DECREF(v);
920 v = NULL;
921 if (hold < 0 && PyErr_Occurred())
922 goto finally;
923 *lineno = (int)hold;
924
925 if (!(v = PyObject_GetAttrString(err, "offset")))
926 goto finally;
Jeremy Hylton9f1b9932001-02-28 07:07:43 +0000927 if (v == Py_None) {
928 *offset = -1;
929 Py_DECREF(v);
930 v = NULL;
931 } else {
932 hold = PyInt_AsLong(v);
933 Py_DECREF(v);
934 v = NULL;
935 if (hold < 0 && PyErr_Occurred())
936 goto finally;
937 *offset = (int)hold;
938 }
Barry Warsaw035574d1997-08-29 22:07:17 +0000939
940 if (!(v = PyObject_GetAttrString(err, "text")))
941 goto finally;
942 if (v == Py_None)
943 *text = NULL;
944 else if (! (*text = PyString_AsString(v)))
945 goto finally;
946 Py_DECREF(v);
947 return 1;
948
949finally:
950 Py_XDECREF(v);
951 return 0;
952}
953
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000954void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000955PyErr_Print(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000956{
Guido van Rossuma61691e1998-02-06 22:27:24 +0000957 PyErr_PrintEx(1);
958}
959
Jeremy Hylton9f1b9932001-02-28 07:07:43 +0000960static void
Martin v. Löwis95292d62002-12-11 14:04:59 +0000961print_error_text(PyObject *f, int offset, const char *text)
Jeremy Hylton9f1b9932001-02-28 07:07:43 +0000962{
963 char *nl;
964 if (offset >= 0) {
965 if (offset > 0 && offset == (int)strlen(text))
966 offset--;
967 for (;;) {
968 nl = strchr(text, '\n');
969 if (nl == NULL || nl-text >= offset)
970 break;
971 offset -= (nl+1-text);
972 text = nl+1;
973 }
974 while (*text == ' ' || *text == '\t') {
975 text++;
976 offset--;
977 }
978 }
979 PyFile_WriteString(" ", f);
980 PyFile_WriteString(text, f);
981 if (*text == '\0' || text[strlen(text)-1] != '\n')
982 PyFile_WriteString("\n", f);
983 if (offset == -1)
984 return;
985 PyFile_WriteString(" ", f);
986 offset--;
987 while (offset > 0) {
988 PyFile_WriteString(" ", f);
989 offset--;
990 }
991 PyFile_WriteString("^\n", f);
992}
993
Guido van Rossum66e8e862001-03-23 17:54:43 +0000994static void
995handle_system_exit(void)
Ka-Ping Yee26fabb02001-03-23 15:36:41 +0000996{
Guido van Rossum66e8e862001-03-23 17:54:43 +0000997 PyObject *exception, *value, *tb;
Tim Peterscf615b52003-04-19 18:47:02 +0000998 int exitcode = 0;
999
Guido van Rossum66e8e862001-03-23 17:54:43 +00001000 PyErr_Fetch(&exception, &value, &tb);
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001001 if (Py_FlushLine())
1002 PyErr_Clear();
1003 fflush(stdout);
1004 if (value == NULL || value == Py_None)
Tim Peterscf615b52003-04-19 18:47:02 +00001005 goto done;
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001006 if (PyInstance_Check(value)) {
1007 /* The error code should be in the `code' attribute. */
1008 PyObject *code = PyObject_GetAttrString(value, "code");
1009 if (code) {
1010 Py_DECREF(value);
1011 value = code;
1012 if (value == Py_None)
Tim Peterscf615b52003-04-19 18:47:02 +00001013 goto done;
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001014 }
1015 /* If we failed to dig out the 'code' attribute,
1016 just let the else clause below print the error. */
1017 }
1018 if (PyInt_Check(value))
Tim Peterscf615b52003-04-19 18:47:02 +00001019 exitcode = (int)PyInt_AsLong(value);
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001020 else {
1021 PyObject_Print(value, stderr, Py_PRINT_RAW);
1022 PySys_WriteStderr("\n");
Tim Peterscf615b52003-04-19 18:47:02 +00001023 exitcode = 1;
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001024 }
Tim Peterscf615b52003-04-19 18:47:02 +00001025 done:
1026 /* Restore and clear the exception info, in order to properly decref
1027 * the exception, value, and traceback. If we just exit instead,
1028 * these leak, which confuses PYTHONDUMPREFS output, and may prevent
1029 * some finalizers from running.
1030 */
1031 PyErr_Restore(exception, value, tb);
1032 PyErr_Clear();
1033 Py_Exit(exitcode);
1034 /* NOTREACHED */
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001035}
1036
1037void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001038PyErr_PrintEx(int set_sys_last_vars)
Guido van Rossuma61691e1998-02-06 22:27:24 +00001039{
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001040 PyObject *exception, *v, *tb, *hook;
Guido van Rossum66e8e862001-03-23 17:54:43 +00001041
1042 if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
1043 handle_system_exit();
1044 }
Guido van Rossum82598051997-03-05 00:20:32 +00001045 PyErr_Fetch(&exception, &v, &tb);
Barry Warsaw035574d1997-08-29 22:07:17 +00001046 PyErr_NormalizeException(&exception, &v, &tb);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001047 if (exception == NULL)
Guido van Rossum296b4751997-05-23 00:19:20 +00001048 return;
Guido van Rossuma61691e1998-02-06 22:27:24 +00001049 if (set_sys_last_vars) {
1050 PySys_SetObject("last_type", exception);
1051 PySys_SetObject("last_value", v);
1052 PySys_SetObject("last_traceback", tb);
1053 }
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001054 hook = PySys_GetObject("excepthook");
1055 if (hook) {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001056 PyObject *args = PyTuple_Pack(3,
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001057 exception, v ? v : Py_None, tb ? tb : Py_None);
1058 PyObject *result = PyEval_CallObject(hook, args);
1059 if (result == NULL) {
1060 PyObject *exception2, *v2, *tb2;
Guido van Rossum66e8e862001-03-23 17:54:43 +00001061 if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
1062 handle_system_exit();
1063 }
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001064 PyErr_Fetch(&exception2, &v2, &tb2);
1065 PyErr_NormalizeException(&exception2, &v2, &tb2);
1066 if (Py_FlushLine())
1067 PyErr_Clear();
1068 fflush(stdout);
1069 PySys_WriteStderr("Error in sys.excepthook:\n");
1070 PyErr_Display(exception2, v2, tb2);
1071 PySys_WriteStderr("\nOriginal exception was:\n");
1072 PyErr_Display(exception, v, tb);
Jeremy Hylton07028582001-12-07 15:35:35 +00001073 Py_XDECREF(exception2);
1074 Py_XDECREF(v2);
1075 Py_XDECREF(tb2);
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001076 }
1077 Py_XDECREF(result);
1078 Py_XDECREF(args);
1079 } else {
1080 PySys_WriteStderr("sys.excepthook is missing\n");
1081 PyErr_Display(exception, v, tb);
1082 }
1083 Py_XDECREF(exception);
1084 Py_XDECREF(v);
1085 Py_XDECREF(tb);
1086}
1087
1088void PyErr_Display(PyObject *exception, PyObject *value, PyObject *tb)
1089{
1090 int err = 0;
1091 PyObject *v = value;
1092 PyObject *f = PySys_GetObject("stderr");
Guido van Rossum3165fe61992-09-25 21:59:05 +00001093 if (f == NULL)
1094 fprintf(stderr, "lost sys.stderr\n");
1095 else {
Guido van Rossum0829c751998-02-28 04:31:39 +00001096 if (Py_FlushLine())
1097 PyErr_Clear();
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001098 fflush(stdout);
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001099 if (tb && tb != Py_None)
1100 err = PyTraceBack_Print(tb, f);
Barry Warsaw36b8f941997-08-26 18:09:48 +00001101 if (err == 0 &&
Martin v. Löwiscfeb3b62002-03-03 21:30:27 +00001102 PyObject_HasAttrString(v, "print_file_and_line"))
Barry Warsaw36b8f941997-08-26 18:09:48 +00001103 {
Guido van Rossum82598051997-03-05 00:20:32 +00001104 PyObject *message;
Martin v. Löwis95292d62002-12-11 14:04:59 +00001105 const char *filename, *text;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001106 int lineno, offset;
Barry Warsaw035574d1997-08-29 22:07:17 +00001107 if (!parse_syntax_error(v, &message, &filename,
1108 &lineno, &offset, &text))
Guido van Rossum82598051997-03-05 00:20:32 +00001109 PyErr_Clear();
Guido van Rossuma110aa61994-08-29 12:50:44 +00001110 else {
1111 char buf[10];
Guido van Rossum82598051997-03-05 00:20:32 +00001112 PyFile_WriteString(" File \"", f);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001113 if (filename == NULL)
Guido van Rossum82598051997-03-05 00:20:32 +00001114 PyFile_WriteString("<string>", f);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001115 else
Guido van Rossum82598051997-03-05 00:20:32 +00001116 PyFile_WriteString(filename, f);
1117 PyFile_WriteString("\", line ", f);
Jeremy Hylton518ab1c2001-11-28 20:42:20 +00001118 PyOS_snprintf(buf, sizeof(buf), "%d", lineno);
Guido van Rossum82598051997-03-05 00:20:32 +00001119 PyFile_WriteString(buf, f);
1120 PyFile_WriteString("\n", f);
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001121 if (text != NULL)
1122 print_error_text(f, offset, text);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001123 v = message;
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001124 /* Can't be bothered to check all those
1125 PyFile_WriteString() calls */
1126 if (PyErr_Occurred())
1127 err = -1;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001128 }
1129 }
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001130 if (err) {
1131 /* Don't do anything else */
1132 }
1133 else if (PyClass_Check(exception)) {
Barry Warsaw2f5f6a21997-09-16 21:42:03 +00001134 PyClassObject* exc = (PyClassObject*)exception;
1135 PyObject* className = exc->cl_name;
1136 PyObject* moduleName =
1137 PyDict_GetItemString(exc->cl_dict, "__module__");
1138
1139 if (moduleName == NULL)
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001140 err = PyFile_WriteString("<unknown>", f);
Barry Warsaw2f5f6a21997-09-16 21:42:03 +00001141 else {
1142 char* modstr = PyString_AsString(moduleName);
Tim Petersd08e3822003-04-17 15:24:21 +00001143 if (modstr && strcmp(modstr, "exceptions"))
Barry Warsaw2f5f6a21997-09-16 21:42:03 +00001144 {
1145 err = PyFile_WriteString(modstr, f);
1146 err += PyFile_WriteString(".", f);
1147 }
1148 }
1149 if (err == 0) {
1150 if (className == NULL)
1151 err = PyFile_WriteString("<unknown>", f);
1152 else
1153 err = PyFile_WriteObject(className, f,
1154 Py_PRINT_RAW);
1155 }
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001156 }
1157 else
1158 err = PyFile_WriteObject(exception, f, Py_PRINT_RAW);
1159 if (err == 0) {
1160 if (v != NULL && v != Py_None) {
Barry Warsaw035574d1997-08-29 22:07:17 +00001161 PyObject *s = PyObject_Str(v);
1162 /* only print colon if the str() of the
1163 object is not the empty string
1164 */
Guido van Rossumd6bf45b1997-09-05 19:11:53 +00001165 if (s == NULL)
1166 err = -1;
1167 else if (!PyString_Check(s) ||
1168 PyString_GET_SIZE(s) != 0)
Barry Warsaw035574d1997-08-29 22:07:17 +00001169 err = PyFile_WriteString(": ", f);
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001170 if (err == 0)
Guido van Rossumd6bf45b1997-09-05 19:11:53 +00001171 err = PyFile_WriteObject(s, f, Py_PRINT_RAW);
1172 Py_XDECREF(s);
Guido van Rossum262e1241995-02-07 15:30:45 +00001173 }
Guido van Rossum262e1241995-02-07 15:30:45 +00001174 }
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001175 if (err == 0)
1176 err = PyFile_WriteString("\n", f);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001177 }
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001178 /* If an error happened here, don't show it.
1179 XXX This is wrong, but too many callers rely on this behavior. */
1180 if (err != 0)
1181 PyErr_Clear();
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001182}
1183
Guido van Rossum82598051997-03-05 00:20:32 +00001184PyObject *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001185PyRun_String(const char *str, int start, PyObject *globals, PyObject *locals)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001186{
Guido van Rossum82598051997-03-05 00:20:32 +00001187 return run_err_node(PyParser_SimpleParseString(str, start),
Jeremy Hylton9f324e92001-03-01 22:59:14 +00001188 "<string>", globals, locals, NULL);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001189}
1190
Guido van Rossum82598051997-03-05 00:20:32 +00001191PyObject *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001192PyRun_File(FILE *fp, const char *filename, int start, PyObject *globals,
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001193 PyObject *locals)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001194{
Tim Peterse8682112000-08-27 20:18:17 +00001195 return PyRun_FileEx(fp, filename, start, globals, locals, 0);
Guido van Rossum0df002c2000-08-27 19:21:52 +00001196}
1197
1198PyObject *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001199PyRun_FileEx(FILE *fp, const char *filename, int start, PyObject *globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001200 PyObject *locals, int closeit)
Guido van Rossum0df002c2000-08-27 19:21:52 +00001201{
1202 node *n = PyParser_SimpleParseFile(fp, filename, start);
1203 if (closeit)
1204 fclose(fp);
Jeremy Hylton9f324e92001-03-01 22:59:14 +00001205 return run_err_node(n, filename, globals, locals, NULL);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001206}
1207
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001208PyObject *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001209PyRun_StringFlags(const char *str, int start, PyObject *globals, PyObject *locals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001210 PyCompilerFlags *flags)
1211{
Guido van Rossuma1b3a472001-07-16 16:51:33 +00001212 return run_err_node(PyParser_SimpleParseStringFlags(
Neil Schemenauerc24ea082002-03-22 23:53:36 +00001213 str, start, PARSER_FLAGS(flags)),
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001214 "<string>", globals, locals, flags);
1215}
1216
1217PyObject *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001218PyRun_FileFlags(FILE *fp, const char *filename, int start, PyObject *globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001219 PyObject *locals, PyCompilerFlags *flags)
1220{
1221 return PyRun_FileExFlags(fp, filename, start, globals, locals, 0,
Tim Petersd08e3822003-04-17 15:24:21 +00001222 flags);
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001223}
1224
1225PyObject *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001226PyRun_FileExFlags(FILE *fp, const char *filename, int start, PyObject *globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001227 PyObject *locals, int closeit, PyCompilerFlags *flags)
1228{
Tim Petersfe2127d2001-07-16 05:37:24 +00001229 node *n = PyParser_SimpleParseFileFlags(fp, filename, start,
Neil Schemenauerc24ea082002-03-22 23:53:36 +00001230 PARSER_FLAGS(flags));
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001231 if (closeit)
1232 fclose(fp);
1233 return run_err_node(n, filename, globals, locals, flags);
1234}
1235
Guido van Rossum82598051997-03-05 00:20:32 +00001236static PyObject *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001237run_err_node(node *n, const char *filename, PyObject *globals, PyObject *locals,
Jeremy Hylton9f324e92001-03-01 22:59:14 +00001238 PyCompilerFlags *flags)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001239{
Guido van Rossuma110aa61994-08-29 12:50:44 +00001240 if (n == NULL)
1241 return NULL;
Jeremy Hylton9f324e92001-03-01 22:59:14 +00001242 return run_node(n, filename, globals, locals, flags);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001243}
1244
Guido van Rossum82598051997-03-05 00:20:32 +00001245static PyObject *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001246run_node(node *n, const char *filename, PyObject *globals, PyObject *locals,
Jeremy Hylton9f324e92001-03-01 22:59:14 +00001247 PyCompilerFlags *flags)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001248{
Guido van Rossum82598051997-03-05 00:20:32 +00001249 PyCodeObject *co;
1250 PyObject *v;
Jeremy Hylton9f324e92001-03-01 22:59:14 +00001251 co = PyNode_CompileFlags(n, filename, flags);
Guido van Rossum82598051997-03-05 00:20:32 +00001252 PyNode_Free(n);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001253 if (co == NULL)
1254 return NULL;
Guido van Rossum82598051997-03-05 00:20:32 +00001255 v = PyEval_EvalCode(co, globals, locals);
1256 Py_DECREF(co);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001257 return v;
1258}
1259
Guido van Rossum82598051997-03-05 00:20:32 +00001260static PyObject *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001261run_pyc_file(FILE *fp, const char *filename, PyObject *globals, PyObject *locals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001262 PyCompilerFlags *flags)
Guido van Rossumfdef2711994-09-14 13:31:04 +00001263{
Guido van Rossum82598051997-03-05 00:20:32 +00001264 PyCodeObject *co;
1265 PyObject *v;
Guido van Rossumfdef2711994-09-14 13:31:04 +00001266 long magic;
Fred Drakee8de31c2000-08-31 05:38:39 +00001267 long PyImport_GetMagicNumber(void);
Guido van Rossumfdef2711994-09-14 13:31:04 +00001268
Guido van Rossum82598051997-03-05 00:20:32 +00001269 magic = PyMarshal_ReadLongFromFile(fp);
1270 if (magic != PyImport_GetMagicNumber()) {
1271 PyErr_SetString(PyExc_RuntimeError,
Guido van Rossumfdef2711994-09-14 13:31:04 +00001272 "Bad magic number in .pyc file");
1273 return NULL;
1274 }
Guido van Rossum82598051997-03-05 00:20:32 +00001275 (void) PyMarshal_ReadLongFromFile(fp);
Tim Petersd9b9ac82001-01-28 00:27:39 +00001276 v = PyMarshal_ReadLastObjectFromFile(fp);
Guido van Rossumfdef2711994-09-14 13:31:04 +00001277 fclose(fp);
Guido van Rossum82598051997-03-05 00:20:32 +00001278 if (v == NULL || !PyCode_Check(v)) {
1279 Py_XDECREF(v);
1280 PyErr_SetString(PyExc_RuntimeError,
Guido van Rossumfdef2711994-09-14 13:31:04 +00001281 "Bad code object in .pyc file");
1282 return NULL;
1283 }
Guido van Rossum82598051997-03-05 00:20:32 +00001284 co = (PyCodeObject *)v;
1285 v = PyEval_EvalCode(co, globals, locals);
Jeremy Hyltonb857ba22001-08-10 21:41:33 +00001286 if (v && flags)
1287 flags->cf_flags |= (co->co_flags & PyCF_MASK);
Guido van Rossum82598051997-03-05 00:20:32 +00001288 Py_DECREF(co);
Guido van Rossumfdef2711994-09-14 13:31:04 +00001289 return v;
1290}
1291
Guido van Rossum82598051997-03-05 00:20:32 +00001292PyObject *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001293Py_CompileString(const char *str, const char *filename, int start)
Guido van Rossum5b722181993-03-30 17:46:03 +00001294{
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001295 return Py_CompileStringFlags(str, filename, start, NULL);
1296}
1297
1298PyObject *
Tim Petersd08e3822003-04-17 15:24:21 +00001299Py_CompileStringFlags(const char *str, const char *filename, int start,
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001300 PyCompilerFlags *flags)
1301{
Guido van Rossum5b722181993-03-30 17:46:03 +00001302 node *n;
Guido van Rossum82598051997-03-05 00:20:32 +00001303 PyCodeObject *co;
Thomas Heller6b17abf2002-07-09 09:23:27 +00001304
1305 n = PyParser_SimpleParseStringFlagsFilename(str, filename, start,
1306 PARSER_FLAGS(flags));
Guido van Rossuma110aa61994-08-29 12:50:44 +00001307 if (n == NULL)
Guido van Rossum5b722181993-03-30 17:46:03 +00001308 return NULL;
Jeremy Hylton673a4fd2001-03-26 19:53:38 +00001309 co = PyNode_CompileFlags(n, filename, flags);
Guido van Rossum82598051997-03-05 00:20:32 +00001310 PyNode_Free(n);
1311 return (PyObject *)co;
Guido van Rossum5b722181993-03-30 17:46:03 +00001312}
1313
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001314struct symtable *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001315Py_SymtableString(const char *str, const char *filename, int start)
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001316{
1317 node *n;
1318 struct symtable *st;
Thomas Heller6b17abf2002-07-09 09:23:27 +00001319 n = PyParser_SimpleParseStringFlagsFilename(str, filename,
1320 start, 0);
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001321 if (n == NULL)
1322 return NULL;
1323 st = PyNode_CompileSymtable(n, filename);
1324 PyNode_Free(n);
1325 return st;
1326}
1327
Guido van Rossuma110aa61994-08-29 12:50:44 +00001328/* Simplified interface to parsefile -- return node or set exception */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001329
Guido van Rossuma110aa61994-08-29 12:50:44 +00001330node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001331PyParser_SimpleParseFileFlags(FILE *fp, const char *filename, int start, int flags)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001332{
Guido van Rossuma110aa61994-08-29 12:50:44 +00001333 node *n;
1334 perrdetail err;
Tim Petersfe2127d2001-07-16 05:37:24 +00001335 n = PyParser_ParseFileFlags(fp, filename, &_PyParser_Grammar, start,
1336 (char *)0, (char *)0, &err, flags);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001337 if (n == NULL)
1338 err_input(&err);
1339 return n;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001340}
1341
Tim Petersfe2127d2001-07-16 05:37:24 +00001342node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001343PyParser_SimpleParseFile(FILE *fp, const char *filename, int start)
Tim Petersfe2127d2001-07-16 05:37:24 +00001344{
1345 return PyParser_SimpleParseFileFlags(fp, filename, start, 0);
1346}
1347
Guido van Rossuma110aa61994-08-29 12:50:44 +00001348/* Simplified interface to parsestring -- return node or set exception */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001349
Guido van Rossuma110aa61994-08-29 12:50:44 +00001350node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001351PyParser_SimpleParseStringFlags(const char *str, int start, int flags)
Tim Petersfe2127d2001-07-16 05:37:24 +00001352{
1353 node *n;
1354 perrdetail err;
1355 n = PyParser_ParseStringFlags(str, &_PyParser_Grammar, start, &err,
1356 flags);
1357 if (n == NULL)
1358 err_input(&err);
1359 return n;
1360}
1361
1362node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001363PyParser_SimpleParseString(const char *str, int start)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001364{
Tim Petersfe2127d2001-07-16 05:37:24 +00001365 return PyParser_SimpleParseStringFlags(str, start, 0);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001366}
1367
Thomas Heller6b17abf2002-07-09 09:23:27 +00001368node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001369PyParser_SimpleParseStringFlagsFilename(const char *str, const char *filename,
Thomas Heller6b17abf2002-07-09 09:23:27 +00001370 int start, int flags)
1371{
1372 node *n;
1373 perrdetail err;
1374
Tim Petersd08e3822003-04-17 15:24:21 +00001375 n = PyParser_ParseStringFlagsFilename(str, filename,
Thomas Heller6b17abf2002-07-09 09:23:27 +00001376 &_PyParser_Grammar,
1377 start, &err, flags);
1378 if (n == NULL)
1379 err_input(&err);
1380 return n;
1381}
1382
1383node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001384PyParser_SimpleParseStringFilename(const char *str, const char *filename, int start)
Thomas Heller6b17abf2002-07-09 09:23:27 +00001385{
1386 return PyParser_SimpleParseStringFlagsFilename(str, filename,
1387 start, 0);
1388}
1389
Guido van Rossum66ebd912003-04-17 16:02:26 +00001390/* May want to move a more generalized form of this to parsetok.c or
1391 even parser modules. */
1392
1393void
1394PyParser_SetError(perrdetail *err)
1395{
1396 err_input(err);
1397}
1398
Guido van Rossuma110aa61994-08-29 12:50:44 +00001399/* Set the error appropriate to the given input error code (see errcode.h) */
1400
1401static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001402err_input(perrdetail *err)
Guido van Rossuma110aa61994-08-29 12:50:44 +00001403{
Fred Drake85f36392000-07-11 17:53:00 +00001404 PyObject *v, *w, *errtype;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001405 PyObject* u = NULL;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001406 char *msg = NULL;
Fred Drake85f36392000-07-11 17:53:00 +00001407 errtype = PyExc_SyntaxError;
Guido van Rossum82598051997-03-05 00:20:32 +00001408 v = Py_BuildValue("(ziiz)", err->filename,
Guido van Rossuma110aa61994-08-29 12:50:44 +00001409 err->lineno, err->offset, err->text);
1410 if (err->text != NULL) {
Guido van Rossumb18618d2000-05-03 23:44:39 +00001411 PyMem_DEL(err->text);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001412 err->text = NULL;
1413 }
1414 switch (err->error) {
1415 case E_SYNTAX:
Fred Drake85f36392000-07-11 17:53:00 +00001416 errtype = PyExc_IndentationError;
1417 if (err->expected == INDENT)
1418 msg = "expected an indented block";
1419 else if (err->token == INDENT)
1420 msg = "unexpected indent";
1421 else if (err->token == DEDENT)
1422 msg = "unexpected unindent";
1423 else {
1424 errtype = PyExc_SyntaxError;
1425 msg = "invalid syntax";
1426 }
Guido van Rossuma110aa61994-08-29 12:50:44 +00001427 break;
1428 case E_TOKEN:
1429 msg = "invalid token";
Guido van Rossuma110aa61994-08-29 12:50:44 +00001430 break;
Skip Montanaro118ec702002-08-15 01:20:16 +00001431 case E_EOFS:
1432 msg = "EOF while scanning triple-quoted string";
1433 break;
1434 case E_EOLS:
1435 msg = "EOL while scanning single-quoted string";
1436 break;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001437 case E_INTR:
Guido van Rossum82598051997-03-05 00:20:32 +00001438 PyErr_SetNone(PyExc_KeyboardInterrupt);
Barry Warsawc80baa31999-01-27 16:39:40 +00001439 Py_XDECREF(v);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001440 return;
1441 case E_NOMEM:
Guido van Rossum82598051997-03-05 00:20:32 +00001442 PyErr_NoMemory();
Barry Warsawc80baa31999-01-27 16:39:40 +00001443 Py_XDECREF(v);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001444 return;
1445 case E_EOF:
1446 msg = "unexpected EOF while parsing";
1447 break;
Fred Drake85f36392000-07-11 17:53:00 +00001448 case E_TABSPACE:
1449 errtype = PyExc_TabError;
Guido van Rossum560e8ad1998-04-10 19:43:42 +00001450 msg = "inconsistent use of tabs and spaces in indentation";
1451 break;
Jeremy Hylton94988062000-06-20 19:10:44 +00001452 case E_OVERFLOW:
1453 msg = "expression too long";
1454 break;
Fred Drake85f36392000-07-11 17:53:00 +00001455 case E_DEDENT:
1456 errtype = PyExc_IndentationError;
1457 msg = "unindent does not match any outer indentation level";
1458 break;
1459 case E_TOODEEP:
1460 errtype = PyExc_IndentationError;
1461 msg = "too many levels of indentation";
1462 break;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001463 case E_DECODE: { /* XXX */
1464 PyThreadState* tstate = PyThreadState_Get();
1465 PyObject* value = tstate->curexc_value;
1466 if (value != NULL) {
1467 u = PyObject_Repr(value);
1468 if (u != NULL) {
1469 msg = PyString_AsString(u);
1470 break;
1471 }
1472 }
1473 }
Guido van Rossuma110aa61994-08-29 12:50:44 +00001474 default:
1475 fprintf(stderr, "error=%d\n", err->error);
1476 msg = "unknown parsing error";
1477 break;
1478 }
Guido van Rossum82598051997-03-05 00:20:32 +00001479 w = Py_BuildValue("(sO)", msg, v);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001480 Py_XDECREF(u);
Guido van Rossum41318302001-03-23 04:01:07 +00001481 Py_XDECREF(v);
Fred Drake85f36392000-07-11 17:53:00 +00001482 PyErr_SetObject(errtype, w);
Guido van Rossum82598051997-03-05 00:20:32 +00001483 Py_XDECREF(w);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001484}
1485
1486/* Print fatal error message and abort */
1487
1488void
Tim Peters7c321a82002-07-09 02:57:01 +00001489Py_FatalError(const char *msg)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001490{
Guido van Rossum83dd6c31994-09-29 09:38:33 +00001491 fprintf(stderr, "Fatal Python error: %s\n", msg);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001492#ifdef MS_WINDOWS
Guido van Rossum23c94461997-05-22 20:21:30 +00001493 OutputDebugString("Fatal Python error: ");
Guido van Rossuma44823b1995-03-14 15:01:17 +00001494 OutputDebugString(msg);
1495 OutputDebugString("\n");
Guido van Rossum0ba35361998-08-13 13:33:16 +00001496#ifdef _DEBUG
1497 DebugBreak();
Guido van Rossuma44823b1995-03-14 15:01:17 +00001498#endif
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001499#endif /* MS_WINDOWS */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001500 abort();
1501}
1502
1503/* Clean up and exit */
1504
Guido van Rossuma110aa61994-08-29 12:50:44 +00001505#ifdef WITH_THREAD
Guido van Rossum49b56061998-10-01 20:42:43 +00001506#include "pythread.h"
Guido van Rossum82598051997-03-05 00:20:32 +00001507int _PyThread_Started = 0; /* Set by threadmodule.c and maybe others */
Guido van Rossumf9f2e821992-08-17 08:59:08 +00001508#endif
1509
Guido van Rossum2dcfc961998-10-01 16:01:57 +00001510#define NEXITFUNCS 32
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001511static void (*exitfuncs[NEXITFUNCS])(void);
Guido van Rossum1662dd51994-09-07 14:38:28 +00001512static int nexitfuncs = 0;
1513
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001514int Py_AtExit(void (*func)(void))
Guido van Rossum1662dd51994-09-07 14:38:28 +00001515{
1516 if (nexitfuncs >= NEXITFUNCS)
1517 return -1;
1518 exitfuncs[nexitfuncs++] = func;
1519 return 0;
1520}
1521
Guido van Rossumcc283f51997-08-05 02:22:03 +00001522static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001523call_sys_exitfunc(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001524{
Guido van Rossum82598051997-03-05 00:20:32 +00001525 PyObject *exitfunc = PySys_GetObject("exitfunc");
Guido van Rossum59bff391992-09-03 20:28:00 +00001526
1527 if (exitfunc) {
Fred Drake6a12d8d2001-03-23 17:34:02 +00001528 PyObject *res;
Guido van Rossum82598051997-03-05 00:20:32 +00001529 Py_INCREF(exitfunc);
1530 PySys_SetObject("exitfunc", (PyObject *)NULL);
1531 res = PyEval_CallObject(exitfunc, (PyObject *)NULL);
Guido van Rossum59bff391992-09-03 20:28:00 +00001532 if (res == NULL) {
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001533 if (!PyErr_ExceptionMatches(PyExc_SystemExit)) {
1534 PySys_WriteStderr("Error in sys.exitfunc:\n");
1535 }
Guido van Rossum82598051997-03-05 00:20:32 +00001536 PyErr_Print();
Guido van Rossum59bff391992-09-03 20:28:00 +00001537 }
Guido van Rossum82598051997-03-05 00:20:32 +00001538 Py_DECREF(exitfunc);
Guido van Rossum59bff391992-09-03 20:28:00 +00001539 }
1540
Guido van Rossum0829c751998-02-28 04:31:39 +00001541 if (Py_FlushLine())
1542 PyErr_Clear();
Guido van Rossumcc283f51997-08-05 02:22:03 +00001543}
Guido van Rossum1662dd51994-09-07 14:38:28 +00001544
Guido van Rossumcc283f51997-08-05 02:22:03 +00001545static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001546call_ll_exitfuncs(void)
Guido van Rossumcc283f51997-08-05 02:22:03 +00001547{
Guido van Rossum1662dd51994-09-07 14:38:28 +00001548 while (nexitfuncs > 0)
1549 (*exitfuncs[--nexitfuncs])();
Guido van Rossum25ce5661997-08-02 03:10:38 +00001550
1551 fflush(stdout);
1552 fflush(stderr);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001553}
1554
1555void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001556Py_Exit(int sts)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001557{
Guido van Rossumcc283f51997-08-05 02:22:03 +00001558 Py_Finalize();
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001559
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001560 exit(sts);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001561}
1562
Guido van Rossumf1dc5661993-07-05 10:31:29 +00001563static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001564initsigs(void)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001565{
Guido van Rossuma110aa61994-08-29 12:50:44 +00001566#ifdef HAVE_SIGNAL_H
1567#ifdef SIGPIPE
1568 signal(SIGPIPE, SIG_IGN);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001569#endif
Guido van Rossum70d893a2001-08-16 08:21:42 +00001570#ifdef SIGXFZ
1571 signal(SIGXFZ, SIG_IGN);
1572#endif
Jeremy Hylton1b0bf9b2002-04-23 20:31:01 +00001573#ifdef SIGXFSZ
1574 signal(SIGXFSZ, SIG_IGN);
1575#endif
Guido van Rossuma110aa61994-08-29 12:50:44 +00001576#endif /* HAVE_SIGNAL_H */
Guido van Rossum82598051997-03-05 00:20:32 +00001577 PyOS_InitInterrupts(); /* May imply initsignal() */
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001578}
1579
Guido van Rossum7433b121997-02-14 19:45:36 +00001580
1581/*
1582 * The file descriptor fd is considered ``interactive'' if either
1583 * a) isatty(fd) is TRUE, or
1584 * b) the -i flag was given, and the filename associated with
1585 * the descriptor is NULL or "<stdin>" or "???".
1586 */
1587int
Martin v. Löwis95292d62002-12-11 14:04:59 +00001588Py_FdIsInteractive(FILE *fp, const char *filename)
Guido van Rossum7433b121997-02-14 19:45:36 +00001589{
1590 if (isatty((int)fileno(fp)))
1591 return 1;
1592 if (!Py_InteractiveFlag)
1593 return 0;
1594 return (filename == NULL) ||
1595 (strcmp(filename, "<stdin>") == 0) ||
1596 (strcmp(filename, "???") == 0);
1597}
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001598
1599
Tim Petersd08e3822003-04-17 15:24:21 +00001600#if defined(USE_STACKCHECK)
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001601#if defined(WIN32) && defined(_MSC_VER)
1602
1603/* Stack checking for Microsoft C */
1604
1605#include <malloc.h>
1606#include <excpt.h>
1607
Fred Drakee8de31c2000-08-31 05:38:39 +00001608/*
1609 * Return non-zero when we run out of memory on the stack; zero otherwise.
1610 */
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001611int
Fred Drake399739f2000-08-31 05:52:44 +00001612PyOS_CheckStack(void)
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001613{
1614 __try {
Tim Peters92e4dd82002-10-05 01:47:34 +00001615 /* alloca throws a stack overflow exception if there's
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001616 not enough space left on the stack */
Tim Peters92e4dd82002-10-05 01:47:34 +00001617 alloca(PYOS_STACK_MARGIN * sizeof(void*));
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001618 return 0;
1619 } __except (EXCEPTION_EXECUTE_HANDLER) {
1620 /* just ignore all errors */
1621 }
1622 return 1;
1623}
1624
1625#endif /* WIN32 && _MSC_VER */
1626
1627/* Alternate implementations can be added here... */
1628
1629#endif /* USE_STACKCHECK */
Guido van Rossum6f256182000-09-16 16:32:19 +00001630
1631
1632/* Wrappers around sigaction() or signal(). */
1633
1634PyOS_sighandler_t
1635PyOS_getsig(int sig)
1636{
1637#ifdef HAVE_SIGACTION
1638 struct sigaction context;
Barry Warsawafeb2a42001-11-13 23:08:26 +00001639 /* Initialize context.sa_handler to SIG_ERR which makes about as
1640 * much sense as anything else. It should get overwritten if
1641 * sigaction actually succeeds and otherwise we avoid an
1642 * uninitialized memory read.
1643 */
1644 context.sa_handler = SIG_ERR;
Guido van Rossum6f256182000-09-16 16:32:19 +00001645 sigaction(sig, NULL, &context);
1646 return context.sa_handler;
1647#else
1648 PyOS_sighandler_t handler;
1649 handler = signal(sig, SIG_IGN);
1650 signal(sig, handler);
1651 return handler;
1652#endif
1653}
1654
1655PyOS_sighandler_t
1656PyOS_setsig(int sig, PyOS_sighandler_t handler)
1657{
1658#ifdef HAVE_SIGACTION
1659 struct sigaction context;
1660 PyOS_sighandler_t oldhandler;
Barry Warsawafeb2a42001-11-13 23:08:26 +00001661 /* Initialize context.sa_handler to SIG_ERR which makes about as
1662 * much sense as anything else. It should get overwritten if
1663 * sigaction actually succeeds and otherwise we avoid an
1664 * uninitialized memory read.
1665 */
1666 context.sa_handler = SIG_ERR;
Guido van Rossum6f256182000-09-16 16:32:19 +00001667 sigaction(sig, NULL, &context);
1668 oldhandler = context.sa_handler;
1669 context.sa_handler = handler;
1670 sigaction(sig, &context, NULL);
1671 return oldhandler;
1672#else
1673 return signal(sig, handler);
1674#endif
1675}