blob: 621ce9d3a2d51b7408373133ce625ada2e3c3e2f [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 Rossum80bb9651996-12-05 23:27:02 +000016#ifdef HAVE_UNISTD_H
17#include <unistd.h>
18#endif
19
Guido van Rossuma110aa61994-08-29 12:50:44 +000020#ifdef HAVE_SIGNAL_H
Guido van Rossuma9e7dc11992-10-18 18:53:57 +000021#include <signal.h>
Guido van Rossuma9e7dc11992-10-18 18:53:57 +000022#endif
23
Guido van Rossum9b38a141996-09-11 23:12:24 +000024#ifdef MS_WIN32
Guido van Rossuma44823b1995-03-14 15:01:17 +000025#undef BYTE
26#include "windows.h"
27#endif
28
Jack Jansencbf630f2000-07-11 21:59:16 +000029#ifdef macintosh
30#include "macglue.h"
31#endif
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000032extern char *Py_GetPath(void);
Guido van Rossum1984f1e1992-08-04 12:41:02 +000033
Guido van Rossum82598051997-03-05 00:20:32 +000034extern grammar _PyParser_Grammar; /* From graminit.c */
Guido van Rossum1984f1e1992-08-04 12:41:02 +000035
Guido van Rossumb73cc041993-11-01 16:28:59 +000036/* Forward */
Tim Petersdbd9ba62000-07-09 03:09:57 +000037static void initmain(void);
38static void initsite(void);
Jeremy Hylton9f324e92001-03-01 22:59:14 +000039static PyObject *run_err_node(node *, char *, PyObject *, PyObject *,
40 PyCompilerFlags *);
41static PyObject *run_node(node *, char *, PyObject *, PyObject *,
42 PyCompilerFlags *);
Jeremy Hyltonbc320242001-03-22 02:47:58 +000043static PyObject *run_pyc_file(FILE *, char *, PyObject *, PyObject *,
44 PyCompilerFlags *);
Tim Petersdbd9ba62000-07-09 03:09:57 +000045static void err_input(perrdetail *);
46static void initsigs(void);
47static void call_sys_exitfunc(void);
48static void call_ll_exitfuncs(void);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +000049
Guido van Rossumbffd6832000-01-20 22:32:56 +000050#ifdef Py_TRACE_REFS
51int _Py_AskYesNo(char *prompt);
52#endif
53
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000054extern void _PyUnicode_Init(void);
55extern void _PyUnicode_Fini(void);
56extern void _PyCodecRegistry_Init(void);
57extern void _PyCodecRegistry_Fini(void);
Guido van Rossumc94044c2000-03-10 23:03:54 +000058
Guido van Rossum82598051997-03-05 00:20:32 +000059int Py_DebugFlag; /* Needed by parser.c */
60int Py_VerboseFlag; /* Needed by import.c */
Guido van Rossum7433b121997-02-14 19:45:36 +000061int Py_InteractiveFlag; /* Needed by Py_FdIsInteractive() below */
Guido van Rossumdcc0c131997-08-29 22:32:42 +000062int Py_NoSiteFlag; /* Suppress 'import site' */
Barry Warsaw3ce09642000-05-02 19:18:59 +000063int Py_UseClassExceptionsFlag = 1; /* Needed by bltinmodule.c: deprecated */
Guido van Rossuma61691e1998-02-06 22:27:24 +000064int Py_FrozenFlag; /* Needed by getpath.c */
Guido van Rossumb16d1972000-05-01 17:55:15 +000065int Py_UnicodeFlag = 0; /* Needed by compile.c */
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +000066int Py_IgnoreEnvironmentFlag; /* e.g. PYTHONPATH, PYTHONHOME */
Guido van Rossum1984f1e1992-08-04 12:41:02 +000067
Guido van Rossum25ce5661997-08-02 03:10:38 +000068static int initialized = 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +000069
Thomas Wouters7e474022000-07-16 12:04:32 +000070/* API to access the initialized flag -- useful for esoteric use */
Guido van Rossume3c0d5e1997-08-22 04:20:13 +000071
72int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000073Py_IsInitialized(void)
Guido van Rossume3c0d5e1997-08-22 04:20:13 +000074{
75 return initialized;
76}
77
Guido van Rossum25ce5661997-08-02 03:10:38 +000078/* Global initializations. Can be undone by Py_Finalize(). Don't
79 call this twice without an intervening Py_Finalize() call. When
80 initializations fail, a fatal error is issued and the function does
81 not return. On return, the first thread and interpreter state have
82 been created.
Guido van Rossum1984f1e1992-08-04 12:41:02 +000083
Guido van Rossum25ce5661997-08-02 03:10:38 +000084 Locking: you must hold the interpreter lock while calling this.
85 (If the lock has not yet been initialized, that's equivalent to
86 having the lock, but you cannot use multiple threads.)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +000087
Guido van Rossum25ce5661997-08-02 03:10:38 +000088*/
Guido van Rossuma027efa1997-05-05 20:56:21 +000089
90void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000091Py_Initialize(void)
Guido van Rossuma027efa1997-05-05 20:56:21 +000092{
Guido van Rossuma027efa1997-05-05 20:56:21 +000093 PyInterpreterState *interp;
Guido van Rossum25ce5661997-08-02 03:10:38 +000094 PyThreadState *tstate;
95 PyObject *bimod, *sysmod;
Guido van Rossumad6dfda1997-07-19 19:17:22 +000096 char *p;
Guido van Rossum70d893a2001-08-16 08:21:42 +000097 extern void _Py_ReadyTypes(void);
Guido van Rossumad6dfda1997-07-19 19:17:22 +000098
Guido van Rossumdcc0c131997-08-29 22:32:42 +000099 if (initialized)
Guido van Rossumaa615051997-08-20 22:40:18 +0000100 return;
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000101 initialized = 1;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000102
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000103 if ((p = Py_GETENV("PYTHONDEBUG")) && *p != '\0')
Marc-André Lemburgdc3d6062000-08-25 21:00:46 +0000104 Py_DebugFlag = Py_DebugFlag ? Py_DebugFlag : 1;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000105 if ((p = Py_GETENV("PYTHONVERBOSE")) && *p != '\0')
Marc-André Lemburgdc3d6062000-08-25 21:00:46 +0000106 Py_VerboseFlag = Py_VerboseFlag ? Py_VerboseFlag : 1;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000107 if ((p = Py_GETENV("PYTHONOPTIMIZE")) && *p != '\0')
Marc-André Lemburgdc3d6062000-08-25 21:00:46 +0000108 Py_OptimizeFlag = Py_OptimizeFlag ? Py_OptimizeFlag : 1;
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000109
Guido van Rossuma027efa1997-05-05 20:56:21 +0000110 interp = PyInterpreterState_New();
111 if (interp == NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000112 Py_FatalError("Py_Initialize: can't make first interpreter");
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000113
Guido van Rossuma027efa1997-05-05 20:56:21 +0000114 tstate = PyThreadState_New(interp);
115 if (tstate == NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000116 Py_FatalError("Py_Initialize: can't make first thread");
Guido van Rossuma027efa1997-05-05 20:56:21 +0000117 (void) PyThreadState_Swap(tstate);
118
Guido van Rossum70d893a2001-08-16 08:21:42 +0000119 _Py_ReadyTypes();
Guido van Rossum528b7eb2001-08-07 17:24:28 +0000120
Guido van Rossum25ce5661997-08-02 03:10:38 +0000121 interp->modules = PyDict_New();
122 if (interp->modules == NULL)
123 Py_FatalError("Py_Initialize: can't make modules dictionary");
Guido van Rossuma027efa1997-05-05 20:56:21 +0000124
Guido van Rossumc94044c2000-03-10 23:03:54 +0000125 /* Init codec registry */
126 _PyCodecRegistry_Init();
127
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000128#ifdef Py_USING_UNICODE
Guido van Rossumc94044c2000-03-10 23:03:54 +0000129 /* Init Unicode implementation; relies on the codec registry */
130 _PyUnicode_Init();
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000131#endif
Guido van Rossumc94044c2000-03-10 23:03:54 +0000132
Barry Warsawf242aa02000-05-25 23:09:49 +0000133 bimod = _PyBuiltin_Init();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000134 if (bimod == NULL)
135 Py_FatalError("Py_Initialize: can't initialize __builtin__");
Guido van Rossum4a1f39a1997-11-04 19:36:18 +0000136 interp->builtins = PyModule_GetDict(bimod);
137 Py_INCREF(interp->builtins);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000138
139 sysmod = _PySys_Init();
140 if (sysmod == NULL)
141 Py_FatalError("Py_Initialize: can't initialize sys");
142 interp->sysdict = PyModule_GetDict(sysmod);
143 Py_INCREF(interp->sysdict);
144 _PyImport_FixupExtension("sys", "sys");
Guido van Rossuma027efa1997-05-05 20:56:21 +0000145 PySys_SetPath(Py_GetPath());
Guido van Rossum25ce5661997-08-02 03:10:38 +0000146 PyDict_SetItemString(interp->sysdict, "modules",
147 interp->modules);
148
Guido van Rossum7c85ab81999-07-08 17:26:56 +0000149 _PyImport_Init();
150
Barry Warsawf242aa02000-05-25 23:09:49 +0000151 /* initialize builtin exceptions */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000152 _PyExc_Init();
Barry Warsaw5821bc52001-08-13 23:04:56 +0000153 _PyImport_FixupExtension("exceptions", "exceptions");
Barry Warsawf242aa02000-05-25 23:09:49 +0000154
Barry Warsaw035574d1997-08-29 22:07:17 +0000155 /* phase 2 of builtins */
Barry Warsaw963b8711997-09-18 16:42:02 +0000156 _PyImport_FixupExtension("__builtin__", "__builtin__");
Barry Warsaw035574d1997-08-29 22:07:17 +0000157
Guido van Rossum25ce5661997-08-02 03:10:38 +0000158 initsigs(); /* Signal handling stuff, including initintr() */
159
160 initmain(); /* Module __main__ */
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000161 if (!Py_NoSiteFlag)
162 initsite(); /* Module site */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000163}
164
Guido van Rossum2edcf0d1998-12-15 16:12:00 +0000165#ifdef COUNT_ALLOCS
Tim Petersdbd9ba62000-07-09 03:09:57 +0000166extern void dump_counts(void);
Guido van Rossum2edcf0d1998-12-15 16:12:00 +0000167#endif
168
Guido van Rossum25ce5661997-08-02 03:10:38 +0000169/* Undo the effect of Py_Initialize().
170
171 Beware: if multiple interpreter and/or thread states exist, these
172 are not wiped out; only the current thread and interpreter state
173 are deleted. But since everything else is deleted, those other
174 interpreter and thread states should no longer be used.
175
176 (XXX We should do better, e.g. wipe out all interpreters and
177 threads.)
178
179 Locking: as above.
180
181*/
182
183void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000184Py_Finalize(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000185{
186 PyInterpreterState *interp;
187 PyThreadState *tstate;
188
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000189 if (!initialized)
Guido van Rossumaa615051997-08-20 22:40:18 +0000190 return;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000191
Tim Peters384fd102001-01-21 03:40:37 +0000192 /* The interpreter is still entirely intact at this point, and the
193 * exit funcs may be relying on that. In particular, if some thread
194 * or exit func is still waiting to do an import, the import machinery
195 * expects Py_IsInitialized() to return true. So don't say the
196 * interpreter is uninitialized until after the exit funcs have run.
197 * Note that Threading.py uses an exit func to do a join on all the
198 * threads created thru it, so this also protects pending imports in
199 * the threads created via Threading.
200 */
Guido van Rossum4cc462e1998-01-19 22:00:38 +0000201 call_sys_exitfunc();
Tim Peters384fd102001-01-21 03:40:37 +0000202 initialized = 0;
Guido van Rossum4cc462e1998-01-19 22:00:38 +0000203
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000204 /* Get current thread state and interpreter pointer */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000205 tstate = PyThreadState_Get();
206 interp = tstate->interp;
207
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000208 /* Disable signal handling */
209 PyOS_FiniInterrupts();
210
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000211#ifdef Py_USING_UNICODE
Guido van Rossumc94044c2000-03-10 23:03:54 +0000212 /* Cleanup Unicode implementation */
213 _PyUnicode_Fini();
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000214#endif
Guido van Rossumc94044c2000-03-10 23:03:54 +0000215
216 /* Cleanup Codec registry */
217 _PyCodecRegistry_Fini();
218
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000219 /* Destroy all modules */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000220 PyImport_Cleanup();
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000221
Guido van Rossum1707aad1997-12-08 23:43:45 +0000222 /* Destroy the database used by _PyImport_{Fixup,Find}Extension */
223 _PyImport_Fini();
224
225 /* Debugging stuff */
226#ifdef COUNT_ALLOCS
227 dump_counts();
228#endif
229
230#ifdef Py_REF_DEBUG
231 fprintf(stderr, "[%ld refs]\n", _Py_RefTotal);
232#endif
233
234#ifdef Py_TRACE_REFS
Guido van Rossum92e2d5c2001-08-09 16:37:16 +0000235 if (Py_GETENV("PYTHONDUMPREFS")) {
Guido van Rossum1707aad1997-12-08 23:43:45 +0000236 _Py_PrintReferences(stderr);
237 }
238#endif /* Py_TRACE_REFS */
239
Barry Warsaw035574d1997-08-29 22:07:17 +0000240 /* Now we decref the exception classes. After this point nothing
241 can raise an exception. That's okay, because each Fini() method
242 below has been checked to make sure no exceptions are ever
243 raised.
244 */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000245 _PyExc_Fini();
Barry Warsawf242aa02000-05-25 23:09:49 +0000246
247 /* Delete current thread */
248 PyInterpreterState_Clear(interp);
249 PyThreadState_Swap(NULL);
250 PyInterpreterState_Delete(interp);
251
Guido van Rossumcc283f51997-08-05 02:22:03 +0000252 PyMethod_Fini();
253 PyFrame_Fini();
254 PyCFunction_Fini();
255 PyTuple_Fini();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000256 PyString_Fini();
Guido van Rossumcc283f51997-08-05 02:22:03 +0000257 PyInt_Fini();
258 PyFloat_Fini();
259
260 /* XXX Still allocated:
261 - various static ad-hoc pointers to interned strings
262 - int and float free list blocks
263 - whatever various modules and libraries allocate
264 */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000265
266 PyGrammar_RemoveAccelerators(&_PyParser_Grammar);
Guido van Rossumcc283f51997-08-05 02:22:03 +0000267
268 call_ll_exitfuncs();
269
Guido van Rossumcc283f51997-08-05 02:22:03 +0000270#ifdef Py_TRACE_REFS
Guido van Rossumcc283f51997-08-05 02:22:03 +0000271 _Py_ResetReferences();
272#endif /* Py_TRACE_REFS */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000273}
274
275/* Create and initialize a new interpreter and thread, and return the
276 new thread. This requires that Py_Initialize() has been called
277 first.
278
279 Unsuccessful initialization yields a NULL pointer. Note that *no*
280 exception information is available even in this case -- the
281 exception information is held in the thread, and there is no
282 thread.
283
284 Locking: as above.
285
286*/
287
288PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000289Py_NewInterpreter(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000290{
291 PyInterpreterState *interp;
292 PyThreadState *tstate, *save_tstate;
293 PyObject *bimod, *sysmod;
294
295 if (!initialized)
296 Py_FatalError("Py_NewInterpreter: call Py_Initialize first");
297
298 interp = PyInterpreterState_New();
299 if (interp == NULL)
300 return NULL;
301
302 tstate = PyThreadState_New(interp);
303 if (tstate == NULL) {
304 PyInterpreterState_Delete(interp);
305 return NULL;
306 }
307
308 save_tstate = PyThreadState_Swap(tstate);
309
310 /* XXX The following is lax in error checking */
311
312 interp->modules = PyDict_New();
313
314 bimod = _PyImport_FindExtension("__builtin__", "__builtin__");
315 if (bimod != NULL) {
Guido van Rossum4a1f39a1997-11-04 19:36:18 +0000316 interp->builtins = PyModule_GetDict(bimod);
317 Py_INCREF(interp->builtins);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000318 }
319 sysmod = _PyImport_FindExtension("sys", "sys");
320 if (bimod != NULL && sysmod != NULL) {
321 interp->sysdict = PyModule_GetDict(sysmod);
322 Py_INCREF(interp->sysdict);
323 PySys_SetPath(Py_GetPath());
324 PyDict_SetItemString(interp->sysdict, "modules",
325 interp->modules);
326 initmain();
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000327 if (!Py_NoSiteFlag)
328 initsite();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000329 }
330
331 if (!PyErr_Occurred())
332 return tstate;
333
334 /* Oops, it didn't work. Undo it all. */
335
336 PyErr_Print();
337 PyThreadState_Clear(tstate);
338 PyThreadState_Swap(save_tstate);
339 PyThreadState_Delete(tstate);
340 PyInterpreterState_Delete(interp);
341
342 return NULL;
343}
344
345/* Delete an interpreter and its last thread. This requires that the
346 given thread state is current, that the thread has no remaining
347 frames, and that it is its interpreter's only remaining thread.
348 It is a fatal error to violate these constraints.
349
350 (Py_Finalize() doesn't have these constraints -- it zaps
351 everything, regardless.)
352
353 Locking: as above.
354
355*/
356
357void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000358Py_EndInterpreter(PyThreadState *tstate)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000359{
360 PyInterpreterState *interp = tstate->interp;
361
362 if (tstate != PyThreadState_Get())
363 Py_FatalError("Py_EndInterpreter: thread is not current");
364 if (tstate->frame != NULL)
365 Py_FatalError("Py_EndInterpreter: thread still has a frame");
366 if (tstate != interp->tstate_head || tstate->next != NULL)
367 Py_FatalError("Py_EndInterpreter: not the last thread");
368
369 PyImport_Cleanup();
370 PyInterpreterState_Clear(interp);
371 PyThreadState_Swap(NULL);
372 PyInterpreterState_Delete(interp);
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000373}
374
375static char *progname = "python";
376
377void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000378Py_SetProgramName(char *pn)
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000379{
380 if (pn && *pn)
381 progname = pn;
382}
383
384char *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000385Py_GetProgramName(void)
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000386{
387 return progname;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000388}
389
Guido van Rossuma61691e1998-02-06 22:27:24 +0000390static char *default_home = NULL;
391
392void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000393Py_SetPythonHome(char *home)
Guido van Rossuma61691e1998-02-06 22:27:24 +0000394{
395 default_home = home;
396}
397
398char *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000399Py_GetPythonHome(void)
Guido van Rossuma61691e1998-02-06 22:27:24 +0000400{
401 char *home = default_home;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000402 if (home == NULL && !Py_IgnoreEnvironmentFlag)
403 home = Py_GETENV("PYTHONHOME");
Guido van Rossuma61691e1998-02-06 22:27:24 +0000404 return home;
405}
406
Guido van Rossum6135a871995-01-09 17:53:26 +0000407/* Create __main__ module */
408
409static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000410initmain(void)
Guido van Rossum6135a871995-01-09 17:53:26 +0000411{
Guido van Rossum82598051997-03-05 00:20:32 +0000412 PyObject *m, *d;
413 m = PyImport_AddModule("__main__");
Guido van Rossum6135a871995-01-09 17:53:26 +0000414 if (m == NULL)
Guido van Rossum82598051997-03-05 00:20:32 +0000415 Py_FatalError("can't create __main__ module");
416 d = PyModule_GetDict(m);
417 if (PyDict_GetItemString(d, "__builtins__") == NULL) {
Guido van Rossum858cb731997-11-19 16:15:37 +0000418 PyObject *bimod = PyImport_ImportModule("__builtin__");
419 if (bimod == NULL ||
420 PyDict_SetItemString(d, "__builtins__", bimod) != 0)
Guido van Rossum82598051997-03-05 00:20:32 +0000421 Py_FatalError("can't add __builtins__ to __main__");
Barry Warsaw3d05b1a1999-01-29 21:30:22 +0000422 Py_DECREF(bimod);
Guido van Rossum6135a871995-01-09 17:53:26 +0000423 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000424}
425
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000426/* Import the site module (not into __main__ though) */
427
428static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000429initsite(void)
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000430{
431 PyObject *m, *f;
432 m = PyImport_ImportModule("site");
433 if (m == NULL) {
434 f = PySys_GetObject("stderr");
435 if (Py_VerboseFlag) {
436 PyFile_WriteString(
437 "'import site' failed; traceback:\n", f);
438 PyErr_Print();
439 }
440 else {
441 PyFile_WriteString(
442 "'import site' failed; use -v for traceback\n", f);
443 PyErr_Clear();
444 }
445 }
446 else {
447 Py_DECREF(m);
448 }
449}
450
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000451/* Parse input from a file and execute it */
452
453int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000454PyRun_AnyFile(FILE *fp, char *filename)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000455{
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000456 return PyRun_AnyFileExFlags(fp, filename, 0, NULL);
457}
458
459int
460PyRun_AnyFileFlags(FILE *fp, char *filename, PyCompilerFlags *flags)
461{
462 return PyRun_AnyFileExFlags(fp, filename, 0, flags);
Guido van Rossum0df002c2000-08-27 19:21:52 +0000463}
464
465int
466PyRun_AnyFileEx(FILE *fp, char *filename, int closeit)
467{
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000468 return PyRun_AnyFileExFlags(fp, filename, closeit, NULL);
469}
470
471int
472PyRun_AnyFileExFlags(FILE *fp, char *filename, int closeit,
473 PyCompilerFlags *flags)
474{
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000475 if (filename == NULL)
476 filename = "???";
Guido van Rossum0df002c2000-08-27 19:21:52 +0000477 if (Py_FdIsInteractive(fp, filename)) {
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000478 int err = PyRun_InteractiveLoopFlags(fp, filename, flags);
Guido van Rossum0df002c2000-08-27 19:21:52 +0000479 if (closeit)
480 fclose(fp);
481 return err;
482 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000483 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000484 return PyRun_SimpleFileExFlags(fp, filename, closeit, flags);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000485}
486
487int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000488PyRun_InteractiveLoop(FILE *fp, char *filename)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000489{
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000490 return PyRun_InteractiveLoopFlags(fp, filename, NULL);
491}
492
493int
494PyRun_InteractiveLoopFlags(FILE *fp, char *filename, PyCompilerFlags *flags)
495{
Guido van Rossum82598051997-03-05 00:20:32 +0000496 PyObject *v;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000497 int ret;
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000498 PyCompilerFlags local_flags;
Jeremy Hylton9f324e92001-03-01 22:59:14 +0000499
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000500 if (flags == NULL) {
501 flags = &local_flags;
Tim Peters5ba58662001-07-16 02:29:45 +0000502 local_flags.cf_flags = 0;
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000503 }
Guido van Rossum82598051997-03-05 00:20:32 +0000504 v = PySys_GetObject("ps1");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000505 if (v == NULL) {
Guido van Rossum82598051997-03-05 00:20:32 +0000506 PySys_SetObject("ps1", v = PyString_FromString(">>> "));
507 Py_XDECREF(v);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000508 }
Guido van Rossum82598051997-03-05 00:20:32 +0000509 v = PySys_GetObject("ps2");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000510 if (v == NULL) {
Guido van Rossum82598051997-03-05 00:20:32 +0000511 PySys_SetObject("ps2", v = PyString_FromString("... "));
512 Py_XDECREF(v);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000513 }
514 for (;;) {
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000515 ret = PyRun_InteractiveOneFlags(fp, filename, flags);
Guido van Rossumaae0d321996-05-22 16:35:33 +0000516#ifdef Py_REF_DEBUG
Guido van Rossum6f9e4331995-03-29 16:57:48 +0000517 fprintf(stderr, "[%ld refs]\n", _Py_RefTotal);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000518#endif
519 if (ret == E_EOF)
520 return 0;
521 /*
522 if (ret == E_NOMEM)
523 return -1;
524 */
525 }
526}
527
528int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000529PyRun_InteractiveOne(FILE *fp, char *filename)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000530{
Jeremy Hylton9f324e92001-03-01 22:59:14 +0000531 return PyRun_InteractiveOneFlags(fp, filename, NULL);
532}
533
534int
535PyRun_InteractiveOneFlags(FILE *fp, char *filename, PyCompilerFlags *flags)
536{
Guido van Rossum82598051997-03-05 00:20:32 +0000537 PyObject *m, *d, *v, *w;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000538 node *n;
Guido van Rossuma110aa61994-08-29 12:50:44 +0000539 perrdetail err;
Guido van Rossumddc3fb51997-11-25 20:58:13 +0000540 char *ps1 = "", *ps2 = "";
Tim Petersfe2127d2001-07-16 05:37:24 +0000541
Guido van Rossum82598051997-03-05 00:20:32 +0000542 v = PySys_GetObject("ps1");
Guido van Rossumddc3fb51997-11-25 20:58:13 +0000543 if (v != NULL) {
544 v = PyObject_Str(v);
545 if (v == NULL)
546 PyErr_Clear();
547 else if (PyString_Check(v))
548 ps1 = PyString_AsString(v);
549 }
Guido van Rossum82598051997-03-05 00:20:32 +0000550 w = PySys_GetObject("ps2");
Guido van Rossumddc3fb51997-11-25 20:58:13 +0000551 if (w != NULL) {
552 w = PyObject_Str(w);
553 if (w == NULL)
554 PyErr_Clear();
555 else if (PyString_Check(w))
556 ps2 = PyString_AsString(w);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000557 }
Tim Petersfe2127d2001-07-16 05:37:24 +0000558 n = PyParser_ParseFileFlags(fp, filename, &_PyParser_Grammar,
559 Py_single_input, ps1, ps2, &err,
560 (flags &&
Jeremy Hyltonb857ba22001-08-10 21:41:33 +0000561 flags->cf_flags & CO_GENERATOR_ALLOWED) ?
Tim Petersfe2127d2001-07-16 05:37:24 +0000562 PyPARSE_YIELD_IS_KEYWORD : 0);
Guido van Rossum82598051997-03-05 00:20:32 +0000563 Py_XDECREF(v);
564 Py_XDECREF(w);
Guido van Rossuma110aa61994-08-29 12:50:44 +0000565 if (n == NULL) {
566 if (err.error == E_EOF) {
567 if (err.text)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000568 PyMem_DEL(err.text);
Guido van Rossuma110aa61994-08-29 12:50:44 +0000569 return E_EOF;
570 }
571 err_input(&err);
Guido van Rossum82598051997-03-05 00:20:32 +0000572 PyErr_Print();
Guido van Rossuma110aa61994-08-29 12:50:44 +0000573 return err.error;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000574 }
Guido van Rossum82598051997-03-05 00:20:32 +0000575 m = PyImport_AddModule("__main__");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000576 if (m == NULL)
577 return -1;
Guido van Rossum82598051997-03-05 00:20:32 +0000578 d = PyModule_GetDict(m);
Jeremy Hylton9f324e92001-03-01 22:59:14 +0000579 v = run_node(n, filename, d, d, flags);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000580 if (v == NULL) {
Guido van Rossum82598051997-03-05 00:20:32 +0000581 PyErr_Print();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000582 return -1;
583 }
Guido van Rossum82598051997-03-05 00:20:32 +0000584 Py_DECREF(v);
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000585 if (Py_FlushLine())
586 PyErr_Clear();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000587 return 0;
588}
589
590int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000591PyRun_SimpleFile(FILE *fp, char *filename)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000592{
Guido van Rossum0df002c2000-08-27 19:21:52 +0000593 return PyRun_SimpleFileEx(fp, filename, 0);
594}
595
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000596/* Check whether a file maybe a pyc file: Look at the extension,
597 the file type, and, if we may close it, at the first few bytes. */
598
599static int
600maybe_pyc_file(FILE *fp, char* filename, char* ext, int closeit)
601{
602 if (strcmp(ext, ".pyc") == 0 || strcmp(ext, ".pyo") == 0)
603 return 1;
604
605#ifdef macintosh
606 /* On a mac, we also assume a pyc file for types 'PYC ' and 'APPL' */
607 if (PyMac_getfiletype(filename) == 'PYC '
608 || PyMac_getfiletype(filename) == 'APPL')
609 return 1;
610#endif /* macintosh */
611
612 /* Only look into the file if we are allowed to close it, since
613 it then should also be seekable. */
614 if (closeit) {
615 /* Read only two bytes of the magic. If the file was opened in
616 text mode, the bytes 3 and 4 of the magic (\r\n) might not
617 be read as they are on disk. */
618 unsigned int halfmagic = PyImport_GetMagicNumber() & 0xFFFF;
619 unsigned char buf[2];
Tim Peters3e876562001-02-11 04:35:39 +0000620 /* Mess: In case of -x, the stream is NOT at its start now,
621 and ungetc() was used to push back the first newline,
Tim Peters6f5a4ef2001-02-17 22:02:34 +0000622 which makes the current stream position formally undefined,
623 and a x-platform nightmare.
624 Unfortunately, we have no direct way to know whether -x
625 was specified. So we use a terrible hack: if the current
626 stream position is not 0, we assume -x was specified, and
627 give up. Bug 132850 on SourceForge spells out the
628 hopelessness of trying anything else (fseek and ftell
629 don't work predictably x-platform for text-mode files).
Tim Peters3e876562001-02-11 04:35:39 +0000630 */
Tim Peters3e876562001-02-11 04:35:39 +0000631 int ispyc = 0;
Tim Peters6f5a4ef2001-02-17 22:02:34 +0000632 if (ftell(fp) == 0) {
633 if (fread(buf, 1, 2, fp) == 2 &&
634 ((unsigned int)buf[1]<<8 | buf[0]) == halfmagic)
635 ispyc = 1;
636 rewind(fp);
637 }
Tim Peters3e876562001-02-11 04:35:39 +0000638 return ispyc;
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000639 }
640 return 0;
641}
642
Guido van Rossum0df002c2000-08-27 19:21:52 +0000643int
644PyRun_SimpleFileEx(FILE *fp, char *filename, int closeit)
645{
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000646 return PyRun_SimpleFileExFlags(fp, filename, closeit, NULL);
647}
648
649int
650PyRun_SimpleFileExFlags(FILE *fp, char *filename, int closeit,
651 PyCompilerFlags *flags)
652{
Guido van Rossum82598051997-03-05 00:20:32 +0000653 PyObject *m, *d, *v;
Guido van Rossumfdef2711994-09-14 13:31:04 +0000654 char *ext;
655
Guido van Rossum82598051997-03-05 00:20:32 +0000656 m = PyImport_AddModule("__main__");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000657 if (m == NULL)
658 return -1;
Guido van Rossum82598051997-03-05 00:20:32 +0000659 d = PyModule_GetDict(m);
Guido van Rossumfdef2711994-09-14 13:31:04 +0000660 ext = filename + strlen(filename) - 4;
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000661 if (maybe_pyc_file(fp, filename, ext, closeit)) {
Guido van Rossumfdef2711994-09-14 13:31:04 +0000662 /* Try to run a pyc file. First, re-open in binary */
Guido van Rossum0df002c2000-08-27 19:21:52 +0000663 if (closeit)
664 fclose(fp);
Guido van Rossumfdef2711994-09-14 13:31:04 +0000665 if( (fp = fopen(filename, "rb")) == NULL ) {
666 fprintf(stderr, "python: Can't reopen .pyc file\n");
667 return -1;
668 }
Guido van Rossum2a7f58d1997-04-02 05:28:38 +0000669 /* Turn on optimization if a .pyo file is given */
670 if (strcmp(ext, ".pyo") == 0)
671 Py_OptimizeFlag = 1;
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000672 v = run_pyc_file(fp, filename, d, d, flags);
Guido van Rossumfdef2711994-09-14 13:31:04 +0000673 } else {
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000674 v = PyRun_FileExFlags(fp, filename, Py_file_input, d, d,
675 closeit, flags);
Guido van Rossumfdef2711994-09-14 13:31:04 +0000676 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000677 if (v == NULL) {
Guido van Rossum82598051997-03-05 00:20:32 +0000678 PyErr_Print();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000679 return -1;
680 }
Guido van Rossum82598051997-03-05 00:20:32 +0000681 Py_DECREF(v);
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000682 if (Py_FlushLine())
683 PyErr_Clear();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000684 return 0;
685}
686
687int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000688PyRun_SimpleString(char *command)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000689{
Guido van Rossum82598051997-03-05 00:20:32 +0000690 PyObject *m, *d, *v;
691 m = PyImport_AddModule("__main__");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000692 if (m == NULL)
693 return -1;
Guido van Rossum82598051997-03-05 00:20:32 +0000694 d = PyModule_GetDict(m);
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000695 v = PyRun_String(command, Py_file_input, d, d);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000696 if (v == NULL) {
Guido van Rossum82598051997-03-05 00:20:32 +0000697 PyErr_Print();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000698 return -1;
699 }
Guido van Rossum82598051997-03-05 00:20:32 +0000700 Py_DECREF(v);
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000701 if (Py_FlushLine())
702 PyErr_Clear();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000703 return 0;
704}
705
Barry Warsaw035574d1997-08-29 22:07:17 +0000706static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000707parse_syntax_error(PyObject *err, PyObject **message, char **filename,
708 int *lineno, int *offset, char **text)
Barry Warsaw035574d1997-08-29 22:07:17 +0000709{
710 long hold;
711 PyObject *v;
712
713 /* old style errors */
714 if (PyTuple_Check(err))
715 return PyArg_Parse(err, "(O(ziiz))", message, filename,
716 lineno, offset, text);
717
718 /* new style errors. `err' is an instance */
719
720 if (! (v = PyObject_GetAttrString(err, "msg")))
721 goto finally;
722 *message = v;
723
724 if (!(v = PyObject_GetAttrString(err, "filename")))
725 goto finally;
726 if (v == Py_None)
727 *filename = NULL;
728 else if (! (*filename = PyString_AsString(v)))
729 goto finally;
730
731 Py_DECREF(v);
732 if (!(v = PyObject_GetAttrString(err, "lineno")))
733 goto finally;
734 hold = PyInt_AsLong(v);
735 Py_DECREF(v);
736 v = NULL;
737 if (hold < 0 && PyErr_Occurred())
738 goto finally;
739 *lineno = (int)hold;
740
741 if (!(v = PyObject_GetAttrString(err, "offset")))
742 goto finally;
Jeremy Hylton9f1b9932001-02-28 07:07:43 +0000743 if (v == Py_None) {
744 *offset = -1;
745 Py_DECREF(v);
746 v = NULL;
747 } else {
748 hold = PyInt_AsLong(v);
749 Py_DECREF(v);
750 v = NULL;
751 if (hold < 0 && PyErr_Occurred())
752 goto finally;
753 *offset = (int)hold;
754 }
Barry Warsaw035574d1997-08-29 22:07:17 +0000755
756 if (!(v = PyObject_GetAttrString(err, "text")))
757 goto finally;
758 if (v == Py_None)
759 *text = NULL;
760 else if (! (*text = PyString_AsString(v)))
761 goto finally;
762 Py_DECREF(v);
763 return 1;
764
765finally:
766 Py_XDECREF(v);
767 return 0;
768}
769
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000770void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000771PyErr_Print(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000772{
Guido van Rossuma61691e1998-02-06 22:27:24 +0000773 PyErr_PrintEx(1);
774}
775
Jeremy Hylton9f1b9932001-02-28 07:07:43 +0000776static void
777print_error_text(PyObject *f, int offset, char *text)
778{
779 char *nl;
780 if (offset >= 0) {
781 if (offset > 0 && offset == (int)strlen(text))
782 offset--;
783 for (;;) {
784 nl = strchr(text, '\n');
785 if (nl == NULL || nl-text >= offset)
786 break;
787 offset -= (nl+1-text);
788 text = nl+1;
789 }
790 while (*text == ' ' || *text == '\t') {
791 text++;
792 offset--;
793 }
794 }
795 PyFile_WriteString(" ", f);
796 PyFile_WriteString(text, f);
797 if (*text == '\0' || text[strlen(text)-1] != '\n')
798 PyFile_WriteString("\n", f);
799 if (offset == -1)
800 return;
801 PyFile_WriteString(" ", f);
802 offset--;
803 while (offset > 0) {
804 PyFile_WriteString(" ", f);
805 offset--;
806 }
807 PyFile_WriteString("^\n", f);
808}
809
Guido van Rossum66e8e862001-03-23 17:54:43 +0000810static void
811handle_system_exit(void)
Ka-Ping Yee26fabb02001-03-23 15:36:41 +0000812{
Guido van Rossum66e8e862001-03-23 17:54:43 +0000813 PyObject *exception, *value, *tb;
814 PyErr_Fetch(&exception, &value, &tb);
Ka-Ping Yee26fabb02001-03-23 15:36:41 +0000815 if (Py_FlushLine())
816 PyErr_Clear();
817 fflush(stdout);
818 if (value == NULL || value == Py_None)
819 Py_Exit(0);
820 if (PyInstance_Check(value)) {
821 /* The error code should be in the `code' attribute. */
822 PyObject *code = PyObject_GetAttrString(value, "code");
823 if (code) {
824 Py_DECREF(value);
825 value = code;
826 if (value == Py_None)
827 Py_Exit(0);
828 }
829 /* If we failed to dig out the 'code' attribute,
830 just let the else clause below print the error. */
831 }
832 if (PyInt_Check(value))
833 Py_Exit((int)PyInt_AsLong(value));
834 else {
835 PyObject_Print(value, stderr, Py_PRINT_RAW);
836 PySys_WriteStderr("\n");
837 Py_Exit(1);
838 }
839}
840
841void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000842PyErr_PrintEx(int set_sys_last_vars)
Guido van Rossuma61691e1998-02-06 22:27:24 +0000843{
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000844 PyObject *exception, *v, *tb, *hook;
Guido van Rossum66e8e862001-03-23 17:54:43 +0000845
846 if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
847 handle_system_exit();
848 }
Guido van Rossum82598051997-03-05 00:20:32 +0000849 PyErr_Fetch(&exception, &v, &tb);
Barry Warsaw035574d1997-08-29 22:07:17 +0000850 PyErr_NormalizeException(&exception, &v, &tb);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000851 if (exception == NULL)
Guido van Rossum296b4751997-05-23 00:19:20 +0000852 return;
Guido van Rossuma61691e1998-02-06 22:27:24 +0000853 if (set_sys_last_vars) {
854 PySys_SetObject("last_type", exception);
855 PySys_SetObject("last_value", v);
856 PySys_SetObject("last_traceback", tb);
857 }
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000858 hook = PySys_GetObject("excepthook");
859 if (hook) {
860 PyObject *args = Py_BuildValue("(OOO)",
861 exception, v ? v : Py_None, tb ? tb : Py_None);
862 PyObject *result = PyEval_CallObject(hook, args);
863 if (result == NULL) {
864 PyObject *exception2, *v2, *tb2;
Guido van Rossum66e8e862001-03-23 17:54:43 +0000865 if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
866 handle_system_exit();
867 }
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000868 PyErr_Fetch(&exception2, &v2, &tb2);
869 PyErr_NormalizeException(&exception2, &v2, &tb2);
870 if (Py_FlushLine())
871 PyErr_Clear();
872 fflush(stdout);
873 PySys_WriteStderr("Error in sys.excepthook:\n");
874 PyErr_Display(exception2, v2, tb2);
875 PySys_WriteStderr("\nOriginal exception was:\n");
876 PyErr_Display(exception, v, tb);
877 }
878 Py_XDECREF(result);
879 Py_XDECREF(args);
880 } else {
881 PySys_WriteStderr("sys.excepthook is missing\n");
882 PyErr_Display(exception, v, tb);
883 }
884 Py_XDECREF(exception);
885 Py_XDECREF(v);
886 Py_XDECREF(tb);
887}
888
889void PyErr_Display(PyObject *exception, PyObject *value, PyObject *tb)
890{
891 int err = 0;
892 PyObject *v = value;
893 PyObject *f = PySys_GetObject("stderr");
Guido van Rossum3165fe61992-09-25 21:59:05 +0000894 if (f == NULL)
895 fprintf(stderr, "lost sys.stderr\n");
896 else {
Guido van Rossum0829c751998-02-28 04:31:39 +0000897 if (Py_FlushLine())
898 PyErr_Clear();
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000899 fflush(stdout);
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000900 if (tb && tb != Py_None)
901 err = PyTraceBack_Print(tb, f);
Barry Warsaw36b8f941997-08-26 18:09:48 +0000902 if (err == 0 &&
903 PyErr_GivenExceptionMatches(exception, PyExc_SyntaxError))
904 {
Guido van Rossum82598051997-03-05 00:20:32 +0000905 PyObject *message;
Guido van Rossuma110aa61994-08-29 12:50:44 +0000906 char *filename, *text;
907 int lineno, offset;
Barry Warsaw035574d1997-08-29 22:07:17 +0000908 if (!parse_syntax_error(v, &message, &filename,
909 &lineno, &offset, &text))
Guido van Rossum82598051997-03-05 00:20:32 +0000910 PyErr_Clear();
Guido van Rossuma110aa61994-08-29 12:50:44 +0000911 else {
912 char buf[10];
Guido van Rossum82598051997-03-05 00:20:32 +0000913 PyFile_WriteString(" File \"", f);
Guido van Rossuma110aa61994-08-29 12:50:44 +0000914 if (filename == NULL)
Guido van Rossum82598051997-03-05 00:20:32 +0000915 PyFile_WriteString("<string>", f);
Guido van Rossuma110aa61994-08-29 12:50:44 +0000916 else
Guido van Rossum82598051997-03-05 00:20:32 +0000917 PyFile_WriteString(filename, f);
918 PyFile_WriteString("\", line ", f);
Guido van Rossuma110aa61994-08-29 12:50:44 +0000919 sprintf(buf, "%d", lineno);
Guido van Rossum82598051997-03-05 00:20:32 +0000920 PyFile_WriteString(buf, f);
921 PyFile_WriteString("\n", f);
Jeremy Hylton9f1b9932001-02-28 07:07:43 +0000922 if (text != NULL)
923 print_error_text(f, offset, text);
Guido van Rossuma110aa61994-08-29 12:50:44 +0000924 v = message;
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000925 /* Can't be bothered to check all those
926 PyFile_WriteString() calls */
927 if (PyErr_Occurred())
928 err = -1;
Guido van Rossuma110aa61994-08-29 12:50:44 +0000929 }
930 }
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000931 if (err) {
932 /* Don't do anything else */
933 }
934 else if (PyClass_Check(exception)) {
Barry Warsaw2f5f6a21997-09-16 21:42:03 +0000935 PyClassObject* exc = (PyClassObject*)exception;
936 PyObject* className = exc->cl_name;
937 PyObject* moduleName =
938 PyDict_GetItemString(exc->cl_dict, "__module__");
939
940 if (moduleName == NULL)
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000941 err = PyFile_WriteString("<unknown>", f);
Barry Warsaw2f5f6a21997-09-16 21:42:03 +0000942 else {
943 char* modstr = PyString_AsString(moduleName);
944 if (modstr && strcmp(modstr, "exceptions"))
945 {
946 err = PyFile_WriteString(modstr, f);
947 err += PyFile_WriteString(".", f);
948 }
949 }
950 if (err == 0) {
951 if (className == NULL)
952 err = PyFile_WriteString("<unknown>", f);
953 else
954 err = PyFile_WriteObject(className, f,
955 Py_PRINT_RAW);
956 }
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000957 }
958 else
959 err = PyFile_WriteObject(exception, f, Py_PRINT_RAW);
960 if (err == 0) {
961 if (v != NULL && v != Py_None) {
Barry Warsaw035574d1997-08-29 22:07:17 +0000962 PyObject *s = PyObject_Str(v);
963 /* only print colon if the str() of the
964 object is not the empty string
965 */
Guido van Rossumd6bf45b1997-09-05 19:11:53 +0000966 if (s == NULL)
967 err = -1;
968 else if (!PyString_Check(s) ||
969 PyString_GET_SIZE(s) != 0)
Barry Warsaw035574d1997-08-29 22:07:17 +0000970 err = PyFile_WriteString(": ", f);
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000971 if (err == 0)
Guido van Rossumd6bf45b1997-09-05 19:11:53 +0000972 err = PyFile_WriteObject(s, f, Py_PRINT_RAW);
973 Py_XDECREF(s);
Guido van Rossum262e1241995-02-07 15:30:45 +0000974 }
Guido van Rossum262e1241995-02-07 15:30:45 +0000975 }
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000976 if (err == 0)
977 err = PyFile_WriteString("\n", f);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000978 }
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000979 /* If an error happened here, don't show it.
980 XXX This is wrong, but too many callers rely on this behavior. */
981 if (err != 0)
982 PyErr_Clear();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000983}
984
Guido van Rossum82598051997-03-05 00:20:32 +0000985PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000986PyRun_String(char *str, int start, PyObject *globals, PyObject *locals)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000987{
Guido van Rossum82598051997-03-05 00:20:32 +0000988 return run_err_node(PyParser_SimpleParseString(str, start),
Jeremy Hylton9f324e92001-03-01 22:59:14 +0000989 "<string>", globals, locals, NULL);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000990}
991
Guido van Rossum82598051997-03-05 00:20:32 +0000992PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000993PyRun_File(FILE *fp, char *filename, int start, PyObject *globals,
994 PyObject *locals)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000995{
Tim Peterse8682112000-08-27 20:18:17 +0000996 return PyRun_FileEx(fp, filename, start, globals, locals, 0);
Guido van Rossum0df002c2000-08-27 19:21:52 +0000997}
998
999PyObject *
1000PyRun_FileEx(FILE *fp, char *filename, int start, PyObject *globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001001 PyObject *locals, int closeit)
Guido van Rossum0df002c2000-08-27 19:21:52 +00001002{
1003 node *n = PyParser_SimpleParseFile(fp, filename, start);
1004 if (closeit)
1005 fclose(fp);
Jeremy Hylton9f324e92001-03-01 22:59:14 +00001006 return run_err_node(n, filename, globals, locals, NULL);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001007}
1008
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001009PyObject *
1010PyRun_StringFlags(char *str, int start, PyObject *globals, PyObject *locals,
1011 PyCompilerFlags *flags)
1012{
Guido van Rossuma1b3a472001-07-16 16:51:33 +00001013 return run_err_node(PyParser_SimpleParseStringFlags(
Jeremy Hyltonb857ba22001-08-10 21:41:33 +00001014 str, start,
1015 (flags && flags->cf_flags & CO_GENERATOR_ALLOWED) ?
Guido van Rossuma1b3a472001-07-16 16:51:33 +00001016 PyPARSE_YIELD_IS_KEYWORD : 0),
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001017 "<string>", globals, locals, flags);
1018}
1019
1020PyObject *
1021PyRun_FileFlags(FILE *fp, char *filename, int start, PyObject *globals,
1022 PyObject *locals, PyCompilerFlags *flags)
1023{
1024 return PyRun_FileExFlags(fp, filename, start, globals, locals, 0,
1025 flags);
1026}
1027
1028PyObject *
1029PyRun_FileExFlags(FILE *fp, char *filename, int start, PyObject *globals,
1030 PyObject *locals, int closeit, PyCompilerFlags *flags)
1031{
Tim Petersfe2127d2001-07-16 05:37:24 +00001032 node *n = PyParser_SimpleParseFileFlags(fp, filename, start,
Jeremy Hyltonb857ba22001-08-10 21:41:33 +00001033 (flags && flags->cf_flags & CO_GENERATOR_ALLOWED) ?
Tim Petersfe2127d2001-07-16 05:37:24 +00001034 PyPARSE_YIELD_IS_KEYWORD : 0);
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001035 if (closeit)
1036 fclose(fp);
1037 return run_err_node(n, filename, globals, locals, flags);
1038}
1039
Guido van Rossum82598051997-03-05 00:20:32 +00001040static PyObject *
Jeremy Hylton9f324e92001-03-01 22:59:14 +00001041run_err_node(node *n, char *filename, PyObject *globals, PyObject *locals,
1042 PyCompilerFlags *flags)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001043{
Guido van Rossuma110aa61994-08-29 12:50:44 +00001044 if (n == NULL)
1045 return NULL;
Jeremy Hylton9f324e92001-03-01 22:59:14 +00001046 return run_node(n, filename, globals, locals, flags);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001047}
1048
Guido van Rossum82598051997-03-05 00:20:32 +00001049static PyObject *
Jeremy Hylton9f324e92001-03-01 22:59:14 +00001050run_node(node *n, char *filename, PyObject *globals, PyObject *locals,
1051 PyCompilerFlags *flags)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001052{
Guido van Rossum82598051997-03-05 00:20:32 +00001053 PyCodeObject *co;
1054 PyObject *v;
Jeremy Hylton9f324e92001-03-01 22:59:14 +00001055 co = PyNode_CompileFlags(n, filename, flags);
Guido van Rossum82598051997-03-05 00:20:32 +00001056 PyNode_Free(n);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001057 if (co == NULL)
1058 return NULL;
Guido van Rossum82598051997-03-05 00:20:32 +00001059 v = PyEval_EvalCode(co, globals, locals);
1060 Py_DECREF(co);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001061 return v;
1062}
1063
Guido van Rossum82598051997-03-05 00:20:32 +00001064static PyObject *
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001065run_pyc_file(FILE *fp, char *filename, PyObject *globals, PyObject *locals,
1066 PyCompilerFlags *flags)
Guido van Rossumfdef2711994-09-14 13:31:04 +00001067{
Guido van Rossum82598051997-03-05 00:20:32 +00001068 PyCodeObject *co;
1069 PyObject *v;
Guido van Rossumfdef2711994-09-14 13:31:04 +00001070 long magic;
Fred Drakee8de31c2000-08-31 05:38:39 +00001071 long PyImport_GetMagicNumber(void);
Guido van Rossumfdef2711994-09-14 13:31:04 +00001072
Guido van Rossum82598051997-03-05 00:20:32 +00001073 magic = PyMarshal_ReadLongFromFile(fp);
1074 if (magic != PyImport_GetMagicNumber()) {
1075 PyErr_SetString(PyExc_RuntimeError,
Guido van Rossumfdef2711994-09-14 13:31:04 +00001076 "Bad magic number in .pyc file");
1077 return NULL;
1078 }
Guido van Rossum82598051997-03-05 00:20:32 +00001079 (void) PyMarshal_ReadLongFromFile(fp);
Tim Petersd9b9ac82001-01-28 00:27:39 +00001080 v = PyMarshal_ReadLastObjectFromFile(fp);
Guido van Rossumfdef2711994-09-14 13:31:04 +00001081 fclose(fp);
Guido van Rossum82598051997-03-05 00:20:32 +00001082 if (v == NULL || !PyCode_Check(v)) {
1083 Py_XDECREF(v);
1084 PyErr_SetString(PyExc_RuntimeError,
Guido van Rossumfdef2711994-09-14 13:31:04 +00001085 "Bad code object in .pyc file");
1086 return NULL;
1087 }
Guido van Rossum82598051997-03-05 00:20:32 +00001088 co = (PyCodeObject *)v;
1089 v = PyEval_EvalCode(co, globals, locals);
Jeremy Hyltonb857ba22001-08-10 21:41:33 +00001090 if (v && flags)
1091 flags->cf_flags |= (co->co_flags & PyCF_MASK);
Guido van Rossum82598051997-03-05 00:20:32 +00001092 Py_DECREF(co);
Guido van Rossumfdef2711994-09-14 13:31:04 +00001093 return v;
1094}
1095
Guido van Rossum82598051997-03-05 00:20:32 +00001096PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001097Py_CompileString(char *str, char *filename, int start)
Guido van Rossum5b722181993-03-30 17:46:03 +00001098{
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001099 return Py_CompileStringFlags(str, filename, start, NULL);
1100}
1101
1102PyObject *
1103Py_CompileStringFlags(char *str, char *filename, int start,
1104 PyCompilerFlags *flags)
1105{
Guido van Rossum5b722181993-03-30 17:46:03 +00001106 node *n;
Guido van Rossum82598051997-03-05 00:20:32 +00001107 PyCodeObject *co;
Tim Petersfe2127d2001-07-16 05:37:24 +00001108 n = PyParser_SimpleParseStringFlags(str, start,
Jeremy Hyltonb857ba22001-08-10 21:41:33 +00001109 (flags && flags->cf_flags & CO_GENERATOR_ALLOWED) ?
Tim Petersfe2127d2001-07-16 05:37:24 +00001110 PyPARSE_YIELD_IS_KEYWORD : 0);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001111 if (n == NULL)
Guido van Rossum5b722181993-03-30 17:46:03 +00001112 return NULL;
Jeremy Hylton673a4fd2001-03-26 19:53:38 +00001113 co = PyNode_CompileFlags(n, filename, flags);
Guido van Rossum82598051997-03-05 00:20:32 +00001114 PyNode_Free(n);
1115 return (PyObject *)co;
Guido van Rossum5b722181993-03-30 17:46:03 +00001116}
1117
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001118struct symtable *
1119Py_SymtableString(char *str, char *filename, int start)
1120{
1121 node *n;
1122 struct symtable *st;
1123 n = PyParser_SimpleParseString(str, start);
1124 if (n == NULL)
1125 return NULL;
1126 st = PyNode_CompileSymtable(n, filename);
1127 PyNode_Free(n);
1128 return st;
1129}
1130
Guido van Rossuma110aa61994-08-29 12:50:44 +00001131/* Simplified interface to parsefile -- return node or set exception */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001132
Guido van Rossuma110aa61994-08-29 12:50:44 +00001133node *
Tim Petersfe2127d2001-07-16 05:37:24 +00001134PyParser_SimpleParseFileFlags(FILE *fp, char *filename, int start, int flags)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001135{
Guido van Rossuma110aa61994-08-29 12:50:44 +00001136 node *n;
1137 perrdetail err;
Tim Petersfe2127d2001-07-16 05:37:24 +00001138 n = PyParser_ParseFileFlags(fp, filename, &_PyParser_Grammar, start,
1139 (char *)0, (char *)0, &err, flags);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001140 if (n == NULL)
1141 err_input(&err);
1142 return n;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001143}
1144
Tim Petersfe2127d2001-07-16 05:37:24 +00001145node *
1146PyParser_SimpleParseFile(FILE *fp, char *filename, int start)
1147{
1148 return PyParser_SimpleParseFileFlags(fp, filename, start, 0);
1149}
1150
Guido van Rossuma110aa61994-08-29 12:50:44 +00001151/* Simplified interface to parsestring -- return node or set exception */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001152
Guido van Rossuma110aa61994-08-29 12:50:44 +00001153node *
Tim Petersfe2127d2001-07-16 05:37:24 +00001154PyParser_SimpleParseStringFlags(char *str, int start, int flags)
1155{
1156 node *n;
1157 perrdetail err;
1158 n = PyParser_ParseStringFlags(str, &_PyParser_Grammar, start, &err,
1159 flags);
1160 if (n == NULL)
1161 err_input(&err);
1162 return n;
1163}
1164
1165node *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001166PyParser_SimpleParseString(char *str, int start)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001167{
Tim Petersfe2127d2001-07-16 05:37:24 +00001168 return PyParser_SimpleParseStringFlags(str, start, 0);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001169}
1170
1171/* Set the error appropriate to the given input error code (see errcode.h) */
1172
1173static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001174err_input(perrdetail *err)
Guido van Rossuma110aa61994-08-29 12:50:44 +00001175{
Fred Drake85f36392000-07-11 17:53:00 +00001176 PyObject *v, *w, *errtype;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001177 char *msg = NULL;
Fred Drake85f36392000-07-11 17:53:00 +00001178 errtype = PyExc_SyntaxError;
Guido van Rossum82598051997-03-05 00:20:32 +00001179 v = Py_BuildValue("(ziiz)", err->filename,
Guido van Rossuma110aa61994-08-29 12:50:44 +00001180 err->lineno, err->offset, err->text);
1181 if (err->text != NULL) {
Guido van Rossumb18618d2000-05-03 23:44:39 +00001182 PyMem_DEL(err->text);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001183 err->text = NULL;
1184 }
1185 switch (err->error) {
1186 case E_SYNTAX:
Fred Drake85f36392000-07-11 17:53:00 +00001187 errtype = PyExc_IndentationError;
1188 if (err->expected == INDENT)
1189 msg = "expected an indented block";
1190 else if (err->token == INDENT)
1191 msg = "unexpected indent";
1192 else if (err->token == DEDENT)
1193 msg = "unexpected unindent";
1194 else {
1195 errtype = PyExc_SyntaxError;
1196 msg = "invalid syntax";
1197 }
Guido van Rossuma110aa61994-08-29 12:50:44 +00001198 break;
1199 case E_TOKEN:
1200 msg = "invalid token";
Guido van Rossuma110aa61994-08-29 12:50:44 +00001201 break;
1202 case E_INTR:
Guido van Rossum82598051997-03-05 00:20:32 +00001203 PyErr_SetNone(PyExc_KeyboardInterrupt);
Barry Warsawc80baa31999-01-27 16:39:40 +00001204 Py_XDECREF(v);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001205 return;
1206 case E_NOMEM:
Guido van Rossum82598051997-03-05 00:20:32 +00001207 PyErr_NoMemory();
Barry Warsawc80baa31999-01-27 16:39:40 +00001208 Py_XDECREF(v);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001209 return;
1210 case E_EOF:
1211 msg = "unexpected EOF while parsing";
1212 break;
Fred Drake85f36392000-07-11 17:53:00 +00001213 case E_TABSPACE:
1214 errtype = PyExc_TabError;
Guido van Rossum560e8ad1998-04-10 19:43:42 +00001215 msg = "inconsistent use of tabs and spaces in indentation";
1216 break;
Jeremy Hylton94988062000-06-20 19:10:44 +00001217 case E_OVERFLOW:
1218 msg = "expression too long";
1219 break;
Fred Drake85f36392000-07-11 17:53:00 +00001220 case E_DEDENT:
1221 errtype = PyExc_IndentationError;
1222 msg = "unindent does not match any outer indentation level";
1223 break;
1224 case E_TOODEEP:
1225 errtype = PyExc_IndentationError;
1226 msg = "too many levels of indentation";
1227 break;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001228 default:
1229 fprintf(stderr, "error=%d\n", err->error);
1230 msg = "unknown parsing error";
1231 break;
1232 }
Guido van Rossum82598051997-03-05 00:20:32 +00001233 w = Py_BuildValue("(sO)", msg, v);
Guido van Rossum41318302001-03-23 04:01:07 +00001234 Py_XDECREF(v);
Fred Drake85f36392000-07-11 17:53:00 +00001235 PyErr_SetObject(errtype, w);
Guido van Rossum82598051997-03-05 00:20:32 +00001236 Py_XDECREF(w);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001237}
1238
1239/* Print fatal error message and abort */
1240
1241void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001242Py_FatalError(char *msg)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001243{
Guido van Rossum83dd6c31994-09-29 09:38:33 +00001244 fprintf(stderr, "Fatal Python error: %s\n", msg);
Guido van Rossum8ae87c01995-01-26 00:40:38 +00001245#ifdef macintosh
1246 for (;;);
1247#endif
Guido van Rossum9b38a141996-09-11 23:12:24 +00001248#ifdef MS_WIN32
Guido van Rossum23c94461997-05-22 20:21:30 +00001249 OutputDebugString("Fatal Python error: ");
Guido van Rossuma44823b1995-03-14 15:01:17 +00001250 OutputDebugString(msg);
1251 OutputDebugString("\n");
Guido van Rossum0ba35361998-08-13 13:33:16 +00001252#ifdef _DEBUG
1253 DebugBreak();
Guido van Rossuma44823b1995-03-14 15:01:17 +00001254#endif
Guido van Rossum0ba35361998-08-13 13:33:16 +00001255#endif /* MS_WIN32 */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001256 abort();
1257}
1258
1259/* Clean up and exit */
1260
Guido van Rossuma110aa61994-08-29 12:50:44 +00001261#ifdef WITH_THREAD
Guido van Rossum49b56061998-10-01 20:42:43 +00001262#include "pythread.h"
Guido van Rossum82598051997-03-05 00:20:32 +00001263int _PyThread_Started = 0; /* Set by threadmodule.c and maybe others */
Guido van Rossumf9f2e821992-08-17 08:59:08 +00001264#endif
1265
Guido van Rossum2dcfc961998-10-01 16:01:57 +00001266#define NEXITFUNCS 32
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001267static void (*exitfuncs[NEXITFUNCS])(void);
Guido van Rossum1662dd51994-09-07 14:38:28 +00001268static int nexitfuncs = 0;
1269
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001270int Py_AtExit(void (*func)(void))
Guido van Rossum1662dd51994-09-07 14:38:28 +00001271{
1272 if (nexitfuncs >= NEXITFUNCS)
1273 return -1;
1274 exitfuncs[nexitfuncs++] = func;
1275 return 0;
1276}
1277
Guido van Rossumcc283f51997-08-05 02:22:03 +00001278static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001279call_sys_exitfunc(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001280{
Guido van Rossum82598051997-03-05 00:20:32 +00001281 PyObject *exitfunc = PySys_GetObject("exitfunc");
Guido van Rossum59bff391992-09-03 20:28:00 +00001282
1283 if (exitfunc) {
Fred Drake6a12d8d2001-03-23 17:34:02 +00001284 PyObject *res;
Guido van Rossum82598051997-03-05 00:20:32 +00001285 Py_INCREF(exitfunc);
1286 PySys_SetObject("exitfunc", (PyObject *)NULL);
1287 res = PyEval_CallObject(exitfunc, (PyObject *)NULL);
Guido van Rossum59bff391992-09-03 20:28:00 +00001288 if (res == NULL) {
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001289 if (!PyErr_ExceptionMatches(PyExc_SystemExit)) {
1290 PySys_WriteStderr("Error in sys.exitfunc:\n");
1291 }
Guido van Rossum82598051997-03-05 00:20:32 +00001292 PyErr_Print();
Guido van Rossum59bff391992-09-03 20:28:00 +00001293 }
Guido van Rossum82598051997-03-05 00:20:32 +00001294 Py_DECREF(exitfunc);
Guido van Rossum59bff391992-09-03 20:28:00 +00001295 }
1296
Guido van Rossum0829c751998-02-28 04:31:39 +00001297 if (Py_FlushLine())
1298 PyErr_Clear();
Guido van Rossumcc283f51997-08-05 02:22:03 +00001299}
Guido van Rossum1662dd51994-09-07 14:38:28 +00001300
Guido van Rossumcc283f51997-08-05 02:22:03 +00001301static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001302call_ll_exitfuncs(void)
Guido van Rossumcc283f51997-08-05 02:22:03 +00001303{
Guido van Rossum1662dd51994-09-07 14:38:28 +00001304 while (nexitfuncs > 0)
1305 (*exitfuncs[--nexitfuncs])();
Guido van Rossum25ce5661997-08-02 03:10:38 +00001306
1307 fflush(stdout);
1308 fflush(stderr);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001309}
1310
1311void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001312Py_Exit(int sts)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001313{
Guido van Rossumcc283f51997-08-05 02:22:03 +00001314 Py_Finalize();
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001315
Jack Jansen66a89771995-10-27 13:22:14 +00001316#ifdef macintosh
1317 PyMac_Exit(sts);
1318#else
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001319 exit(sts);
Jack Jansen66a89771995-10-27 13:22:14 +00001320#endif
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001321}
1322
Guido van Rossumf1dc5661993-07-05 10:31:29 +00001323static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001324initsigs(void)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001325{
Guido van Rossuma110aa61994-08-29 12:50:44 +00001326#ifdef HAVE_SIGNAL_H
1327#ifdef SIGPIPE
1328 signal(SIGPIPE, SIG_IGN);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001329#endif
Guido van Rossum70d893a2001-08-16 08:21:42 +00001330#ifdef SIGXFZ
1331 signal(SIGXFZ, SIG_IGN);
1332#endif
Guido van Rossuma110aa61994-08-29 12:50:44 +00001333#endif /* HAVE_SIGNAL_H */
Guido van Rossum82598051997-03-05 00:20:32 +00001334 PyOS_InitInterrupts(); /* May imply initsignal() */
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001335}
1336
Guido van Rossumaae0d321996-05-22 16:35:33 +00001337#ifdef Py_TRACE_REFS
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001338/* Ask a yes/no question */
1339
Guido van Rossum59bff391992-09-03 20:28:00 +00001340int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001341_Py_AskYesNo(char *prompt)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001342{
1343 char buf[256];
1344
Tim Peters6d6c1a32001-08-02 04:15:00 +00001345 fprintf(stderr, "%s [ny] ", prompt);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001346 if (fgets(buf, sizeof buf, stdin) == NULL)
1347 return 0;
1348 return buf[0] == 'y' || buf[0] == 'Y';
1349}
1350#endif
1351
Guido van Rossuma110aa61994-08-29 12:50:44 +00001352#ifdef MPW
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001353
1354/* Check for file descriptor connected to interactive device.
1355 Pretend that stdin is always interactive, other files never. */
1356
1357int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001358isatty(int fd)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001359{
1360 return fd == fileno(stdin);
1361}
1362
1363#endif
Guido van Rossum7433b121997-02-14 19:45:36 +00001364
1365/*
1366 * The file descriptor fd is considered ``interactive'' if either
1367 * a) isatty(fd) is TRUE, or
1368 * b) the -i flag was given, and the filename associated with
1369 * the descriptor is NULL or "<stdin>" or "???".
1370 */
1371int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001372Py_FdIsInteractive(FILE *fp, char *filename)
Guido van Rossum7433b121997-02-14 19:45:36 +00001373{
1374 if (isatty((int)fileno(fp)))
1375 return 1;
1376 if (!Py_InteractiveFlag)
1377 return 0;
1378 return (filename == NULL) ||
1379 (strcmp(filename, "<stdin>") == 0) ||
1380 (strcmp(filename, "???") == 0);
1381}
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001382
1383
1384#if defined(USE_STACKCHECK)
1385#if defined(WIN32) && defined(_MSC_VER)
1386
1387/* Stack checking for Microsoft C */
1388
1389#include <malloc.h>
1390#include <excpt.h>
1391
Fred Drakee8de31c2000-08-31 05:38:39 +00001392/*
1393 * Return non-zero when we run out of memory on the stack; zero otherwise.
1394 */
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001395int
Fred Drake399739f2000-08-31 05:52:44 +00001396PyOS_CheckStack(void)
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001397{
1398 __try {
1399 /* _alloca throws a stack overflow exception if there's
1400 not enough space left on the stack */
1401 _alloca(PYOS_STACK_MARGIN * sizeof(void*));
1402 return 0;
1403 } __except (EXCEPTION_EXECUTE_HANDLER) {
1404 /* just ignore all errors */
1405 }
1406 return 1;
1407}
1408
1409#endif /* WIN32 && _MSC_VER */
1410
1411/* Alternate implementations can be added here... */
1412
1413#endif /* USE_STACKCHECK */
Guido van Rossum6f256182000-09-16 16:32:19 +00001414
1415
1416/* Wrappers around sigaction() or signal(). */
1417
1418PyOS_sighandler_t
1419PyOS_getsig(int sig)
1420{
1421#ifdef HAVE_SIGACTION
1422 struct sigaction context;
1423 sigaction(sig, NULL, &context);
1424 return context.sa_handler;
1425#else
1426 PyOS_sighandler_t handler;
1427 handler = signal(sig, SIG_IGN);
1428 signal(sig, handler);
1429 return handler;
1430#endif
1431}
1432
1433PyOS_sighandler_t
1434PyOS_setsig(int sig, PyOS_sighandler_t handler)
1435{
1436#ifdef HAVE_SIGACTION
1437 struct sigaction context;
1438 PyOS_sighandler_t oldhandler;
1439 sigaction(sig, NULL, &context);
1440 oldhandler = context.sa_handler;
1441 context.sa_handler = handler;
1442 sigaction(sig, &context, NULL);
1443 return oldhandler;
1444#else
1445 return signal(sig, handler);
1446#endif
1447}