blob: 44b55d2708c3cbd341d59cae3e258d5d6b5e216d [file] [log] [blame]
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001/***********************************************************
Guido van Rossumfd71b9e2000-06-30 23:50:40 +00002Copyright (c) 2000, BeOpen.com.
3Copyright (c) 1995-2000, Corporation for National Research Initiatives.
4Copyright (c) 1990-1995, Stichting Mathematisch Centrum.
5All rights reserved.
Guido van Rossum1984f1e1992-08-04 12:41:02 +00006
Guido van Rossumfd71b9e2000-06-30 23:50:40 +00007See the file "Misc/COPYRIGHT" for information on usage and
8redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
Guido van Rossum1984f1e1992-08-04 12:41:02 +00009******************************************************************/
10
11/* Python interpreter top-level routines, including init/exit */
12
Guido van Rossum82598051997-03-05 00:20:32 +000013#include "Python.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +000014
15#include "grammar.h"
16#include "node.h"
Fred Drake85f36392000-07-11 17:53:00 +000017#include "token.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +000018#include "parsetok.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +000019#include "errcode.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +000020#include "compile.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +000021#include "eval.h"
Guido van Rossumfdef2711994-09-14 13:31:04 +000022#include "marshal.h"
Fred Drake83cb7972000-08-15 15:49:03 +000023#include "osdefs.h" /* SEP */
Guido van Rossum1984f1e1992-08-04 12:41:02 +000024
Guido van Rossum80bb9651996-12-05 23:27:02 +000025#ifdef HAVE_UNISTD_H
26#include <unistd.h>
27#endif
28
Guido van Rossuma110aa61994-08-29 12:50:44 +000029#ifdef HAVE_SIGNAL_H
Guido van Rossuma9e7dc11992-10-18 18:53:57 +000030#include <signal.h>
Guido van Rossuma9e7dc11992-10-18 18:53:57 +000031#endif
32
Guido van Rossum9b38a141996-09-11 23:12:24 +000033#ifdef MS_WIN32
Guido van Rossuma44823b1995-03-14 15:01:17 +000034#undef BYTE
35#include "windows.h"
36#endif
37
Jack Jansencbf630f2000-07-11 21:59:16 +000038#ifdef macintosh
39#include "macglue.h"
40#endif
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000041extern char *Py_GetPath(void);
Guido van Rossum1984f1e1992-08-04 12:41:02 +000042
Guido van Rossum82598051997-03-05 00:20:32 +000043extern grammar _PyParser_Grammar; /* From graminit.c */
Guido van Rossum1984f1e1992-08-04 12:41:02 +000044
Guido van Rossumb73cc041993-11-01 16:28:59 +000045/* Forward */
Tim Petersdbd9ba62000-07-09 03:09:57 +000046static void initmain(void);
47static void initsite(void);
48static PyObject *run_err_node(node *n, char *filename,
49 PyObject *globals, PyObject *locals);
50static PyObject *run_node(node *n, char *filename,
51 PyObject *globals, PyObject *locals);
52static PyObject *run_pyc_file(FILE *fp, char *filename,
53 PyObject *globals, PyObject *locals);
54static void err_input(perrdetail *);
55static void initsigs(void);
56static void call_sys_exitfunc(void);
57static void call_ll_exitfuncs(void);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +000058
Guido van Rossumbffd6832000-01-20 22:32:56 +000059#ifdef Py_TRACE_REFS
60int _Py_AskYesNo(char *prompt);
61#endif
62
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000063extern void _PyUnicode_Init(void);
64extern void _PyUnicode_Fini(void);
65extern void _PyCodecRegistry_Init(void);
66extern void _PyCodecRegistry_Fini(void);
Guido van Rossumc94044c2000-03-10 23:03:54 +000067
68
Guido van Rossum82598051997-03-05 00:20:32 +000069int Py_DebugFlag; /* Needed by parser.c */
70int Py_VerboseFlag; /* Needed by import.c */
Guido van Rossum7433b121997-02-14 19:45:36 +000071int Py_InteractiveFlag; /* Needed by Py_FdIsInteractive() below */
Guido van Rossumdcc0c131997-08-29 22:32:42 +000072int Py_NoSiteFlag; /* Suppress 'import site' */
Barry Warsaw3ce09642000-05-02 19:18:59 +000073int Py_UseClassExceptionsFlag = 1; /* Needed by bltinmodule.c: deprecated */
Guido van Rossuma61691e1998-02-06 22:27:24 +000074int Py_FrozenFlag; /* Needed by getpath.c */
Guido van Rossumb16d1972000-05-01 17:55:15 +000075int Py_UnicodeFlag = 0; /* Needed by compile.c */
Guido van Rossum1984f1e1992-08-04 12:41:02 +000076
Guido van Rossum25ce5661997-08-02 03:10:38 +000077static int initialized = 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +000078
Thomas Wouters7e474022000-07-16 12:04:32 +000079/* API to access the initialized flag -- useful for esoteric use */
Guido van Rossume3c0d5e1997-08-22 04:20:13 +000080
81int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000082Py_IsInitialized(void)
Guido van Rossume3c0d5e1997-08-22 04:20:13 +000083{
84 return initialized;
85}
86
Guido van Rossum25ce5661997-08-02 03:10:38 +000087/* Global initializations. Can be undone by Py_Finalize(). Don't
88 call this twice without an intervening Py_Finalize() call. When
89 initializations fail, a fatal error is issued and the function does
90 not return. On return, the first thread and interpreter state have
91 been created.
Guido van Rossum1984f1e1992-08-04 12:41:02 +000092
Guido van Rossum25ce5661997-08-02 03:10:38 +000093 Locking: you must hold the interpreter lock while calling this.
94 (If the lock has not yet been initialized, that's equivalent to
95 having the lock, but you cannot use multiple threads.)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +000096
Guido van Rossum25ce5661997-08-02 03:10:38 +000097*/
Guido van Rossuma027efa1997-05-05 20:56:21 +000098
99void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000100Py_Initialize(void)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000101{
Guido van Rossuma027efa1997-05-05 20:56:21 +0000102 PyInterpreterState *interp;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000103 PyThreadState *tstate;
104 PyObject *bimod, *sysmod;
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000105 char *p;
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000106
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000107 if (initialized)
Guido van Rossumaa615051997-08-20 22:40:18 +0000108 return;
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000109 initialized = 1;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000110
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000111 if ((p = getenv("PYTHONDEBUG")) && *p != '\0')
112 Py_DebugFlag = 1;
113 if ((p = getenv("PYTHONVERBOSE")) && *p != '\0')
114 Py_VerboseFlag = 1;
Guido van Rossum562f5b11998-10-07 14:50:42 +0000115 if ((p = getenv("PYTHONOPTIMIZE")) && *p != '\0')
116 Py_OptimizeFlag = 1;
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000117
Guido van Rossuma027efa1997-05-05 20:56:21 +0000118 interp = PyInterpreterState_New();
119 if (interp == NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000120 Py_FatalError("Py_Initialize: can't make first interpreter");
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000121
Guido van Rossuma027efa1997-05-05 20:56:21 +0000122 tstate = PyThreadState_New(interp);
123 if (tstate == NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000124 Py_FatalError("Py_Initialize: can't make first thread");
Guido van Rossuma027efa1997-05-05 20:56:21 +0000125 (void) PyThreadState_Swap(tstate);
126
Guido van Rossum25ce5661997-08-02 03:10:38 +0000127 interp->modules = PyDict_New();
128 if (interp->modules == NULL)
129 Py_FatalError("Py_Initialize: can't make modules dictionary");
Guido van Rossuma027efa1997-05-05 20:56:21 +0000130
Guido van Rossumc94044c2000-03-10 23:03:54 +0000131 /* Init codec registry */
132 _PyCodecRegistry_Init();
133
134 /* Init Unicode implementation; relies on the codec registry */
135 _PyUnicode_Init();
136
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000137 _PyCompareState_Key = PyString_InternFromString("cmp_state");
138
Barry Warsawf242aa02000-05-25 23:09:49 +0000139 bimod = _PyBuiltin_Init();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000140 if (bimod == NULL)
141 Py_FatalError("Py_Initialize: can't initialize __builtin__");
Guido van Rossum4a1f39a1997-11-04 19:36:18 +0000142 interp->builtins = PyModule_GetDict(bimod);
143 Py_INCREF(interp->builtins);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000144
145 sysmod = _PySys_Init();
146 if (sysmod == NULL)
147 Py_FatalError("Py_Initialize: can't initialize sys");
148 interp->sysdict = PyModule_GetDict(sysmod);
149 Py_INCREF(interp->sysdict);
150 _PyImport_FixupExtension("sys", "sys");
Guido van Rossuma027efa1997-05-05 20:56:21 +0000151 PySys_SetPath(Py_GetPath());
Guido van Rossum25ce5661997-08-02 03:10:38 +0000152 PyDict_SetItemString(interp->sysdict, "modules",
153 interp->modules);
154
Guido van Rossum7c85ab81999-07-08 17:26:56 +0000155 _PyImport_Init();
156
Barry Warsawf242aa02000-05-25 23:09:49 +0000157 /* initialize builtin exceptions */
158 init_exceptions();
159
Barry Warsaw035574d1997-08-29 22:07:17 +0000160 /* phase 2 of builtins */
Barry Warsaw963b8711997-09-18 16:42:02 +0000161 _PyImport_FixupExtension("__builtin__", "__builtin__");
Barry Warsaw035574d1997-08-29 22:07:17 +0000162
Guido van Rossum25ce5661997-08-02 03:10:38 +0000163 initsigs(); /* Signal handling stuff, including initintr() */
164
165 initmain(); /* Module __main__ */
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000166 if (!Py_NoSiteFlag)
167 initsite(); /* Module site */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000168}
169
Guido van Rossum2edcf0d1998-12-15 16:12:00 +0000170#ifdef COUNT_ALLOCS
Tim Petersdbd9ba62000-07-09 03:09:57 +0000171extern void dump_counts(void);
Guido van Rossum2edcf0d1998-12-15 16:12:00 +0000172#endif
173
Guido van Rossum25ce5661997-08-02 03:10:38 +0000174/* Undo the effect of Py_Initialize().
175
176 Beware: if multiple interpreter and/or thread states exist, these
177 are not wiped out; only the current thread and interpreter state
178 are deleted. But since everything else is deleted, those other
179 interpreter and thread states should no longer be used.
180
181 (XXX We should do better, e.g. wipe out all interpreters and
182 threads.)
183
184 Locking: as above.
185
186*/
187
188void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000189Py_Finalize(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000190{
191 PyInterpreterState *interp;
192 PyThreadState *tstate;
193
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000194 if (!initialized)
Guido van Rossumaa615051997-08-20 22:40:18 +0000195 return;
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000196 initialized = 0;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000197
Guido van Rossum4cc462e1998-01-19 22:00:38 +0000198 call_sys_exitfunc();
199
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000200 /* Get current thread state and interpreter pointer */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000201 tstate = PyThreadState_Get();
202 interp = tstate->interp;
203
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000204 /* Disable signal handling */
205 PyOS_FiniInterrupts();
206
Guido van Rossumc94044c2000-03-10 23:03:54 +0000207 /* Cleanup Unicode implementation */
208 _PyUnicode_Fini();
209
210 /* Cleanup Codec registry */
211 _PyCodecRegistry_Fini();
212
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000213 /* Destroy all modules */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000214 PyImport_Cleanup();
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000215
Guido van Rossum1707aad1997-12-08 23:43:45 +0000216 /* Destroy the database used by _PyImport_{Fixup,Find}Extension */
217 _PyImport_Fini();
218
219 /* Debugging stuff */
220#ifdef COUNT_ALLOCS
221 dump_counts();
222#endif
223
224#ifdef Py_REF_DEBUG
225 fprintf(stderr, "[%ld refs]\n", _Py_RefTotal);
226#endif
227
228#ifdef Py_TRACE_REFS
Guido van Rossumeca47842000-04-27 23:44:15 +0000229 if (
230#ifdef MS_WINDOWS /* Only ask on Windows if env var set */
231 getenv("PYTHONDUMPREFS") &&
232#endif /* MS_WINDOWS */
233 _Py_AskYesNo("Print left references?")) {
Guido van Rossum1707aad1997-12-08 23:43:45 +0000234 _Py_PrintReferences(stderr);
235 }
236#endif /* Py_TRACE_REFS */
237
Barry Warsaw035574d1997-08-29 22:07:17 +0000238 /* Now we decref the exception classes. After this point nothing
239 can raise an exception. That's okay, because each Fini() method
240 below has been checked to make sure no exceptions are ever
241 raised.
242 */
Barry Warsawf242aa02000-05-25 23:09:49 +0000243 fini_exceptions();
244
245 /* Delete current thread */
246 PyInterpreterState_Clear(interp);
247 PyThreadState_Swap(NULL);
248 PyInterpreterState_Delete(interp);
249
Guido van Rossumcc283f51997-08-05 02:22:03 +0000250 PyMethod_Fini();
251 PyFrame_Fini();
252 PyCFunction_Fini();
253 PyTuple_Fini();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000254 PyString_Fini();
Guido van Rossumcc283f51997-08-05 02:22:03 +0000255 PyInt_Fini();
256 PyFloat_Fini();
257
258 /* XXX Still allocated:
259 - various static ad-hoc pointers to interned strings
260 - int and float free list blocks
261 - whatever various modules and libraries allocate
262 */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000263
264 PyGrammar_RemoveAccelerators(&_PyParser_Grammar);
Guido van Rossumcc283f51997-08-05 02:22:03 +0000265
266 call_ll_exitfuncs();
267
Guido van Rossumcc283f51997-08-05 02:22:03 +0000268#ifdef Py_TRACE_REFS
Guido van Rossumcc283f51997-08-05 02:22:03 +0000269 _Py_ResetReferences();
270#endif /* Py_TRACE_REFS */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000271}
272
273/* Create and initialize a new interpreter and thread, and return the
274 new thread. This requires that Py_Initialize() has been called
275 first.
276
277 Unsuccessful initialization yields a NULL pointer. Note that *no*
278 exception information is available even in this case -- the
279 exception information is held in the thread, and there is no
280 thread.
281
282 Locking: as above.
283
284*/
285
286PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000287Py_NewInterpreter(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000288{
289 PyInterpreterState *interp;
290 PyThreadState *tstate, *save_tstate;
291 PyObject *bimod, *sysmod;
292
293 if (!initialized)
294 Py_FatalError("Py_NewInterpreter: call Py_Initialize first");
295
296 interp = PyInterpreterState_New();
297 if (interp == NULL)
298 return NULL;
299
300 tstate = PyThreadState_New(interp);
301 if (tstate == NULL) {
302 PyInterpreterState_Delete(interp);
303 return NULL;
304 }
305
306 save_tstate = PyThreadState_Swap(tstate);
307
308 /* XXX The following is lax in error checking */
309
310 interp->modules = PyDict_New();
311
312 bimod = _PyImport_FindExtension("__builtin__", "__builtin__");
313 if (bimod != NULL) {
Guido van Rossum4a1f39a1997-11-04 19:36:18 +0000314 interp->builtins = PyModule_GetDict(bimod);
315 Py_INCREF(interp->builtins);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000316 }
317 sysmod = _PyImport_FindExtension("sys", "sys");
318 if (bimod != NULL && sysmod != NULL) {
319 interp->sysdict = PyModule_GetDict(sysmod);
320 Py_INCREF(interp->sysdict);
321 PySys_SetPath(Py_GetPath());
322 PyDict_SetItemString(interp->sysdict, "modules",
323 interp->modules);
324 initmain();
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000325 if (!Py_NoSiteFlag)
326 initsite();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000327 }
328
329 if (!PyErr_Occurred())
330 return tstate;
331
332 /* Oops, it didn't work. Undo it all. */
333
334 PyErr_Print();
335 PyThreadState_Clear(tstate);
336 PyThreadState_Swap(save_tstate);
337 PyThreadState_Delete(tstate);
338 PyInterpreterState_Delete(interp);
339
340 return NULL;
341}
342
343/* Delete an interpreter and its last thread. This requires that the
344 given thread state is current, that the thread has no remaining
345 frames, and that it is its interpreter's only remaining thread.
346 It is a fatal error to violate these constraints.
347
348 (Py_Finalize() doesn't have these constraints -- it zaps
349 everything, regardless.)
350
351 Locking: as above.
352
353*/
354
355void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000356Py_EndInterpreter(PyThreadState *tstate)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000357{
358 PyInterpreterState *interp = tstate->interp;
359
360 if (tstate != PyThreadState_Get())
361 Py_FatalError("Py_EndInterpreter: thread is not current");
362 if (tstate->frame != NULL)
363 Py_FatalError("Py_EndInterpreter: thread still has a frame");
364 if (tstate != interp->tstate_head || tstate->next != NULL)
365 Py_FatalError("Py_EndInterpreter: not the last thread");
366
367 PyImport_Cleanup();
368 PyInterpreterState_Clear(interp);
369 PyThreadState_Swap(NULL);
370 PyInterpreterState_Delete(interp);
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000371}
372
373static char *progname = "python";
374
375void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000376Py_SetProgramName(char *pn)
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000377{
378 if (pn && *pn)
379 progname = pn;
380}
381
382char *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000383Py_GetProgramName(void)
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000384{
385 return progname;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000386}
387
Guido van Rossuma61691e1998-02-06 22:27:24 +0000388static char *default_home = NULL;
389
390void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000391Py_SetPythonHome(char *home)
Guido van Rossuma61691e1998-02-06 22:27:24 +0000392{
393 default_home = home;
394}
395
396char *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000397Py_GetPythonHome(void)
Guido van Rossuma61691e1998-02-06 22:27:24 +0000398{
399 char *home = default_home;
400 if (home == NULL)
401 home = getenv("PYTHONHOME");
402 return home;
403}
404
Guido van Rossum6135a871995-01-09 17:53:26 +0000405/* Create __main__ module */
406
407static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000408initmain(void)
Guido van Rossum6135a871995-01-09 17:53:26 +0000409{
Guido van Rossum82598051997-03-05 00:20:32 +0000410 PyObject *m, *d;
411 m = PyImport_AddModule("__main__");
Guido van Rossum6135a871995-01-09 17:53:26 +0000412 if (m == NULL)
Guido van Rossum82598051997-03-05 00:20:32 +0000413 Py_FatalError("can't create __main__ module");
414 d = PyModule_GetDict(m);
415 if (PyDict_GetItemString(d, "__builtins__") == NULL) {
Guido van Rossum858cb731997-11-19 16:15:37 +0000416 PyObject *bimod = PyImport_ImportModule("__builtin__");
417 if (bimod == NULL ||
418 PyDict_SetItemString(d, "__builtins__", bimod) != 0)
Guido van Rossum82598051997-03-05 00:20:32 +0000419 Py_FatalError("can't add __builtins__ to __main__");
Barry Warsaw3d05b1a1999-01-29 21:30:22 +0000420 Py_DECREF(bimod);
Guido van Rossum6135a871995-01-09 17:53:26 +0000421 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000422}
423
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000424/* Import the site module (not into __main__ though) */
425
426static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000427initsite(void)
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000428{
429 PyObject *m, *f;
430 m = PyImport_ImportModule("site");
431 if (m == NULL) {
432 f = PySys_GetObject("stderr");
433 if (Py_VerboseFlag) {
434 PyFile_WriteString(
435 "'import site' failed; traceback:\n", f);
436 PyErr_Print();
437 }
438 else {
439 PyFile_WriteString(
440 "'import site' failed; use -v for traceback\n", f);
441 PyErr_Clear();
442 }
443 }
444 else {
445 Py_DECREF(m);
446 }
447}
448
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000449/* Parse input from a file and execute it */
450
451int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000452PyRun_AnyFile(FILE *fp, char *filename)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000453{
454 if (filename == NULL)
455 filename = "???";
Guido van Rossum7433b121997-02-14 19:45:36 +0000456 if (Py_FdIsInteractive(fp, filename))
Guido van Rossum82598051997-03-05 00:20:32 +0000457 return PyRun_InteractiveLoop(fp, filename);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000458 else
Guido van Rossum82598051997-03-05 00:20:32 +0000459 return PyRun_SimpleFile(fp, filename);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000460}
461
462int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000463PyRun_InteractiveLoop(FILE *fp, char *filename)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000464{
Guido van Rossum82598051997-03-05 00:20:32 +0000465 PyObject *v;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000466 int ret;
Guido van Rossum82598051997-03-05 00:20:32 +0000467 v = PySys_GetObject("ps1");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000468 if (v == NULL) {
Guido van Rossum82598051997-03-05 00:20:32 +0000469 PySys_SetObject("ps1", v = PyString_FromString(">>> "));
470 Py_XDECREF(v);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000471 }
Guido van Rossum82598051997-03-05 00:20:32 +0000472 v = PySys_GetObject("ps2");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000473 if (v == NULL) {
Guido van Rossum82598051997-03-05 00:20:32 +0000474 PySys_SetObject("ps2", v = PyString_FromString("... "));
475 Py_XDECREF(v);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000476 }
477 for (;;) {
Guido van Rossum82598051997-03-05 00:20:32 +0000478 ret = PyRun_InteractiveOne(fp, filename);
Guido van Rossumaae0d321996-05-22 16:35:33 +0000479#ifdef Py_REF_DEBUG
Guido van Rossum6f9e4331995-03-29 16:57:48 +0000480 fprintf(stderr, "[%ld refs]\n", _Py_RefTotal);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000481#endif
482 if (ret == E_EOF)
483 return 0;
484 /*
485 if (ret == E_NOMEM)
486 return -1;
487 */
488 }
489}
490
491int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000492PyRun_InteractiveOne(FILE *fp, char *filename)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000493{
Guido van Rossum82598051997-03-05 00:20:32 +0000494 PyObject *m, *d, *v, *w;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000495 node *n;
Guido van Rossuma110aa61994-08-29 12:50:44 +0000496 perrdetail err;
Guido van Rossumddc3fb51997-11-25 20:58:13 +0000497 char *ps1 = "", *ps2 = "";
Guido van Rossum82598051997-03-05 00:20:32 +0000498 v = PySys_GetObject("ps1");
Guido van Rossumddc3fb51997-11-25 20:58:13 +0000499 if (v != NULL) {
500 v = PyObject_Str(v);
501 if (v == NULL)
502 PyErr_Clear();
503 else if (PyString_Check(v))
504 ps1 = PyString_AsString(v);
505 }
Guido van Rossum82598051997-03-05 00:20:32 +0000506 w = PySys_GetObject("ps2");
Guido van Rossumddc3fb51997-11-25 20:58:13 +0000507 if (w != NULL) {
508 w = PyObject_Str(w);
509 if (w == NULL)
510 PyErr_Clear();
511 else if (PyString_Check(w))
512 ps2 = PyString_AsString(w);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000513 }
Guido van Rossum82598051997-03-05 00:20:32 +0000514 n = PyParser_ParseFile(fp, filename, &_PyParser_Grammar,
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000515 Py_single_input, ps1, ps2, &err);
Guido van Rossum82598051997-03-05 00:20:32 +0000516 Py_XDECREF(v);
517 Py_XDECREF(w);
Guido van Rossuma110aa61994-08-29 12:50:44 +0000518 if (n == NULL) {
519 if (err.error == E_EOF) {
520 if (err.text)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000521 PyMem_DEL(err.text);
Guido van Rossuma110aa61994-08-29 12:50:44 +0000522 return E_EOF;
523 }
524 err_input(&err);
Guido van Rossum82598051997-03-05 00:20:32 +0000525 PyErr_Print();
Guido van Rossuma110aa61994-08-29 12:50:44 +0000526 return err.error;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000527 }
Guido van Rossum82598051997-03-05 00:20:32 +0000528 m = PyImport_AddModule("__main__");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000529 if (m == NULL)
530 return -1;
Guido van Rossum82598051997-03-05 00:20:32 +0000531 d = PyModule_GetDict(m);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000532 v = run_node(n, filename, d, d);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000533 if (v == NULL) {
Guido van Rossum82598051997-03-05 00:20:32 +0000534 PyErr_Print();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000535 return -1;
536 }
Guido van Rossum82598051997-03-05 00:20:32 +0000537 Py_DECREF(v);
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000538 if (Py_FlushLine())
539 PyErr_Clear();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000540 return 0;
541}
542
543int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000544PyRun_SimpleFile(FILE *fp, char *filename)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000545{
Guido van Rossum82598051997-03-05 00:20:32 +0000546 PyObject *m, *d, *v;
Guido van Rossumfdef2711994-09-14 13:31:04 +0000547 char *ext;
548
Guido van Rossum82598051997-03-05 00:20:32 +0000549 m = PyImport_AddModule("__main__");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000550 if (m == NULL)
551 return -1;
Guido van Rossum82598051997-03-05 00:20:32 +0000552 d = PyModule_GetDict(m);
Guido van Rossumfdef2711994-09-14 13:31:04 +0000553 ext = filename + strlen(filename) - 4;
Guido van Rossum2a7f58d1997-04-02 05:28:38 +0000554 if (strcmp(ext, ".pyc") == 0 || strcmp(ext, ".pyo") == 0
Jack Jansenbd06e961995-02-13 11:44:56 +0000555#ifdef macintosh
556 /* On a mac, we also assume a pyc file for types 'PYC ' and 'APPL' */
Jack Jansencbf630f2000-07-11 21:59:16 +0000557 || PyMac_getfiletype(filename) == 'PYC '
558 || PyMac_getfiletype(filename) == 'APPL'
Jack Jansenbd06e961995-02-13 11:44:56 +0000559#endif /* macintosh */
Guido van Rossum2a7f58d1997-04-02 05:28:38 +0000560 ) {
Guido van Rossumfdef2711994-09-14 13:31:04 +0000561 /* Try to run a pyc file. First, re-open in binary */
Jack Jansene00637b1994-12-14 12:58:37 +0000562 /* Don't close, done in main: fclose(fp); */
Guido van Rossumfdef2711994-09-14 13:31:04 +0000563 if( (fp = fopen(filename, "rb")) == NULL ) {
564 fprintf(stderr, "python: Can't reopen .pyc file\n");
565 return -1;
566 }
Guido van Rossum2a7f58d1997-04-02 05:28:38 +0000567 /* Turn on optimization if a .pyo file is given */
568 if (strcmp(ext, ".pyo") == 0)
569 Py_OptimizeFlag = 1;
Guido van Rossumfdef2711994-09-14 13:31:04 +0000570 v = run_pyc_file(fp, filename, d, d);
571 } else {
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000572 v = PyRun_File(fp, filename, Py_file_input, d, d);
Guido van Rossumfdef2711994-09-14 13:31:04 +0000573 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000574 if (v == NULL) {
Guido van Rossum82598051997-03-05 00:20:32 +0000575 PyErr_Print();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000576 return -1;
577 }
Guido van Rossum82598051997-03-05 00:20:32 +0000578 Py_DECREF(v);
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000579 if (Py_FlushLine())
580 PyErr_Clear();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000581 return 0;
582}
583
584int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000585PyRun_SimpleString(char *command)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000586{
Guido van Rossum82598051997-03-05 00:20:32 +0000587 PyObject *m, *d, *v;
588 m = PyImport_AddModule("__main__");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000589 if (m == NULL)
590 return -1;
Guido van Rossum82598051997-03-05 00:20:32 +0000591 d = PyModule_GetDict(m);
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000592 v = PyRun_String(command, Py_file_input, d, d);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000593 if (v == NULL) {
Guido van Rossum82598051997-03-05 00:20:32 +0000594 PyErr_Print();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000595 return -1;
596 }
Guido van Rossum82598051997-03-05 00:20:32 +0000597 Py_DECREF(v);
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000598 if (Py_FlushLine())
599 PyErr_Clear();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000600 return 0;
601}
602
Barry Warsaw035574d1997-08-29 22:07:17 +0000603static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000604parse_syntax_error(PyObject *err, PyObject **message, char **filename,
605 int *lineno, int *offset, char **text)
Barry Warsaw035574d1997-08-29 22:07:17 +0000606{
607 long hold;
608 PyObject *v;
609
610 /* old style errors */
611 if (PyTuple_Check(err))
612 return PyArg_Parse(err, "(O(ziiz))", message, filename,
613 lineno, offset, text);
614
615 /* new style errors. `err' is an instance */
616
617 if (! (v = PyObject_GetAttrString(err, "msg")))
618 goto finally;
619 *message = v;
620
621 if (!(v = PyObject_GetAttrString(err, "filename")))
622 goto finally;
623 if (v == Py_None)
624 *filename = NULL;
625 else if (! (*filename = PyString_AsString(v)))
626 goto finally;
627
628 Py_DECREF(v);
629 if (!(v = PyObject_GetAttrString(err, "lineno")))
630 goto finally;
631 hold = PyInt_AsLong(v);
632 Py_DECREF(v);
633 v = NULL;
634 if (hold < 0 && PyErr_Occurred())
635 goto finally;
636 *lineno = (int)hold;
637
638 if (!(v = PyObject_GetAttrString(err, "offset")))
639 goto finally;
640 hold = PyInt_AsLong(v);
641 Py_DECREF(v);
642 v = NULL;
643 if (hold < 0 && PyErr_Occurred())
644 goto finally;
645 *offset = (int)hold;
646
647 if (!(v = PyObject_GetAttrString(err, "text")))
648 goto finally;
649 if (v == Py_None)
650 *text = NULL;
651 else if (! (*text = PyString_AsString(v)))
652 goto finally;
653 Py_DECREF(v);
654 return 1;
655
656finally:
657 Py_XDECREF(v);
658 return 0;
659}
660
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000661void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000662PyErr_Print(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000663{
Guido van Rossuma61691e1998-02-06 22:27:24 +0000664 PyErr_PrintEx(1);
665}
666
667void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000668PyErr_PrintEx(int set_sys_last_vars)
Guido van Rossuma61691e1998-02-06 22:27:24 +0000669{
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000670 int err = 0;
Guido van Rossum82598051997-03-05 00:20:32 +0000671 PyObject *exception, *v, *tb, *f;
672 PyErr_Fetch(&exception, &v, &tb);
Barry Warsaw035574d1997-08-29 22:07:17 +0000673 PyErr_NormalizeException(&exception, &v, &tb);
674
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000675 if (exception == NULL)
Guido van Rossum296b4751997-05-23 00:19:20 +0000676 return;
Barry Warsaw035574d1997-08-29 22:07:17 +0000677
Barry Warsaw36b8f941997-08-26 18:09:48 +0000678 if (PyErr_GivenExceptionMatches(exception, PyExc_SystemExit)) {
Guido van Rossum0829c751998-02-28 04:31:39 +0000679 if (Py_FlushLine())
680 PyErr_Clear();
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000681 fflush(stdout);
Guido van Rossum82598051997-03-05 00:20:32 +0000682 if (v == NULL || v == Py_None)
683 Py_Exit(0);
Barry Warsaw035574d1997-08-29 22:07:17 +0000684 if (PyInstance_Check(v)) {
685 /* we expect the error code to be store in the
686 `code' attribute
687 */
688 PyObject *code = PyObject_GetAttrString(v, "code");
689 if (code) {
690 Py_DECREF(v);
691 v = code;
Guido van Rossumaa9606f1997-10-03 13:53:28 +0000692 if (v == Py_None)
693 Py_Exit(0);
Barry Warsaw035574d1997-08-29 22:07:17 +0000694 }
695 /* if we failed to dig out the "code" attribute,
696 then just let the else clause below print the
697 error
698 */
699 }
Guido van Rossum82598051997-03-05 00:20:32 +0000700 if (PyInt_Check(v))
701 Py_Exit((int)PyInt_AsLong(v));
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000702 else {
Guido van Rossum3165fe61992-09-25 21:59:05 +0000703 /* OK to use real stderr here */
Guido van Rossum82598051997-03-05 00:20:32 +0000704 PyObject_Print(v, stderr, Py_PRINT_RAW);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000705 fprintf(stderr, "\n");
Guido van Rossum82598051997-03-05 00:20:32 +0000706 Py_Exit(1);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000707 }
708 }
Guido van Rossuma61691e1998-02-06 22:27:24 +0000709 if (set_sys_last_vars) {
710 PySys_SetObject("last_type", exception);
711 PySys_SetObject("last_value", v);
712 PySys_SetObject("last_traceback", tb);
713 }
Guido van Rossum82598051997-03-05 00:20:32 +0000714 f = PySys_GetObject("stderr");
Guido van Rossum3165fe61992-09-25 21:59:05 +0000715 if (f == NULL)
716 fprintf(stderr, "lost sys.stderr\n");
717 else {
Guido van Rossum0829c751998-02-28 04:31:39 +0000718 if (Py_FlushLine())
719 PyErr_Clear();
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000720 fflush(stdout);
Guido van Rossum0829c751998-02-28 04:31:39 +0000721 err = PyTraceBack_Print(tb, f);
Barry Warsaw36b8f941997-08-26 18:09:48 +0000722 if (err == 0 &&
723 PyErr_GivenExceptionMatches(exception, PyExc_SyntaxError))
724 {
Guido van Rossum82598051997-03-05 00:20:32 +0000725 PyObject *message;
Guido van Rossuma110aa61994-08-29 12:50:44 +0000726 char *filename, *text;
727 int lineno, offset;
Barry Warsaw035574d1997-08-29 22:07:17 +0000728 if (!parse_syntax_error(v, &message, &filename,
729 &lineno, &offset, &text))
Guido van Rossum82598051997-03-05 00:20:32 +0000730 PyErr_Clear();
Guido van Rossuma110aa61994-08-29 12:50:44 +0000731 else {
732 char buf[10];
Guido van Rossum82598051997-03-05 00:20:32 +0000733 PyFile_WriteString(" File \"", f);
Guido van Rossuma110aa61994-08-29 12:50:44 +0000734 if (filename == NULL)
Guido van Rossum82598051997-03-05 00:20:32 +0000735 PyFile_WriteString("<string>", f);
Guido van Rossuma110aa61994-08-29 12:50:44 +0000736 else
Guido van Rossum82598051997-03-05 00:20:32 +0000737 PyFile_WriteString(filename, f);
738 PyFile_WriteString("\", line ", f);
Guido van Rossuma110aa61994-08-29 12:50:44 +0000739 sprintf(buf, "%d", lineno);
Guido van Rossum82598051997-03-05 00:20:32 +0000740 PyFile_WriteString(buf, f);
741 PyFile_WriteString("\n", f);
Guido van Rossuma110aa61994-08-29 12:50:44 +0000742 if (text != NULL) {
Guido van Rossum798199d1994-09-19 08:08:50 +0000743 char *nl;
744 if (offset > 0 &&
Guido van Rossum478e7181997-05-06 15:24:59 +0000745 offset == (int)strlen(text))
Guido van Rossum798199d1994-09-19 08:08:50 +0000746 offset--;
747 for (;;) {
748 nl = strchr(text, '\n');
749 if (nl == NULL ||
750 nl-text >= offset)
751 break;
752 offset -= (nl+1-text);
753 text = nl+1;
754 }
Guido van Rossuma110aa61994-08-29 12:50:44 +0000755 while (*text == ' ' || *text == '\t') {
756 text++;
757 offset--;
758 }
Guido van Rossum82598051997-03-05 00:20:32 +0000759 PyFile_WriteString(" ", f);
760 PyFile_WriteString(text, f);
Guido van Rossuma110aa61994-08-29 12:50:44 +0000761 if (*text == '\0' ||
762 text[strlen(text)-1] != '\n')
Guido van Rossum82598051997-03-05 00:20:32 +0000763 PyFile_WriteString("\n", f);
764 PyFile_WriteString(" ", f);
Guido van Rossuma110aa61994-08-29 12:50:44 +0000765 offset--;
766 while (offset > 0) {
Guido van Rossum82598051997-03-05 00:20:32 +0000767 PyFile_WriteString(" ", f);
Guido van Rossuma110aa61994-08-29 12:50:44 +0000768 offset--;
769 }
Guido van Rossum82598051997-03-05 00:20:32 +0000770 PyFile_WriteString("^\n", f);
Guido van Rossuma110aa61994-08-29 12:50:44 +0000771 }
Guido van Rossum82598051997-03-05 00:20:32 +0000772 Py_INCREF(message);
773 Py_DECREF(v);
Guido van Rossuma110aa61994-08-29 12:50:44 +0000774 v = message;
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000775 /* Can't be bothered to check all those
776 PyFile_WriteString() calls */
777 if (PyErr_Occurred())
778 err = -1;
Guido van Rossuma110aa61994-08-29 12:50:44 +0000779 }
780 }
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000781 if (err) {
782 /* Don't do anything else */
783 }
784 else if (PyClass_Check(exception)) {
Barry Warsaw2f5f6a21997-09-16 21:42:03 +0000785 PyClassObject* exc = (PyClassObject*)exception;
786 PyObject* className = exc->cl_name;
787 PyObject* moduleName =
788 PyDict_GetItemString(exc->cl_dict, "__module__");
789
790 if (moduleName == NULL)
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000791 err = PyFile_WriteString("<unknown>", f);
Barry Warsaw2f5f6a21997-09-16 21:42:03 +0000792 else {
793 char* modstr = PyString_AsString(moduleName);
794 if (modstr && strcmp(modstr, "exceptions"))
795 {
796 err = PyFile_WriteString(modstr, f);
797 err += PyFile_WriteString(".", f);
798 }
799 }
800 if (err == 0) {
801 if (className == NULL)
802 err = PyFile_WriteString("<unknown>", f);
803 else
804 err = PyFile_WriteObject(className, f,
805 Py_PRINT_RAW);
806 }
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000807 }
808 else
809 err = PyFile_WriteObject(exception, f, Py_PRINT_RAW);
810 if (err == 0) {
811 if (v != NULL && v != Py_None) {
Barry Warsaw035574d1997-08-29 22:07:17 +0000812 PyObject *s = PyObject_Str(v);
813 /* only print colon if the str() of the
814 object is not the empty string
815 */
Guido van Rossumd6bf45b1997-09-05 19:11:53 +0000816 if (s == NULL)
817 err = -1;
818 else if (!PyString_Check(s) ||
819 PyString_GET_SIZE(s) != 0)
Barry Warsaw035574d1997-08-29 22:07:17 +0000820 err = PyFile_WriteString(": ", f);
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000821 if (err == 0)
Guido van Rossumd6bf45b1997-09-05 19:11:53 +0000822 err = PyFile_WriteObject(s, f, Py_PRINT_RAW);
823 Py_XDECREF(s);
Guido van Rossum262e1241995-02-07 15:30:45 +0000824 }
Guido van Rossum262e1241995-02-07 15:30:45 +0000825 }
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000826 if (err == 0)
827 err = PyFile_WriteString("\n", f);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000828 }
Guido van Rossum82598051997-03-05 00:20:32 +0000829 Py_XDECREF(exception);
830 Py_XDECREF(v);
831 Py_XDECREF(tb);
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000832 /* If an error happened here, don't show it.
833 XXX This is wrong, but too many callers rely on this behavior. */
834 if (err != 0)
835 PyErr_Clear();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000836}
837
Guido van Rossum82598051997-03-05 00:20:32 +0000838PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000839PyRun_String(char *str, int start, PyObject *globals, PyObject *locals)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000840{
Guido van Rossum82598051997-03-05 00:20:32 +0000841 return run_err_node(PyParser_SimpleParseString(str, start),
Guido van Rossuma110aa61994-08-29 12:50:44 +0000842 "<string>", globals, locals);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000843}
844
Guido van Rossum82598051997-03-05 00:20:32 +0000845PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000846PyRun_File(FILE *fp, char *filename, int start, PyObject *globals,
847 PyObject *locals)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000848{
Guido van Rossum82598051997-03-05 00:20:32 +0000849 return run_err_node(PyParser_SimpleParseFile(fp, filename, start),
Guido van Rossuma110aa61994-08-29 12:50:44 +0000850 filename, globals, locals);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000851}
852
Guido van Rossum82598051997-03-05 00:20:32 +0000853static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000854run_err_node(node *n, char *filename, PyObject *globals, PyObject *locals)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000855{
Guido van Rossuma110aa61994-08-29 12:50:44 +0000856 if (n == NULL)
857 return NULL;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000858 return run_node(n, filename, globals, locals);
859}
860
Guido van Rossum82598051997-03-05 00:20:32 +0000861static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000862run_node(node *n, char *filename, PyObject *globals, PyObject *locals)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000863{
Guido van Rossum82598051997-03-05 00:20:32 +0000864 PyCodeObject *co;
865 PyObject *v;
866 co = PyNode_Compile(n, filename);
867 PyNode_Free(n);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000868 if (co == NULL)
869 return NULL;
Guido van Rossum82598051997-03-05 00:20:32 +0000870 v = PyEval_EvalCode(co, globals, locals);
871 Py_DECREF(co);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000872 return v;
873}
874
Guido van Rossum82598051997-03-05 00:20:32 +0000875static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000876run_pyc_file(FILE *fp, char *filename, PyObject *globals, PyObject *locals)
Guido van Rossumfdef2711994-09-14 13:31:04 +0000877{
Guido van Rossum82598051997-03-05 00:20:32 +0000878 PyCodeObject *co;
879 PyObject *v;
Guido van Rossumfdef2711994-09-14 13:31:04 +0000880 long magic;
Guido van Rossum82598051997-03-05 00:20:32 +0000881 long PyImport_GetMagicNumber();
Guido van Rossumfdef2711994-09-14 13:31:04 +0000882
Guido van Rossum82598051997-03-05 00:20:32 +0000883 magic = PyMarshal_ReadLongFromFile(fp);
884 if (magic != PyImport_GetMagicNumber()) {
885 PyErr_SetString(PyExc_RuntimeError,
Guido van Rossumfdef2711994-09-14 13:31:04 +0000886 "Bad magic number in .pyc file");
887 return NULL;
888 }
Guido van Rossum82598051997-03-05 00:20:32 +0000889 (void) PyMarshal_ReadLongFromFile(fp);
890 v = PyMarshal_ReadObjectFromFile(fp);
Guido van Rossumfdef2711994-09-14 13:31:04 +0000891 fclose(fp);
Guido van Rossum82598051997-03-05 00:20:32 +0000892 if (v == NULL || !PyCode_Check(v)) {
893 Py_XDECREF(v);
894 PyErr_SetString(PyExc_RuntimeError,
Guido van Rossumfdef2711994-09-14 13:31:04 +0000895 "Bad code object in .pyc file");
896 return NULL;
897 }
Guido van Rossum82598051997-03-05 00:20:32 +0000898 co = (PyCodeObject *)v;
899 v = PyEval_EvalCode(co, globals, locals);
900 Py_DECREF(co);
Guido van Rossumfdef2711994-09-14 13:31:04 +0000901 return v;
902}
903
Guido van Rossum82598051997-03-05 00:20:32 +0000904PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000905Py_CompileString(char *str, char *filename, int start)
Guido van Rossum5b722181993-03-30 17:46:03 +0000906{
907 node *n;
Guido van Rossum82598051997-03-05 00:20:32 +0000908 PyCodeObject *co;
909 n = PyParser_SimpleParseString(str, start);
Guido van Rossuma110aa61994-08-29 12:50:44 +0000910 if (n == NULL)
Guido van Rossum5b722181993-03-30 17:46:03 +0000911 return NULL;
Guido van Rossum82598051997-03-05 00:20:32 +0000912 co = PyNode_Compile(n, filename);
913 PyNode_Free(n);
914 return (PyObject *)co;
Guido van Rossum5b722181993-03-30 17:46:03 +0000915}
916
Guido van Rossuma110aa61994-08-29 12:50:44 +0000917/* Simplified interface to parsefile -- return node or set exception */
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000918
Guido van Rossuma110aa61994-08-29 12:50:44 +0000919node *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000920PyParser_SimpleParseFile(FILE *fp, char *filename, int start)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000921{
Guido van Rossuma110aa61994-08-29 12:50:44 +0000922 node *n;
923 perrdetail err;
Guido van Rossum82598051997-03-05 00:20:32 +0000924 n = PyParser_ParseFile(fp, filename, &_PyParser_Grammar, start,
Guido van Rossuma110aa61994-08-29 12:50:44 +0000925 (char *)0, (char *)0, &err);
Guido van Rossuma110aa61994-08-29 12:50:44 +0000926 if (n == NULL)
927 err_input(&err);
928 return n;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000929}
930
Guido van Rossuma110aa61994-08-29 12:50:44 +0000931/* Simplified interface to parsestring -- return node or set exception */
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000932
Guido van Rossuma110aa61994-08-29 12:50:44 +0000933node *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000934PyParser_SimpleParseString(char *str, int start)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000935{
Guido van Rossuma110aa61994-08-29 12:50:44 +0000936 node *n;
937 perrdetail err;
Guido van Rossum82598051997-03-05 00:20:32 +0000938 n = PyParser_ParseString(str, &_PyParser_Grammar, start, &err);
Guido van Rossuma110aa61994-08-29 12:50:44 +0000939 if (n == NULL)
940 err_input(&err);
941 return n;
942}
943
944/* Set the error appropriate to the given input error code (see errcode.h) */
945
946static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000947err_input(perrdetail *err)
Guido van Rossuma110aa61994-08-29 12:50:44 +0000948{
Fred Drake85f36392000-07-11 17:53:00 +0000949 PyObject *v, *w, *errtype;
Guido van Rossuma110aa61994-08-29 12:50:44 +0000950 char *msg = NULL;
Fred Drake85f36392000-07-11 17:53:00 +0000951 errtype = PyExc_SyntaxError;
Guido van Rossum82598051997-03-05 00:20:32 +0000952 v = Py_BuildValue("(ziiz)", err->filename,
Guido van Rossuma110aa61994-08-29 12:50:44 +0000953 err->lineno, err->offset, err->text);
954 if (err->text != NULL) {
Guido van Rossumb18618d2000-05-03 23:44:39 +0000955 PyMem_DEL(err->text);
Guido van Rossuma110aa61994-08-29 12:50:44 +0000956 err->text = NULL;
957 }
958 switch (err->error) {
959 case E_SYNTAX:
Fred Drake85f36392000-07-11 17:53:00 +0000960 errtype = PyExc_IndentationError;
961 if (err->expected == INDENT)
962 msg = "expected an indented block";
963 else if (err->token == INDENT)
964 msg = "unexpected indent";
965 else if (err->token == DEDENT)
966 msg = "unexpected unindent";
967 else {
968 errtype = PyExc_SyntaxError;
969 msg = "invalid syntax";
970 }
Guido van Rossuma110aa61994-08-29 12:50:44 +0000971 break;
972 case E_TOKEN:
973 msg = "invalid token";
Guido van Rossuma110aa61994-08-29 12:50:44 +0000974 break;
975 case E_INTR:
Guido van Rossum82598051997-03-05 00:20:32 +0000976 PyErr_SetNone(PyExc_KeyboardInterrupt);
Barry Warsawc80baa31999-01-27 16:39:40 +0000977 Py_XDECREF(v);
Guido van Rossuma110aa61994-08-29 12:50:44 +0000978 return;
979 case E_NOMEM:
Guido van Rossum82598051997-03-05 00:20:32 +0000980 PyErr_NoMemory();
Barry Warsawc80baa31999-01-27 16:39:40 +0000981 Py_XDECREF(v);
Guido van Rossuma110aa61994-08-29 12:50:44 +0000982 return;
983 case E_EOF:
984 msg = "unexpected EOF while parsing";
985 break;
Fred Drake85f36392000-07-11 17:53:00 +0000986 case E_TABSPACE:
987 errtype = PyExc_TabError;
Guido van Rossum560e8ad1998-04-10 19:43:42 +0000988 msg = "inconsistent use of tabs and spaces in indentation";
989 break;
Jeremy Hylton94988062000-06-20 19:10:44 +0000990 case E_OVERFLOW:
991 msg = "expression too long";
992 break;
Fred Drake85f36392000-07-11 17:53:00 +0000993 case E_DEDENT:
994 errtype = PyExc_IndentationError;
995 msg = "unindent does not match any outer indentation level";
996 break;
997 case E_TOODEEP:
998 errtype = PyExc_IndentationError;
999 msg = "too many levels of indentation";
1000 break;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001001 default:
1002 fprintf(stderr, "error=%d\n", err->error);
1003 msg = "unknown parsing error";
1004 break;
1005 }
Guido van Rossum82598051997-03-05 00:20:32 +00001006 w = Py_BuildValue("(sO)", msg, v);
Fred Drake85f36392000-07-11 17:53:00 +00001007 PyErr_SetObject(errtype, w);
Guido van Rossum82598051997-03-05 00:20:32 +00001008 Py_XDECREF(w);
Fred Drake83cb7972000-08-15 15:49:03 +00001009
1010 if (v != NULL) {
1011 PyObject *exc, *tb;
1012
1013 PyErr_Fetch(&errtype, &exc, &tb);
1014 PyErr_NormalizeException(&errtype, &exc, &tb);
1015 if (PyObject_SetAttrString(exc, "filename",
1016 PyTuple_GET_ITEM(v, 0)))
1017 PyErr_Clear();
1018 if (PyObject_SetAttrString(exc, "lineno",
1019 PyTuple_GET_ITEM(v, 1)))
1020 PyErr_Clear();
1021 if (PyObject_SetAttrString(exc, "offset",
1022 PyTuple_GET_ITEM(v, 2)))
1023 PyErr_Clear();
1024 Py_DECREF(v);
1025 PyErr_Restore(errtype, exc, tb);
1026 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001027}
1028
1029/* Print fatal error message and abort */
1030
1031void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001032Py_FatalError(char *msg)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001033{
Guido van Rossum83dd6c31994-09-29 09:38:33 +00001034 fprintf(stderr, "Fatal Python error: %s\n", msg);
Guido van Rossum8ae87c01995-01-26 00:40:38 +00001035#ifdef macintosh
1036 for (;;);
1037#endif
Guido van Rossum9b38a141996-09-11 23:12:24 +00001038#ifdef MS_WIN32
Guido van Rossum23c94461997-05-22 20:21:30 +00001039 OutputDebugString("Fatal Python error: ");
Guido van Rossuma44823b1995-03-14 15:01:17 +00001040 OutputDebugString(msg);
1041 OutputDebugString("\n");
Guido van Rossum0ba35361998-08-13 13:33:16 +00001042#ifdef _DEBUG
1043 DebugBreak();
Guido van Rossuma44823b1995-03-14 15:01:17 +00001044#endif
Guido van Rossum0ba35361998-08-13 13:33:16 +00001045#endif /* MS_WIN32 */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001046 abort();
1047}
1048
1049/* Clean up and exit */
1050
Guido van Rossuma110aa61994-08-29 12:50:44 +00001051#ifdef WITH_THREAD
Guido van Rossum49b56061998-10-01 20:42:43 +00001052#include "pythread.h"
Guido van Rossum82598051997-03-05 00:20:32 +00001053int _PyThread_Started = 0; /* Set by threadmodule.c and maybe others */
Guido van Rossumf9f2e821992-08-17 08:59:08 +00001054#endif
1055
Guido van Rossum2dcfc961998-10-01 16:01:57 +00001056#define NEXITFUNCS 32
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001057static void (*exitfuncs[NEXITFUNCS])(void);
Guido van Rossum1662dd51994-09-07 14:38:28 +00001058static int nexitfuncs = 0;
1059
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001060int Py_AtExit(void (*func)(void))
Guido van Rossum1662dd51994-09-07 14:38:28 +00001061{
1062 if (nexitfuncs >= NEXITFUNCS)
1063 return -1;
1064 exitfuncs[nexitfuncs++] = func;
1065 return 0;
1066}
1067
Guido van Rossumcc283f51997-08-05 02:22:03 +00001068static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001069call_sys_exitfunc(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001070{
Guido van Rossum82598051997-03-05 00:20:32 +00001071 PyObject *exitfunc = PySys_GetObject("exitfunc");
Guido van Rossum59bff391992-09-03 20:28:00 +00001072
1073 if (exitfunc) {
Guido van Rossumbf02fb21998-04-03 21:12:12 +00001074 PyObject *res, *f;
Guido van Rossum82598051997-03-05 00:20:32 +00001075 Py_INCREF(exitfunc);
1076 PySys_SetObject("exitfunc", (PyObject *)NULL);
Guido van Rossumbf02fb21998-04-03 21:12:12 +00001077 f = PySys_GetObject("stderr");
Guido van Rossum82598051997-03-05 00:20:32 +00001078 res = PyEval_CallObject(exitfunc, (PyObject *)NULL);
Guido van Rossum59bff391992-09-03 20:28:00 +00001079 if (res == NULL) {
Guido van Rossumbf02fb21998-04-03 21:12:12 +00001080 if (f)
1081 PyFile_WriteString("Error in sys.exitfunc:\n", f);
Guido van Rossum82598051997-03-05 00:20:32 +00001082 PyErr_Print();
Guido van Rossum59bff391992-09-03 20:28:00 +00001083 }
Guido van Rossum82598051997-03-05 00:20:32 +00001084 Py_DECREF(exitfunc);
Guido van Rossum59bff391992-09-03 20:28:00 +00001085 }
1086
Guido van Rossum0829c751998-02-28 04:31:39 +00001087 if (Py_FlushLine())
1088 PyErr_Clear();
Guido van Rossumcc283f51997-08-05 02:22:03 +00001089}
Guido van Rossum1662dd51994-09-07 14:38:28 +00001090
Guido van Rossumcc283f51997-08-05 02:22:03 +00001091static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001092call_ll_exitfuncs(void)
Guido van Rossumcc283f51997-08-05 02:22:03 +00001093{
Guido van Rossum1662dd51994-09-07 14:38:28 +00001094 while (nexitfuncs > 0)
1095 (*exitfuncs[--nexitfuncs])();
Guido van Rossum25ce5661997-08-02 03:10:38 +00001096
1097 fflush(stdout);
1098 fflush(stderr);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001099}
1100
1101void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001102Py_Exit(int sts)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001103{
Guido van Rossumcc283f51997-08-05 02:22:03 +00001104 Py_Finalize();
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001105
Jack Jansen66a89771995-10-27 13:22:14 +00001106#ifdef macintosh
1107 PyMac_Exit(sts);
1108#else
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001109 exit(sts);
Jack Jansen66a89771995-10-27 13:22:14 +00001110#endif
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001111}
1112
Guido van Rossumf1dc5661993-07-05 10:31:29 +00001113static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001114initsigs(void)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001115{
Guido van Rossuma110aa61994-08-29 12:50:44 +00001116#ifdef HAVE_SIGNAL_H
1117#ifdef SIGPIPE
1118 signal(SIGPIPE, SIG_IGN);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001119#endif
Guido van Rossuma110aa61994-08-29 12:50:44 +00001120#endif /* HAVE_SIGNAL_H */
Guido van Rossum82598051997-03-05 00:20:32 +00001121 PyOS_InitInterrupts(); /* May imply initsignal() */
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001122}
1123
Guido van Rossumaae0d321996-05-22 16:35:33 +00001124#ifdef Py_TRACE_REFS
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001125/* Ask a yes/no question */
1126
Guido van Rossum59bff391992-09-03 20:28:00 +00001127int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001128_Py_AskYesNo(char *prompt)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001129{
1130 char buf[256];
1131
1132 printf("%s [ny] ", prompt);
1133 if (fgets(buf, sizeof buf, stdin) == NULL)
1134 return 0;
1135 return buf[0] == 'y' || buf[0] == 'Y';
1136}
1137#endif
1138
Guido van Rossuma110aa61994-08-29 12:50:44 +00001139#ifdef MPW
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001140
1141/* Check for file descriptor connected to interactive device.
1142 Pretend that stdin is always interactive, other files never. */
1143
1144int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001145isatty(int fd)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001146{
1147 return fd == fileno(stdin);
1148}
1149
1150#endif
Guido van Rossum7433b121997-02-14 19:45:36 +00001151
1152/*
1153 * The file descriptor fd is considered ``interactive'' if either
1154 * a) isatty(fd) is TRUE, or
1155 * b) the -i flag was given, and the filename associated with
1156 * the descriptor is NULL or "<stdin>" or "???".
1157 */
1158int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001159Py_FdIsInteractive(FILE *fp, char *filename)
Guido van Rossum7433b121997-02-14 19:45:36 +00001160{
1161 if (isatty((int)fileno(fp)))
1162 return 1;
1163 if (!Py_InteractiveFlag)
1164 return 0;
1165 return (filename == NULL) ||
1166 (strcmp(filename, "<stdin>") == 0) ||
1167 (strcmp(filename, "???") == 0);
1168}