blob: 8dd9c14c3b402852661b98ff2454263dc9253f0b [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 Rossum393661d2001-08-31 17:40:15 +0000690 return PyRun_SimpleStringFlags(command, NULL);
691}
692
693int
694PyRun_SimpleStringFlags(char *command, PyCompilerFlags *flags)
695{
Guido van Rossum82598051997-03-05 00:20:32 +0000696 PyObject *m, *d, *v;
697 m = PyImport_AddModule("__main__");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000698 if (m == NULL)
699 return -1;
Guido van Rossum82598051997-03-05 00:20:32 +0000700 d = PyModule_GetDict(m);
Guido van Rossum393661d2001-08-31 17:40:15 +0000701 v = PyRun_StringFlags(command, Py_file_input, d, d, flags);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000702 if (v == NULL) {
Guido van Rossum82598051997-03-05 00:20:32 +0000703 PyErr_Print();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000704 return -1;
705 }
Guido van Rossum82598051997-03-05 00:20:32 +0000706 Py_DECREF(v);
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000707 if (Py_FlushLine())
708 PyErr_Clear();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000709 return 0;
710}
711
Barry Warsaw035574d1997-08-29 22:07:17 +0000712static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000713parse_syntax_error(PyObject *err, PyObject **message, char **filename,
714 int *lineno, int *offset, char **text)
Barry Warsaw035574d1997-08-29 22:07:17 +0000715{
716 long hold;
717 PyObject *v;
718
719 /* old style errors */
720 if (PyTuple_Check(err))
721 return PyArg_Parse(err, "(O(ziiz))", message, filename,
722 lineno, offset, text);
723
724 /* new style errors. `err' is an instance */
725
726 if (! (v = PyObject_GetAttrString(err, "msg")))
727 goto finally;
728 *message = v;
729
730 if (!(v = PyObject_GetAttrString(err, "filename")))
731 goto finally;
732 if (v == Py_None)
733 *filename = NULL;
734 else if (! (*filename = PyString_AsString(v)))
735 goto finally;
736
737 Py_DECREF(v);
738 if (!(v = PyObject_GetAttrString(err, "lineno")))
739 goto finally;
740 hold = PyInt_AsLong(v);
741 Py_DECREF(v);
742 v = NULL;
743 if (hold < 0 && PyErr_Occurred())
744 goto finally;
745 *lineno = (int)hold;
746
747 if (!(v = PyObject_GetAttrString(err, "offset")))
748 goto finally;
Jeremy Hylton9f1b9932001-02-28 07:07:43 +0000749 if (v == Py_None) {
750 *offset = -1;
751 Py_DECREF(v);
752 v = NULL;
753 } else {
754 hold = PyInt_AsLong(v);
755 Py_DECREF(v);
756 v = NULL;
757 if (hold < 0 && PyErr_Occurred())
758 goto finally;
759 *offset = (int)hold;
760 }
Barry Warsaw035574d1997-08-29 22:07:17 +0000761
762 if (!(v = PyObject_GetAttrString(err, "text")))
763 goto finally;
764 if (v == Py_None)
765 *text = NULL;
766 else if (! (*text = PyString_AsString(v)))
767 goto finally;
768 Py_DECREF(v);
769 return 1;
770
771finally:
772 Py_XDECREF(v);
773 return 0;
774}
775
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000776void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000777PyErr_Print(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000778{
Guido van Rossuma61691e1998-02-06 22:27:24 +0000779 PyErr_PrintEx(1);
780}
781
Jeremy Hylton9f1b9932001-02-28 07:07:43 +0000782static void
783print_error_text(PyObject *f, int offset, char *text)
784{
785 char *nl;
786 if (offset >= 0) {
787 if (offset > 0 && offset == (int)strlen(text))
788 offset--;
789 for (;;) {
790 nl = strchr(text, '\n');
791 if (nl == NULL || nl-text >= offset)
792 break;
793 offset -= (nl+1-text);
794 text = nl+1;
795 }
796 while (*text == ' ' || *text == '\t') {
797 text++;
798 offset--;
799 }
800 }
801 PyFile_WriteString(" ", f);
802 PyFile_WriteString(text, f);
803 if (*text == '\0' || text[strlen(text)-1] != '\n')
804 PyFile_WriteString("\n", f);
805 if (offset == -1)
806 return;
807 PyFile_WriteString(" ", f);
808 offset--;
809 while (offset > 0) {
810 PyFile_WriteString(" ", f);
811 offset--;
812 }
813 PyFile_WriteString("^\n", f);
814}
815
Guido van Rossum66e8e862001-03-23 17:54:43 +0000816static void
817handle_system_exit(void)
Ka-Ping Yee26fabb02001-03-23 15:36:41 +0000818{
Guido van Rossum66e8e862001-03-23 17:54:43 +0000819 PyObject *exception, *value, *tb;
820 PyErr_Fetch(&exception, &value, &tb);
Ka-Ping Yee26fabb02001-03-23 15:36:41 +0000821 if (Py_FlushLine())
822 PyErr_Clear();
823 fflush(stdout);
824 if (value == NULL || value == Py_None)
825 Py_Exit(0);
826 if (PyInstance_Check(value)) {
827 /* The error code should be in the `code' attribute. */
828 PyObject *code = PyObject_GetAttrString(value, "code");
829 if (code) {
830 Py_DECREF(value);
831 value = code;
832 if (value == Py_None)
833 Py_Exit(0);
834 }
835 /* If we failed to dig out the 'code' attribute,
836 just let the else clause below print the error. */
837 }
838 if (PyInt_Check(value))
839 Py_Exit((int)PyInt_AsLong(value));
840 else {
841 PyObject_Print(value, stderr, Py_PRINT_RAW);
842 PySys_WriteStderr("\n");
843 Py_Exit(1);
844 }
845}
846
847void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000848PyErr_PrintEx(int set_sys_last_vars)
Guido van Rossuma61691e1998-02-06 22:27:24 +0000849{
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000850 PyObject *exception, *v, *tb, *hook;
Guido van Rossum66e8e862001-03-23 17:54:43 +0000851
852 if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
853 handle_system_exit();
854 }
Guido van Rossum82598051997-03-05 00:20:32 +0000855 PyErr_Fetch(&exception, &v, &tb);
Barry Warsaw035574d1997-08-29 22:07:17 +0000856 PyErr_NormalizeException(&exception, &v, &tb);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000857 if (exception == NULL)
Guido van Rossum296b4751997-05-23 00:19:20 +0000858 return;
Guido van Rossuma61691e1998-02-06 22:27:24 +0000859 if (set_sys_last_vars) {
860 PySys_SetObject("last_type", exception);
861 PySys_SetObject("last_value", v);
862 PySys_SetObject("last_traceback", tb);
863 }
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000864 hook = PySys_GetObject("excepthook");
865 if (hook) {
866 PyObject *args = Py_BuildValue("(OOO)",
867 exception, v ? v : Py_None, tb ? tb : Py_None);
868 PyObject *result = PyEval_CallObject(hook, args);
869 if (result == NULL) {
870 PyObject *exception2, *v2, *tb2;
Guido van Rossum66e8e862001-03-23 17:54:43 +0000871 if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
872 handle_system_exit();
873 }
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000874 PyErr_Fetch(&exception2, &v2, &tb2);
875 PyErr_NormalizeException(&exception2, &v2, &tb2);
876 if (Py_FlushLine())
877 PyErr_Clear();
878 fflush(stdout);
879 PySys_WriteStderr("Error in sys.excepthook:\n");
880 PyErr_Display(exception2, v2, tb2);
881 PySys_WriteStderr("\nOriginal exception was:\n");
882 PyErr_Display(exception, v, tb);
883 }
884 Py_XDECREF(result);
885 Py_XDECREF(args);
886 } else {
887 PySys_WriteStderr("sys.excepthook is missing\n");
888 PyErr_Display(exception, v, tb);
889 }
890 Py_XDECREF(exception);
891 Py_XDECREF(v);
892 Py_XDECREF(tb);
893}
894
895void PyErr_Display(PyObject *exception, PyObject *value, PyObject *tb)
896{
897 int err = 0;
898 PyObject *v = value;
899 PyObject *f = PySys_GetObject("stderr");
Guido van Rossum3165fe61992-09-25 21:59:05 +0000900 if (f == NULL)
901 fprintf(stderr, "lost sys.stderr\n");
902 else {
Guido van Rossum0829c751998-02-28 04:31:39 +0000903 if (Py_FlushLine())
904 PyErr_Clear();
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000905 fflush(stdout);
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000906 if (tb && tb != Py_None)
907 err = PyTraceBack_Print(tb, f);
Barry Warsaw36b8f941997-08-26 18:09:48 +0000908 if (err == 0 &&
909 PyErr_GivenExceptionMatches(exception, PyExc_SyntaxError))
910 {
Guido van Rossum82598051997-03-05 00:20:32 +0000911 PyObject *message;
Guido van Rossuma110aa61994-08-29 12:50:44 +0000912 char *filename, *text;
913 int lineno, offset;
Barry Warsaw035574d1997-08-29 22:07:17 +0000914 if (!parse_syntax_error(v, &message, &filename,
915 &lineno, &offset, &text))
Guido van Rossum82598051997-03-05 00:20:32 +0000916 PyErr_Clear();
Guido van Rossuma110aa61994-08-29 12:50:44 +0000917 else {
918 char buf[10];
Guido van Rossum82598051997-03-05 00:20:32 +0000919 PyFile_WriteString(" File \"", f);
Guido van Rossuma110aa61994-08-29 12:50:44 +0000920 if (filename == NULL)
Guido van Rossum82598051997-03-05 00:20:32 +0000921 PyFile_WriteString("<string>", f);
Guido van Rossuma110aa61994-08-29 12:50:44 +0000922 else
Guido van Rossum82598051997-03-05 00:20:32 +0000923 PyFile_WriteString(filename, f);
924 PyFile_WriteString("\", line ", f);
Guido van Rossuma110aa61994-08-29 12:50:44 +0000925 sprintf(buf, "%d", lineno);
Guido van Rossum82598051997-03-05 00:20:32 +0000926 PyFile_WriteString(buf, f);
927 PyFile_WriteString("\n", f);
Jeremy Hylton9f1b9932001-02-28 07:07:43 +0000928 if (text != NULL)
929 print_error_text(f, offset, text);
Guido van Rossuma110aa61994-08-29 12:50:44 +0000930 v = message;
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000931 /* Can't be bothered to check all those
932 PyFile_WriteString() calls */
933 if (PyErr_Occurred())
934 err = -1;
Guido van Rossuma110aa61994-08-29 12:50:44 +0000935 }
936 }
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000937 if (err) {
938 /* Don't do anything else */
939 }
940 else if (PyClass_Check(exception)) {
Barry Warsaw2f5f6a21997-09-16 21:42:03 +0000941 PyClassObject* exc = (PyClassObject*)exception;
942 PyObject* className = exc->cl_name;
943 PyObject* moduleName =
944 PyDict_GetItemString(exc->cl_dict, "__module__");
945
946 if (moduleName == NULL)
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000947 err = PyFile_WriteString("<unknown>", f);
Barry Warsaw2f5f6a21997-09-16 21:42:03 +0000948 else {
949 char* modstr = PyString_AsString(moduleName);
950 if (modstr && strcmp(modstr, "exceptions"))
951 {
952 err = PyFile_WriteString(modstr, f);
953 err += PyFile_WriteString(".", f);
954 }
955 }
956 if (err == 0) {
957 if (className == NULL)
958 err = PyFile_WriteString("<unknown>", f);
959 else
960 err = PyFile_WriteObject(className, f,
961 Py_PRINT_RAW);
962 }
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000963 }
964 else
965 err = PyFile_WriteObject(exception, f, Py_PRINT_RAW);
966 if (err == 0) {
967 if (v != NULL && v != Py_None) {
Barry Warsaw035574d1997-08-29 22:07:17 +0000968 PyObject *s = PyObject_Str(v);
969 /* only print colon if the str() of the
970 object is not the empty string
971 */
Guido van Rossumd6bf45b1997-09-05 19:11:53 +0000972 if (s == NULL)
973 err = -1;
974 else if (!PyString_Check(s) ||
975 PyString_GET_SIZE(s) != 0)
Barry Warsaw035574d1997-08-29 22:07:17 +0000976 err = PyFile_WriteString(": ", f);
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000977 if (err == 0)
Guido van Rossumd6bf45b1997-09-05 19:11:53 +0000978 err = PyFile_WriteObject(s, f, Py_PRINT_RAW);
979 Py_XDECREF(s);
Guido van Rossum262e1241995-02-07 15:30:45 +0000980 }
Guido van Rossum262e1241995-02-07 15:30:45 +0000981 }
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000982 if (err == 0)
983 err = PyFile_WriteString("\n", f);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000984 }
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000985 /* If an error happened here, don't show it.
986 XXX This is wrong, but too many callers rely on this behavior. */
987 if (err != 0)
988 PyErr_Clear();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000989}
990
Guido van Rossum82598051997-03-05 00:20:32 +0000991PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000992PyRun_String(char *str, int start, PyObject *globals, PyObject *locals)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000993{
Guido van Rossum82598051997-03-05 00:20:32 +0000994 return run_err_node(PyParser_SimpleParseString(str, start),
Jeremy Hylton9f324e92001-03-01 22:59:14 +0000995 "<string>", globals, locals, NULL);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000996}
997
Guido van Rossum82598051997-03-05 00:20:32 +0000998PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000999PyRun_File(FILE *fp, char *filename, int start, PyObject *globals,
1000 PyObject *locals)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001001{
Tim Peterse8682112000-08-27 20:18:17 +00001002 return PyRun_FileEx(fp, filename, start, globals, locals, 0);
Guido van Rossum0df002c2000-08-27 19:21:52 +00001003}
1004
1005PyObject *
1006PyRun_FileEx(FILE *fp, char *filename, int start, PyObject *globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001007 PyObject *locals, int closeit)
Guido van Rossum0df002c2000-08-27 19:21:52 +00001008{
1009 node *n = PyParser_SimpleParseFile(fp, filename, start);
1010 if (closeit)
1011 fclose(fp);
Jeremy Hylton9f324e92001-03-01 22:59:14 +00001012 return run_err_node(n, filename, globals, locals, NULL);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001013}
1014
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001015PyObject *
1016PyRun_StringFlags(char *str, int start, PyObject *globals, PyObject *locals,
1017 PyCompilerFlags *flags)
1018{
Guido van Rossuma1b3a472001-07-16 16:51:33 +00001019 return run_err_node(PyParser_SimpleParseStringFlags(
Jeremy Hyltonb857ba22001-08-10 21:41:33 +00001020 str, start,
1021 (flags && flags->cf_flags & CO_GENERATOR_ALLOWED) ?
Guido van Rossuma1b3a472001-07-16 16:51:33 +00001022 PyPARSE_YIELD_IS_KEYWORD : 0),
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001023 "<string>", globals, locals, flags);
1024}
1025
1026PyObject *
1027PyRun_FileFlags(FILE *fp, char *filename, int start, PyObject *globals,
1028 PyObject *locals, PyCompilerFlags *flags)
1029{
1030 return PyRun_FileExFlags(fp, filename, start, globals, locals, 0,
1031 flags);
1032}
1033
1034PyObject *
1035PyRun_FileExFlags(FILE *fp, char *filename, int start, PyObject *globals,
1036 PyObject *locals, int closeit, PyCompilerFlags *flags)
1037{
Tim Petersfe2127d2001-07-16 05:37:24 +00001038 node *n = PyParser_SimpleParseFileFlags(fp, filename, start,
Jeremy Hyltonb857ba22001-08-10 21:41:33 +00001039 (flags && flags->cf_flags & CO_GENERATOR_ALLOWED) ?
Tim Petersfe2127d2001-07-16 05:37:24 +00001040 PyPARSE_YIELD_IS_KEYWORD : 0);
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001041 if (closeit)
1042 fclose(fp);
1043 return run_err_node(n, filename, globals, locals, flags);
1044}
1045
Guido van Rossum82598051997-03-05 00:20:32 +00001046static PyObject *
Jeremy Hylton9f324e92001-03-01 22:59:14 +00001047run_err_node(node *n, char *filename, PyObject *globals, PyObject *locals,
1048 PyCompilerFlags *flags)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001049{
Guido van Rossuma110aa61994-08-29 12:50:44 +00001050 if (n == NULL)
1051 return NULL;
Jeremy Hylton9f324e92001-03-01 22:59:14 +00001052 return run_node(n, filename, globals, locals, flags);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001053}
1054
Guido van Rossum82598051997-03-05 00:20:32 +00001055static PyObject *
Jeremy Hylton9f324e92001-03-01 22:59:14 +00001056run_node(node *n, char *filename, PyObject *globals, PyObject *locals,
1057 PyCompilerFlags *flags)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001058{
Guido van Rossum82598051997-03-05 00:20:32 +00001059 PyCodeObject *co;
1060 PyObject *v;
Jeremy Hylton9f324e92001-03-01 22:59:14 +00001061 co = PyNode_CompileFlags(n, filename, flags);
Guido van Rossum82598051997-03-05 00:20:32 +00001062 PyNode_Free(n);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001063 if (co == NULL)
1064 return NULL;
Guido van Rossum82598051997-03-05 00:20:32 +00001065 v = PyEval_EvalCode(co, globals, locals);
1066 Py_DECREF(co);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001067 return v;
1068}
1069
Guido van Rossum82598051997-03-05 00:20:32 +00001070static PyObject *
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001071run_pyc_file(FILE *fp, char *filename, PyObject *globals, PyObject *locals,
1072 PyCompilerFlags *flags)
Guido van Rossumfdef2711994-09-14 13:31:04 +00001073{
Guido van Rossum82598051997-03-05 00:20:32 +00001074 PyCodeObject *co;
1075 PyObject *v;
Guido van Rossumfdef2711994-09-14 13:31:04 +00001076 long magic;
Fred Drakee8de31c2000-08-31 05:38:39 +00001077 long PyImport_GetMagicNumber(void);
Guido van Rossumfdef2711994-09-14 13:31:04 +00001078
Guido van Rossum82598051997-03-05 00:20:32 +00001079 magic = PyMarshal_ReadLongFromFile(fp);
1080 if (magic != PyImport_GetMagicNumber()) {
1081 PyErr_SetString(PyExc_RuntimeError,
Guido van Rossumfdef2711994-09-14 13:31:04 +00001082 "Bad magic number in .pyc file");
1083 return NULL;
1084 }
Guido van Rossum82598051997-03-05 00:20:32 +00001085 (void) PyMarshal_ReadLongFromFile(fp);
Tim Petersd9b9ac82001-01-28 00:27:39 +00001086 v = PyMarshal_ReadLastObjectFromFile(fp);
Guido van Rossumfdef2711994-09-14 13:31:04 +00001087 fclose(fp);
Guido van Rossum82598051997-03-05 00:20:32 +00001088 if (v == NULL || !PyCode_Check(v)) {
1089 Py_XDECREF(v);
1090 PyErr_SetString(PyExc_RuntimeError,
Guido van Rossumfdef2711994-09-14 13:31:04 +00001091 "Bad code object in .pyc file");
1092 return NULL;
1093 }
Guido van Rossum82598051997-03-05 00:20:32 +00001094 co = (PyCodeObject *)v;
1095 v = PyEval_EvalCode(co, globals, locals);
Jeremy Hyltonb857ba22001-08-10 21:41:33 +00001096 if (v && flags)
1097 flags->cf_flags |= (co->co_flags & PyCF_MASK);
Guido van Rossum82598051997-03-05 00:20:32 +00001098 Py_DECREF(co);
Guido van Rossumfdef2711994-09-14 13:31:04 +00001099 return v;
1100}
1101
Guido van Rossum82598051997-03-05 00:20:32 +00001102PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001103Py_CompileString(char *str, char *filename, int start)
Guido van Rossum5b722181993-03-30 17:46:03 +00001104{
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001105 return Py_CompileStringFlags(str, filename, start, NULL);
1106}
1107
1108PyObject *
1109Py_CompileStringFlags(char *str, char *filename, int start,
1110 PyCompilerFlags *flags)
1111{
Guido van Rossum5b722181993-03-30 17:46:03 +00001112 node *n;
Guido van Rossum82598051997-03-05 00:20:32 +00001113 PyCodeObject *co;
Tim Petersfe2127d2001-07-16 05:37:24 +00001114 n = PyParser_SimpleParseStringFlags(str, start,
Jeremy Hyltonb857ba22001-08-10 21:41:33 +00001115 (flags && flags->cf_flags & CO_GENERATOR_ALLOWED) ?
Tim Petersfe2127d2001-07-16 05:37:24 +00001116 PyPARSE_YIELD_IS_KEYWORD : 0);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001117 if (n == NULL)
Guido van Rossum5b722181993-03-30 17:46:03 +00001118 return NULL;
Jeremy Hylton673a4fd2001-03-26 19:53:38 +00001119 co = PyNode_CompileFlags(n, filename, flags);
Guido van Rossum82598051997-03-05 00:20:32 +00001120 PyNode_Free(n);
1121 return (PyObject *)co;
Guido van Rossum5b722181993-03-30 17:46:03 +00001122}
1123
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001124struct symtable *
1125Py_SymtableString(char *str, char *filename, int start)
1126{
1127 node *n;
1128 struct symtable *st;
1129 n = PyParser_SimpleParseString(str, start);
1130 if (n == NULL)
1131 return NULL;
1132 st = PyNode_CompileSymtable(n, filename);
1133 PyNode_Free(n);
1134 return st;
1135}
1136
Guido van Rossuma110aa61994-08-29 12:50:44 +00001137/* Simplified interface to parsefile -- return node or set exception */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001138
Guido van Rossuma110aa61994-08-29 12:50:44 +00001139node *
Tim Petersfe2127d2001-07-16 05:37:24 +00001140PyParser_SimpleParseFileFlags(FILE *fp, char *filename, int start, int flags)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001141{
Guido van Rossuma110aa61994-08-29 12:50:44 +00001142 node *n;
1143 perrdetail err;
Tim Petersfe2127d2001-07-16 05:37:24 +00001144 n = PyParser_ParseFileFlags(fp, filename, &_PyParser_Grammar, start,
1145 (char *)0, (char *)0, &err, flags);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001146 if (n == NULL)
1147 err_input(&err);
1148 return n;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001149}
1150
Tim Petersfe2127d2001-07-16 05:37:24 +00001151node *
1152PyParser_SimpleParseFile(FILE *fp, char *filename, int start)
1153{
1154 return PyParser_SimpleParseFileFlags(fp, filename, start, 0);
1155}
1156
Guido van Rossuma110aa61994-08-29 12:50:44 +00001157/* Simplified interface to parsestring -- return node or set exception */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001158
Guido van Rossuma110aa61994-08-29 12:50:44 +00001159node *
Tim Petersfe2127d2001-07-16 05:37:24 +00001160PyParser_SimpleParseStringFlags(char *str, int start, int flags)
1161{
1162 node *n;
1163 perrdetail err;
1164 n = PyParser_ParseStringFlags(str, &_PyParser_Grammar, start, &err,
1165 flags);
1166 if (n == NULL)
1167 err_input(&err);
1168 return n;
1169}
1170
1171node *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001172PyParser_SimpleParseString(char *str, int start)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001173{
Tim Petersfe2127d2001-07-16 05:37:24 +00001174 return PyParser_SimpleParseStringFlags(str, start, 0);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001175}
1176
1177/* Set the error appropriate to the given input error code (see errcode.h) */
1178
1179static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001180err_input(perrdetail *err)
Guido van Rossuma110aa61994-08-29 12:50:44 +00001181{
Fred Drake85f36392000-07-11 17:53:00 +00001182 PyObject *v, *w, *errtype;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001183 char *msg = NULL;
Fred Drake85f36392000-07-11 17:53:00 +00001184 errtype = PyExc_SyntaxError;
Guido van Rossum82598051997-03-05 00:20:32 +00001185 v = Py_BuildValue("(ziiz)", err->filename,
Guido van Rossuma110aa61994-08-29 12:50:44 +00001186 err->lineno, err->offset, err->text);
1187 if (err->text != NULL) {
Guido van Rossumb18618d2000-05-03 23:44:39 +00001188 PyMem_DEL(err->text);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001189 err->text = NULL;
1190 }
1191 switch (err->error) {
1192 case E_SYNTAX:
Fred Drake85f36392000-07-11 17:53:00 +00001193 errtype = PyExc_IndentationError;
1194 if (err->expected == INDENT)
1195 msg = "expected an indented block";
1196 else if (err->token == INDENT)
1197 msg = "unexpected indent";
1198 else if (err->token == DEDENT)
1199 msg = "unexpected unindent";
1200 else {
1201 errtype = PyExc_SyntaxError;
1202 msg = "invalid syntax";
1203 }
Guido van Rossuma110aa61994-08-29 12:50:44 +00001204 break;
1205 case E_TOKEN:
1206 msg = "invalid token";
Guido van Rossuma110aa61994-08-29 12:50:44 +00001207 break;
1208 case E_INTR:
Guido van Rossum82598051997-03-05 00:20:32 +00001209 PyErr_SetNone(PyExc_KeyboardInterrupt);
Barry Warsawc80baa31999-01-27 16:39:40 +00001210 Py_XDECREF(v);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001211 return;
1212 case E_NOMEM:
Guido van Rossum82598051997-03-05 00:20:32 +00001213 PyErr_NoMemory();
Barry Warsawc80baa31999-01-27 16:39:40 +00001214 Py_XDECREF(v);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001215 return;
1216 case E_EOF:
1217 msg = "unexpected EOF while parsing";
1218 break;
Fred Drake85f36392000-07-11 17:53:00 +00001219 case E_TABSPACE:
1220 errtype = PyExc_TabError;
Guido van Rossum560e8ad1998-04-10 19:43:42 +00001221 msg = "inconsistent use of tabs and spaces in indentation";
1222 break;
Jeremy Hylton94988062000-06-20 19:10:44 +00001223 case E_OVERFLOW:
1224 msg = "expression too long";
1225 break;
Fred Drake85f36392000-07-11 17:53:00 +00001226 case E_DEDENT:
1227 errtype = PyExc_IndentationError;
1228 msg = "unindent does not match any outer indentation level";
1229 break;
1230 case E_TOODEEP:
1231 errtype = PyExc_IndentationError;
1232 msg = "too many levels of indentation";
1233 break;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001234 default:
1235 fprintf(stderr, "error=%d\n", err->error);
1236 msg = "unknown parsing error";
1237 break;
1238 }
Guido van Rossum82598051997-03-05 00:20:32 +00001239 w = Py_BuildValue("(sO)", msg, v);
Guido van Rossum41318302001-03-23 04:01:07 +00001240 Py_XDECREF(v);
Fred Drake85f36392000-07-11 17:53:00 +00001241 PyErr_SetObject(errtype, w);
Guido van Rossum82598051997-03-05 00:20:32 +00001242 Py_XDECREF(w);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001243}
1244
1245/* Print fatal error message and abort */
1246
1247void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001248Py_FatalError(char *msg)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001249{
Guido van Rossum83dd6c31994-09-29 09:38:33 +00001250 fprintf(stderr, "Fatal Python error: %s\n", msg);
Guido van Rossum8ae87c01995-01-26 00:40:38 +00001251#ifdef macintosh
1252 for (;;);
1253#endif
Guido van Rossum9b38a141996-09-11 23:12:24 +00001254#ifdef MS_WIN32
Guido van Rossum23c94461997-05-22 20:21:30 +00001255 OutputDebugString("Fatal Python error: ");
Guido van Rossuma44823b1995-03-14 15:01:17 +00001256 OutputDebugString(msg);
1257 OutputDebugString("\n");
Guido van Rossum0ba35361998-08-13 13:33:16 +00001258#ifdef _DEBUG
1259 DebugBreak();
Guido van Rossuma44823b1995-03-14 15:01:17 +00001260#endif
Guido van Rossum0ba35361998-08-13 13:33:16 +00001261#endif /* MS_WIN32 */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001262 abort();
1263}
1264
1265/* Clean up and exit */
1266
Guido van Rossuma110aa61994-08-29 12:50:44 +00001267#ifdef WITH_THREAD
Guido van Rossum49b56061998-10-01 20:42:43 +00001268#include "pythread.h"
Guido van Rossum82598051997-03-05 00:20:32 +00001269int _PyThread_Started = 0; /* Set by threadmodule.c and maybe others */
Guido van Rossumf9f2e821992-08-17 08:59:08 +00001270#endif
1271
Guido van Rossum2dcfc961998-10-01 16:01:57 +00001272#define NEXITFUNCS 32
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001273static void (*exitfuncs[NEXITFUNCS])(void);
Guido van Rossum1662dd51994-09-07 14:38:28 +00001274static int nexitfuncs = 0;
1275
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001276int Py_AtExit(void (*func)(void))
Guido van Rossum1662dd51994-09-07 14:38:28 +00001277{
1278 if (nexitfuncs >= NEXITFUNCS)
1279 return -1;
1280 exitfuncs[nexitfuncs++] = func;
1281 return 0;
1282}
1283
Guido van Rossumcc283f51997-08-05 02:22:03 +00001284static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001285call_sys_exitfunc(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001286{
Guido van Rossum82598051997-03-05 00:20:32 +00001287 PyObject *exitfunc = PySys_GetObject("exitfunc");
Guido van Rossum59bff391992-09-03 20:28:00 +00001288
1289 if (exitfunc) {
Fred Drake6a12d8d2001-03-23 17:34:02 +00001290 PyObject *res;
Guido van Rossum82598051997-03-05 00:20:32 +00001291 Py_INCREF(exitfunc);
1292 PySys_SetObject("exitfunc", (PyObject *)NULL);
1293 res = PyEval_CallObject(exitfunc, (PyObject *)NULL);
Guido van Rossum59bff391992-09-03 20:28:00 +00001294 if (res == NULL) {
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001295 if (!PyErr_ExceptionMatches(PyExc_SystemExit)) {
1296 PySys_WriteStderr("Error in sys.exitfunc:\n");
1297 }
Guido van Rossum82598051997-03-05 00:20:32 +00001298 PyErr_Print();
Guido van Rossum59bff391992-09-03 20:28:00 +00001299 }
Guido van Rossum82598051997-03-05 00:20:32 +00001300 Py_DECREF(exitfunc);
Guido van Rossum59bff391992-09-03 20:28:00 +00001301 }
1302
Guido van Rossum0829c751998-02-28 04:31:39 +00001303 if (Py_FlushLine())
1304 PyErr_Clear();
Guido van Rossumcc283f51997-08-05 02:22:03 +00001305}
Guido van Rossum1662dd51994-09-07 14:38:28 +00001306
Guido van Rossumcc283f51997-08-05 02:22:03 +00001307static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001308call_ll_exitfuncs(void)
Guido van Rossumcc283f51997-08-05 02:22:03 +00001309{
Guido van Rossum1662dd51994-09-07 14:38:28 +00001310 while (nexitfuncs > 0)
1311 (*exitfuncs[--nexitfuncs])();
Guido van Rossum25ce5661997-08-02 03:10:38 +00001312
1313 fflush(stdout);
1314 fflush(stderr);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001315}
1316
1317void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001318Py_Exit(int sts)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001319{
Guido van Rossumcc283f51997-08-05 02:22:03 +00001320 Py_Finalize();
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001321
Jack Jansen66a89771995-10-27 13:22:14 +00001322#ifdef macintosh
1323 PyMac_Exit(sts);
1324#else
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001325 exit(sts);
Jack Jansen66a89771995-10-27 13:22:14 +00001326#endif
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001327}
1328
Guido van Rossumf1dc5661993-07-05 10:31:29 +00001329static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001330initsigs(void)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001331{
Guido van Rossuma110aa61994-08-29 12:50:44 +00001332#ifdef HAVE_SIGNAL_H
1333#ifdef SIGPIPE
1334 signal(SIGPIPE, SIG_IGN);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001335#endif
Guido van Rossum70d893a2001-08-16 08:21:42 +00001336#ifdef SIGXFZ
1337 signal(SIGXFZ, SIG_IGN);
1338#endif
Guido van Rossuma110aa61994-08-29 12:50:44 +00001339#endif /* HAVE_SIGNAL_H */
Guido van Rossum82598051997-03-05 00:20:32 +00001340 PyOS_InitInterrupts(); /* May imply initsignal() */
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001341}
1342
Guido van Rossumaae0d321996-05-22 16:35:33 +00001343#ifdef Py_TRACE_REFS
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001344/* Ask a yes/no question */
1345
Guido van Rossum59bff391992-09-03 20:28:00 +00001346int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001347_Py_AskYesNo(char *prompt)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001348{
1349 char buf[256];
1350
Tim Peters6d6c1a32001-08-02 04:15:00 +00001351 fprintf(stderr, "%s [ny] ", prompt);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001352 if (fgets(buf, sizeof buf, stdin) == NULL)
1353 return 0;
1354 return buf[0] == 'y' || buf[0] == 'Y';
1355}
1356#endif
1357
Guido van Rossuma110aa61994-08-29 12:50:44 +00001358#ifdef MPW
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001359
1360/* Check for file descriptor connected to interactive device.
1361 Pretend that stdin is always interactive, other files never. */
1362
1363int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001364isatty(int fd)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001365{
1366 return fd == fileno(stdin);
1367}
1368
1369#endif
Guido van Rossum7433b121997-02-14 19:45:36 +00001370
1371/*
1372 * The file descriptor fd is considered ``interactive'' if either
1373 * a) isatty(fd) is TRUE, or
1374 * b) the -i flag was given, and the filename associated with
1375 * the descriptor is NULL or "<stdin>" or "???".
1376 */
1377int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001378Py_FdIsInteractive(FILE *fp, char *filename)
Guido van Rossum7433b121997-02-14 19:45:36 +00001379{
1380 if (isatty((int)fileno(fp)))
1381 return 1;
1382 if (!Py_InteractiveFlag)
1383 return 0;
1384 return (filename == NULL) ||
1385 (strcmp(filename, "<stdin>") == 0) ||
1386 (strcmp(filename, "???") == 0);
1387}
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001388
1389
1390#if defined(USE_STACKCHECK)
1391#if defined(WIN32) && defined(_MSC_VER)
1392
1393/* Stack checking for Microsoft C */
1394
1395#include <malloc.h>
1396#include <excpt.h>
1397
Fred Drakee8de31c2000-08-31 05:38:39 +00001398/*
1399 * Return non-zero when we run out of memory on the stack; zero otherwise.
1400 */
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001401int
Fred Drake399739f2000-08-31 05:52:44 +00001402PyOS_CheckStack(void)
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001403{
1404 __try {
1405 /* _alloca throws a stack overflow exception if there's
1406 not enough space left on the stack */
1407 _alloca(PYOS_STACK_MARGIN * sizeof(void*));
1408 return 0;
1409 } __except (EXCEPTION_EXECUTE_HANDLER) {
1410 /* just ignore all errors */
1411 }
1412 return 1;
1413}
1414
1415#endif /* WIN32 && _MSC_VER */
1416
1417/* Alternate implementations can be added here... */
1418
1419#endif /* USE_STACKCHECK */
Guido van Rossum6f256182000-09-16 16:32:19 +00001420
1421
1422/* Wrappers around sigaction() or signal(). */
1423
1424PyOS_sighandler_t
1425PyOS_getsig(int sig)
1426{
1427#ifdef HAVE_SIGACTION
1428 struct sigaction context;
1429 sigaction(sig, NULL, &context);
1430 return context.sa_handler;
1431#else
1432 PyOS_sighandler_t handler;
1433 handler = signal(sig, SIG_IGN);
1434 signal(sig, handler);
1435 return handler;
1436#endif
1437}
1438
1439PyOS_sighandler_t
1440PyOS_setsig(int sig, PyOS_sighandler_t handler)
1441{
1442#ifdef HAVE_SIGACTION
1443 struct sigaction context;
1444 PyOS_sighandler_t oldhandler;
1445 sigaction(sig, NULL, &context);
1446 oldhandler = context.sa_handler;
1447 context.sa_handler = handler;
1448 sigaction(sig, &context, NULL);
1449 return oldhandler;
1450#else
1451 return signal(sig, handler);
1452#endif
1453}