blob: 908bc42f6efc702ec1269aee02c82def9aada927 [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
Mark Hammonda43fd0c2003-02-19 00:33:33 +000063/* Reference to 'warnings' module, to avoid importing it
64 on the fly when the import lock may be held. See 683658
65*/
66PyObject *PyModule_WarningsModule = NULL;
67
Guido van Rossum25ce5661997-08-02 03:10:38 +000068static int initialized = 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +000069
Thomas Wouters7e474022000-07-16 12:04:32 +000070/* API to access the initialized flag -- useful for esoteric use */
Guido van Rossume3c0d5e1997-08-22 04:20:13 +000071
72int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000073Py_IsInitialized(void)
Guido van Rossume3c0d5e1997-08-22 04:20:13 +000074{
75 return initialized;
76}
77
Guido van Rossum25ce5661997-08-02 03:10:38 +000078/* Global initializations. Can be undone by Py_Finalize(). Don't
79 call this twice without an intervening Py_Finalize() call. When
80 initializations fail, a fatal error is issued and the function does
81 not return. On return, the first thread and interpreter state have
82 been created.
Guido van Rossum1984f1e1992-08-04 12:41:02 +000083
Guido van Rossum25ce5661997-08-02 03:10:38 +000084 Locking: you must hold the interpreter lock while calling this.
85 (If the lock has not yet been initialized, that's equivalent to
86 having the lock, but you cannot use multiple threads.)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +000087
Guido van Rossum25ce5661997-08-02 03:10:38 +000088*/
Guido van Rossuma027efa1997-05-05 20:56:21 +000089
Guido van Rossum9abaf4d2001-10-12 22:17:56 +000090static int
91add_flag(int flag, const char *envs)
92{
93 int env = atoi(envs);
94 if (flag < env)
95 flag = env;
96 if (flag < 1)
97 flag = 1;
98 return flag;
99}
100
Guido van Rossuma027efa1997-05-05 20:56:21 +0000101void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000102Py_Initialize(void)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000103{
Guido van Rossuma027efa1997-05-05 20:56:21 +0000104 PyInterpreterState *interp;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000105 PyThreadState *tstate;
106 PyObject *bimod, *sysmod;
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000107 char *p;
Guido van Rossum70d893a2001-08-16 08:21:42 +0000108 extern void _Py_ReadyTypes(void);
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000109
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000110 if (initialized)
Guido van Rossumaa615051997-08-20 22:40:18 +0000111 return;
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000112 initialized = 1;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000113
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000114 if ((p = Py_GETENV("PYTHONDEBUG")) && *p != '\0')
Guido van Rossum9abaf4d2001-10-12 22:17:56 +0000115 Py_DebugFlag = add_flag(Py_DebugFlag, p);
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000116 if ((p = Py_GETENV("PYTHONVERBOSE")) && *p != '\0')
Guido van Rossum9abaf4d2001-10-12 22:17:56 +0000117 Py_VerboseFlag = add_flag(Py_VerboseFlag, p);
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000118 if ((p = Py_GETENV("PYTHONOPTIMIZE")) && *p != '\0')
Guido van Rossum9abaf4d2001-10-12 22:17:56 +0000119 Py_OptimizeFlag = add_flag(Py_OptimizeFlag, p);
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000120
Guido van Rossuma027efa1997-05-05 20:56:21 +0000121 interp = PyInterpreterState_New();
122 if (interp == NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000123 Py_FatalError("Py_Initialize: can't make first interpreter");
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000124
Guido van Rossuma027efa1997-05-05 20:56:21 +0000125 tstate = PyThreadState_New(interp);
126 if (tstate == NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000127 Py_FatalError("Py_Initialize: can't make first thread");
Guido van Rossuma027efa1997-05-05 20:56:21 +0000128 (void) PyThreadState_Swap(tstate);
129
Guido van Rossum70d893a2001-08-16 08:21:42 +0000130 _Py_ReadyTypes();
Guido van Rossum528b7eb2001-08-07 17:24:28 +0000131
Neal Norwitzb2501f42002-12-31 03:42:13 +0000132 if (!_PyFrame_Init())
Neal Norwitzc91ed402002-12-30 22:29:22 +0000133 Py_FatalError("Py_Initialize: can't init frames");
134
Neal Norwitzb2501f42002-12-31 03:42:13 +0000135 if (!_PyInt_Init())
Neal Norwitzc91ed402002-12-30 22:29:22 +0000136 Py_FatalError("Py_Initialize: can't init ints");
137
Guido van Rossum25ce5661997-08-02 03:10:38 +0000138 interp->modules = PyDict_New();
139 if (interp->modules == NULL)
140 Py_FatalError("Py_Initialize: can't make modules dictionary");
Guido van Rossuma027efa1997-05-05 20:56:21 +0000141
Guido van Rossumc94044c2000-03-10 23:03:54 +0000142 /* Init codec registry */
143 _PyCodecRegistry_Init();
144
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000145#ifdef Py_USING_UNICODE
Guido van Rossumc94044c2000-03-10 23:03:54 +0000146 /* Init Unicode implementation; relies on the codec registry */
147 _PyUnicode_Init();
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000148#endif
Guido van Rossumc94044c2000-03-10 23:03:54 +0000149
Barry Warsawf242aa02000-05-25 23:09:49 +0000150 bimod = _PyBuiltin_Init();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000151 if (bimod == NULL)
152 Py_FatalError("Py_Initialize: can't initialize __builtin__");
Guido van Rossum4a1f39a1997-11-04 19:36:18 +0000153 interp->builtins = PyModule_GetDict(bimod);
154 Py_INCREF(interp->builtins);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000155
156 sysmod = _PySys_Init();
157 if (sysmod == NULL)
158 Py_FatalError("Py_Initialize: can't initialize sys");
159 interp->sysdict = PyModule_GetDict(sysmod);
160 Py_INCREF(interp->sysdict);
161 _PyImport_FixupExtension("sys", "sys");
Guido van Rossuma027efa1997-05-05 20:56:21 +0000162 PySys_SetPath(Py_GetPath());
Guido van Rossum25ce5661997-08-02 03:10:38 +0000163 PyDict_SetItemString(interp->sysdict, "modules",
164 interp->modules);
165
Guido van Rossum7c85ab81999-07-08 17:26:56 +0000166 _PyImport_Init();
167
Barry Warsawf242aa02000-05-25 23:09:49 +0000168 /* initialize builtin exceptions */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000169 _PyExc_Init();
Barry Warsaw5821bc52001-08-13 23:04:56 +0000170 _PyImport_FixupExtension("exceptions", "exceptions");
Barry Warsawf242aa02000-05-25 23:09:49 +0000171
Barry Warsaw035574d1997-08-29 22:07:17 +0000172 /* phase 2 of builtins */
Barry Warsaw963b8711997-09-18 16:42:02 +0000173 _PyImport_FixupExtension("__builtin__", "__builtin__");
Barry Warsaw035574d1997-08-29 22:07:17 +0000174
Just van Rossum52e14d62002-12-30 22:08:05 +0000175 _PyImportHooks_Init();
176
Guido van Rossum25ce5661997-08-02 03:10:38 +0000177 initsigs(); /* Signal handling stuff, including initintr() */
178
179 initmain(); /* Module __main__ */
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000180 if (!Py_NoSiteFlag)
181 initsite(); /* Module site */
Just van Rossum5bfba3a2003-02-25 20:25:12 +0000182
183 PyModule_WarningsModule = PyImport_ImportModule("warnings");
Guido van Rossum25ce5661997-08-02 03:10:38 +0000184}
185
Guido van Rossum2edcf0d1998-12-15 16:12:00 +0000186#ifdef COUNT_ALLOCS
Tim Petersdbd9ba62000-07-09 03:09:57 +0000187extern void dump_counts(void);
Guido van Rossum2edcf0d1998-12-15 16:12:00 +0000188#endif
189
Guido van Rossum25ce5661997-08-02 03:10:38 +0000190/* Undo the effect of Py_Initialize().
191
192 Beware: if multiple interpreter and/or thread states exist, these
193 are not wiped out; only the current thread and interpreter state
194 are deleted. But since everything else is deleted, those other
195 interpreter and thread states should no longer be used.
196
197 (XXX We should do better, e.g. wipe out all interpreters and
198 threads.)
199
200 Locking: as above.
201
202*/
203
204void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000205Py_Finalize(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000206{
207 PyInterpreterState *interp;
208 PyThreadState *tstate;
209
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000210 if (!initialized)
Guido van Rossumaa615051997-08-20 22:40:18 +0000211 return;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000212
Tim Peters384fd102001-01-21 03:40:37 +0000213 /* The interpreter is still entirely intact at this point, and the
214 * exit funcs may be relying on that. In particular, if some thread
215 * or exit func is still waiting to do an import, the import machinery
216 * expects Py_IsInitialized() to return true. So don't say the
217 * interpreter is uninitialized until after the exit funcs have run.
218 * Note that Threading.py uses an exit func to do a join on all the
219 * threads created thru it, so this also protects pending imports in
220 * the threads created via Threading.
221 */
Guido van Rossum4cc462e1998-01-19 22:00:38 +0000222 call_sys_exitfunc();
Tim Peters384fd102001-01-21 03:40:37 +0000223 initialized = 0;
Guido van Rossum4cc462e1998-01-19 22:00:38 +0000224
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000225 /* Get current thread state and interpreter pointer */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000226 tstate = PyThreadState_Get();
227 interp = tstate->interp;
228
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000229 /* Disable signal handling */
230 PyOS_FiniInterrupts();
231
Guido van Rossumc94044c2000-03-10 23:03:54 +0000232 /* Cleanup Codec registry */
233 _PyCodecRegistry_Fini();
234
Mark Hammonda43fd0c2003-02-19 00:33:33 +0000235 /* drop module references we saved */
236 Py_XDECREF(PyModule_WarningsModule);
237 PyModule_WarningsModule = NULL;
238
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000239 /* Destroy all modules */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000240 PyImport_Cleanup();
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000241
Guido van Rossum1707aad1997-12-08 23:43:45 +0000242 /* Destroy the database used by _PyImport_{Fixup,Find}Extension */
243 _PyImport_Fini();
244
245 /* Debugging stuff */
246#ifdef COUNT_ALLOCS
247 dump_counts();
248#endif
249
250#ifdef Py_REF_DEBUG
251 fprintf(stderr, "[%ld refs]\n", _Py_RefTotal);
252#endif
253
254#ifdef Py_TRACE_REFS
Guido van Rossum92e2d5c2001-08-09 16:37:16 +0000255 if (Py_GETENV("PYTHONDUMPREFS")) {
Guido van Rossum1707aad1997-12-08 23:43:45 +0000256 _Py_PrintReferences(stderr);
257 }
258#endif /* Py_TRACE_REFS */
259
Barry Warsaw035574d1997-08-29 22:07:17 +0000260 /* Now we decref the exception classes. After this point nothing
261 can raise an exception. That's okay, because each Fini() method
262 below has been checked to make sure no exceptions are ever
263 raised.
264 */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000265 _PyExc_Fini();
Barry Warsawf242aa02000-05-25 23:09:49 +0000266
267 /* Delete current thread */
268 PyInterpreterState_Clear(interp);
269 PyThreadState_Swap(NULL);
270 PyInterpreterState_Delete(interp);
271
Guido van Rossumcc283f51997-08-05 02:22:03 +0000272 PyMethod_Fini();
273 PyFrame_Fini();
274 PyCFunction_Fini();
275 PyTuple_Fini();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000276 PyString_Fini();
Guido van Rossumcc283f51997-08-05 02:22:03 +0000277 PyInt_Fini();
278 PyFloat_Fini();
279
Marc-André Lemburg95de5c12002-04-08 08:19:36 +0000280#ifdef Py_USING_UNICODE
281 /* Cleanup Unicode implementation */
282 _PyUnicode_Fini();
283#endif
284
Guido van Rossumcc283f51997-08-05 02:22:03 +0000285 /* XXX Still allocated:
286 - various static ad-hoc pointers to interned strings
287 - int and float free list blocks
288 - whatever various modules and libraries allocate
289 */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000290
291 PyGrammar_RemoveAccelerators(&_PyParser_Grammar);
Guido van Rossumcc283f51997-08-05 02:22:03 +0000292
Tim Peters0e871182002-04-13 08:29:14 +0000293#ifdef PYMALLOC_DEBUG
294 if (Py_GETENV("PYTHONMALLOCSTATS"))
295 _PyObject_DebugMallocStats();
296#endif
297
Guido van Rossumcc283f51997-08-05 02:22:03 +0000298 call_ll_exitfuncs();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000299}
300
301/* Create and initialize a new interpreter and thread, and return the
302 new thread. This requires that Py_Initialize() has been called
303 first.
304
305 Unsuccessful initialization yields a NULL pointer. Note that *no*
306 exception information is available even in this case -- the
307 exception information is held in the thread, and there is no
308 thread.
309
310 Locking: as above.
311
312*/
313
314PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000315Py_NewInterpreter(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000316{
317 PyInterpreterState *interp;
318 PyThreadState *tstate, *save_tstate;
319 PyObject *bimod, *sysmod;
320
321 if (!initialized)
322 Py_FatalError("Py_NewInterpreter: call Py_Initialize first");
323
324 interp = PyInterpreterState_New();
325 if (interp == NULL)
326 return NULL;
327
328 tstate = PyThreadState_New(interp);
329 if (tstate == NULL) {
330 PyInterpreterState_Delete(interp);
331 return NULL;
332 }
333
334 save_tstate = PyThreadState_Swap(tstate);
335
336 /* XXX The following is lax in error checking */
337
338 interp->modules = PyDict_New();
339
340 bimod = _PyImport_FindExtension("__builtin__", "__builtin__");
341 if (bimod != NULL) {
Guido van Rossum4a1f39a1997-11-04 19:36:18 +0000342 interp->builtins = PyModule_GetDict(bimod);
343 Py_INCREF(interp->builtins);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000344 }
345 sysmod = _PyImport_FindExtension("sys", "sys");
346 if (bimod != NULL && sysmod != NULL) {
347 interp->sysdict = PyModule_GetDict(sysmod);
348 Py_INCREF(interp->sysdict);
349 PySys_SetPath(Py_GetPath());
350 PyDict_SetItemString(interp->sysdict, "modules",
351 interp->modules);
Martin v. Löwisd7ceb222003-01-22 09:00:38 +0000352 _PyImportHooks_Init();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000353 initmain();
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000354 if (!Py_NoSiteFlag)
355 initsite();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000356 }
357
358 if (!PyErr_Occurred())
359 return tstate;
360
361 /* Oops, it didn't work. Undo it all. */
362
363 PyErr_Print();
364 PyThreadState_Clear(tstate);
365 PyThreadState_Swap(save_tstate);
366 PyThreadState_Delete(tstate);
367 PyInterpreterState_Delete(interp);
368
369 return NULL;
370}
371
372/* Delete an interpreter and its last thread. This requires that the
373 given thread state is current, that the thread has no remaining
374 frames, and that it is its interpreter's only remaining thread.
375 It is a fatal error to violate these constraints.
376
377 (Py_Finalize() doesn't have these constraints -- it zaps
378 everything, regardless.)
379
380 Locking: as above.
381
382*/
383
384void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000385Py_EndInterpreter(PyThreadState *tstate)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000386{
387 PyInterpreterState *interp = tstate->interp;
388
389 if (tstate != PyThreadState_Get())
390 Py_FatalError("Py_EndInterpreter: thread is not current");
391 if (tstate->frame != NULL)
392 Py_FatalError("Py_EndInterpreter: thread still has a frame");
393 if (tstate != interp->tstate_head || tstate->next != NULL)
394 Py_FatalError("Py_EndInterpreter: not the last thread");
395
396 PyImport_Cleanup();
397 PyInterpreterState_Clear(interp);
398 PyThreadState_Swap(NULL);
399 PyInterpreterState_Delete(interp);
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000400}
401
402static char *progname = "python";
403
404void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000405Py_SetProgramName(char *pn)
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000406{
407 if (pn && *pn)
408 progname = pn;
409}
410
411char *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000412Py_GetProgramName(void)
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000413{
414 return progname;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000415}
416
Guido van Rossuma61691e1998-02-06 22:27:24 +0000417static char *default_home = NULL;
418
419void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000420Py_SetPythonHome(char *home)
Guido van Rossuma61691e1998-02-06 22:27:24 +0000421{
422 default_home = home;
423}
424
425char *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000426Py_GetPythonHome(void)
Guido van Rossuma61691e1998-02-06 22:27:24 +0000427{
428 char *home = default_home;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000429 if (home == NULL && !Py_IgnoreEnvironmentFlag)
430 home = Py_GETENV("PYTHONHOME");
Guido van Rossuma61691e1998-02-06 22:27:24 +0000431 return home;
432}
433
Guido van Rossum6135a871995-01-09 17:53:26 +0000434/* Create __main__ module */
435
436static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000437initmain(void)
Guido van Rossum6135a871995-01-09 17:53:26 +0000438{
Guido van Rossum82598051997-03-05 00:20:32 +0000439 PyObject *m, *d;
440 m = PyImport_AddModule("__main__");
Guido van Rossum6135a871995-01-09 17:53:26 +0000441 if (m == NULL)
Guido van Rossum82598051997-03-05 00:20:32 +0000442 Py_FatalError("can't create __main__ module");
443 d = PyModule_GetDict(m);
444 if (PyDict_GetItemString(d, "__builtins__") == NULL) {
Guido van Rossum858cb731997-11-19 16:15:37 +0000445 PyObject *bimod = PyImport_ImportModule("__builtin__");
446 if (bimod == NULL ||
447 PyDict_SetItemString(d, "__builtins__", bimod) != 0)
Guido van Rossum82598051997-03-05 00:20:32 +0000448 Py_FatalError("can't add __builtins__ to __main__");
Barry Warsaw3d05b1a1999-01-29 21:30:22 +0000449 Py_DECREF(bimod);
Guido van Rossum6135a871995-01-09 17:53:26 +0000450 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000451}
452
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000453/* Import the site module (not into __main__ though) */
454
455static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000456initsite(void)
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000457{
458 PyObject *m, *f;
459 m = PyImport_ImportModule("site");
460 if (m == NULL) {
461 f = PySys_GetObject("stderr");
462 if (Py_VerboseFlag) {
463 PyFile_WriteString(
464 "'import site' failed; traceback:\n", f);
465 PyErr_Print();
466 }
467 else {
468 PyFile_WriteString(
469 "'import site' failed; use -v for traceback\n", f);
470 PyErr_Clear();
471 }
472 }
473 else {
474 Py_DECREF(m);
475 }
476}
477
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000478/* Parse input from a file and execute it */
479
480int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000481PyRun_AnyFile(FILE *fp, const char *filename)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000482{
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000483 return PyRun_AnyFileExFlags(fp, filename, 0, NULL);
484}
485
486int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000487PyRun_AnyFileFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000488{
489 return PyRun_AnyFileExFlags(fp, filename, 0, flags);
Guido van Rossum0df002c2000-08-27 19:21:52 +0000490}
491
492int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000493PyRun_AnyFileEx(FILE *fp, const char *filename, int closeit)
Guido van Rossum0df002c2000-08-27 19:21:52 +0000494{
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000495 return PyRun_AnyFileExFlags(fp, filename, closeit, NULL);
496}
497
498int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000499PyRun_AnyFileExFlags(FILE *fp, const char *filename, int closeit,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000500 PyCompilerFlags *flags)
501{
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000502 if (filename == NULL)
503 filename = "???";
Guido van Rossum0df002c2000-08-27 19:21:52 +0000504 if (Py_FdIsInteractive(fp, filename)) {
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000505 int err = PyRun_InteractiveLoopFlags(fp, filename, flags);
Guido van Rossum0df002c2000-08-27 19:21:52 +0000506 if (closeit)
507 fclose(fp);
508 return err;
509 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000510 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000511 return PyRun_SimpleFileExFlags(fp, filename, closeit, flags);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000512}
513
514int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000515PyRun_InteractiveLoop(FILE *fp, const char *filename)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000516{
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000517 return PyRun_InteractiveLoopFlags(fp, filename, NULL);
518}
519
520int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000521PyRun_InteractiveLoopFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000522{
Guido van Rossum82598051997-03-05 00:20:32 +0000523 PyObject *v;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000524 int ret;
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000525 PyCompilerFlags local_flags;
Jeremy Hylton9f324e92001-03-01 22:59:14 +0000526
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000527 if (flags == NULL) {
528 flags = &local_flags;
Tim Peters5ba58662001-07-16 02:29:45 +0000529 local_flags.cf_flags = 0;
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000530 }
Guido van Rossum82598051997-03-05 00:20:32 +0000531 v = PySys_GetObject("ps1");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000532 if (v == NULL) {
Guido van Rossum82598051997-03-05 00:20:32 +0000533 PySys_SetObject("ps1", v = PyString_FromString(">>> "));
534 Py_XDECREF(v);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000535 }
Guido van Rossum82598051997-03-05 00:20:32 +0000536 v = PySys_GetObject("ps2");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000537 if (v == NULL) {
Guido van Rossum82598051997-03-05 00:20:32 +0000538 PySys_SetObject("ps2", v = PyString_FromString("... "));
539 Py_XDECREF(v);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000540 }
541 for (;;) {
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000542 ret = PyRun_InteractiveOneFlags(fp, filename, flags);
Guido van Rossumaae0d321996-05-22 16:35:33 +0000543#ifdef Py_REF_DEBUG
Guido van Rossum6f9e4331995-03-29 16:57:48 +0000544 fprintf(stderr, "[%ld refs]\n", _Py_RefTotal);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000545#endif
546 if (ret == E_EOF)
547 return 0;
548 /*
549 if (ret == E_NOMEM)
550 return -1;
551 */
552 }
553}
554
555int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000556PyRun_InteractiveOne(FILE *fp, const char *filename)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000557{
Jeremy Hylton9f324e92001-03-01 22:59:14 +0000558 return PyRun_InteractiveOneFlags(fp, filename, NULL);
559}
560
Neil Schemenauerc24ea082002-03-22 23:53:36 +0000561/* compute parser flags based on compiler flags */
Neil Schemenauerc24ea082002-03-22 23:53:36 +0000562#define PARSER_FLAGS(flags) \
Guido van Rossum4b499dd32003-02-13 22:07:59 +0000563 (((flags) && (flags)->cf_flags & PyCF_DONT_IMPLY_DEDENT) ? \
564 PyPARSE_DONT_IMPLY_DEDENT : 0)
Neil Schemenauerc24ea082002-03-22 23:53:36 +0000565
Jeremy Hylton9f324e92001-03-01 22:59:14 +0000566int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000567PyRun_InteractiveOneFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
Jeremy Hylton9f324e92001-03-01 22:59:14 +0000568{
Guido van Rossum82598051997-03-05 00:20:32 +0000569 PyObject *m, *d, *v, *w;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000570 node *n;
Guido van Rossuma110aa61994-08-29 12:50:44 +0000571 perrdetail err;
Guido van Rossumddc3fb51997-11-25 20:58:13 +0000572 char *ps1 = "", *ps2 = "";
Tim Petersfe2127d2001-07-16 05:37:24 +0000573
Guido van Rossum82598051997-03-05 00:20:32 +0000574 v = PySys_GetObject("ps1");
Guido van Rossumddc3fb51997-11-25 20:58:13 +0000575 if (v != NULL) {
576 v = PyObject_Str(v);
577 if (v == NULL)
578 PyErr_Clear();
579 else if (PyString_Check(v))
580 ps1 = PyString_AsString(v);
581 }
Guido van Rossum82598051997-03-05 00:20:32 +0000582 w = PySys_GetObject("ps2");
Guido van Rossumddc3fb51997-11-25 20:58:13 +0000583 if (w != NULL) {
584 w = PyObject_Str(w);
585 if (w == NULL)
586 PyErr_Clear();
587 else if (PyString_Check(w))
588 ps2 = PyString_AsString(w);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000589 }
Tim Petersfe2127d2001-07-16 05:37:24 +0000590 n = PyParser_ParseFileFlags(fp, filename, &_PyParser_Grammar,
591 Py_single_input, ps1, ps2, &err,
Neil Schemenauerc24ea082002-03-22 23:53:36 +0000592 PARSER_FLAGS(flags));
Guido van Rossum82598051997-03-05 00:20:32 +0000593 Py_XDECREF(v);
594 Py_XDECREF(w);
Guido van Rossuma110aa61994-08-29 12:50:44 +0000595 if (n == NULL) {
596 if (err.error == E_EOF) {
597 if (err.text)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000598 PyMem_DEL(err.text);
Guido van Rossuma110aa61994-08-29 12:50:44 +0000599 return E_EOF;
600 }
601 err_input(&err);
Guido van Rossum82598051997-03-05 00:20:32 +0000602 PyErr_Print();
Guido van Rossuma110aa61994-08-29 12:50:44 +0000603 return err.error;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000604 }
Guido van Rossum82598051997-03-05 00:20:32 +0000605 m = PyImport_AddModule("__main__");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000606 if (m == NULL)
607 return -1;
Guido van Rossum82598051997-03-05 00:20:32 +0000608 d = PyModule_GetDict(m);
Jeremy Hylton9f324e92001-03-01 22:59:14 +0000609 v = run_node(n, filename, d, d, flags);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000610 if (v == NULL) {
Guido van Rossum82598051997-03-05 00:20:32 +0000611 PyErr_Print();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000612 return -1;
613 }
Guido van Rossum82598051997-03-05 00:20:32 +0000614 Py_DECREF(v);
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000615 if (Py_FlushLine())
616 PyErr_Clear();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000617 return 0;
618}
619
620int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000621PyRun_SimpleFile(FILE *fp, const char *filename)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000622{
Guido van Rossum0df002c2000-08-27 19:21:52 +0000623 return PyRun_SimpleFileEx(fp, filename, 0);
624}
625
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000626/* Check whether a file maybe a pyc file: Look at the extension,
627 the file type, and, if we may close it, at the first few bytes. */
628
629static int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000630maybe_pyc_file(FILE *fp, const char* filename, const char* ext, int closeit)
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000631{
632 if (strcmp(ext, ".pyc") == 0 || strcmp(ext, ".pyo") == 0)
633 return 1;
634
635#ifdef macintosh
636 /* On a mac, we also assume a pyc file for types 'PYC ' and 'APPL' */
Jack Jansen72f3b7a2002-12-13 15:23:10 +0000637 if (PyMac_getfiletype((char *)filename) == 'PYC '
638 || PyMac_getfiletype((char *)filename) == 'APPL')
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000639 return 1;
640#endif /* macintosh */
641
642 /* Only look into the file if we are allowed to close it, since
643 it then should also be seekable. */
644 if (closeit) {
645 /* Read only two bytes of the magic. If the file was opened in
646 text mode, the bytes 3 and 4 of the magic (\r\n) might not
647 be read as they are on disk. */
648 unsigned int halfmagic = PyImport_GetMagicNumber() & 0xFFFF;
649 unsigned char buf[2];
Tim Peters3e876562001-02-11 04:35:39 +0000650 /* Mess: In case of -x, the stream is NOT at its start now,
651 and ungetc() was used to push back the first newline,
Tim Peters6f5a4ef2001-02-17 22:02:34 +0000652 which makes the current stream position formally undefined,
653 and a x-platform nightmare.
654 Unfortunately, we have no direct way to know whether -x
655 was specified. So we use a terrible hack: if the current
656 stream position is not 0, we assume -x was specified, and
657 give up. Bug 132850 on SourceForge spells out the
658 hopelessness of trying anything else (fseek and ftell
659 don't work predictably x-platform for text-mode files).
Tim Peters3e876562001-02-11 04:35:39 +0000660 */
Tim Peters3e876562001-02-11 04:35:39 +0000661 int ispyc = 0;
Tim Peters6f5a4ef2001-02-17 22:02:34 +0000662 if (ftell(fp) == 0) {
663 if (fread(buf, 1, 2, fp) == 2 &&
664 ((unsigned int)buf[1]<<8 | buf[0]) == halfmagic)
665 ispyc = 1;
666 rewind(fp);
667 }
Tim Peters3e876562001-02-11 04:35:39 +0000668 return ispyc;
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000669 }
670 return 0;
671}
672
Guido van Rossum0df002c2000-08-27 19:21:52 +0000673int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000674PyRun_SimpleFileEx(FILE *fp, const char *filename, int closeit)
Guido van Rossum0df002c2000-08-27 19:21:52 +0000675{
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000676 return PyRun_SimpleFileExFlags(fp, filename, closeit, NULL);
677}
678
679int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000680PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000681 PyCompilerFlags *flags)
682{
Guido van Rossum82598051997-03-05 00:20:32 +0000683 PyObject *m, *d, *v;
Martin v. Löwis95292d62002-12-11 14:04:59 +0000684 const char *ext;
Guido van Rossumfdef2711994-09-14 13:31:04 +0000685
Guido van Rossum82598051997-03-05 00:20:32 +0000686 m = PyImport_AddModule("__main__");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000687 if (m == NULL)
688 return -1;
Guido van Rossum82598051997-03-05 00:20:32 +0000689 d = PyModule_GetDict(m);
Fred Drake8ed02042002-10-17 21:24:58 +0000690 if (PyDict_GetItemString(d, "__file__") == NULL) {
691 PyObject *f = PyString_FromString(filename);
692 if (f == NULL)
693 return -1;
694 if (PyDict_SetItemString(d, "__file__", f) < 0) {
695 Py_DECREF(f);
696 return -1;
697 }
698 Py_DECREF(f);
699 }
Guido van Rossumfdef2711994-09-14 13:31:04 +0000700 ext = filename + strlen(filename) - 4;
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000701 if (maybe_pyc_file(fp, filename, ext, closeit)) {
Guido van Rossumfdef2711994-09-14 13:31:04 +0000702 /* Try to run a pyc file. First, re-open in binary */
Guido van Rossum0df002c2000-08-27 19:21:52 +0000703 if (closeit)
704 fclose(fp);
Fred Drake8ed02042002-10-17 21:24:58 +0000705 if ((fp = fopen(filename, "rb")) == NULL) {
Guido van Rossumfdef2711994-09-14 13:31:04 +0000706 fprintf(stderr, "python: Can't reopen .pyc file\n");
707 return -1;
708 }
Guido van Rossum2a7f58d1997-04-02 05:28:38 +0000709 /* Turn on optimization if a .pyo file is given */
710 if (strcmp(ext, ".pyo") == 0)
711 Py_OptimizeFlag = 1;
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000712 v = run_pyc_file(fp, filename, d, d, flags);
Guido van Rossumfdef2711994-09-14 13:31:04 +0000713 } else {
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000714 v = PyRun_FileExFlags(fp, filename, Py_file_input, d, d,
715 closeit, flags);
Guido van Rossumfdef2711994-09-14 13:31:04 +0000716 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000717 if (v == NULL) {
Guido van Rossum82598051997-03-05 00:20:32 +0000718 PyErr_Print();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000719 return -1;
720 }
Guido van Rossum82598051997-03-05 00:20:32 +0000721 Py_DECREF(v);
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000722 if (Py_FlushLine())
723 PyErr_Clear();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000724 return 0;
725}
726
727int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000728PyRun_SimpleString(const char *command)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000729{
Guido van Rossum393661d2001-08-31 17:40:15 +0000730 return PyRun_SimpleStringFlags(command, NULL);
731}
732
733int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000734PyRun_SimpleStringFlags(const char *command, PyCompilerFlags *flags)
Guido van Rossum393661d2001-08-31 17:40:15 +0000735{
Guido van Rossum82598051997-03-05 00:20:32 +0000736 PyObject *m, *d, *v;
737 m = PyImport_AddModule("__main__");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000738 if (m == NULL)
739 return -1;
Guido van Rossum82598051997-03-05 00:20:32 +0000740 d = PyModule_GetDict(m);
Guido van Rossum393661d2001-08-31 17:40:15 +0000741 v = PyRun_StringFlags(command, Py_file_input, d, d, flags);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000742 if (v == NULL) {
Guido van Rossum82598051997-03-05 00:20:32 +0000743 PyErr_Print();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000744 return -1;
745 }
Guido van Rossum82598051997-03-05 00:20:32 +0000746 Py_DECREF(v);
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000747 if (Py_FlushLine())
748 PyErr_Clear();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000749 return 0;
750}
751
Barry Warsaw035574d1997-08-29 22:07:17 +0000752static int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000753parse_syntax_error(PyObject *err, PyObject **message, const char **filename,
754 int *lineno, int *offset, const char **text)
Barry Warsaw035574d1997-08-29 22:07:17 +0000755{
756 long hold;
757 PyObject *v;
758
759 /* old style errors */
760 if (PyTuple_Check(err))
Neal Norwitz78293352002-04-01 01:41:20 +0000761 return PyArg_ParseTuple(err, "O(ziiz)", message, filename,
762 lineno, offset, text);
Barry Warsaw035574d1997-08-29 22:07:17 +0000763
764 /* new style errors. `err' is an instance */
765
766 if (! (v = PyObject_GetAttrString(err, "msg")))
767 goto finally;
768 *message = v;
769
770 if (!(v = PyObject_GetAttrString(err, "filename")))
771 goto finally;
772 if (v == Py_None)
773 *filename = NULL;
774 else if (! (*filename = PyString_AsString(v)))
775 goto finally;
776
777 Py_DECREF(v);
778 if (!(v = PyObject_GetAttrString(err, "lineno")))
779 goto finally;
780 hold = PyInt_AsLong(v);
781 Py_DECREF(v);
782 v = NULL;
783 if (hold < 0 && PyErr_Occurred())
784 goto finally;
785 *lineno = (int)hold;
786
787 if (!(v = PyObject_GetAttrString(err, "offset")))
788 goto finally;
Jeremy Hylton9f1b9932001-02-28 07:07:43 +0000789 if (v == Py_None) {
790 *offset = -1;
791 Py_DECREF(v);
792 v = NULL;
793 } else {
794 hold = PyInt_AsLong(v);
795 Py_DECREF(v);
796 v = NULL;
797 if (hold < 0 && PyErr_Occurred())
798 goto finally;
799 *offset = (int)hold;
800 }
Barry Warsaw035574d1997-08-29 22:07:17 +0000801
802 if (!(v = PyObject_GetAttrString(err, "text")))
803 goto finally;
804 if (v == Py_None)
805 *text = NULL;
806 else if (! (*text = PyString_AsString(v)))
807 goto finally;
808 Py_DECREF(v);
809 return 1;
810
811finally:
812 Py_XDECREF(v);
813 return 0;
814}
815
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000816void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000817PyErr_Print(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000818{
Guido van Rossuma61691e1998-02-06 22:27:24 +0000819 PyErr_PrintEx(1);
820}
821
Jeremy Hylton9f1b9932001-02-28 07:07:43 +0000822static void
Martin v. Löwis95292d62002-12-11 14:04:59 +0000823print_error_text(PyObject *f, int offset, const char *text)
Jeremy Hylton9f1b9932001-02-28 07:07:43 +0000824{
825 char *nl;
826 if (offset >= 0) {
827 if (offset > 0 && offset == (int)strlen(text))
828 offset--;
829 for (;;) {
830 nl = strchr(text, '\n');
831 if (nl == NULL || nl-text >= offset)
832 break;
833 offset -= (nl+1-text);
834 text = nl+1;
835 }
836 while (*text == ' ' || *text == '\t') {
837 text++;
838 offset--;
839 }
840 }
841 PyFile_WriteString(" ", f);
842 PyFile_WriteString(text, f);
843 if (*text == '\0' || text[strlen(text)-1] != '\n')
844 PyFile_WriteString("\n", f);
845 if (offset == -1)
846 return;
847 PyFile_WriteString(" ", f);
848 offset--;
849 while (offset > 0) {
850 PyFile_WriteString(" ", f);
851 offset--;
852 }
853 PyFile_WriteString("^\n", f);
854}
855
Guido van Rossum66e8e862001-03-23 17:54:43 +0000856static void
857handle_system_exit(void)
Ka-Ping Yee26fabb02001-03-23 15:36:41 +0000858{
Guido van Rossum66e8e862001-03-23 17:54:43 +0000859 PyObject *exception, *value, *tb;
860 PyErr_Fetch(&exception, &value, &tb);
Ka-Ping Yee26fabb02001-03-23 15:36:41 +0000861 if (Py_FlushLine())
862 PyErr_Clear();
863 fflush(stdout);
864 if (value == NULL || value == Py_None)
865 Py_Exit(0);
866 if (PyInstance_Check(value)) {
867 /* The error code should be in the `code' attribute. */
868 PyObject *code = PyObject_GetAttrString(value, "code");
869 if (code) {
870 Py_DECREF(value);
871 value = code;
872 if (value == Py_None)
873 Py_Exit(0);
874 }
875 /* If we failed to dig out the 'code' attribute,
876 just let the else clause below print the error. */
877 }
878 if (PyInt_Check(value))
879 Py_Exit((int)PyInt_AsLong(value));
880 else {
881 PyObject_Print(value, stderr, Py_PRINT_RAW);
882 PySys_WriteStderr("\n");
883 Py_Exit(1);
884 }
885}
886
887void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000888PyErr_PrintEx(int set_sys_last_vars)
Guido van Rossuma61691e1998-02-06 22:27:24 +0000889{
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000890 PyObject *exception, *v, *tb, *hook;
Guido van Rossum66e8e862001-03-23 17:54:43 +0000891
892 if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
893 handle_system_exit();
894 }
Guido van Rossum82598051997-03-05 00:20:32 +0000895 PyErr_Fetch(&exception, &v, &tb);
Barry Warsaw035574d1997-08-29 22:07:17 +0000896 PyErr_NormalizeException(&exception, &v, &tb);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000897 if (exception == NULL)
Guido van Rossum296b4751997-05-23 00:19:20 +0000898 return;
Guido van Rossuma61691e1998-02-06 22:27:24 +0000899 if (set_sys_last_vars) {
900 PySys_SetObject("last_type", exception);
901 PySys_SetObject("last_value", v);
902 PySys_SetObject("last_traceback", tb);
903 }
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000904 hook = PySys_GetObject("excepthook");
905 if (hook) {
906 PyObject *args = Py_BuildValue("(OOO)",
907 exception, v ? v : Py_None, tb ? tb : Py_None);
908 PyObject *result = PyEval_CallObject(hook, args);
909 if (result == NULL) {
910 PyObject *exception2, *v2, *tb2;
Guido van Rossum66e8e862001-03-23 17:54:43 +0000911 if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
912 handle_system_exit();
913 }
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000914 PyErr_Fetch(&exception2, &v2, &tb2);
915 PyErr_NormalizeException(&exception2, &v2, &tb2);
916 if (Py_FlushLine())
917 PyErr_Clear();
918 fflush(stdout);
919 PySys_WriteStderr("Error in sys.excepthook:\n");
920 PyErr_Display(exception2, v2, tb2);
921 PySys_WriteStderr("\nOriginal exception was:\n");
922 PyErr_Display(exception, v, tb);
Jeremy Hylton07028582001-12-07 15:35:35 +0000923 Py_XDECREF(exception2);
924 Py_XDECREF(v2);
925 Py_XDECREF(tb2);
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000926 }
927 Py_XDECREF(result);
928 Py_XDECREF(args);
929 } else {
930 PySys_WriteStderr("sys.excepthook is missing\n");
931 PyErr_Display(exception, v, tb);
932 }
933 Py_XDECREF(exception);
934 Py_XDECREF(v);
935 Py_XDECREF(tb);
936}
937
938void PyErr_Display(PyObject *exception, PyObject *value, PyObject *tb)
939{
940 int err = 0;
941 PyObject *v = value;
942 PyObject *f = PySys_GetObject("stderr");
Guido van Rossum3165fe61992-09-25 21:59:05 +0000943 if (f == NULL)
944 fprintf(stderr, "lost sys.stderr\n");
945 else {
Guido van Rossum0829c751998-02-28 04:31:39 +0000946 if (Py_FlushLine())
947 PyErr_Clear();
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000948 fflush(stdout);
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000949 if (tb && tb != Py_None)
950 err = PyTraceBack_Print(tb, f);
Barry Warsaw36b8f941997-08-26 18:09:48 +0000951 if (err == 0 &&
Martin v. Löwiscfeb3b62002-03-03 21:30:27 +0000952 PyObject_HasAttrString(v, "print_file_and_line"))
Barry Warsaw36b8f941997-08-26 18:09:48 +0000953 {
Guido van Rossum82598051997-03-05 00:20:32 +0000954 PyObject *message;
Martin v. Löwis95292d62002-12-11 14:04:59 +0000955 const char *filename, *text;
Guido van Rossuma110aa61994-08-29 12:50:44 +0000956 int lineno, offset;
Barry Warsaw035574d1997-08-29 22:07:17 +0000957 if (!parse_syntax_error(v, &message, &filename,
958 &lineno, &offset, &text))
Guido van Rossum82598051997-03-05 00:20:32 +0000959 PyErr_Clear();
Guido van Rossuma110aa61994-08-29 12:50:44 +0000960 else {
961 char buf[10];
Guido van Rossum82598051997-03-05 00:20:32 +0000962 PyFile_WriteString(" File \"", f);
Guido van Rossuma110aa61994-08-29 12:50:44 +0000963 if (filename == NULL)
Guido van Rossum82598051997-03-05 00:20:32 +0000964 PyFile_WriteString("<string>", f);
Guido van Rossuma110aa61994-08-29 12:50:44 +0000965 else
Guido van Rossum82598051997-03-05 00:20:32 +0000966 PyFile_WriteString(filename, f);
967 PyFile_WriteString("\", line ", f);
Jeremy Hylton518ab1c2001-11-28 20:42:20 +0000968 PyOS_snprintf(buf, sizeof(buf), "%d", lineno);
Guido van Rossum82598051997-03-05 00:20:32 +0000969 PyFile_WriteString(buf, f);
970 PyFile_WriteString("\n", f);
Jeremy Hylton9f1b9932001-02-28 07:07:43 +0000971 if (text != NULL)
972 print_error_text(f, offset, text);
Guido van Rossuma110aa61994-08-29 12:50:44 +0000973 v = message;
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000974 /* Can't be bothered to check all those
975 PyFile_WriteString() calls */
976 if (PyErr_Occurred())
977 err = -1;
Guido van Rossuma110aa61994-08-29 12:50:44 +0000978 }
979 }
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000980 if (err) {
981 /* Don't do anything else */
982 }
983 else if (PyClass_Check(exception)) {
Barry Warsaw2f5f6a21997-09-16 21:42:03 +0000984 PyClassObject* exc = (PyClassObject*)exception;
985 PyObject* className = exc->cl_name;
986 PyObject* moduleName =
987 PyDict_GetItemString(exc->cl_dict, "__module__");
988
989 if (moduleName == NULL)
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000990 err = PyFile_WriteString("<unknown>", f);
Barry Warsaw2f5f6a21997-09-16 21:42:03 +0000991 else {
992 char* modstr = PyString_AsString(moduleName);
993 if (modstr && strcmp(modstr, "exceptions"))
994 {
995 err = PyFile_WriteString(modstr, f);
996 err += PyFile_WriteString(".", f);
997 }
998 }
999 if (err == 0) {
1000 if (className == NULL)
1001 err = PyFile_WriteString("<unknown>", f);
1002 else
1003 err = PyFile_WriteObject(className, f,
1004 Py_PRINT_RAW);
1005 }
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001006 }
1007 else
1008 err = PyFile_WriteObject(exception, f, Py_PRINT_RAW);
1009 if (err == 0) {
1010 if (v != NULL && v != Py_None) {
Barry Warsaw035574d1997-08-29 22:07:17 +00001011 PyObject *s = PyObject_Str(v);
1012 /* only print colon if the str() of the
1013 object is not the empty string
1014 */
Guido van Rossumd6bf45b1997-09-05 19:11:53 +00001015 if (s == NULL)
1016 err = -1;
1017 else if (!PyString_Check(s) ||
1018 PyString_GET_SIZE(s) != 0)
Barry Warsaw035574d1997-08-29 22:07:17 +00001019 err = PyFile_WriteString(": ", f);
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001020 if (err == 0)
Guido van Rossumd6bf45b1997-09-05 19:11:53 +00001021 err = PyFile_WriteObject(s, f, Py_PRINT_RAW);
1022 Py_XDECREF(s);
Guido van Rossum262e1241995-02-07 15:30:45 +00001023 }
Guido van Rossum262e1241995-02-07 15:30:45 +00001024 }
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001025 if (err == 0)
1026 err = PyFile_WriteString("\n", f);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001027 }
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001028 /* If an error happened here, don't show it.
1029 XXX This is wrong, but too many callers rely on this behavior. */
1030 if (err != 0)
1031 PyErr_Clear();
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001032}
1033
Guido van Rossum82598051997-03-05 00:20:32 +00001034PyObject *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001035PyRun_String(const char *str, int start, PyObject *globals, PyObject *locals)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001036{
Guido van Rossum82598051997-03-05 00:20:32 +00001037 return run_err_node(PyParser_SimpleParseString(str, start),
Jeremy Hylton9f324e92001-03-01 22:59:14 +00001038 "<string>", globals, locals, NULL);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001039}
1040
Guido van Rossum82598051997-03-05 00:20:32 +00001041PyObject *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001042PyRun_File(FILE *fp, const char *filename, int start, PyObject *globals,
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001043 PyObject *locals)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001044{
Tim Peterse8682112000-08-27 20:18:17 +00001045 return PyRun_FileEx(fp, filename, start, globals, locals, 0);
Guido van Rossum0df002c2000-08-27 19:21:52 +00001046}
1047
1048PyObject *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001049PyRun_FileEx(FILE *fp, const char *filename, int start, PyObject *globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001050 PyObject *locals, int closeit)
Guido van Rossum0df002c2000-08-27 19:21:52 +00001051{
1052 node *n = PyParser_SimpleParseFile(fp, filename, start);
1053 if (closeit)
1054 fclose(fp);
Jeremy Hylton9f324e92001-03-01 22:59:14 +00001055 return run_err_node(n, filename, globals, locals, NULL);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001056}
1057
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001058PyObject *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001059PyRun_StringFlags(const char *str, int start, PyObject *globals, PyObject *locals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001060 PyCompilerFlags *flags)
1061{
Guido van Rossuma1b3a472001-07-16 16:51:33 +00001062 return run_err_node(PyParser_SimpleParseStringFlags(
Neil Schemenauerc24ea082002-03-22 23:53:36 +00001063 str, start, PARSER_FLAGS(flags)),
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001064 "<string>", globals, locals, flags);
1065}
1066
1067PyObject *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001068PyRun_FileFlags(FILE *fp, const char *filename, int start, PyObject *globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001069 PyObject *locals, PyCompilerFlags *flags)
1070{
1071 return PyRun_FileExFlags(fp, filename, start, globals, locals, 0,
1072 flags);
1073}
1074
1075PyObject *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001076PyRun_FileExFlags(FILE *fp, const char *filename, int start, PyObject *globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001077 PyObject *locals, int closeit, PyCompilerFlags *flags)
1078{
Tim Petersfe2127d2001-07-16 05:37:24 +00001079 node *n = PyParser_SimpleParseFileFlags(fp, filename, start,
Neil Schemenauerc24ea082002-03-22 23:53:36 +00001080 PARSER_FLAGS(flags));
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001081 if (closeit)
1082 fclose(fp);
1083 return run_err_node(n, filename, globals, locals, flags);
1084}
1085
Guido van Rossum82598051997-03-05 00:20:32 +00001086static PyObject *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001087run_err_node(node *n, const char *filename, PyObject *globals, PyObject *locals,
Jeremy Hylton9f324e92001-03-01 22:59:14 +00001088 PyCompilerFlags *flags)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001089{
Guido van Rossuma110aa61994-08-29 12:50:44 +00001090 if (n == NULL)
1091 return NULL;
Jeremy Hylton9f324e92001-03-01 22:59:14 +00001092 return run_node(n, filename, globals, locals, flags);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001093}
1094
Guido van Rossum82598051997-03-05 00:20:32 +00001095static PyObject *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001096run_node(node *n, const char *filename, PyObject *globals, PyObject *locals,
Jeremy Hylton9f324e92001-03-01 22:59:14 +00001097 PyCompilerFlags *flags)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001098{
Guido van Rossum82598051997-03-05 00:20:32 +00001099 PyCodeObject *co;
1100 PyObject *v;
Jeremy Hylton9f324e92001-03-01 22:59:14 +00001101 co = PyNode_CompileFlags(n, filename, flags);
Guido van Rossum82598051997-03-05 00:20:32 +00001102 PyNode_Free(n);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001103 if (co == NULL)
1104 return NULL;
Guido van Rossum82598051997-03-05 00:20:32 +00001105 v = PyEval_EvalCode(co, globals, locals);
1106 Py_DECREF(co);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001107 return v;
1108}
1109
Guido van Rossum82598051997-03-05 00:20:32 +00001110static PyObject *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001111run_pyc_file(FILE *fp, const char *filename, PyObject *globals, PyObject *locals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001112 PyCompilerFlags *flags)
Guido van Rossumfdef2711994-09-14 13:31:04 +00001113{
Guido van Rossum82598051997-03-05 00:20:32 +00001114 PyCodeObject *co;
1115 PyObject *v;
Guido van Rossumfdef2711994-09-14 13:31:04 +00001116 long magic;
Fred Drakee8de31c2000-08-31 05:38:39 +00001117 long PyImport_GetMagicNumber(void);
Guido van Rossumfdef2711994-09-14 13:31:04 +00001118
Guido van Rossum82598051997-03-05 00:20:32 +00001119 magic = PyMarshal_ReadLongFromFile(fp);
1120 if (magic != PyImport_GetMagicNumber()) {
1121 PyErr_SetString(PyExc_RuntimeError,
Guido van Rossumfdef2711994-09-14 13:31:04 +00001122 "Bad magic number in .pyc file");
1123 return NULL;
1124 }
Guido van Rossum82598051997-03-05 00:20:32 +00001125 (void) PyMarshal_ReadLongFromFile(fp);
Tim Petersd9b9ac82001-01-28 00:27:39 +00001126 v = PyMarshal_ReadLastObjectFromFile(fp);
Guido van Rossumfdef2711994-09-14 13:31:04 +00001127 fclose(fp);
Guido van Rossum82598051997-03-05 00:20:32 +00001128 if (v == NULL || !PyCode_Check(v)) {
1129 Py_XDECREF(v);
1130 PyErr_SetString(PyExc_RuntimeError,
Guido van Rossumfdef2711994-09-14 13:31:04 +00001131 "Bad code object in .pyc file");
1132 return NULL;
1133 }
Guido van Rossum82598051997-03-05 00:20:32 +00001134 co = (PyCodeObject *)v;
1135 v = PyEval_EvalCode(co, globals, locals);
Jeremy Hyltonb857ba22001-08-10 21:41:33 +00001136 if (v && flags)
1137 flags->cf_flags |= (co->co_flags & PyCF_MASK);
Guido van Rossum82598051997-03-05 00:20:32 +00001138 Py_DECREF(co);
Guido van Rossumfdef2711994-09-14 13:31:04 +00001139 return v;
1140}
1141
Guido van Rossum82598051997-03-05 00:20:32 +00001142PyObject *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001143Py_CompileString(const char *str, const char *filename, int start)
Guido van Rossum5b722181993-03-30 17:46:03 +00001144{
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001145 return Py_CompileStringFlags(str, filename, start, NULL);
1146}
1147
1148PyObject *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001149Py_CompileStringFlags(const char *str, const char *filename, int start,
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001150 PyCompilerFlags *flags)
1151{
Guido van Rossum5b722181993-03-30 17:46:03 +00001152 node *n;
Guido van Rossum82598051997-03-05 00:20:32 +00001153 PyCodeObject *co;
Thomas Heller6b17abf2002-07-09 09:23:27 +00001154
1155 n = PyParser_SimpleParseStringFlagsFilename(str, filename, start,
1156 PARSER_FLAGS(flags));
Guido van Rossuma110aa61994-08-29 12:50:44 +00001157 if (n == NULL)
Guido van Rossum5b722181993-03-30 17:46:03 +00001158 return NULL;
Jeremy Hylton673a4fd2001-03-26 19:53:38 +00001159 co = PyNode_CompileFlags(n, filename, flags);
Guido van Rossum82598051997-03-05 00:20:32 +00001160 PyNode_Free(n);
1161 return (PyObject *)co;
Guido van Rossum5b722181993-03-30 17:46:03 +00001162}
1163
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001164struct symtable *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001165Py_SymtableString(const char *str, const char *filename, int start)
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001166{
1167 node *n;
1168 struct symtable *st;
Thomas Heller6b17abf2002-07-09 09:23:27 +00001169 n = PyParser_SimpleParseStringFlagsFilename(str, filename,
1170 start, 0);
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001171 if (n == NULL)
1172 return NULL;
1173 st = PyNode_CompileSymtable(n, filename);
1174 PyNode_Free(n);
1175 return st;
1176}
1177
Guido van Rossuma110aa61994-08-29 12:50:44 +00001178/* Simplified interface to parsefile -- return node or set exception */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001179
Guido van Rossuma110aa61994-08-29 12:50:44 +00001180node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001181PyParser_SimpleParseFileFlags(FILE *fp, const char *filename, int start, int flags)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001182{
Guido van Rossuma110aa61994-08-29 12:50:44 +00001183 node *n;
1184 perrdetail err;
Tim Petersfe2127d2001-07-16 05:37:24 +00001185 n = PyParser_ParseFileFlags(fp, filename, &_PyParser_Grammar, start,
1186 (char *)0, (char *)0, &err, flags);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001187 if (n == NULL)
1188 err_input(&err);
1189 return n;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001190}
1191
Tim Petersfe2127d2001-07-16 05:37:24 +00001192node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001193PyParser_SimpleParseFile(FILE *fp, const char *filename, int start)
Tim Petersfe2127d2001-07-16 05:37:24 +00001194{
1195 return PyParser_SimpleParseFileFlags(fp, filename, start, 0);
1196}
1197
Guido van Rossuma110aa61994-08-29 12:50:44 +00001198/* Simplified interface to parsestring -- return node or set exception */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001199
Guido van Rossuma110aa61994-08-29 12:50:44 +00001200node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001201PyParser_SimpleParseStringFlags(const char *str, int start, int flags)
Tim Petersfe2127d2001-07-16 05:37:24 +00001202{
1203 node *n;
1204 perrdetail err;
1205 n = PyParser_ParseStringFlags(str, &_PyParser_Grammar, start, &err,
1206 flags);
1207 if (n == NULL)
1208 err_input(&err);
1209 return n;
1210}
1211
1212node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001213PyParser_SimpleParseString(const char *str, int start)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001214{
Tim Petersfe2127d2001-07-16 05:37:24 +00001215 return PyParser_SimpleParseStringFlags(str, start, 0);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001216}
1217
Thomas Heller6b17abf2002-07-09 09:23:27 +00001218node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001219PyParser_SimpleParseStringFlagsFilename(const char *str, const char *filename,
Thomas Heller6b17abf2002-07-09 09:23:27 +00001220 int start, int flags)
1221{
1222 node *n;
1223 perrdetail err;
1224
1225 n = PyParser_ParseStringFlagsFilename(str, filename,
1226 &_PyParser_Grammar,
1227 start, &err, flags);
1228 if (n == NULL)
1229 err_input(&err);
1230 return n;
1231}
1232
1233node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001234PyParser_SimpleParseStringFilename(const char *str, const char *filename, int start)
Thomas Heller6b17abf2002-07-09 09:23:27 +00001235{
1236 return PyParser_SimpleParseStringFlagsFilename(str, filename,
1237 start, 0);
1238}
1239
Guido van Rossuma110aa61994-08-29 12:50:44 +00001240/* Set the error appropriate to the given input error code (see errcode.h) */
1241
1242static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001243err_input(perrdetail *err)
Guido van Rossuma110aa61994-08-29 12:50:44 +00001244{
Fred Drake85f36392000-07-11 17:53:00 +00001245 PyObject *v, *w, *errtype;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001246 PyObject* u = NULL;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001247 char *msg = NULL;
Fred Drake85f36392000-07-11 17:53:00 +00001248 errtype = PyExc_SyntaxError;
Guido van Rossum82598051997-03-05 00:20:32 +00001249 v = Py_BuildValue("(ziiz)", err->filename,
Guido van Rossuma110aa61994-08-29 12:50:44 +00001250 err->lineno, err->offset, err->text);
1251 if (err->text != NULL) {
Guido van Rossumb18618d2000-05-03 23:44:39 +00001252 PyMem_DEL(err->text);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001253 err->text = NULL;
1254 }
1255 switch (err->error) {
1256 case E_SYNTAX:
Fred Drake85f36392000-07-11 17:53:00 +00001257 errtype = PyExc_IndentationError;
1258 if (err->expected == INDENT)
1259 msg = "expected an indented block";
1260 else if (err->token == INDENT)
1261 msg = "unexpected indent";
1262 else if (err->token == DEDENT)
1263 msg = "unexpected unindent";
1264 else {
1265 errtype = PyExc_SyntaxError;
1266 msg = "invalid syntax";
1267 }
Guido van Rossuma110aa61994-08-29 12:50:44 +00001268 break;
1269 case E_TOKEN:
1270 msg = "invalid token";
Guido van Rossuma110aa61994-08-29 12:50:44 +00001271 break;
Skip Montanaro118ec702002-08-15 01:20:16 +00001272 case E_EOFS:
1273 msg = "EOF while scanning triple-quoted string";
1274 break;
1275 case E_EOLS:
1276 msg = "EOL while scanning single-quoted string";
1277 break;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001278 case E_INTR:
Guido van Rossum82598051997-03-05 00:20:32 +00001279 PyErr_SetNone(PyExc_KeyboardInterrupt);
Barry Warsawc80baa31999-01-27 16:39:40 +00001280 Py_XDECREF(v);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001281 return;
1282 case E_NOMEM:
Guido van Rossum82598051997-03-05 00:20:32 +00001283 PyErr_NoMemory();
Barry Warsawc80baa31999-01-27 16:39:40 +00001284 Py_XDECREF(v);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001285 return;
1286 case E_EOF:
1287 msg = "unexpected EOF while parsing";
1288 break;
Fred Drake85f36392000-07-11 17:53:00 +00001289 case E_TABSPACE:
1290 errtype = PyExc_TabError;
Guido van Rossum560e8ad1998-04-10 19:43:42 +00001291 msg = "inconsistent use of tabs and spaces in indentation";
1292 break;
Jeremy Hylton94988062000-06-20 19:10:44 +00001293 case E_OVERFLOW:
1294 msg = "expression too long";
1295 break;
Fred Drake85f36392000-07-11 17:53:00 +00001296 case E_DEDENT:
1297 errtype = PyExc_IndentationError;
1298 msg = "unindent does not match any outer indentation level";
1299 break;
1300 case E_TOODEEP:
1301 errtype = PyExc_IndentationError;
1302 msg = "too many levels of indentation";
1303 break;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001304 case E_DECODE: { /* XXX */
1305 PyThreadState* tstate = PyThreadState_Get();
1306 PyObject* value = tstate->curexc_value;
1307 if (value != NULL) {
1308 u = PyObject_Repr(value);
1309 if (u != NULL) {
1310 msg = PyString_AsString(u);
1311 break;
1312 }
1313 }
1314 }
Guido van Rossuma110aa61994-08-29 12:50:44 +00001315 default:
1316 fprintf(stderr, "error=%d\n", err->error);
1317 msg = "unknown parsing error";
1318 break;
1319 }
Guido van Rossum82598051997-03-05 00:20:32 +00001320 w = Py_BuildValue("(sO)", msg, v);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001321 Py_XDECREF(u);
Guido van Rossum41318302001-03-23 04:01:07 +00001322 Py_XDECREF(v);
Fred Drake85f36392000-07-11 17:53:00 +00001323 PyErr_SetObject(errtype, w);
Guido van Rossum82598051997-03-05 00:20:32 +00001324 Py_XDECREF(w);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001325}
1326
1327/* Print fatal error message and abort */
1328
1329void
Tim Peters7c321a82002-07-09 02:57:01 +00001330Py_FatalError(const char *msg)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001331{
Guido van Rossum83dd6c31994-09-29 09:38:33 +00001332 fprintf(stderr, "Fatal Python error: %s\n", msg);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001333#ifdef MS_WINDOWS
Guido van Rossum23c94461997-05-22 20:21:30 +00001334 OutputDebugString("Fatal Python error: ");
Guido van Rossuma44823b1995-03-14 15:01:17 +00001335 OutputDebugString(msg);
1336 OutputDebugString("\n");
Guido van Rossum0ba35361998-08-13 13:33:16 +00001337#ifdef _DEBUG
1338 DebugBreak();
Guido van Rossuma44823b1995-03-14 15:01:17 +00001339#endif
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001340#endif /* MS_WINDOWS */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001341 abort();
1342}
1343
1344/* Clean up and exit */
1345
Guido van Rossuma110aa61994-08-29 12:50:44 +00001346#ifdef WITH_THREAD
Guido van Rossum49b56061998-10-01 20:42:43 +00001347#include "pythread.h"
Guido van Rossum82598051997-03-05 00:20:32 +00001348int _PyThread_Started = 0; /* Set by threadmodule.c and maybe others */
Guido van Rossumf9f2e821992-08-17 08:59:08 +00001349#endif
1350
Guido van Rossum2dcfc961998-10-01 16:01:57 +00001351#define NEXITFUNCS 32
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001352static void (*exitfuncs[NEXITFUNCS])(void);
Guido van Rossum1662dd51994-09-07 14:38:28 +00001353static int nexitfuncs = 0;
1354
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001355int Py_AtExit(void (*func)(void))
Guido van Rossum1662dd51994-09-07 14:38:28 +00001356{
1357 if (nexitfuncs >= NEXITFUNCS)
1358 return -1;
1359 exitfuncs[nexitfuncs++] = func;
1360 return 0;
1361}
1362
Guido van Rossumcc283f51997-08-05 02:22:03 +00001363static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001364call_sys_exitfunc(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001365{
Guido van Rossum82598051997-03-05 00:20:32 +00001366 PyObject *exitfunc = PySys_GetObject("exitfunc");
Guido van Rossum59bff391992-09-03 20:28:00 +00001367
1368 if (exitfunc) {
Fred Drake6a12d8d2001-03-23 17:34:02 +00001369 PyObject *res;
Guido van Rossum82598051997-03-05 00:20:32 +00001370 Py_INCREF(exitfunc);
1371 PySys_SetObject("exitfunc", (PyObject *)NULL);
1372 res = PyEval_CallObject(exitfunc, (PyObject *)NULL);
Guido van Rossum59bff391992-09-03 20:28:00 +00001373 if (res == NULL) {
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001374 if (!PyErr_ExceptionMatches(PyExc_SystemExit)) {
1375 PySys_WriteStderr("Error in sys.exitfunc:\n");
1376 }
Guido van Rossum82598051997-03-05 00:20:32 +00001377 PyErr_Print();
Guido van Rossum59bff391992-09-03 20:28:00 +00001378 }
Guido van Rossum82598051997-03-05 00:20:32 +00001379 Py_DECREF(exitfunc);
Guido van Rossum59bff391992-09-03 20:28:00 +00001380 }
1381
Guido van Rossum0829c751998-02-28 04:31:39 +00001382 if (Py_FlushLine())
1383 PyErr_Clear();
Guido van Rossumcc283f51997-08-05 02:22:03 +00001384}
Guido van Rossum1662dd51994-09-07 14:38:28 +00001385
Guido van Rossumcc283f51997-08-05 02:22:03 +00001386static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001387call_ll_exitfuncs(void)
Guido van Rossumcc283f51997-08-05 02:22:03 +00001388{
Guido van Rossum1662dd51994-09-07 14:38:28 +00001389 while (nexitfuncs > 0)
1390 (*exitfuncs[--nexitfuncs])();
Guido van Rossum25ce5661997-08-02 03:10:38 +00001391
1392 fflush(stdout);
1393 fflush(stderr);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001394}
1395
1396void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001397Py_Exit(int sts)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001398{
Guido van Rossumcc283f51997-08-05 02:22:03 +00001399 Py_Finalize();
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001400
Jack Jansen66a89771995-10-27 13:22:14 +00001401#ifdef macintosh
1402 PyMac_Exit(sts);
1403#else
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001404 exit(sts);
Jack Jansen66a89771995-10-27 13:22:14 +00001405#endif
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001406}
1407
Guido van Rossumf1dc5661993-07-05 10:31:29 +00001408static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001409initsigs(void)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001410{
Guido van Rossuma110aa61994-08-29 12:50:44 +00001411#ifdef HAVE_SIGNAL_H
1412#ifdef SIGPIPE
1413 signal(SIGPIPE, SIG_IGN);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001414#endif
Guido van Rossum70d893a2001-08-16 08:21:42 +00001415#ifdef SIGXFZ
1416 signal(SIGXFZ, SIG_IGN);
1417#endif
Jeremy Hylton1b0bf9b2002-04-23 20:31:01 +00001418#ifdef SIGXFSZ
1419 signal(SIGXFSZ, SIG_IGN);
1420#endif
Guido van Rossuma110aa61994-08-29 12:50:44 +00001421#endif /* HAVE_SIGNAL_H */
Guido van Rossum82598051997-03-05 00:20:32 +00001422 PyOS_InitInterrupts(); /* May imply initsignal() */
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001423}
1424
Guido van Rossuma110aa61994-08-29 12:50:44 +00001425#ifdef MPW
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001426
1427/* Check for file descriptor connected to interactive device.
1428 Pretend that stdin is always interactive, other files never. */
1429
1430int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001431isatty(int fd)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001432{
1433 return fd == fileno(stdin);
1434}
1435
1436#endif
Guido van Rossum7433b121997-02-14 19:45:36 +00001437
1438/*
1439 * The file descriptor fd is considered ``interactive'' if either
1440 * a) isatty(fd) is TRUE, or
1441 * b) the -i flag was given, and the filename associated with
1442 * the descriptor is NULL or "<stdin>" or "???".
1443 */
1444int
Martin v. Löwis95292d62002-12-11 14:04:59 +00001445Py_FdIsInteractive(FILE *fp, const char *filename)
Guido van Rossum7433b121997-02-14 19:45:36 +00001446{
1447 if (isatty((int)fileno(fp)))
1448 return 1;
1449 if (!Py_InteractiveFlag)
1450 return 0;
1451 return (filename == NULL) ||
1452 (strcmp(filename, "<stdin>") == 0) ||
1453 (strcmp(filename, "???") == 0);
1454}
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001455
1456
1457#if defined(USE_STACKCHECK)
1458#if defined(WIN32) && defined(_MSC_VER)
1459
1460/* Stack checking for Microsoft C */
1461
1462#include <malloc.h>
1463#include <excpt.h>
1464
Fred Drakee8de31c2000-08-31 05:38:39 +00001465/*
1466 * Return non-zero when we run out of memory on the stack; zero otherwise.
1467 */
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001468int
Fred Drake399739f2000-08-31 05:52:44 +00001469PyOS_CheckStack(void)
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001470{
1471 __try {
Tim Peters92e4dd82002-10-05 01:47:34 +00001472 /* alloca throws a stack overflow exception if there's
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001473 not enough space left on the stack */
Tim Peters92e4dd82002-10-05 01:47:34 +00001474 alloca(PYOS_STACK_MARGIN * sizeof(void*));
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001475 return 0;
1476 } __except (EXCEPTION_EXECUTE_HANDLER) {
1477 /* just ignore all errors */
1478 }
1479 return 1;
1480}
1481
1482#endif /* WIN32 && _MSC_VER */
1483
1484/* Alternate implementations can be added here... */
1485
1486#endif /* USE_STACKCHECK */
Guido van Rossum6f256182000-09-16 16:32:19 +00001487
1488
1489/* Wrappers around sigaction() or signal(). */
1490
1491PyOS_sighandler_t
1492PyOS_getsig(int sig)
1493{
1494#ifdef HAVE_SIGACTION
1495 struct sigaction context;
Barry Warsawafeb2a42001-11-13 23:08:26 +00001496 /* Initialize context.sa_handler to SIG_ERR which makes about as
1497 * much sense as anything else. It should get overwritten if
1498 * sigaction actually succeeds and otherwise we avoid an
1499 * uninitialized memory read.
1500 */
1501 context.sa_handler = SIG_ERR;
Guido van Rossum6f256182000-09-16 16:32:19 +00001502 sigaction(sig, NULL, &context);
1503 return context.sa_handler;
1504#else
1505 PyOS_sighandler_t handler;
1506 handler = signal(sig, SIG_IGN);
1507 signal(sig, handler);
1508 return handler;
1509#endif
1510}
1511
1512PyOS_sighandler_t
1513PyOS_setsig(int sig, PyOS_sighandler_t handler)
1514{
1515#ifdef HAVE_SIGACTION
1516 struct sigaction context;
1517 PyOS_sighandler_t oldhandler;
Barry Warsawafeb2a42001-11-13 23:08:26 +00001518 /* Initialize context.sa_handler to SIG_ERR which makes about as
1519 * much sense as anything else. It should get overwritten if
1520 * sigaction actually succeeds and otherwise we avoid an
1521 * uninitialized memory read.
1522 */
1523 context.sa_handler = SIG_ERR;
Guido van Rossum6f256182000-09-16 16:32:19 +00001524 sigaction(sig, NULL, &context);
1525 oldhandler = context.sa_handler;
1526 context.sa_handler = handler;
1527 sigaction(sig, &context, NULL);
1528 return oldhandler;
1529#else
1530 return signal(sig, handler);
1531#endif
1532}