blob: fba3ade8003a03396fe1a9fc81fb65ff0cca8e71 [file] [log] [blame]
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001
2/* Python interpreter top-level routines, including init/exit */
3
Guido van Rossum82598051997-03-05 00:20:32 +00004#include "Python.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +00005
6#include "grammar.h"
7#include "node.h"
Fred Drake85f36392000-07-11 17:53:00 +00008#include "token.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +00009#include "parsetok.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +000010#include "errcode.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +000011#include "compile.h"
Jeremy Hylton4b38da62001-02-02 18:19:15 +000012#include "symtable.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +000013#include "eval.h"
Guido van Rossumfdef2711994-09-14 13:31:04 +000014#include "marshal.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +000015
Guido van Rossuma110aa61994-08-29 12:50:44 +000016#ifdef HAVE_SIGNAL_H
Guido van Rossuma9e7dc11992-10-18 18:53:57 +000017#include <signal.h>
Guido van Rossuma9e7dc11992-10-18 18:53:57 +000018#endif
19
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000020#ifdef MS_WINDOWS
Guido van Rossuma44823b1995-03-14 15:01:17 +000021#undef BYTE
22#include "windows.h"
23#endif
24
Jack Jansencbf630f2000-07-11 21:59:16 +000025#ifdef macintosh
26#include "macglue.h"
27#endif
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000028extern char *Py_GetPath(void);
Guido van Rossum1984f1e1992-08-04 12:41:02 +000029
Guido van Rossum82598051997-03-05 00:20:32 +000030extern grammar _PyParser_Grammar; /* From graminit.c */
Guido van Rossum1984f1e1992-08-04 12:41:02 +000031
Guido van Rossumb73cc041993-11-01 16:28:59 +000032/* Forward */
Tim Petersdbd9ba62000-07-09 03:09:57 +000033static void initmain(void);
34static void initsite(void);
Martin v. Löwis95292d62002-12-11 14:04:59 +000035static PyObject *run_err_node(node *, const char *, PyObject *, PyObject *,
Jeremy Hylton9f324e92001-03-01 22:59:14 +000036 PyCompilerFlags *);
Martin v. Löwis95292d62002-12-11 14:04:59 +000037static PyObject *run_node(node *, const char *, PyObject *, PyObject *,
Jeremy Hylton9f324e92001-03-01 22:59:14 +000038 PyCompilerFlags *);
Martin v. Löwis95292d62002-12-11 14:04:59 +000039static PyObject *run_pyc_file(FILE *, const char *, PyObject *, PyObject *,
Jeremy Hyltonbc320242001-03-22 02:47:58 +000040 PyCompilerFlags *);
Tim Petersdbd9ba62000-07-09 03:09:57 +000041static void err_input(perrdetail *);
42static void initsigs(void);
43static void call_sys_exitfunc(void);
44static void call_ll_exitfuncs(void);
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000045extern void _PyUnicode_Init(void);
46extern void _PyUnicode_Fini(void);
47extern void _PyCodecRegistry_Init(void);
48extern void _PyCodecRegistry_Fini(void);
Guido van Rossumc94044c2000-03-10 23:03:54 +000049
Guido van Rossum82598051997-03-05 00:20:32 +000050int Py_DebugFlag; /* Needed by parser.c */
51int Py_VerboseFlag; /* Needed by import.c */
Guido van Rossum7433b121997-02-14 19:45:36 +000052int Py_InteractiveFlag; /* Needed by Py_FdIsInteractive() below */
Guido van Rossumdcc0c131997-08-29 22:32:42 +000053int Py_NoSiteFlag; /* Suppress 'import site' */
Barry Warsaw3ce09642000-05-02 19:18:59 +000054int Py_UseClassExceptionsFlag = 1; /* Needed by bltinmodule.c: deprecated */
Guido van Rossuma61691e1998-02-06 22:27:24 +000055int Py_FrozenFlag; /* Needed by getpath.c */
Guido van Rossumb16d1972000-05-01 17:55:15 +000056int Py_UnicodeFlag = 0; /* Needed by compile.c */
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +000057int Py_IgnoreEnvironmentFlag; /* e.g. PYTHONPATH, PYTHONHOME */
Tim Peters3caca232001-12-06 06:23:26 +000058/* _XXX Py_QnewFlag should go away in 2.3. It's true iff -Qnew is passed,
59 on the command line, and is used in 2.2 by ceval.c to make all "/" divisions
60 true divisions (which they will be in 2.3). */
61int _Py_QnewFlag = 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +000062
Guido van Rossum25ce5661997-08-02 03:10:38 +000063static int initialized = 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +000064
Thomas Wouters7e474022000-07-16 12:04:32 +000065/* API to access the initialized flag -- useful for esoteric use */
Guido van Rossume3c0d5e1997-08-22 04:20:13 +000066
67int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000068Py_IsInitialized(void)
Guido van Rossume3c0d5e1997-08-22 04:20:13 +000069{
70 return initialized;
71}
72
Guido van Rossum25ce5661997-08-02 03:10:38 +000073/* Global initializations. Can be undone by Py_Finalize(). Don't
74 call this twice without an intervening Py_Finalize() call. When
75 initializations fail, a fatal error is issued and the function does
76 not return. On return, the first thread and interpreter state have
77 been created.
Guido van Rossum1984f1e1992-08-04 12:41:02 +000078
Guido van Rossum25ce5661997-08-02 03:10:38 +000079 Locking: you must hold the interpreter lock while calling this.
80 (If the lock has not yet been initialized, that's equivalent to
81 having the lock, but you cannot use multiple threads.)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +000082
Guido van Rossum25ce5661997-08-02 03:10:38 +000083*/
Guido van Rossuma027efa1997-05-05 20:56:21 +000084
Guido van Rossum9abaf4d2001-10-12 22:17:56 +000085static int
86add_flag(int flag, const char *envs)
87{
88 int env = atoi(envs);
89 if (flag < env)
90 flag = env;
91 if (flag < 1)
92 flag = 1;
93 return flag;
94}
95
Guido van Rossuma027efa1997-05-05 20:56:21 +000096void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000097Py_Initialize(void)
Guido van Rossuma027efa1997-05-05 20:56:21 +000098{
Guido van Rossuma027efa1997-05-05 20:56:21 +000099 PyInterpreterState *interp;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000100 PyThreadState *tstate;
101 PyObject *bimod, *sysmod;
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000102 char *p;
Guido van Rossum70d893a2001-08-16 08:21:42 +0000103 extern void _Py_ReadyTypes(void);
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000104
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000105 if (initialized)
Guido van Rossumaa615051997-08-20 22:40:18 +0000106 return;
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000107 initialized = 1;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000108
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000109 if ((p = Py_GETENV("PYTHONDEBUG")) && *p != '\0')
Guido van Rossum9abaf4d2001-10-12 22:17:56 +0000110 Py_DebugFlag = add_flag(Py_DebugFlag, p);
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000111 if ((p = Py_GETENV("PYTHONVERBOSE")) && *p != '\0')
Guido van Rossum9abaf4d2001-10-12 22:17:56 +0000112 Py_VerboseFlag = add_flag(Py_VerboseFlag, p);
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000113 if ((p = Py_GETENV("PYTHONOPTIMIZE")) && *p != '\0')
Guido van Rossum9abaf4d2001-10-12 22:17:56 +0000114 Py_OptimizeFlag = add_flag(Py_OptimizeFlag, p);
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000115
Guido van Rossuma027efa1997-05-05 20:56:21 +0000116 interp = PyInterpreterState_New();
117 if (interp == NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000118 Py_FatalError("Py_Initialize: can't make first interpreter");
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000119
Guido van Rossuma027efa1997-05-05 20:56:21 +0000120 tstate = PyThreadState_New(interp);
121 if (tstate == NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000122 Py_FatalError("Py_Initialize: can't make first thread");
Guido van Rossuma027efa1997-05-05 20:56:21 +0000123 (void) PyThreadState_Swap(tstate);
124
Guido van Rossum70d893a2001-08-16 08:21:42 +0000125 _Py_ReadyTypes();
Guido van Rossum528b7eb2001-08-07 17:24:28 +0000126
Neal Norwitzb2501f42002-12-31 03:42:13 +0000127 if (!_PyFrame_Init())
Neal Norwitzc91ed402002-12-30 22:29:22 +0000128 Py_FatalError("Py_Initialize: can't init frames");
129
Neal Norwitzb2501f42002-12-31 03:42:13 +0000130 if (!_PyInt_Init())
Neal Norwitzc91ed402002-12-30 22:29:22 +0000131 Py_FatalError("Py_Initialize: can't init ints");
132
Guido van Rossum25ce5661997-08-02 03:10:38 +0000133 interp->modules = PyDict_New();
134 if (interp->modules == NULL)
135 Py_FatalError("Py_Initialize: can't make modules dictionary");
Guido van Rossuma027efa1997-05-05 20:56:21 +0000136
Guido van Rossumc94044c2000-03-10 23:03:54 +0000137 /* Init codec registry */
138 _PyCodecRegistry_Init();
139
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000140#ifdef Py_USING_UNICODE
Guido van Rossumc94044c2000-03-10 23:03:54 +0000141 /* Init Unicode implementation; relies on the codec registry */
142 _PyUnicode_Init();
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000143#endif
Guido van Rossumc94044c2000-03-10 23:03:54 +0000144
Barry Warsawf242aa02000-05-25 23:09:49 +0000145 bimod = _PyBuiltin_Init();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000146 if (bimod == NULL)
147 Py_FatalError("Py_Initialize: can't initialize __builtin__");
Guido van Rossum4a1f39a1997-11-04 19:36:18 +0000148 interp->builtins = PyModule_GetDict(bimod);
149 Py_INCREF(interp->builtins);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000150
151 sysmod = _PySys_Init();
152 if (sysmod == NULL)
153 Py_FatalError("Py_Initialize: can't initialize sys");
154 interp->sysdict = PyModule_GetDict(sysmod);
155 Py_INCREF(interp->sysdict);
156 _PyImport_FixupExtension("sys", "sys");
Guido van Rossuma027efa1997-05-05 20:56:21 +0000157 PySys_SetPath(Py_GetPath());
Guido van Rossum25ce5661997-08-02 03:10:38 +0000158 PyDict_SetItemString(interp->sysdict, "modules",
159 interp->modules);
160
Guido van Rossum7c85ab81999-07-08 17:26:56 +0000161 _PyImport_Init();
162
Barry Warsawf242aa02000-05-25 23:09:49 +0000163 /* initialize builtin exceptions */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000164 _PyExc_Init();
Barry Warsaw5821bc52001-08-13 23:04:56 +0000165 _PyImport_FixupExtension("exceptions", "exceptions");
Barry Warsawf242aa02000-05-25 23:09:49 +0000166
Barry Warsaw035574d1997-08-29 22:07:17 +0000167 /* phase 2 of builtins */
Barry Warsaw963b8711997-09-18 16:42:02 +0000168 _PyImport_FixupExtension("__builtin__", "__builtin__");
Barry Warsaw035574d1997-08-29 22:07:17 +0000169
Just van Rossum52e14d62002-12-30 22:08:05 +0000170 _PyImportHooks_Init();
171
Guido van Rossum25ce5661997-08-02 03:10:38 +0000172 initsigs(); /* Signal handling stuff, including initintr() */
173
174 initmain(); /* Module __main__ */
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000175 if (!Py_NoSiteFlag)
176 initsite(); /* Module site */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000177}
178
Guido van Rossum2edcf0d1998-12-15 16:12:00 +0000179#ifdef COUNT_ALLOCS
Tim Petersdbd9ba62000-07-09 03:09:57 +0000180extern void dump_counts(void);
Guido van Rossum2edcf0d1998-12-15 16:12:00 +0000181#endif
182
Guido van Rossum25ce5661997-08-02 03:10:38 +0000183/* Undo the effect of Py_Initialize().
184
185 Beware: if multiple interpreter and/or thread states exist, these
186 are not wiped out; only the current thread and interpreter state
187 are deleted. But since everything else is deleted, those other
188 interpreter and thread states should no longer be used.
189
190 (XXX We should do better, e.g. wipe out all interpreters and
191 threads.)
192
193 Locking: as above.
194
195*/
196
197void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000198Py_Finalize(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000199{
200 PyInterpreterState *interp;
201 PyThreadState *tstate;
202
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000203 if (!initialized)
Guido van Rossumaa615051997-08-20 22:40:18 +0000204 return;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000205
Tim Peters384fd102001-01-21 03:40:37 +0000206 /* The interpreter is still entirely intact at this point, and the
207 * exit funcs may be relying on that. In particular, if some thread
208 * or exit func is still waiting to do an import, the import machinery
209 * expects Py_IsInitialized() to return true. So don't say the
210 * interpreter is uninitialized until after the exit funcs have run.
211 * Note that Threading.py uses an exit func to do a join on all the
212 * threads created thru it, so this also protects pending imports in
213 * the threads created via Threading.
214 */
Guido van Rossum4cc462e1998-01-19 22:00:38 +0000215 call_sys_exitfunc();
Tim Peters384fd102001-01-21 03:40:37 +0000216 initialized = 0;
Guido van Rossum4cc462e1998-01-19 22:00:38 +0000217
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000218 /* Get current thread state and interpreter pointer */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000219 tstate = PyThreadState_Get();
220 interp = tstate->interp;
221
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000222 /* Disable signal handling */
223 PyOS_FiniInterrupts();
224
Guido van Rossumc94044c2000-03-10 23:03:54 +0000225 /* Cleanup Codec registry */
226 _PyCodecRegistry_Fini();
227
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000228 /* Destroy all modules */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000229 PyImport_Cleanup();
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000230
Guido van Rossum1707aad1997-12-08 23:43:45 +0000231 /* Destroy the database used by _PyImport_{Fixup,Find}Extension */
232 _PyImport_Fini();
233
234 /* Debugging stuff */
235#ifdef COUNT_ALLOCS
236 dump_counts();
237#endif
238
239#ifdef Py_REF_DEBUG
240 fprintf(stderr, "[%ld refs]\n", _Py_RefTotal);
241#endif
242
243#ifdef Py_TRACE_REFS
Guido van Rossum92e2d5c2001-08-09 16:37:16 +0000244 if (Py_GETENV("PYTHONDUMPREFS")) {
Guido van Rossum1707aad1997-12-08 23:43:45 +0000245 _Py_PrintReferences(stderr);
246 }
247#endif /* Py_TRACE_REFS */
248
Barry Warsaw035574d1997-08-29 22:07:17 +0000249 /* Now we decref the exception classes. After this point nothing
250 can raise an exception. That's okay, because each Fini() method
251 below has been checked to make sure no exceptions are ever
252 raised.
253 */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000254 _PyExc_Fini();
Barry Warsawf242aa02000-05-25 23:09:49 +0000255
256 /* Delete current thread */
257 PyInterpreterState_Clear(interp);
258 PyThreadState_Swap(NULL);
259 PyInterpreterState_Delete(interp);
260
Guido van Rossumcc283f51997-08-05 02:22:03 +0000261 PyMethod_Fini();
262 PyFrame_Fini();
263 PyCFunction_Fini();
264 PyTuple_Fini();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000265 PyString_Fini();
Guido van Rossumcc283f51997-08-05 02:22:03 +0000266 PyInt_Fini();
267 PyFloat_Fini();
268
Marc-André Lemburg95de5c12002-04-08 08:19:36 +0000269#ifdef Py_USING_UNICODE
270 /* Cleanup Unicode implementation */
271 _PyUnicode_Fini();
272#endif
273
Guido van Rossumcc283f51997-08-05 02:22:03 +0000274 /* XXX Still allocated:
275 - various static ad-hoc pointers to interned strings
276 - int and float free list blocks
277 - whatever various modules and libraries allocate
278 */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000279
280 PyGrammar_RemoveAccelerators(&_PyParser_Grammar);
Guido van Rossumcc283f51997-08-05 02:22:03 +0000281
Tim Peters0e871182002-04-13 08:29:14 +0000282#ifdef PYMALLOC_DEBUG
283 if (Py_GETENV("PYTHONMALLOCSTATS"))
284 _PyObject_DebugMallocStats();
285#endif
286
Guido van Rossumcc283f51997-08-05 02:22:03 +0000287 call_ll_exitfuncs();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000288}
289
290/* Create and initialize a new interpreter and thread, and return the
291 new thread. This requires that Py_Initialize() has been called
292 first.
293
294 Unsuccessful initialization yields a NULL pointer. Note that *no*
295 exception information is available even in this case -- the
296 exception information is held in the thread, and there is no
297 thread.
298
299 Locking: as above.
300
301*/
302
303PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000304Py_NewInterpreter(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000305{
306 PyInterpreterState *interp;
307 PyThreadState *tstate, *save_tstate;
308 PyObject *bimod, *sysmod;
309
310 if (!initialized)
311 Py_FatalError("Py_NewInterpreter: call Py_Initialize first");
312
313 interp = PyInterpreterState_New();
314 if (interp == NULL)
315 return NULL;
316
317 tstate = PyThreadState_New(interp);
318 if (tstate == NULL) {
319 PyInterpreterState_Delete(interp);
320 return NULL;
321 }
322
323 save_tstate = PyThreadState_Swap(tstate);
324
325 /* XXX The following is lax in error checking */
326
327 interp->modules = PyDict_New();
328
329 bimod = _PyImport_FindExtension("__builtin__", "__builtin__");
330 if (bimod != NULL) {
Guido van Rossum4a1f39a1997-11-04 19:36:18 +0000331 interp->builtins = PyModule_GetDict(bimod);
332 Py_INCREF(interp->builtins);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000333 }
334 sysmod = _PyImport_FindExtension("sys", "sys");
335 if (bimod != NULL && sysmod != NULL) {
336 interp->sysdict = PyModule_GetDict(sysmod);
337 Py_INCREF(interp->sysdict);
338 PySys_SetPath(Py_GetPath());
339 PyDict_SetItemString(interp->sysdict, "modules",
340 interp->modules);
341 initmain();
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000342 if (!Py_NoSiteFlag)
343 initsite();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000344 }
345
346 if (!PyErr_Occurred())
347 return tstate;
348
349 /* Oops, it didn't work. Undo it all. */
350
351 PyErr_Print();
352 PyThreadState_Clear(tstate);
353 PyThreadState_Swap(save_tstate);
354 PyThreadState_Delete(tstate);
355 PyInterpreterState_Delete(interp);
356
357 return NULL;
358}
359
360/* Delete an interpreter and its last thread. This requires that the
361 given thread state is current, that the thread has no remaining
362 frames, and that it is its interpreter's only remaining thread.
363 It is a fatal error to violate these constraints.
364
365 (Py_Finalize() doesn't have these constraints -- it zaps
366 everything, regardless.)
367
368 Locking: as above.
369
370*/
371
372void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000373Py_EndInterpreter(PyThreadState *tstate)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000374{
375 PyInterpreterState *interp = tstate->interp;
376
377 if (tstate != PyThreadState_Get())
378 Py_FatalError("Py_EndInterpreter: thread is not current");
379 if (tstate->frame != NULL)
380 Py_FatalError("Py_EndInterpreter: thread still has a frame");
381 if (tstate != interp->tstate_head || tstate->next != NULL)
382 Py_FatalError("Py_EndInterpreter: not the last thread");
383
384 PyImport_Cleanup();
385 PyInterpreterState_Clear(interp);
386 PyThreadState_Swap(NULL);
387 PyInterpreterState_Delete(interp);
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000388}
389
390static char *progname = "python";
391
392void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000393Py_SetProgramName(char *pn)
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000394{
395 if (pn && *pn)
396 progname = pn;
397}
398
399char *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000400Py_GetProgramName(void)
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000401{
402 return progname;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000403}
404
Guido van Rossuma61691e1998-02-06 22:27:24 +0000405static char *default_home = NULL;
406
407void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000408Py_SetPythonHome(char *home)
Guido van Rossuma61691e1998-02-06 22:27:24 +0000409{
410 default_home = home;
411}
412
413char *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000414Py_GetPythonHome(void)
Guido van Rossuma61691e1998-02-06 22:27:24 +0000415{
416 char *home = default_home;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000417 if (home == NULL && !Py_IgnoreEnvironmentFlag)
418 home = Py_GETENV("PYTHONHOME");
Guido van Rossuma61691e1998-02-06 22:27:24 +0000419 return home;
420}
421
Guido van Rossum6135a871995-01-09 17:53:26 +0000422/* Create __main__ module */
423
424static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000425initmain(void)
Guido van Rossum6135a871995-01-09 17:53:26 +0000426{
Guido van Rossum82598051997-03-05 00:20:32 +0000427 PyObject *m, *d;
428 m = PyImport_AddModule("__main__");
Guido van Rossum6135a871995-01-09 17:53:26 +0000429 if (m == NULL)
Guido van Rossum82598051997-03-05 00:20:32 +0000430 Py_FatalError("can't create __main__ module");
431 d = PyModule_GetDict(m);
432 if (PyDict_GetItemString(d, "__builtins__") == NULL) {
Guido van Rossum858cb731997-11-19 16:15:37 +0000433 PyObject *bimod = PyImport_ImportModule("__builtin__");
434 if (bimod == NULL ||
435 PyDict_SetItemString(d, "__builtins__", bimod) != 0)
Guido van Rossum82598051997-03-05 00:20:32 +0000436 Py_FatalError("can't add __builtins__ to __main__");
Barry Warsaw3d05b1a1999-01-29 21:30:22 +0000437 Py_DECREF(bimod);
Guido van Rossum6135a871995-01-09 17:53:26 +0000438 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000439}
440
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000441/* Import the site module (not into __main__ though) */
442
443static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000444initsite(void)
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000445{
446 PyObject *m, *f;
447 m = PyImport_ImportModule("site");
448 if (m == NULL) {
449 f = PySys_GetObject("stderr");
450 if (Py_VerboseFlag) {
451 PyFile_WriteString(
452 "'import site' failed; traceback:\n", f);
453 PyErr_Print();
454 }
455 else {
456 PyFile_WriteString(
457 "'import site' failed; use -v for traceback\n", f);
458 PyErr_Clear();
459 }
460 }
461 else {
462 Py_DECREF(m);
463 }
464}
465
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000466/* Parse input from a file and execute it */
467
468int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000469PyRun_AnyFile(FILE *fp, const char *filename)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000470{
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000471 return PyRun_AnyFileExFlags(fp, filename, 0, NULL);
472}
473
474int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000475PyRun_AnyFileFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000476{
477 return PyRun_AnyFileExFlags(fp, filename, 0, flags);
Guido van Rossum0df002c2000-08-27 19:21:52 +0000478}
479
480int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000481PyRun_AnyFileEx(FILE *fp, const char *filename, int closeit)
Guido van Rossum0df002c2000-08-27 19:21:52 +0000482{
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000483 return PyRun_AnyFileExFlags(fp, filename, closeit, NULL);
484}
485
486int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000487PyRun_AnyFileExFlags(FILE *fp, const char *filename, int closeit,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000488 PyCompilerFlags *flags)
489{
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000490 if (filename == NULL)
491 filename = "???";
Guido van Rossum0df002c2000-08-27 19:21:52 +0000492 if (Py_FdIsInteractive(fp, filename)) {
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000493 int err = PyRun_InteractiveLoopFlags(fp, filename, flags);
Guido van Rossum0df002c2000-08-27 19:21:52 +0000494 if (closeit)
495 fclose(fp);
496 return err;
497 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000498 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000499 return PyRun_SimpleFileExFlags(fp, filename, closeit, flags);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000500}
501
502int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000503PyRun_InteractiveLoop(FILE *fp, const char *filename)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000504{
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000505 return PyRun_InteractiveLoopFlags(fp, filename, NULL);
506}
507
508int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000509PyRun_InteractiveLoopFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000510{
Guido van Rossum82598051997-03-05 00:20:32 +0000511 PyObject *v;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000512 int ret;
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000513 PyCompilerFlags local_flags;
Jeremy Hylton9f324e92001-03-01 22:59:14 +0000514
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000515 if (flags == NULL) {
516 flags = &local_flags;
Tim Peters5ba58662001-07-16 02:29:45 +0000517 local_flags.cf_flags = 0;
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000518 }
Guido van Rossum82598051997-03-05 00:20:32 +0000519 v = PySys_GetObject("ps1");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000520 if (v == NULL) {
Guido van Rossum82598051997-03-05 00:20:32 +0000521 PySys_SetObject("ps1", v = PyString_FromString(">>> "));
522 Py_XDECREF(v);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000523 }
Guido van Rossum82598051997-03-05 00:20:32 +0000524 v = PySys_GetObject("ps2");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000525 if (v == NULL) {
Guido van Rossum82598051997-03-05 00:20:32 +0000526 PySys_SetObject("ps2", v = PyString_FromString("... "));
527 Py_XDECREF(v);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000528 }
529 for (;;) {
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000530 ret = PyRun_InteractiveOneFlags(fp, filename, flags);
Guido van Rossumaae0d321996-05-22 16:35:33 +0000531#ifdef Py_REF_DEBUG
Guido van Rossum6f9e4331995-03-29 16:57:48 +0000532 fprintf(stderr, "[%ld refs]\n", _Py_RefTotal);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000533#endif
534 if (ret == E_EOF)
535 return 0;
536 /*
537 if (ret == E_NOMEM)
538 return -1;
539 */
540 }
541}
542
543int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000544PyRun_InteractiveOne(FILE *fp, const char *filename)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000545{
Jeremy Hylton9f324e92001-03-01 22:59:14 +0000546 return PyRun_InteractiveOneFlags(fp, filename, NULL);
547}
548
Neil Schemenauerc24ea082002-03-22 23:53:36 +0000549/* compute parser flags based on compiler flags */
550#if 0 /* future keyword */
551#define PARSER_FLAGS(flags) \
552 (((flags) && (flags)->cf_flags & CO_GENERATOR_ALLOWED) ? \
553 PyPARSE_YIELD_IS_KEYWORD : 0)
554#else
555#define PARSER_FLAGS(flags) 0
556#endif
557
Jeremy Hylton9f324e92001-03-01 22:59:14 +0000558int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000559PyRun_InteractiveOneFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
Jeremy Hylton9f324e92001-03-01 22:59:14 +0000560{
Guido van Rossum82598051997-03-05 00:20:32 +0000561 PyObject *m, *d, *v, *w;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000562 node *n;
Guido van Rossuma110aa61994-08-29 12:50:44 +0000563 perrdetail err;
Guido van Rossumddc3fb51997-11-25 20:58:13 +0000564 char *ps1 = "", *ps2 = "";
Tim Petersfe2127d2001-07-16 05:37:24 +0000565
Guido van Rossum82598051997-03-05 00:20:32 +0000566 v = PySys_GetObject("ps1");
Guido van Rossumddc3fb51997-11-25 20:58:13 +0000567 if (v != NULL) {
568 v = PyObject_Str(v);
569 if (v == NULL)
570 PyErr_Clear();
571 else if (PyString_Check(v))
572 ps1 = PyString_AsString(v);
573 }
Guido van Rossum82598051997-03-05 00:20:32 +0000574 w = PySys_GetObject("ps2");
Guido van Rossumddc3fb51997-11-25 20:58:13 +0000575 if (w != NULL) {
576 w = PyObject_Str(w);
577 if (w == NULL)
578 PyErr_Clear();
579 else if (PyString_Check(w))
580 ps2 = PyString_AsString(w);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000581 }
Tim Petersfe2127d2001-07-16 05:37:24 +0000582 n = PyParser_ParseFileFlags(fp, filename, &_PyParser_Grammar,
583 Py_single_input, ps1, ps2, &err,
Neil Schemenauerc24ea082002-03-22 23:53:36 +0000584 PARSER_FLAGS(flags));
Guido van Rossum82598051997-03-05 00:20:32 +0000585 Py_XDECREF(v);
586 Py_XDECREF(w);
Guido van Rossuma110aa61994-08-29 12:50:44 +0000587 if (n == NULL) {
588 if (err.error == E_EOF) {
589 if (err.text)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000590 PyMem_DEL(err.text);
Guido van Rossuma110aa61994-08-29 12:50:44 +0000591 return E_EOF;
592 }
593 err_input(&err);
Guido van Rossum82598051997-03-05 00:20:32 +0000594 PyErr_Print();
Guido van Rossuma110aa61994-08-29 12:50:44 +0000595 return err.error;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000596 }
Guido van Rossum82598051997-03-05 00:20:32 +0000597 m = PyImport_AddModule("__main__");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000598 if (m == NULL)
599 return -1;
Guido van Rossum82598051997-03-05 00:20:32 +0000600 d = PyModule_GetDict(m);
Jeremy Hylton9f324e92001-03-01 22:59:14 +0000601 v = run_node(n, filename, d, d, flags);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000602 if (v == NULL) {
Guido van Rossum82598051997-03-05 00:20:32 +0000603 PyErr_Print();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000604 return -1;
605 }
Guido van Rossum82598051997-03-05 00:20:32 +0000606 Py_DECREF(v);
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000607 if (Py_FlushLine())
608 PyErr_Clear();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000609 return 0;
610}
611
612int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000613PyRun_SimpleFile(FILE *fp, const char *filename)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000614{
Guido van Rossum0df002c2000-08-27 19:21:52 +0000615 return PyRun_SimpleFileEx(fp, filename, 0);
616}
617
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000618/* Check whether a file maybe a pyc file: Look at the extension,
619 the file type, and, if we may close it, at the first few bytes. */
620
621static int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000622maybe_pyc_file(FILE *fp, const char* filename, const char* ext, int closeit)
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000623{
624 if (strcmp(ext, ".pyc") == 0 || strcmp(ext, ".pyo") == 0)
625 return 1;
626
627#ifdef macintosh
628 /* On a mac, we also assume a pyc file for types 'PYC ' and 'APPL' */
Jack Jansen72f3b7a2002-12-13 15:23:10 +0000629 if (PyMac_getfiletype((char *)filename) == 'PYC '
630 || PyMac_getfiletype((char *)filename) == 'APPL')
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000631 return 1;
632#endif /* macintosh */
633
634 /* Only look into the file if we are allowed to close it, since
635 it then should also be seekable. */
636 if (closeit) {
637 /* Read only two bytes of the magic. If the file was opened in
638 text mode, the bytes 3 and 4 of the magic (\r\n) might not
639 be read as they are on disk. */
640 unsigned int halfmagic = PyImport_GetMagicNumber() & 0xFFFF;
641 unsigned char buf[2];
Tim Peters3e876562001-02-11 04:35:39 +0000642 /* Mess: In case of -x, the stream is NOT at its start now,
643 and ungetc() was used to push back the first newline,
Tim Peters6f5a4ef2001-02-17 22:02:34 +0000644 which makes the current stream position formally undefined,
645 and a x-platform nightmare.
646 Unfortunately, we have no direct way to know whether -x
647 was specified. So we use a terrible hack: if the current
648 stream position is not 0, we assume -x was specified, and
649 give up. Bug 132850 on SourceForge spells out the
650 hopelessness of trying anything else (fseek and ftell
651 don't work predictably x-platform for text-mode files).
Tim Peters3e876562001-02-11 04:35:39 +0000652 */
Tim Peters3e876562001-02-11 04:35:39 +0000653 int ispyc = 0;
Tim Peters6f5a4ef2001-02-17 22:02:34 +0000654 if (ftell(fp) == 0) {
655 if (fread(buf, 1, 2, fp) == 2 &&
656 ((unsigned int)buf[1]<<8 | buf[0]) == halfmagic)
657 ispyc = 1;
658 rewind(fp);
659 }
Tim Peters3e876562001-02-11 04:35:39 +0000660 return ispyc;
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000661 }
662 return 0;
663}
664
Guido van Rossum0df002c2000-08-27 19:21:52 +0000665int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000666PyRun_SimpleFileEx(FILE *fp, const char *filename, int closeit)
Guido van Rossum0df002c2000-08-27 19:21:52 +0000667{
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000668 return PyRun_SimpleFileExFlags(fp, filename, closeit, NULL);
669}
670
671int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000672PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000673 PyCompilerFlags *flags)
674{
Guido van Rossum82598051997-03-05 00:20:32 +0000675 PyObject *m, *d, *v;
Martin v. Löwis95292d62002-12-11 14:04:59 +0000676 const char *ext;
Guido van Rossumfdef2711994-09-14 13:31:04 +0000677
Guido van Rossum82598051997-03-05 00:20:32 +0000678 m = PyImport_AddModule("__main__");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000679 if (m == NULL)
680 return -1;
Guido van Rossum82598051997-03-05 00:20:32 +0000681 d = PyModule_GetDict(m);
Fred Drake8ed02042002-10-17 21:24:58 +0000682 if (PyDict_GetItemString(d, "__file__") == NULL) {
683 PyObject *f = PyString_FromString(filename);
684 if (f == NULL)
685 return -1;
686 if (PyDict_SetItemString(d, "__file__", f) < 0) {
687 Py_DECREF(f);
688 return -1;
689 }
690 Py_DECREF(f);
691 }
Guido van Rossumfdef2711994-09-14 13:31:04 +0000692 ext = filename + strlen(filename) - 4;
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000693 if (maybe_pyc_file(fp, filename, ext, closeit)) {
Guido van Rossumfdef2711994-09-14 13:31:04 +0000694 /* Try to run a pyc file. First, re-open in binary */
Guido van Rossum0df002c2000-08-27 19:21:52 +0000695 if (closeit)
696 fclose(fp);
Fred Drake8ed02042002-10-17 21:24:58 +0000697 if ((fp = fopen(filename, "rb")) == NULL) {
Guido van Rossumfdef2711994-09-14 13:31:04 +0000698 fprintf(stderr, "python: Can't reopen .pyc file\n");
699 return -1;
700 }
Guido van Rossum2a7f58d1997-04-02 05:28:38 +0000701 /* Turn on optimization if a .pyo file is given */
702 if (strcmp(ext, ".pyo") == 0)
703 Py_OptimizeFlag = 1;
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000704 v = run_pyc_file(fp, filename, d, d, flags);
Guido van Rossumfdef2711994-09-14 13:31:04 +0000705 } else {
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000706 v = PyRun_FileExFlags(fp, filename, Py_file_input, d, d,
707 closeit, flags);
Guido van Rossumfdef2711994-09-14 13:31:04 +0000708 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000709 if (v == NULL) {
Guido van Rossum82598051997-03-05 00:20:32 +0000710 PyErr_Print();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000711 return -1;
712 }
Guido van Rossum82598051997-03-05 00:20:32 +0000713 Py_DECREF(v);
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000714 if (Py_FlushLine())
715 PyErr_Clear();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000716 return 0;
717}
718
719int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000720PyRun_SimpleString(const char *command)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000721{
Guido van Rossum393661d2001-08-31 17:40:15 +0000722 return PyRun_SimpleStringFlags(command, NULL);
723}
724
725int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000726PyRun_SimpleStringFlags(const char *command, PyCompilerFlags *flags)
Guido van Rossum393661d2001-08-31 17:40:15 +0000727{
Guido van Rossum82598051997-03-05 00:20:32 +0000728 PyObject *m, *d, *v;
729 m = PyImport_AddModule("__main__");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000730 if (m == NULL)
731 return -1;
Guido van Rossum82598051997-03-05 00:20:32 +0000732 d = PyModule_GetDict(m);
Guido van Rossum393661d2001-08-31 17:40:15 +0000733 v = PyRun_StringFlags(command, Py_file_input, d, d, flags);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000734 if (v == NULL) {
Guido van Rossum82598051997-03-05 00:20:32 +0000735 PyErr_Print();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000736 return -1;
737 }
Guido van Rossum82598051997-03-05 00:20:32 +0000738 Py_DECREF(v);
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000739 if (Py_FlushLine())
740 PyErr_Clear();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000741 return 0;
742}
743
Barry Warsaw035574d1997-08-29 22:07:17 +0000744static int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000745parse_syntax_error(PyObject *err, PyObject **message, const char **filename,
746 int *lineno, int *offset, const char **text)
Barry Warsaw035574d1997-08-29 22:07:17 +0000747{
748 long hold;
749 PyObject *v;
750
751 /* old style errors */
752 if (PyTuple_Check(err))
Neal Norwitz78293352002-04-01 01:41:20 +0000753 return PyArg_ParseTuple(err, "O(ziiz)", message, filename,
754 lineno, offset, text);
Barry Warsaw035574d1997-08-29 22:07:17 +0000755
756 /* new style errors. `err' is an instance */
757
758 if (! (v = PyObject_GetAttrString(err, "msg")))
759 goto finally;
760 *message = v;
761
762 if (!(v = PyObject_GetAttrString(err, "filename")))
763 goto finally;
764 if (v == Py_None)
765 *filename = NULL;
766 else if (! (*filename = PyString_AsString(v)))
767 goto finally;
768
769 Py_DECREF(v);
770 if (!(v = PyObject_GetAttrString(err, "lineno")))
771 goto finally;
772 hold = PyInt_AsLong(v);
773 Py_DECREF(v);
774 v = NULL;
775 if (hold < 0 && PyErr_Occurred())
776 goto finally;
777 *lineno = (int)hold;
778
779 if (!(v = PyObject_GetAttrString(err, "offset")))
780 goto finally;
Jeremy Hylton9f1b9932001-02-28 07:07:43 +0000781 if (v == Py_None) {
782 *offset = -1;
783 Py_DECREF(v);
784 v = NULL;
785 } else {
786 hold = PyInt_AsLong(v);
787 Py_DECREF(v);
788 v = NULL;
789 if (hold < 0 && PyErr_Occurred())
790 goto finally;
791 *offset = (int)hold;
792 }
Barry Warsaw035574d1997-08-29 22:07:17 +0000793
794 if (!(v = PyObject_GetAttrString(err, "text")))
795 goto finally;
796 if (v == Py_None)
797 *text = NULL;
798 else if (! (*text = PyString_AsString(v)))
799 goto finally;
800 Py_DECREF(v);
801 return 1;
802
803finally:
804 Py_XDECREF(v);
805 return 0;
806}
807
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000808void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000809PyErr_Print(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000810{
Guido van Rossuma61691e1998-02-06 22:27:24 +0000811 PyErr_PrintEx(1);
812}
813
Jeremy Hylton9f1b9932001-02-28 07:07:43 +0000814static void
Martin v. Löwis95292d62002-12-11 14:04:59 +0000815print_error_text(PyObject *f, int offset, const char *text)
Jeremy Hylton9f1b9932001-02-28 07:07:43 +0000816{
817 char *nl;
818 if (offset >= 0) {
819 if (offset > 0 && offset == (int)strlen(text))
820 offset--;
821 for (;;) {
822 nl = strchr(text, '\n');
823 if (nl == NULL || nl-text >= offset)
824 break;
825 offset -= (nl+1-text);
826 text = nl+1;
827 }
828 while (*text == ' ' || *text == '\t') {
829 text++;
830 offset--;
831 }
832 }
833 PyFile_WriteString(" ", f);
834 PyFile_WriteString(text, f);
835 if (*text == '\0' || text[strlen(text)-1] != '\n')
836 PyFile_WriteString("\n", f);
837 if (offset == -1)
838 return;
839 PyFile_WriteString(" ", f);
840 offset--;
841 while (offset > 0) {
842 PyFile_WriteString(" ", f);
843 offset--;
844 }
845 PyFile_WriteString("^\n", f);
846}
847
Guido van Rossum66e8e862001-03-23 17:54:43 +0000848static void
849handle_system_exit(void)
Ka-Ping Yee26fabb02001-03-23 15:36:41 +0000850{
Guido van Rossum66e8e862001-03-23 17:54:43 +0000851 PyObject *exception, *value, *tb;
852 PyErr_Fetch(&exception, &value, &tb);
Ka-Ping Yee26fabb02001-03-23 15:36:41 +0000853 if (Py_FlushLine())
854 PyErr_Clear();
855 fflush(stdout);
856 if (value == NULL || value == Py_None)
857 Py_Exit(0);
858 if (PyInstance_Check(value)) {
859 /* The error code should be in the `code' attribute. */
860 PyObject *code = PyObject_GetAttrString(value, "code");
861 if (code) {
862 Py_DECREF(value);
863 value = code;
864 if (value == Py_None)
865 Py_Exit(0);
866 }
867 /* If we failed to dig out the 'code' attribute,
868 just let the else clause below print the error. */
869 }
870 if (PyInt_Check(value))
871 Py_Exit((int)PyInt_AsLong(value));
872 else {
873 PyObject_Print(value, stderr, Py_PRINT_RAW);
874 PySys_WriteStderr("\n");
875 Py_Exit(1);
876 }
877}
878
879void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000880PyErr_PrintEx(int set_sys_last_vars)
Guido van Rossuma61691e1998-02-06 22:27:24 +0000881{
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000882 PyObject *exception, *v, *tb, *hook;
Guido van Rossum66e8e862001-03-23 17:54:43 +0000883
884 if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
885 handle_system_exit();
886 }
Guido van Rossum82598051997-03-05 00:20:32 +0000887 PyErr_Fetch(&exception, &v, &tb);
Barry Warsaw035574d1997-08-29 22:07:17 +0000888 PyErr_NormalizeException(&exception, &v, &tb);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000889 if (exception == NULL)
Guido van Rossum296b4751997-05-23 00:19:20 +0000890 return;
Guido van Rossuma61691e1998-02-06 22:27:24 +0000891 if (set_sys_last_vars) {
892 PySys_SetObject("last_type", exception);
893 PySys_SetObject("last_value", v);
894 PySys_SetObject("last_traceback", tb);
895 }
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000896 hook = PySys_GetObject("excepthook");
897 if (hook) {
898 PyObject *args = Py_BuildValue("(OOO)",
899 exception, v ? v : Py_None, tb ? tb : Py_None);
900 PyObject *result = PyEval_CallObject(hook, args);
901 if (result == NULL) {
902 PyObject *exception2, *v2, *tb2;
Guido van Rossum66e8e862001-03-23 17:54:43 +0000903 if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
904 handle_system_exit();
905 }
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000906 PyErr_Fetch(&exception2, &v2, &tb2);
907 PyErr_NormalizeException(&exception2, &v2, &tb2);
908 if (Py_FlushLine())
909 PyErr_Clear();
910 fflush(stdout);
911 PySys_WriteStderr("Error in sys.excepthook:\n");
912 PyErr_Display(exception2, v2, tb2);
913 PySys_WriteStderr("\nOriginal exception was:\n");
914 PyErr_Display(exception, v, tb);
Jeremy Hylton07028582001-12-07 15:35:35 +0000915 Py_XDECREF(exception2);
916 Py_XDECREF(v2);
917 Py_XDECREF(tb2);
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000918 }
919 Py_XDECREF(result);
920 Py_XDECREF(args);
921 } else {
922 PySys_WriteStderr("sys.excepthook is missing\n");
923 PyErr_Display(exception, v, tb);
924 }
925 Py_XDECREF(exception);
926 Py_XDECREF(v);
927 Py_XDECREF(tb);
928}
929
930void PyErr_Display(PyObject *exception, PyObject *value, PyObject *tb)
931{
932 int err = 0;
933 PyObject *v = value;
934 PyObject *f = PySys_GetObject("stderr");
Guido van Rossum3165fe61992-09-25 21:59:05 +0000935 if (f == NULL)
936 fprintf(stderr, "lost sys.stderr\n");
937 else {
Guido van Rossum0829c751998-02-28 04:31:39 +0000938 if (Py_FlushLine())
939 PyErr_Clear();
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000940 fflush(stdout);
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000941 if (tb && tb != Py_None)
942 err = PyTraceBack_Print(tb, f);
Barry Warsaw36b8f941997-08-26 18:09:48 +0000943 if (err == 0 &&
Martin v. Löwiscfeb3b62002-03-03 21:30:27 +0000944 PyObject_HasAttrString(v, "print_file_and_line"))
Barry Warsaw36b8f941997-08-26 18:09:48 +0000945 {
Guido van Rossum82598051997-03-05 00:20:32 +0000946 PyObject *message;
Martin v. Löwis95292d62002-12-11 14:04:59 +0000947 const char *filename, *text;
Guido van Rossuma110aa61994-08-29 12:50:44 +0000948 int lineno, offset;
Barry Warsaw035574d1997-08-29 22:07:17 +0000949 if (!parse_syntax_error(v, &message, &filename,
950 &lineno, &offset, &text))
Guido van Rossum82598051997-03-05 00:20:32 +0000951 PyErr_Clear();
Guido van Rossuma110aa61994-08-29 12:50:44 +0000952 else {
953 char buf[10];
Guido van Rossum82598051997-03-05 00:20:32 +0000954 PyFile_WriteString(" File \"", f);
Guido van Rossuma110aa61994-08-29 12:50:44 +0000955 if (filename == NULL)
Guido van Rossum82598051997-03-05 00:20:32 +0000956 PyFile_WriteString("<string>", f);
Guido van Rossuma110aa61994-08-29 12:50:44 +0000957 else
Guido van Rossum82598051997-03-05 00:20:32 +0000958 PyFile_WriteString(filename, f);
959 PyFile_WriteString("\", line ", f);
Jeremy Hylton518ab1c2001-11-28 20:42:20 +0000960 PyOS_snprintf(buf, sizeof(buf), "%d", lineno);
Guido van Rossum82598051997-03-05 00:20:32 +0000961 PyFile_WriteString(buf, f);
962 PyFile_WriteString("\n", f);
Jeremy Hylton9f1b9932001-02-28 07:07:43 +0000963 if (text != NULL)
964 print_error_text(f, offset, text);
Guido van Rossuma110aa61994-08-29 12:50:44 +0000965 v = message;
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000966 /* Can't be bothered to check all those
967 PyFile_WriteString() calls */
968 if (PyErr_Occurred())
969 err = -1;
Guido van Rossuma110aa61994-08-29 12:50:44 +0000970 }
971 }
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000972 if (err) {
973 /* Don't do anything else */
974 }
975 else if (PyClass_Check(exception)) {
Barry Warsaw2f5f6a21997-09-16 21:42:03 +0000976 PyClassObject* exc = (PyClassObject*)exception;
977 PyObject* className = exc->cl_name;
978 PyObject* moduleName =
979 PyDict_GetItemString(exc->cl_dict, "__module__");
980
981 if (moduleName == NULL)
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000982 err = PyFile_WriteString("<unknown>", f);
Barry Warsaw2f5f6a21997-09-16 21:42:03 +0000983 else {
984 char* modstr = PyString_AsString(moduleName);
985 if (modstr && strcmp(modstr, "exceptions"))
986 {
987 err = PyFile_WriteString(modstr, f);
988 err += PyFile_WriteString(".", f);
989 }
990 }
991 if (err == 0) {
992 if (className == NULL)
993 err = PyFile_WriteString("<unknown>", f);
994 else
995 err = PyFile_WriteObject(className, f,
996 Py_PRINT_RAW);
997 }
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000998 }
999 else
1000 err = PyFile_WriteObject(exception, f, Py_PRINT_RAW);
1001 if (err == 0) {
1002 if (v != NULL && v != Py_None) {
Barry Warsaw035574d1997-08-29 22:07:17 +00001003 PyObject *s = PyObject_Str(v);
1004 /* only print colon if the str() of the
1005 object is not the empty string
1006 */
Guido van Rossumd6bf45b1997-09-05 19:11:53 +00001007 if (s == NULL)
1008 err = -1;
1009 else if (!PyString_Check(s) ||
1010 PyString_GET_SIZE(s) != 0)
Barry Warsaw035574d1997-08-29 22:07:17 +00001011 err = PyFile_WriteString(": ", f);
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001012 if (err == 0)
Guido van Rossumd6bf45b1997-09-05 19:11:53 +00001013 err = PyFile_WriteObject(s, f, Py_PRINT_RAW);
1014 Py_XDECREF(s);
Guido van Rossum262e1241995-02-07 15:30:45 +00001015 }
Guido van Rossum262e1241995-02-07 15:30:45 +00001016 }
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001017 if (err == 0)
1018 err = PyFile_WriteString("\n", f);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001019 }
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001020 /* If an error happened here, don't show it.
1021 XXX This is wrong, but too many callers rely on this behavior. */
1022 if (err != 0)
1023 PyErr_Clear();
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001024}
1025
Guido van Rossum82598051997-03-05 00:20:32 +00001026PyObject *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001027PyRun_String(const char *str, int start, PyObject *globals, PyObject *locals)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001028{
Guido van Rossum82598051997-03-05 00:20:32 +00001029 return run_err_node(PyParser_SimpleParseString(str, start),
Jeremy Hylton9f324e92001-03-01 22:59:14 +00001030 "<string>", globals, locals, NULL);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001031}
1032
Guido van Rossum82598051997-03-05 00:20:32 +00001033PyObject *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001034PyRun_File(FILE *fp, const char *filename, int start, PyObject *globals,
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001035 PyObject *locals)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001036{
Tim Peterse8682112000-08-27 20:18:17 +00001037 return PyRun_FileEx(fp, filename, start, globals, locals, 0);
Guido van Rossum0df002c2000-08-27 19:21:52 +00001038}
1039
1040PyObject *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001041PyRun_FileEx(FILE *fp, const char *filename, int start, PyObject *globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001042 PyObject *locals, int closeit)
Guido van Rossum0df002c2000-08-27 19:21:52 +00001043{
1044 node *n = PyParser_SimpleParseFile(fp, filename, start);
1045 if (closeit)
1046 fclose(fp);
Jeremy Hylton9f324e92001-03-01 22:59:14 +00001047 return run_err_node(n, filename, globals, locals, NULL);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001048}
1049
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001050PyObject *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001051PyRun_StringFlags(const char *str, int start, PyObject *globals, PyObject *locals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001052 PyCompilerFlags *flags)
1053{
Guido van Rossuma1b3a472001-07-16 16:51:33 +00001054 return run_err_node(PyParser_SimpleParseStringFlags(
Neil Schemenauerc24ea082002-03-22 23:53:36 +00001055 str, start, PARSER_FLAGS(flags)),
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001056 "<string>", globals, locals, flags);
1057}
1058
1059PyObject *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001060PyRun_FileFlags(FILE *fp, const char *filename, int start, PyObject *globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001061 PyObject *locals, PyCompilerFlags *flags)
1062{
1063 return PyRun_FileExFlags(fp, filename, start, globals, locals, 0,
1064 flags);
1065}
1066
1067PyObject *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001068PyRun_FileExFlags(FILE *fp, const char *filename, int start, PyObject *globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001069 PyObject *locals, int closeit, PyCompilerFlags *flags)
1070{
Tim Petersfe2127d2001-07-16 05:37:24 +00001071 node *n = PyParser_SimpleParseFileFlags(fp, filename, start,
Neil Schemenauerc24ea082002-03-22 23:53:36 +00001072 PARSER_FLAGS(flags));
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001073 if (closeit)
1074 fclose(fp);
1075 return run_err_node(n, filename, globals, locals, flags);
1076}
1077
Guido van Rossum82598051997-03-05 00:20:32 +00001078static PyObject *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001079run_err_node(node *n, const char *filename, PyObject *globals, PyObject *locals,
Jeremy Hylton9f324e92001-03-01 22:59:14 +00001080 PyCompilerFlags *flags)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001081{
Guido van Rossuma110aa61994-08-29 12:50:44 +00001082 if (n == NULL)
1083 return NULL;
Jeremy Hylton9f324e92001-03-01 22:59:14 +00001084 return run_node(n, filename, globals, locals, flags);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001085}
1086
Guido van Rossum82598051997-03-05 00:20:32 +00001087static PyObject *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001088run_node(node *n, const char *filename, PyObject *globals, PyObject *locals,
Jeremy Hylton9f324e92001-03-01 22:59:14 +00001089 PyCompilerFlags *flags)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001090{
Guido van Rossum82598051997-03-05 00:20:32 +00001091 PyCodeObject *co;
1092 PyObject *v;
Jeremy Hylton9f324e92001-03-01 22:59:14 +00001093 co = PyNode_CompileFlags(n, filename, flags);
Guido van Rossum82598051997-03-05 00:20:32 +00001094 PyNode_Free(n);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001095 if (co == NULL)
1096 return NULL;
Guido van Rossum82598051997-03-05 00:20:32 +00001097 v = PyEval_EvalCode(co, globals, locals);
1098 Py_DECREF(co);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001099 return v;
1100}
1101
Guido van Rossum82598051997-03-05 00:20:32 +00001102static PyObject *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001103run_pyc_file(FILE *fp, const char *filename, PyObject *globals, PyObject *locals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001104 PyCompilerFlags *flags)
Guido van Rossumfdef2711994-09-14 13:31:04 +00001105{
Guido van Rossum82598051997-03-05 00:20:32 +00001106 PyCodeObject *co;
1107 PyObject *v;
Guido van Rossumfdef2711994-09-14 13:31:04 +00001108 long magic;
Fred Drakee8de31c2000-08-31 05:38:39 +00001109 long PyImport_GetMagicNumber(void);
Guido van Rossumfdef2711994-09-14 13:31:04 +00001110
Guido van Rossum82598051997-03-05 00:20:32 +00001111 magic = PyMarshal_ReadLongFromFile(fp);
1112 if (magic != PyImport_GetMagicNumber()) {
1113 PyErr_SetString(PyExc_RuntimeError,
Guido van Rossumfdef2711994-09-14 13:31:04 +00001114 "Bad magic number in .pyc file");
1115 return NULL;
1116 }
Guido van Rossum82598051997-03-05 00:20:32 +00001117 (void) PyMarshal_ReadLongFromFile(fp);
Tim Petersd9b9ac82001-01-28 00:27:39 +00001118 v = PyMarshal_ReadLastObjectFromFile(fp);
Guido van Rossumfdef2711994-09-14 13:31:04 +00001119 fclose(fp);
Guido van Rossum82598051997-03-05 00:20:32 +00001120 if (v == NULL || !PyCode_Check(v)) {
1121 Py_XDECREF(v);
1122 PyErr_SetString(PyExc_RuntimeError,
Guido van Rossumfdef2711994-09-14 13:31:04 +00001123 "Bad code object in .pyc file");
1124 return NULL;
1125 }
Guido van Rossum82598051997-03-05 00:20:32 +00001126 co = (PyCodeObject *)v;
1127 v = PyEval_EvalCode(co, globals, locals);
Jeremy Hyltonb857ba22001-08-10 21:41:33 +00001128 if (v && flags)
1129 flags->cf_flags |= (co->co_flags & PyCF_MASK);
Guido van Rossum82598051997-03-05 00:20:32 +00001130 Py_DECREF(co);
Guido van Rossumfdef2711994-09-14 13:31:04 +00001131 return v;
1132}
1133
Guido van Rossum82598051997-03-05 00:20:32 +00001134PyObject *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001135Py_CompileString(const char *str, const char *filename, int start)
Guido van Rossum5b722181993-03-30 17:46:03 +00001136{
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001137 return Py_CompileStringFlags(str, filename, start, NULL);
1138}
1139
1140PyObject *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001141Py_CompileStringFlags(const char *str, const char *filename, int start,
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001142 PyCompilerFlags *flags)
1143{
Guido van Rossum5b722181993-03-30 17:46:03 +00001144 node *n;
Guido van Rossum82598051997-03-05 00:20:32 +00001145 PyCodeObject *co;
Thomas Heller6b17abf2002-07-09 09:23:27 +00001146
1147 n = PyParser_SimpleParseStringFlagsFilename(str, filename, start,
1148 PARSER_FLAGS(flags));
Guido van Rossuma110aa61994-08-29 12:50:44 +00001149 if (n == NULL)
Guido van Rossum5b722181993-03-30 17:46:03 +00001150 return NULL;
Jeremy Hylton673a4fd2001-03-26 19:53:38 +00001151 co = PyNode_CompileFlags(n, filename, flags);
Guido van Rossum82598051997-03-05 00:20:32 +00001152 PyNode_Free(n);
1153 return (PyObject *)co;
Guido van Rossum5b722181993-03-30 17:46:03 +00001154}
1155
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001156struct symtable *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001157Py_SymtableString(const char *str, const char *filename, int start)
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001158{
1159 node *n;
1160 struct symtable *st;
Thomas Heller6b17abf2002-07-09 09:23:27 +00001161 n = PyParser_SimpleParseStringFlagsFilename(str, filename,
1162 start, 0);
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001163 if (n == NULL)
1164 return NULL;
1165 st = PyNode_CompileSymtable(n, filename);
1166 PyNode_Free(n);
1167 return st;
1168}
1169
Guido van Rossuma110aa61994-08-29 12:50:44 +00001170/* Simplified interface to parsefile -- return node or set exception */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001171
Guido van Rossuma110aa61994-08-29 12:50:44 +00001172node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001173PyParser_SimpleParseFileFlags(FILE *fp, const char *filename, int start, int flags)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001174{
Guido van Rossuma110aa61994-08-29 12:50:44 +00001175 node *n;
1176 perrdetail err;
Tim Petersfe2127d2001-07-16 05:37:24 +00001177 n = PyParser_ParseFileFlags(fp, filename, &_PyParser_Grammar, start,
1178 (char *)0, (char *)0, &err, flags);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001179 if (n == NULL)
1180 err_input(&err);
1181 return n;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001182}
1183
Tim Petersfe2127d2001-07-16 05:37:24 +00001184node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001185PyParser_SimpleParseFile(FILE *fp, const char *filename, int start)
Tim Petersfe2127d2001-07-16 05:37:24 +00001186{
1187 return PyParser_SimpleParseFileFlags(fp, filename, start, 0);
1188}
1189
Guido van Rossuma110aa61994-08-29 12:50:44 +00001190/* Simplified interface to parsestring -- return node or set exception */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001191
Guido van Rossuma110aa61994-08-29 12:50:44 +00001192node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001193PyParser_SimpleParseStringFlags(const char *str, int start, int flags)
Tim Petersfe2127d2001-07-16 05:37:24 +00001194{
1195 node *n;
1196 perrdetail err;
1197 n = PyParser_ParseStringFlags(str, &_PyParser_Grammar, start, &err,
1198 flags);
1199 if (n == NULL)
1200 err_input(&err);
1201 return n;
1202}
1203
1204node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001205PyParser_SimpleParseString(const char *str, int start)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001206{
Tim Petersfe2127d2001-07-16 05:37:24 +00001207 return PyParser_SimpleParseStringFlags(str, start, 0);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001208}
1209
Thomas Heller6b17abf2002-07-09 09:23:27 +00001210node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001211PyParser_SimpleParseStringFlagsFilename(const char *str, const char *filename,
Thomas Heller6b17abf2002-07-09 09:23:27 +00001212 int start, int flags)
1213{
1214 node *n;
1215 perrdetail err;
1216
1217 n = PyParser_ParseStringFlagsFilename(str, filename,
1218 &_PyParser_Grammar,
1219 start, &err, flags);
1220 if (n == NULL)
1221 err_input(&err);
1222 return n;
1223}
1224
1225node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001226PyParser_SimpleParseStringFilename(const char *str, const char *filename, int start)
Thomas Heller6b17abf2002-07-09 09:23:27 +00001227{
1228 return PyParser_SimpleParseStringFlagsFilename(str, filename,
1229 start, 0);
1230}
1231
Guido van Rossuma110aa61994-08-29 12:50:44 +00001232/* Set the error appropriate to the given input error code (see errcode.h) */
1233
1234static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001235err_input(perrdetail *err)
Guido van Rossuma110aa61994-08-29 12:50:44 +00001236{
Fred Drake85f36392000-07-11 17:53:00 +00001237 PyObject *v, *w, *errtype;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001238 PyObject* u = NULL;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001239 char *msg = NULL;
Fred Drake85f36392000-07-11 17:53:00 +00001240 errtype = PyExc_SyntaxError;
Guido van Rossum82598051997-03-05 00:20:32 +00001241 v = Py_BuildValue("(ziiz)", err->filename,
Guido van Rossuma110aa61994-08-29 12:50:44 +00001242 err->lineno, err->offset, err->text);
1243 if (err->text != NULL) {
Guido van Rossumb18618d2000-05-03 23:44:39 +00001244 PyMem_DEL(err->text);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001245 err->text = NULL;
1246 }
1247 switch (err->error) {
1248 case E_SYNTAX:
Fred Drake85f36392000-07-11 17:53:00 +00001249 errtype = PyExc_IndentationError;
1250 if (err->expected == INDENT)
1251 msg = "expected an indented block";
1252 else if (err->token == INDENT)
1253 msg = "unexpected indent";
1254 else if (err->token == DEDENT)
1255 msg = "unexpected unindent";
1256 else {
1257 errtype = PyExc_SyntaxError;
1258 msg = "invalid syntax";
1259 }
Guido van Rossuma110aa61994-08-29 12:50:44 +00001260 break;
1261 case E_TOKEN:
1262 msg = "invalid token";
Guido van Rossuma110aa61994-08-29 12:50:44 +00001263 break;
Skip Montanaro118ec702002-08-15 01:20:16 +00001264 case E_EOFS:
1265 msg = "EOF while scanning triple-quoted string";
1266 break;
1267 case E_EOLS:
1268 msg = "EOL while scanning single-quoted string";
1269 break;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001270 case E_INTR:
Guido van Rossum82598051997-03-05 00:20:32 +00001271 PyErr_SetNone(PyExc_KeyboardInterrupt);
Barry Warsawc80baa31999-01-27 16:39:40 +00001272 Py_XDECREF(v);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001273 return;
1274 case E_NOMEM:
Guido van Rossum82598051997-03-05 00:20:32 +00001275 PyErr_NoMemory();
Barry Warsawc80baa31999-01-27 16:39:40 +00001276 Py_XDECREF(v);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001277 return;
1278 case E_EOF:
1279 msg = "unexpected EOF while parsing";
1280 break;
Fred Drake85f36392000-07-11 17:53:00 +00001281 case E_TABSPACE:
1282 errtype = PyExc_TabError;
Guido van Rossum560e8ad1998-04-10 19:43:42 +00001283 msg = "inconsistent use of tabs and spaces in indentation";
1284 break;
Jeremy Hylton94988062000-06-20 19:10:44 +00001285 case E_OVERFLOW:
1286 msg = "expression too long";
1287 break;
Fred Drake85f36392000-07-11 17:53:00 +00001288 case E_DEDENT:
1289 errtype = PyExc_IndentationError;
1290 msg = "unindent does not match any outer indentation level";
1291 break;
1292 case E_TOODEEP:
1293 errtype = PyExc_IndentationError;
1294 msg = "too many levels of indentation";
1295 break;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001296 case E_DECODE: { /* XXX */
1297 PyThreadState* tstate = PyThreadState_Get();
1298 PyObject* value = tstate->curexc_value;
1299 if (value != NULL) {
1300 u = PyObject_Repr(value);
1301 if (u != NULL) {
1302 msg = PyString_AsString(u);
1303 break;
1304 }
1305 }
1306 }
Guido van Rossuma110aa61994-08-29 12:50:44 +00001307 default:
1308 fprintf(stderr, "error=%d\n", err->error);
1309 msg = "unknown parsing error";
1310 break;
1311 }
Guido van Rossum82598051997-03-05 00:20:32 +00001312 w = Py_BuildValue("(sO)", msg, v);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001313 Py_XDECREF(u);
Guido van Rossum41318302001-03-23 04:01:07 +00001314 Py_XDECREF(v);
Fred Drake85f36392000-07-11 17:53:00 +00001315 PyErr_SetObject(errtype, w);
Guido van Rossum82598051997-03-05 00:20:32 +00001316 Py_XDECREF(w);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001317}
1318
1319/* Print fatal error message and abort */
1320
1321void
Tim Peters7c321a82002-07-09 02:57:01 +00001322Py_FatalError(const char *msg)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001323{
Guido van Rossum83dd6c31994-09-29 09:38:33 +00001324 fprintf(stderr, "Fatal Python error: %s\n", msg);
Guido van Rossum8ae87c01995-01-26 00:40:38 +00001325#ifdef macintosh
1326 for (;;);
1327#endif
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001328#ifdef MS_WINDOWS
Guido van Rossum23c94461997-05-22 20:21:30 +00001329 OutputDebugString("Fatal Python error: ");
Guido van Rossuma44823b1995-03-14 15:01:17 +00001330 OutputDebugString(msg);
1331 OutputDebugString("\n");
Guido van Rossum0ba35361998-08-13 13:33:16 +00001332#ifdef _DEBUG
1333 DebugBreak();
Guido van Rossuma44823b1995-03-14 15:01:17 +00001334#endif
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001335#endif /* MS_WINDOWS */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001336 abort();
1337}
1338
1339/* Clean up and exit */
1340
Guido van Rossuma110aa61994-08-29 12:50:44 +00001341#ifdef WITH_THREAD
Guido van Rossum49b56061998-10-01 20:42:43 +00001342#include "pythread.h"
Guido van Rossum82598051997-03-05 00:20:32 +00001343int _PyThread_Started = 0; /* Set by threadmodule.c and maybe others */
Guido van Rossumf9f2e821992-08-17 08:59:08 +00001344#endif
1345
Guido van Rossum2dcfc961998-10-01 16:01:57 +00001346#define NEXITFUNCS 32
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001347static void (*exitfuncs[NEXITFUNCS])(void);
Guido van Rossum1662dd51994-09-07 14:38:28 +00001348static int nexitfuncs = 0;
1349
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001350int Py_AtExit(void (*func)(void))
Guido van Rossum1662dd51994-09-07 14:38:28 +00001351{
1352 if (nexitfuncs >= NEXITFUNCS)
1353 return -1;
1354 exitfuncs[nexitfuncs++] = func;
1355 return 0;
1356}
1357
Guido van Rossumcc283f51997-08-05 02:22:03 +00001358static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001359call_sys_exitfunc(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001360{
Guido van Rossum82598051997-03-05 00:20:32 +00001361 PyObject *exitfunc = PySys_GetObject("exitfunc");
Guido van Rossum59bff391992-09-03 20:28:00 +00001362
1363 if (exitfunc) {
Fred Drake6a12d8d2001-03-23 17:34:02 +00001364 PyObject *res;
Guido van Rossum82598051997-03-05 00:20:32 +00001365 Py_INCREF(exitfunc);
1366 PySys_SetObject("exitfunc", (PyObject *)NULL);
1367 res = PyEval_CallObject(exitfunc, (PyObject *)NULL);
Guido van Rossum59bff391992-09-03 20:28:00 +00001368 if (res == NULL) {
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001369 if (!PyErr_ExceptionMatches(PyExc_SystemExit)) {
1370 PySys_WriteStderr("Error in sys.exitfunc:\n");
1371 }
Guido van Rossum82598051997-03-05 00:20:32 +00001372 PyErr_Print();
Guido van Rossum59bff391992-09-03 20:28:00 +00001373 }
Guido van Rossum82598051997-03-05 00:20:32 +00001374 Py_DECREF(exitfunc);
Guido van Rossum59bff391992-09-03 20:28:00 +00001375 }
1376
Guido van Rossum0829c751998-02-28 04:31:39 +00001377 if (Py_FlushLine())
1378 PyErr_Clear();
Guido van Rossumcc283f51997-08-05 02:22:03 +00001379}
Guido van Rossum1662dd51994-09-07 14:38:28 +00001380
Guido van Rossumcc283f51997-08-05 02:22:03 +00001381static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001382call_ll_exitfuncs(void)
Guido van Rossumcc283f51997-08-05 02:22:03 +00001383{
Guido van Rossum1662dd51994-09-07 14:38:28 +00001384 while (nexitfuncs > 0)
1385 (*exitfuncs[--nexitfuncs])();
Guido van Rossum25ce5661997-08-02 03:10:38 +00001386
1387 fflush(stdout);
1388 fflush(stderr);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001389}
1390
1391void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001392Py_Exit(int sts)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001393{
Guido van Rossumcc283f51997-08-05 02:22:03 +00001394 Py_Finalize();
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001395
Jack Jansen66a89771995-10-27 13:22:14 +00001396#ifdef macintosh
1397 PyMac_Exit(sts);
1398#else
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001399 exit(sts);
Jack Jansen66a89771995-10-27 13:22:14 +00001400#endif
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001401}
1402
Guido van Rossumf1dc5661993-07-05 10:31:29 +00001403static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001404initsigs(void)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001405{
Guido van Rossuma110aa61994-08-29 12:50:44 +00001406#ifdef HAVE_SIGNAL_H
1407#ifdef SIGPIPE
1408 signal(SIGPIPE, SIG_IGN);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001409#endif
Guido van Rossum70d893a2001-08-16 08:21:42 +00001410#ifdef SIGXFZ
1411 signal(SIGXFZ, SIG_IGN);
1412#endif
Jeremy Hylton1b0bf9b2002-04-23 20:31:01 +00001413#ifdef SIGXFSZ
1414 signal(SIGXFSZ, SIG_IGN);
1415#endif
Guido van Rossuma110aa61994-08-29 12:50:44 +00001416#endif /* HAVE_SIGNAL_H */
Guido van Rossum82598051997-03-05 00:20:32 +00001417 PyOS_InitInterrupts(); /* May imply initsignal() */
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001418}
1419
Guido van Rossuma110aa61994-08-29 12:50:44 +00001420#ifdef MPW
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001421
1422/* Check for file descriptor connected to interactive device.
1423 Pretend that stdin is always interactive, other files never. */
1424
1425int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001426isatty(int fd)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001427{
1428 return fd == fileno(stdin);
1429}
1430
1431#endif
Guido van Rossum7433b121997-02-14 19:45:36 +00001432
1433/*
1434 * The file descriptor fd is considered ``interactive'' if either
1435 * a) isatty(fd) is TRUE, or
1436 * b) the -i flag was given, and the filename associated with
1437 * the descriptor is NULL or "<stdin>" or "???".
1438 */
1439int
Martin v. Löwis95292d62002-12-11 14:04:59 +00001440Py_FdIsInteractive(FILE *fp, const char *filename)
Guido van Rossum7433b121997-02-14 19:45:36 +00001441{
1442 if (isatty((int)fileno(fp)))
1443 return 1;
1444 if (!Py_InteractiveFlag)
1445 return 0;
1446 return (filename == NULL) ||
1447 (strcmp(filename, "<stdin>") == 0) ||
1448 (strcmp(filename, "???") == 0);
1449}
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001450
1451
1452#if defined(USE_STACKCHECK)
1453#if defined(WIN32) && defined(_MSC_VER)
1454
1455/* Stack checking for Microsoft C */
1456
1457#include <malloc.h>
1458#include <excpt.h>
1459
Fred Drakee8de31c2000-08-31 05:38:39 +00001460/*
1461 * Return non-zero when we run out of memory on the stack; zero otherwise.
1462 */
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001463int
Fred Drake399739f2000-08-31 05:52:44 +00001464PyOS_CheckStack(void)
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001465{
1466 __try {
Tim Peters92e4dd82002-10-05 01:47:34 +00001467 /* alloca throws a stack overflow exception if there's
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001468 not enough space left on the stack */
Tim Peters92e4dd82002-10-05 01:47:34 +00001469 alloca(PYOS_STACK_MARGIN * sizeof(void*));
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001470 return 0;
1471 } __except (EXCEPTION_EXECUTE_HANDLER) {
1472 /* just ignore all errors */
1473 }
1474 return 1;
1475}
1476
1477#endif /* WIN32 && _MSC_VER */
1478
1479/* Alternate implementations can be added here... */
1480
1481#endif /* USE_STACKCHECK */
Guido van Rossum6f256182000-09-16 16:32:19 +00001482
1483
1484/* Wrappers around sigaction() or signal(). */
1485
1486PyOS_sighandler_t
1487PyOS_getsig(int sig)
1488{
1489#ifdef HAVE_SIGACTION
1490 struct sigaction context;
Barry Warsawafeb2a42001-11-13 23:08:26 +00001491 /* Initialize context.sa_handler to SIG_ERR which makes about as
1492 * much sense as anything else. It should get overwritten if
1493 * sigaction actually succeeds and otherwise we avoid an
1494 * uninitialized memory read.
1495 */
1496 context.sa_handler = SIG_ERR;
Guido van Rossum6f256182000-09-16 16:32:19 +00001497 sigaction(sig, NULL, &context);
1498 return context.sa_handler;
1499#else
1500 PyOS_sighandler_t handler;
1501 handler = signal(sig, SIG_IGN);
1502 signal(sig, handler);
1503 return handler;
1504#endif
1505}
1506
1507PyOS_sighandler_t
1508PyOS_setsig(int sig, PyOS_sighandler_t handler)
1509{
1510#ifdef HAVE_SIGACTION
1511 struct sigaction context;
1512 PyOS_sighandler_t oldhandler;
Barry Warsawafeb2a42001-11-13 23:08:26 +00001513 /* Initialize context.sa_handler to SIG_ERR which makes about as
1514 * much sense as anything else. It should get overwritten if
1515 * sigaction actually succeeds and otherwise we avoid an
1516 * uninitialized memory read.
1517 */
1518 context.sa_handler = SIG_ERR;
Guido van Rossum6f256182000-09-16 16:32:19 +00001519 sigaction(sig, NULL, &context);
1520 oldhandler = context.sa_handler;
1521 context.sa_handler = handler;
1522 sigaction(sig, &context, NULL);
1523 return oldhandler;
1524#else
1525 return signal(sig, handler);
1526#endif
1527}