blob: 0b5d0d17e82e084216eb77ca43c9fb17feb4b06a [file] [log] [blame]
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001/***********************************************************
Guido van Rossum6d023c91995-01-04 19:12:13 +00002Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
Guido van Rossum1984f1e1992-08-04 12:41:02 +00004
5 All Rights Reserved
6
Guido van Rossumd266eb41996-10-25 14:44:06 +00007Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
Guido van Rossum1984f1e1992-08-04 12:41:02 +00009provided that the above copyright notice appear in all copies and that
Guido van Rossumd266eb41996-10-25 14:44:06 +000010both that copyright notice and this permission notice appear in
Guido van Rossum1984f1e1992-08-04 12:41:02 +000011supporting documentation, and that the names of Stichting Mathematisch
Guido van Rossumd266eb41996-10-25 14:44:06 +000012Centrum or CWI or Corporation for National Research Initiatives or
13CNRI not be used in advertising or publicity pertaining to
14distribution of the software without specific, written prior
15permission.
Guido van Rossum1984f1e1992-08-04 12:41:02 +000016
Guido van Rossumd266eb41996-10-25 14:44:06 +000017While CWI is the initial source for this software, a modified version
18is made available by the Corporation for National Research Initiatives
19(CNRI) at the Internet address ftp://ftp.python.org.
20
21STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28PERFORMANCE OF THIS SOFTWARE.
Guido van Rossum1984f1e1992-08-04 12:41:02 +000029
30******************************************************************/
31
32/* Python interpreter top-level routines, including init/exit */
33
Guido van Rossum82598051997-03-05 00:20:32 +000034#include "Python.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +000035
36#include "grammar.h"
37#include "node.h"
38#include "parsetok.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +000039#include "errcode.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +000040#include "compile.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +000041#include "eval.h"
Guido van Rossumfdef2711994-09-14 13:31:04 +000042#include "marshal.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +000043
Guido van Rossum80bb9651996-12-05 23:27:02 +000044#ifdef HAVE_UNISTD_H
45#include <unistd.h>
46#endif
47
Guido van Rossuma110aa61994-08-29 12:50:44 +000048#ifdef HAVE_SIGNAL_H
Guido van Rossuma9e7dc11992-10-18 18:53:57 +000049#include <signal.h>
Guido van Rossuma9e7dc11992-10-18 18:53:57 +000050#endif
51
Guido van Rossum9b38a141996-09-11 23:12:24 +000052#ifdef MS_WIN32
Guido van Rossuma44823b1995-03-14 15:01:17 +000053#undef BYTE
54#include "windows.h"
55#endif
56
Guido van Rossum82598051997-03-05 00:20:32 +000057extern char *Py_GetPath();
Guido van Rossum1984f1e1992-08-04 12:41:02 +000058
Guido van Rossum82598051997-03-05 00:20:32 +000059extern grammar _PyParser_Grammar; /* From graminit.c */
Guido van Rossum1984f1e1992-08-04 12:41:02 +000060
Guido van Rossumb73cc041993-11-01 16:28:59 +000061/* Forward */
Guido van Rossum82598051997-03-05 00:20:32 +000062static void initmain Py_PROTO((void));
Guido van Rossumdcc0c131997-08-29 22:32:42 +000063static void initsite Py_PROTO((void));
Guido van Rossum82598051997-03-05 00:20:32 +000064static PyObject *run_err_node Py_PROTO((node *n, char *filename,
65 PyObject *globals, PyObject *locals));
66static PyObject *run_node Py_PROTO((node *n, char *filename,
67 PyObject *globals, PyObject *locals));
68static PyObject *run_pyc_file Py_PROTO((FILE *fp, char *filename,
69 PyObject *globals, PyObject *locals));
70static void err_input Py_PROTO((perrdetail *));
71static void initsigs Py_PROTO((void));
Guido van Rossumcc283f51997-08-05 02:22:03 +000072static void call_sys_exitfunc Py_PROTO((void));
73static void call_ll_exitfuncs Py_PROTO((void));
Guido van Rossuma9e7dc11992-10-18 18:53:57 +000074
Guido van Rossum82598051997-03-05 00:20:32 +000075int Py_DebugFlag; /* Needed by parser.c */
76int Py_VerboseFlag; /* Needed by import.c */
Guido van Rossum7433b121997-02-14 19:45:36 +000077int Py_InteractiveFlag; /* Needed by Py_FdIsInteractive() below */
Guido van Rossumdcc0c131997-08-29 22:32:42 +000078int Py_NoSiteFlag; /* Suppress 'import site' */
Guido van Rossum999e5e91997-10-03 19:46:02 +000079int Py_UseClassExceptionsFlag = 1; /* Needed by bltinmodule.c */
Guido van Rossuma61691e1998-02-06 22:27:24 +000080int Py_FrozenFlag; /* Needed by getpath.c */
Guido van Rossum1984f1e1992-08-04 12:41:02 +000081
Guido van Rossum25ce5661997-08-02 03:10:38 +000082static int initialized = 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +000083
Guido van Rossume3c0d5e1997-08-22 04:20:13 +000084/* API to access the initialized flag -- useful for eroteric use */
85
86int
87Py_IsInitialized()
88{
89 return initialized;
90}
91
Guido van Rossum25ce5661997-08-02 03:10:38 +000092/* Global initializations. Can be undone by Py_Finalize(). Don't
93 call this twice without an intervening Py_Finalize() call. When
94 initializations fail, a fatal error is issued and the function does
95 not return. On return, the first thread and interpreter state have
96 been created.
Guido van Rossum1984f1e1992-08-04 12:41:02 +000097
Guido van Rossum25ce5661997-08-02 03:10:38 +000098 Locking: you must hold the interpreter lock while calling this.
99 (If the lock has not yet been initialized, that's equivalent to
100 having the lock, but you cannot use multiple threads.)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000101
Guido van Rossum25ce5661997-08-02 03:10:38 +0000102*/
Guido van Rossuma027efa1997-05-05 20:56:21 +0000103
104void
105Py_Initialize()
106{
Guido van Rossuma027efa1997-05-05 20:56:21 +0000107 PyInterpreterState *interp;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000108 PyThreadState *tstate;
109 PyObject *bimod, *sysmod;
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000110 char *p;
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000111
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000112 if (initialized)
Guido van Rossumaa615051997-08-20 22:40:18 +0000113 return;
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000114 initialized = 1;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000115
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000116 if ((p = getenv("PYTHONDEBUG")) && *p != '\0')
117 Py_DebugFlag = 1;
118 if ((p = getenv("PYTHONVERBOSE")) && *p != '\0')
119 Py_VerboseFlag = 1;
Guido van Rossum562f5b11998-10-07 14:50:42 +0000120 if ((p = getenv("PYTHONOPTIMIZE")) && *p != '\0')
121 Py_OptimizeFlag = 1;
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000122
Guido van Rossuma027efa1997-05-05 20:56:21 +0000123 interp = PyInterpreterState_New();
124 if (interp == NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000125 Py_FatalError("Py_Initialize: can't make first interpreter");
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000126
Guido van Rossuma027efa1997-05-05 20:56:21 +0000127 tstate = PyThreadState_New(interp);
128 if (tstate == NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000129 Py_FatalError("Py_Initialize: can't make first thread");
Guido van Rossuma027efa1997-05-05 20:56:21 +0000130 (void) PyThreadState_Swap(tstate);
131
Guido van Rossum25ce5661997-08-02 03:10:38 +0000132 interp->modules = PyDict_New();
133 if (interp->modules == NULL)
134 Py_FatalError("Py_Initialize: can't make modules dictionary");
Guido van Rossuma027efa1997-05-05 20:56:21 +0000135
Barry Warsaw035574d1997-08-29 22:07:17 +0000136 bimod = _PyBuiltin_Init_1();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000137 if (bimod == NULL)
138 Py_FatalError("Py_Initialize: can't initialize __builtin__");
Guido van Rossum4a1f39a1997-11-04 19:36:18 +0000139 interp->builtins = PyModule_GetDict(bimod);
140 Py_INCREF(interp->builtins);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000141
142 sysmod = _PySys_Init();
143 if (sysmod == NULL)
144 Py_FatalError("Py_Initialize: can't initialize sys");
145 interp->sysdict = PyModule_GetDict(sysmod);
146 Py_INCREF(interp->sysdict);
147 _PyImport_FixupExtension("sys", "sys");
Guido van Rossuma027efa1997-05-05 20:56:21 +0000148 PySys_SetPath(Py_GetPath());
Guido van Rossum25ce5661997-08-02 03:10:38 +0000149 PyDict_SetItemString(interp->sysdict, "modules",
150 interp->modules);
151
Barry Warsaw035574d1997-08-29 22:07:17 +0000152 /* phase 2 of builtins */
Guido van Rossum4a1f39a1997-11-04 19:36:18 +0000153 _PyBuiltin_Init_2(interp->builtins);
Barry Warsaw963b8711997-09-18 16:42:02 +0000154 _PyImport_FixupExtension("__builtin__", "__builtin__");
Barry Warsaw035574d1997-08-29 22:07:17 +0000155
Guido van Rossum25ce5661997-08-02 03:10:38 +0000156 _PyImport_Init();
157
158 initsigs(); /* Signal handling stuff, including initintr() */
159
160 initmain(); /* Module __main__ */
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000161 if (!Py_NoSiteFlag)
162 initsite(); /* Module site */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000163}
164
Guido van Rossum2edcf0d1998-12-15 16:12:00 +0000165#ifdef COUNT_ALLOCS
166extern void dump_counts Py_PROTO((void));
167#endif
168
Guido van Rossum25ce5661997-08-02 03:10:38 +0000169/* Undo the effect of Py_Initialize().
170
171 Beware: if multiple interpreter and/or thread states exist, these
172 are not wiped out; only the current thread and interpreter state
173 are deleted. But since everything else is deleted, those other
174 interpreter and thread states should no longer be used.
175
176 (XXX We should do better, e.g. wipe out all interpreters and
177 threads.)
178
179 Locking: as above.
180
181*/
182
183void
184Py_Finalize()
185{
186 PyInterpreterState *interp;
187 PyThreadState *tstate;
188
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000189 if (!initialized)
Guido van Rossumaa615051997-08-20 22:40:18 +0000190 return;
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000191 initialized = 0;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000192
Guido van Rossum4cc462e1998-01-19 22:00:38 +0000193 call_sys_exitfunc();
194
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000195 /* Get current thread state and interpreter pointer */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000196 tstate = PyThreadState_Get();
197 interp = tstate->interp;
198
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000199 /* Disable signal handling */
200 PyOS_FiniInterrupts();
201
202 /* Destroy PyExc_MemoryErrorInst */
203 _PyBuiltin_Fini_1();
204
205 /* Destroy all modules */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000206 PyImport_Cleanup();
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000207
Guido van Rossum1707aad1997-12-08 23:43:45 +0000208 /* Destroy the database used by _PyImport_{Fixup,Find}Extension */
209 _PyImport_Fini();
210
211 /* Debugging stuff */
212#ifdef COUNT_ALLOCS
213 dump_counts();
214#endif
215
216#ifdef Py_REF_DEBUG
217 fprintf(stderr, "[%ld refs]\n", _Py_RefTotal);
218#endif
219
220#ifdef Py_TRACE_REFS
221 if (_Py_AskYesNo("Print left references?")) {
222 _Py_PrintReferences(stderr);
223 }
224#endif /* Py_TRACE_REFS */
225
Guido van Rossum4a1f39a1997-11-04 19:36:18 +0000226 /* Delete current thread */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000227 PyInterpreterState_Clear(interp);
228 PyThreadState_Swap(NULL);
229 PyInterpreterState_Delete(interp);
230
Barry Warsaw035574d1997-08-29 22:07:17 +0000231 /* Now we decref the exception classes. After this point nothing
232 can raise an exception. That's okay, because each Fini() method
233 below has been checked to make sure no exceptions are ever
234 raised.
235 */
236 _PyBuiltin_Fini_2();
Guido van Rossumcc283f51997-08-05 02:22:03 +0000237 PyMethod_Fini();
238 PyFrame_Fini();
239 PyCFunction_Fini();
240 PyTuple_Fini();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000241 PyString_Fini();
Guido van Rossumcc283f51997-08-05 02:22:03 +0000242 PyInt_Fini();
243 PyFloat_Fini();
244
245 /* XXX Still allocated:
246 - various static ad-hoc pointers to interned strings
247 - int and float free list blocks
248 - whatever various modules and libraries allocate
249 */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000250
251 PyGrammar_RemoveAccelerators(&_PyParser_Grammar);
Guido van Rossumcc283f51997-08-05 02:22:03 +0000252
253 call_ll_exitfuncs();
254
Guido van Rossumcc283f51997-08-05 02:22:03 +0000255#ifdef Py_TRACE_REFS
Guido van Rossumcc283f51997-08-05 02:22:03 +0000256 _Py_ResetReferences();
257#endif /* Py_TRACE_REFS */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000258}
259
260/* Create and initialize a new interpreter and thread, and return the
261 new thread. This requires that Py_Initialize() has been called
262 first.
263
264 Unsuccessful initialization yields a NULL pointer. Note that *no*
265 exception information is available even in this case -- the
266 exception information is held in the thread, and there is no
267 thread.
268
269 Locking: as above.
270
271*/
272
273PyThreadState *
274Py_NewInterpreter()
275{
276 PyInterpreterState *interp;
277 PyThreadState *tstate, *save_tstate;
278 PyObject *bimod, *sysmod;
279
280 if (!initialized)
281 Py_FatalError("Py_NewInterpreter: call Py_Initialize first");
282
283 interp = PyInterpreterState_New();
284 if (interp == NULL)
285 return NULL;
286
287 tstate = PyThreadState_New(interp);
288 if (tstate == NULL) {
289 PyInterpreterState_Delete(interp);
290 return NULL;
291 }
292
293 save_tstate = PyThreadState_Swap(tstate);
294
295 /* XXX The following is lax in error checking */
296
297 interp->modules = PyDict_New();
298
299 bimod = _PyImport_FindExtension("__builtin__", "__builtin__");
300 if (bimod != NULL) {
Guido van Rossum4a1f39a1997-11-04 19:36:18 +0000301 interp->builtins = PyModule_GetDict(bimod);
302 Py_INCREF(interp->builtins);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000303 }
304 sysmod = _PyImport_FindExtension("sys", "sys");
305 if (bimod != NULL && sysmod != NULL) {
306 interp->sysdict = PyModule_GetDict(sysmod);
307 Py_INCREF(interp->sysdict);
308 PySys_SetPath(Py_GetPath());
309 PyDict_SetItemString(interp->sysdict, "modules",
310 interp->modules);
311 initmain();
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000312 if (!Py_NoSiteFlag)
313 initsite();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000314 }
315
316 if (!PyErr_Occurred())
317 return tstate;
318
319 /* Oops, it didn't work. Undo it all. */
320
321 PyErr_Print();
322 PyThreadState_Clear(tstate);
323 PyThreadState_Swap(save_tstate);
324 PyThreadState_Delete(tstate);
325 PyInterpreterState_Delete(interp);
326
327 return NULL;
328}
329
330/* Delete an interpreter and its last thread. This requires that the
331 given thread state is current, that the thread has no remaining
332 frames, and that it is its interpreter's only remaining thread.
333 It is a fatal error to violate these constraints.
334
335 (Py_Finalize() doesn't have these constraints -- it zaps
336 everything, regardless.)
337
338 Locking: as above.
339
340*/
341
342void
343Py_EndInterpreter(tstate)
344 PyThreadState *tstate;
345{
346 PyInterpreterState *interp = tstate->interp;
347
348 if (tstate != PyThreadState_Get())
349 Py_FatalError("Py_EndInterpreter: thread is not current");
350 if (tstate->frame != NULL)
351 Py_FatalError("Py_EndInterpreter: thread still has a frame");
352 if (tstate != interp->tstate_head || tstate->next != NULL)
353 Py_FatalError("Py_EndInterpreter: not the last thread");
354
355 PyImport_Cleanup();
356 PyInterpreterState_Clear(interp);
357 PyThreadState_Swap(NULL);
358 PyInterpreterState_Delete(interp);
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000359}
360
361static char *progname = "python";
362
363void
364Py_SetProgramName(pn)
365 char *pn;
366{
367 if (pn && *pn)
368 progname = pn;
369}
370
371char *
372Py_GetProgramName()
373{
374 return progname;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000375}
376
Guido van Rossuma61691e1998-02-06 22:27:24 +0000377static char *default_home = NULL;
378
379void
380Py_SetPythonHome(home)
381 char *home;
382{
383 default_home = home;
384}
385
386char *
387Py_GetPythonHome()
388{
389 char *home = default_home;
390 if (home == NULL)
391 home = getenv("PYTHONHOME");
392 return home;
393}
394
Guido van Rossum6135a871995-01-09 17:53:26 +0000395/* Create __main__ module */
396
397static void
398initmain()
399{
Guido van Rossum82598051997-03-05 00:20:32 +0000400 PyObject *m, *d;
401 m = PyImport_AddModule("__main__");
Guido van Rossum6135a871995-01-09 17:53:26 +0000402 if (m == NULL)
Guido van Rossum82598051997-03-05 00:20:32 +0000403 Py_FatalError("can't create __main__ module");
404 d = PyModule_GetDict(m);
405 if (PyDict_GetItemString(d, "__builtins__") == NULL) {
Guido van Rossum858cb731997-11-19 16:15:37 +0000406 PyObject *bimod = PyImport_ImportModule("__builtin__");
407 if (bimod == NULL ||
408 PyDict_SetItemString(d, "__builtins__", bimod) != 0)
Guido van Rossum82598051997-03-05 00:20:32 +0000409 Py_FatalError("can't add __builtins__ to __main__");
Barry Warsaw3d05b1a1999-01-29 21:30:22 +0000410 Py_DECREF(bimod);
Guido van Rossum6135a871995-01-09 17:53:26 +0000411 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000412}
413
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000414/* Import the site module (not into __main__ though) */
415
416static void
417initsite()
418{
419 PyObject *m, *f;
420 m = PyImport_ImportModule("site");
421 if (m == NULL) {
422 f = PySys_GetObject("stderr");
423 if (Py_VerboseFlag) {
424 PyFile_WriteString(
425 "'import site' failed; traceback:\n", f);
426 PyErr_Print();
427 }
428 else {
429 PyFile_WriteString(
430 "'import site' failed; use -v for traceback\n", f);
431 PyErr_Clear();
432 }
433 }
434 else {
435 Py_DECREF(m);
436 }
437}
438
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000439/* Parse input from a file and execute it */
440
441int
Guido van Rossum82598051997-03-05 00:20:32 +0000442PyRun_AnyFile(fp, filename)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000443 FILE *fp;
444 char *filename;
445{
446 if (filename == NULL)
447 filename = "???";
Guido van Rossum7433b121997-02-14 19:45:36 +0000448 if (Py_FdIsInteractive(fp, filename))
Guido van Rossum82598051997-03-05 00:20:32 +0000449 return PyRun_InteractiveLoop(fp, filename);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000450 else
Guido van Rossum82598051997-03-05 00:20:32 +0000451 return PyRun_SimpleFile(fp, filename);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000452}
453
454int
Guido van Rossum82598051997-03-05 00:20:32 +0000455PyRun_InteractiveLoop(fp, filename)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000456 FILE *fp;
457 char *filename;
458{
Guido van Rossum82598051997-03-05 00:20:32 +0000459 PyObject *v;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000460 int ret;
Guido van Rossum82598051997-03-05 00:20:32 +0000461 v = PySys_GetObject("ps1");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000462 if (v == NULL) {
Guido van Rossum82598051997-03-05 00:20:32 +0000463 PySys_SetObject("ps1", v = PyString_FromString(">>> "));
464 Py_XDECREF(v);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000465 }
Guido van Rossum82598051997-03-05 00:20:32 +0000466 v = PySys_GetObject("ps2");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000467 if (v == NULL) {
Guido van Rossum82598051997-03-05 00:20:32 +0000468 PySys_SetObject("ps2", v = PyString_FromString("... "));
469 Py_XDECREF(v);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000470 }
471 for (;;) {
Guido van Rossum82598051997-03-05 00:20:32 +0000472 ret = PyRun_InteractiveOne(fp, filename);
Guido van Rossumaae0d321996-05-22 16:35:33 +0000473#ifdef Py_REF_DEBUG
Guido van Rossum6f9e4331995-03-29 16:57:48 +0000474 fprintf(stderr, "[%ld refs]\n", _Py_RefTotal);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000475#endif
476 if (ret == E_EOF)
477 return 0;
478 /*
479 if (ret == E_NOMEM)
480 return -1;
481 */
482 }
483}
484
485int
Guido van Rossum82598051997-03-05 00:20:32 +0000486PyRun_InteractiveOne(fp, filename)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000487 FILE *fp;
488 char *filename;
489{
Guido van Rossum82598051997-03-05 00:20:32 +0000490 PyObject *m, *d, *v, *w;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000491 node *n;
Guido van Rossuma110aa61994-08-29 12:50:44 +0000492 perrdetail err;
Guido van Rossumddc3fb51997-11-25 20:58:13 +0000493 char *ps1 = "", *ps2 = "";
Guido van Rossum82598051997-03-05 00:20:32 +0000494 v = PySys_GetObject("ps1");
Guido van Rossumddc3fb51997-11-25 20:58:13 +0000495 if (v != NULL) {
496 v = PyObject_Str(v);
497 if (v == NULL)
498 PyErr_Clear();
499 else if (PyString_Check(v))
500 ps1 = PyString_AsString(v);
501 }
Guido van Rossum82598051997-03-05 00:20:32 +0000502 w = PySys_GetObject("ps2");
Guido van Rossumddc3fb51997-11-25 20:58:13 +0000503 if (w != NULL) {
504 w = PyObject_Str(w);
505 if (w == NULL)
506 PyErr_Clear();
507 else if (PyString_Check(w))
508 ps2 = PyString_AsString(w);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000509 }
Guido van Rossum82598051997-03-05 00:20:32 +0000510 n = PyParser_ParseFile(fp, filename, &_PyParser_Grammar,
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000511 Py_single_input, ps1, ps2, &err);
Guido van Rossum82598051997-03-05 00:20:32 +0000512 Py_XDECREF(v);
513 Py_XDECREF(w);
Guido van Rossuma110aa61994-08-29 12:50:44 +0000514 if (n == NULL) {
515 if (err.error == E_EOF) {
516 if (err.text)
517 free(err.text);
518 return E_EOF;
519 }
520 err_input(&err);
Guido van Rossum82598051997-03-05 00:20:32 +0000521 PyErr_Print();
Guido van Rossuma110aa61994-08-29 12:50:44 +0000522 return err.error;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000523 }
Guido van Rossum82598051997-03-05 00:20:32 +0000524 m = PyImport_AddModule("__main__");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000525 if (m == NULL)
526 return -1;
Guido van Rossum82598051997-03-05 00:20:32 +0000527 d = PyModule_GetDict(m);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000528 v = run_node(n, filename, d, d);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000529 if (v == NULL) {
Guido van Rossum82598051997-03-05 00:20:32 +0000530 PyErr_Print();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000531 return -1;
532 }
Guido van Rossum82598051997-03-05 00:20:32 +0000533 Py_DECREF(v);
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000534 if (Py_FlushLine())
535 PyErr_Clear();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000536 return 0;
537}
538
539int
Guido van Rossum82598051997-03-05 00:20:32 +0000540PyRun_SimpleFile(fp, filename)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000541 FILE *fp;
542 char *filename;
543{
Guido van Rossum82598051997-03-05 00:20:32 +0000544 PyObject *m, *d, *v;
Guido van Rossumfdef2711994-09-14 13:31:04 +0000545 char *ext;
546
Guido van Rossum82598051997-03-05 00:20:32 +0000547 m = PyImport_AddModule("__main__");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000548 if (m == NULL)
549 return -1;
Guido van Rossum82598051997-03-05 00:20:32 +0000550 d = PyModule_GetDict(m);
Guido van Rossumfdef2711994-09-14 13:31:04 +0000551 ext = filename + strlen(filename) - 4;
Guido van Rossum2a7f58d1997-04-02 05:28:38 +0000552 if (strcmp(ext, ".pyc") == 0 || strcmp(ext, ".pyo") == 0
Jack Jansenbd06e961995-02-13 11:44:56 +0000553#ifdef macintosh
554 /* On a mac, we also assume a pyc file for types 'PYC ' and 'APPL' */
Guido van Rossum2a7f58d1997-04-02 05:28:38 +0000555 || getfiletype(filename) == 'PYC '
556 || getfiletype(filename) == 'APPL'
Jack Jansenbd06e961995-02-13 11:44:56 +0000557#endif /* macintosh */
Guido van Rossum2a7f58d1997-04-02 05:28:38 +0000558 ) {
Guido van Rossumfdef2711994-09-14 13:31:04 +0000559 /* Try to run a pyc file. First, re-open in binary */
Jack Jansene00637b1994-12-14 12:58:37 +0000560 /* Don't close, done in main: fclose(fp); */
Guido van Rossumfdef2711994-09-14 13:31:04 +0000561 if( (fp = fopen(filename, "rb")) == NULL ) {
562 fprintf(stderr, "python: Can't reopen .pyc file\n");
563 return -1;
564 }
Guido van Rossum2a7f58d1997-04-02 05:28:38 +0000565 /* Turn on optimization if a .pyo file is given */
566 if (strcmp(ext, ".pyo") == 0)
567 Py_OptimizeFlag = 1;
Guido van Rossumfdef2711994-09-14 13:31:04 +0000568 v = run_pyc_file(fp, filename, d, d);
569 } else {
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000570 v = PyRun_File(fp, filename, Py_file_input, d, d);
Guido van Rossumfdef2711994-09-14 13:31:04 +0000571 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000572 if (v == NULL) {
Guido van Rossum82598051997-03-05 00:20:32 +0000573 PyErr_Print();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000574 return -1;
575 }
Guido van Rossum82598051997-03-05 00:20:32 +0000576 Py_DECREF(v);
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000577 if (Py_FlushLine())
578 PyErr_Clear();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000579 return 0;
580}
581
582int
Guido van Rossum82598051997-03-05 00:20:32 +0000583PyRun_SimpleString(command)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000584 char *command;
585{
Guido van Rossum82598051997-03-05 00:20:32 +0000586 PyObject *m, *d, *v;
587 m = PyImport_AddModule("__main__");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000588 if (m == NULL)
589 return -1;
Guido van Rossum82598051997-03-05 00:20:32 +0000590 d = PyModule_GetDict(m);
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000591 v = PyRun_String(command, Py_file_input, d, d);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000592 if (v == NULL) {
Guido van Rossum82598051997-03-05 00:20:32 +0000593 PyErr_Print();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000594 return -1;
595 }
Guido van Rossum82598051997-03-05 00:20:32 +0000596 Py_DECREF(v);
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000597 if (Py_FlushLine())
598 PyErr_Clear();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000599 return 0;
600}
601
Barry Warsaw035574d1997-08-29 22:07:17 +0000602static int
603parse_syntax_error(err, message, filename, lineno, offset, text)
604 PyObject* err;
605 PyObject** message;
606 char** filename;
607 int* lineno;
608 int* offset;
609 char** text;
610{
611 long hold;
612 PyObject *v;
613
614 /* old style errors */
615 if (PyTuple_Check(err))
616 return PyArg_Parse(err, "(O(ziiz))", message, filename,
617 lineno, offset, text);
618
619 /* new style errors. `err' is an instance */
620
621 if (! (v = PyObject_GetAttrString(err, "msg")))
622 goto finally;
623 *message = v;
624
625 if (!(v = PyObject_GetAttrString(err, "filename")))
626 goto finally;
627 if (v == Py_None)
628 *filename = NULL;
629 else if (! (*filename = PyString_AsString(v)))
630 goto finally;
631
632 Py_DECREF(v);
633 if (!(v = PyObject_GetAttrString(err, "lineno")))
634 goto finally;
635 hold = PyInt_AsLong(v);
636 Py_DECREF(v);
637 v = NULL;
638 if (hold < 0 && PyErr_Occurred())
639 goto finally;
640 *lineno = (int)hold;
641
642 if (!(v = PyObject_GetAttrString(err, "offset")))
643 goto finally;
644 hold = PyInt_AsLong(v);
645 Py_DECREF(v);
646 v = NULL;
647 if (hold < 0 && PyErr_Occurred())
648 goto finally;
649 *offset = (int)hold;
650
651 if (!(v = PyObject_GetAttrString(err, "text")))
652 goto finally;
653 if (v == Py_None)
654 *text = NULL;
655 else if (! (*text = PyString_AsString(v)))
656 goto finally;
657 Py_DECREF(v);
658 return 1;
659
660finally:
661 Py_XDECREF(v);
662 return 0;
663}
664
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000665void
Guido van Rossum82598051997-03-05 00:20:32 +0000666PyErr_Print()
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000667{
Guido van Rossuma61691e1998-02-06 22:27:24 +0000668 PyErr_PrintEx(1);
669}
670
671void
672PyErr_PrintEx(set_sys_last_vars)
673 int set_sys_last_vars;
674{
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000675 int err = 0;
Guido van Rossum82598051997-03-05 00:20:32 +0000676 PyObject *exception, *v, *tb, *f;
677 PyErr_Fetch(&exception, &v, &tb);
Barry Warsaw035574d1997-08-29 22:07:17 +0000678 PyErr_NormalizeException(&exception, &v, &tb);
679
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000680 if (exception == NULL)
Guido van Rossum296b4751997-05-23 00:19:20 +0000681 return;
Barry Warsaw035574d1997-08-29 22:07:17 +0000682
Barry Warsaw36b8f941997-08-26 18:09:48 +0000683 if (PyErr_GivenExceptionMatches(exception, PyExc_SystemExit)) {
Guido van Rossum0829c751998-02-28 04:31:39 +0000684 if (Py_FlushLine())
685 PyErr_Clear();
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000686 fflush(stdout);
Guido van Rossum82598051997-03-05 00:20:32 +0000687 if (v == NULL || v == Py_None)
688 Py_Exit(0);
Barry Warsaw035574d1997-08-29 22:07:17 +0000689 if (PyInstance_Check(v)) {
690 /* we expect the error code to be store in the
691 `code' attribute
692 */
693 PyObject *code = PyObject_GetAttrString(v, "code");
694 if (code) {
695 Py_DECREF(v);
696 v = code;
Guido van Rossumaa9606f1997-10-03 13:53:28 +0000697 if (v == Py_None)
698 Py_Exit(0);
Barry Warsaw035574d1997-08-29 22:07:17 +0000699 }
700 /* if we failed to dig out the "code" attribute,
701 then just let the else clause below print the
702 error
703 */
704 }
Guido van Rossum82598051997-03-05 00:20:32 +0000705 if (PyInt_Check(v))
706 Py_Exit((int)PyInt_AsLong(v));
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000707 else {
Guido van Rossum3165fe61992-09-25 21:59:05 +0000708 /* OK to use real stderr here */
Guido van Rossum82598051997-03-05 00:20:32 +0000709 PyObject_Print(v, stderr, Py_PRINT_RAW);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000710 fprintf(stderr, "\n");
Guido van Rossum82598051997-03-05 00:20:32 +0000711 Py_Exit(1);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000712 }
713 }
Guido van Rossuma61691e1998-02-06 22:27:24 +0000714 if (set_sys_last_vars) {
715 PySys_SetObject("last_type", exception);
716 PySys_SetObject("last_value", v);
717 PySys_SetObject("last_traceback", tb);
718 }
Guido van Rossum82598051997-03-05 00:20:32 +0000719 f = PySys_GetObject("stderr");
Guido van Rossum3165fe61992-09-25 21:59:05 +0000720 if (f == NULL)
721 fprintf(stderr, "lost sys.stderr\n");
722 else {
Guido van Rossum0829c751998-02-28 04:31:39 +0000723 if (Py_FlushLine())
724 PyErr_Clear();
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000725 fflush(stdout);
Guido van Rossum0829c751998-02-28 04:31:39 +0000726 err = PyTraceBack_Print(tb, f);
Barry Warsaw36b8f941997-08-26 18:09:48 +0000727 if (err == 0 &&
728 PyErr_GivenExceptionMatches(exception, PyExc_SyntaxError))
729 {
Guido van Rossum82598051997-03-05 00:20:32 +0000730 PyObject *message;
Guido van Rossuma110aa61994-08-29 12:50:44 +0000731 char *filename, *text;
732 int lineno, offset;
Barry Warsaw035574d1997-08-29 22:07:17 +0000733 if (!parse_syntax_error(v, &message, &filename,
734 &lineno, &offset, &text))
Guido van Rossum82598051997-03-05 00:20:32 +0000735 PyErr_Clear();
Guido van Rossuma110aa61994-08-29 12:50:44 +0000736 else {
737 char buf[10];
Guido van Rossum82598051997-03-05 00:20:32 +0000738 PyFile_WriteString(" File \"", f);
Guido van Rossuma110aa61994-08-29 12:50:44 +0000739 if (filename == NULL)
Guido van Rossum82598051997-03-05 00:20:32 +0000740 PyFile_WriteString("<string>", f);
Guido van Rossuma110aa61994-08-29 12:50:44 +0000741 else
Guido van Rossum82598051997-03-05 00:20:32 +0000742 PyFile_WriteString(filename, f);
743 PyFile_WriteString("\", line ", f);
Guido van Rossuma110aa61994-08-29 12:50:44 +0000744 sprintf(buf, "%d", lineno);
Guido van Rossum82598051997-03-05 00:20:32 +0000745 PyFile_WriteString(buf, f);
746 PyFile_WriteString("\n", f);
Guido van Rossuma110aa61994-08-29 12:50:44 +0000747 if (text != NULL) {
Guido van Rossum798199d1994-09-19 08:08:50 +0000748 char *nl;
749 if (offset > 0 &&
Guido van Rossum478e7181997-05-06 15:24:59 +0000750 offset == (int)strlen(text))
Guido van Rossum798199d1994-09-19 08:08:50 +0000751 offset--;
752 for (;;) {
753 nl = strchr(text, '\n');
754 if (nl == NULL ||
755 nl-text >= offset)
756 break;
757 offset -= (nl+1-text);
758 text = nl+1;
759 }
Guido van Rossuma110aa61994-08-29 12:50:44 +0000760 while (*text == ' ' || *text == '\t') {
761 text++;
762 offset--;
763 }
Guido van Rossum82598051997-03-05 00:20:32 +0000764 PyFile_WriteString(" ", f);
765 PyFile_WriteString(text, f);
Guido van Rossuma110aa61994-08-29 12:50:44 +0000766 if (*text == '\0' ||
767 text[strlen(text)-1] != '\n')
Guido van Rossum82598051997-03-05 00:20:32 +0000768 PyFile_WriteString("\n", f);
769 PyFile_WriteString(" ", f);
Guido van Rossuma110aa61994-08-29 12:50:44 +0000770 offset--;
771 while (offset > 0) {
Guido van Rossum82598051997-03-05 00:20:32 +0000772 PyFile_WriteString(" ", f);
Guido van Rossuma110aa61994-08-29 12:50:44 +0000773 offset--;
774 }
Guido van Rossum82598051997-03-05 00:20:32 +0000775 PyFile_WriteString("^\n", f);
Guido van Rossuma110aa61994-08-29 12:50:44 +0000776 }
Guido van Rossum82598051997-03-05 00:20:32 +0000777 Py_INCREF(message);
778 Py_DECREF(v);
Guido van Rossuma110aa61994-08-29 12:50:44 +0000779 v = message;
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000780 /* Can't be bothered to check all those
781 PyFile_WriteString() calls */
782 if (PyErr_Occurred())
783 err = -1;
Guido van Rossuma110aa61994-08-29 12:50:44 +0000784 }
785 }
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000786 if (err) {
787 /* Don't do anything else */
788 }
789 else if (PyClass_Check(exception)) {
Barry Warsaw2f5f6a21997-09-16 21:42:03 +0000790 PyClassObject* exc = (PyClassObject*)exception;
791 PyObject* className = exc->cl_name;
792 PyObject* moduleName =
793 PyDict_GetItemString(exc->cl_dict, "__module__");
794
795 if (moduleName == NULL)
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000796 err = PyFile_WriteString("<unknown>", f);
Barry Warsaw2f5f6a21997-09-16 21:42:03 +0000797 else {
798 char* modstr = PyString_AsString(moduleName);
799 if (modstr && strcmp(modstr, "exceptions"))
800 {
801 err = PyFile_WriteString(modstr, f);
802 err += PyFile_WriteString(".", f);
803 }
804 }
805 if (err == 0) {
806 if (className == NULL)
807 err = PyFile_WriteString("<unknown>", f);
808 else
809 err = PyFile_WriteObject(className, f,
810 Py_PRINT_RAW);
811 }
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000812 }
813 else
814 err = PyFile_WriteObject(exception, f, Py_PRINT_RAW);
815 if (err == 0) {
816 if (v != NULL && v != Py_None) {
Barry Warsaw035574d1997-08-29 22:07:17 +0000817 PyObject *s = PyObject_Str(v);
818 /* only print colon if the str() of the
819 object is not the empty string
820 */
Guido van Rossumd6bf45b1997-09-05 19:11:53 +0000821 if (s == NULL)
822 err = -1;
823 else if (!PyString_Check(s) ||
824 PyString_GET_SIZE(s) != 0)
Barry Warsaw035574d1997-08-29 22:07:17 +0000825 err = PyFile_WriteString(": ", f);
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000826 if (err == 0)
Guido van Rossumd6bf45b1997-09-05 19:11:53 +0000827 err = PyFile_WriteObject(s, f, Py_PRINT_RAW);
828 Py_XDECREF(s);
Guido van Rossum262e1241995-02-07 15:30:45 +0000829 }
Guido van Rossum262e1241995-02-07 15:30:45 +0000830 }
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000831 if (err == 0)
832 err = PyFile_WriteString("\n", f);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000833 }
Guido van Rossum82598051997-03-05 00:20:32 +0000834 Py_XDECREF(exception);
835 Py_XDECREF(v);
836 Py_XDECREF(tb);
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000837 /* If an error happened here, don't show it.
838 XXX This is wrong, but too many callers rely on this behavior. */
839 if (err != 0)
840 PyErr_Clear();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000841}
842
Guido van Rossum82598051997-03-05 00:20:32 +0000843PyObject *
844PyRun_String(str, start, globals, locals)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000845 char *str;
846 int start;
Guido van Rossum82598051997-03-05 00:20:32 +0000847 PyObject *globals, *locals;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000848{
Guido van Rossum82598051997-03-05 00:20:32 +0000849 return run_err_node(PyParser_SimpleParseString(str, start),
Guido van Rossuma110aa61994-08-29 12:50:44 +0000850 "<string>", globals, locals);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000851}
852
Guido van Rossum82598051997-03-05 00:20:32 +0000853PyObject *
854PyRun_File(fp, filename, start, globals, locals)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000855 FILE *fp;
856 char *filename;
857 int start;
Guido van Rossum82598051997-03-05 00:20:32 +0000858 PyObject *globals, *locals;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000859{
Guido van Rossum82598051997-03-05 00:20:32 +0000860 return run_err_node(PyParser_SimpleParseFile(fp, filename, start),
Guido van Rossuma110aa61994-08-29 12:50:44 +0000861 filename, globals, locals);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000862}
863
Guido van Rossum82598051997-03-05 00:20:32 +0000864static PyObject *
Guido van Rossuma110aa61994-08-29 12:50:44 +0000865run_err_node(n, filename, globals, locals)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000866 node *n;
867 char *filename;
Guido van Rossum82598051997-03-05 00:20:32 +0000868 PyObject *globals, *locals;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000869{
Guido van Rossuma110aa61994-08-29 12:50:44 +0000870 if (n == NULL)
871 return NULL;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000872 return run_node(n, filename, globals, locals);
873}
874
Guido van Rossum82598051997-03-05 00:20:32 +0000875static PyObject *
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000876run_node(n, filename, globals, locals)
877 node *n;
878 char *filename;
Guido van Rossum82598051997-03-05 00:20:32 +0000879 PyObject *globals, *locals;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000880{
Guido van Rossum82598051997-03-05 00:20:32 +0000881 PyCodeObject *co;
882 PyObject *v;
883 co = PyNode_Compile(n, filename);
884 PyNode_Free(n);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000885 if (co == NULL)
886 return NULL;
Guido van Rossum82598051997-03-05 00:20:32 +0000887 v = PyEval_EvalCode(co, globals, locals);
888 Py_DECREF(co);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000889 return v;
890}
891
Guido van Rossum82598051997-03-05 00:20:32 +0000892static PyObject *
Guido van Rossumfdef2711994-09-14 13:31:04 +0000893run_pyc_file(fp, filename, globals, locals)
894 FILE *fp;
895 char *filename;
Guido van Rossum82598051997-03-05 00:20:32 +0000896 PyObject *globals, *locals;
Guido van Rossumfdef2711994-09-14 13:31:04 +0000897{
Guido van Rossum82598051997-03-05 00:20:32 +0000898 PyCodeObject *co;
899 PyObject *v;
Guido van Rossumfdef2711994-09-14 13:31:04 +0000900 long magic;
Guido van Rossum82598051997-03-05 00:20:32 +0000901 long PyImport_GetMagicNumber();
Guido van Rossumfdef2711994-09-14 13:31:04 +0000902
Guido van Rossum82598051997-03-05 00:20:32 +0000903 magic = PyMarshal_ReadLongFromFile(fp);
904 if (magic != PyImport_GetMagicNumber()) {
905 PyErr_SetString(PyExc_RuntimeError,
Guido van Rossumfdef2711994-09-14 13:31:04 +0000906 "Bad magic number in .pyc file");
907 return NULL;
908 }
Guido van Rossum82598051997-03-05 00:20:32 +0000909 (void) PyMarshal_ReadLongFromFile(fp);
910 v = PyMarshal_ReadObjectFromFile(fp);
Guido van Rossumfdef2711994-09-14 13:31:04 +0000911 fclose(fp);
Guido van Rossum82598051997-03-05 00:20:32 +0000912 if (v == NULL || !PyCode_Check(v)) {
913 Py_XDECREF(v);
914 PyErr_SetString(PyExc_RuntimeError,
Guido van Rossumfdef2711994-09-14 13:31:04 +0000915 "Bad code object in .pyc file");
916 return NULL;
917 }
Guido van Rossum82598051997-03-05 00:20:32 +0000918 co = (PyCodeObject *)v;
919 v = PyEval_EvalCode(co, globals, locals);
920 Py_DECREF(co);
Guido van Rossumfdef2711994-09-14 13:31:04 +0000921 return v;
922}
923
Guido van Rossum82598051997-03-05 00:20:32 +0000924PyObject *
925Py_CompileString(str, filename, start)
Guido van Rossum5b722181993-03-30 17:46:03 +0000926 char *str;
927 char *filename;
928 int start;
929{
930 node *n;
Guido van Rossum82598051997-03-05 00:20:32 +0000931 PyCodeObject *co;
932 n = PyParser_SimpleParseString(str, start);
Guido van Rossuma110aa61994-08-29 12:50:44 +0000933 if (n == NULL)
Guido van Rossum5b722181993-03-30 17:46:03 +0000934 return NULL;
Guido van Rossum82598051997-03-05 00:20:32 +0000935 co = PyNode_Compile(n, filename);
936 PyNode_Free(n);
937 return (PyObject *)co;
Guido van Rossum5b722181993-03-30 17:46:03 +0000938}
939
Guido van Rossuma110aa61994-08-29 12:50:44 +0000940/* Simplified interface to parsefile -- return node or set exception */
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000941
Guido van Rossuma110aa61994-08-29 12:50:44 +0000942node *
Guido van Rossum82598051997-03-05 00:20:32 +0000943PyParser_SimpleParseFile(fp, filename, start)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000944 FILE *fp;
945 char *filename;
946 int start;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000947{
Guido van Rossuma110aa61994-08-29 12:50:44 +0000948 node *n;
949 perrdetail err;
Guido van Rossum82598051997-03-05 00:20:32 +0000950 n = PyParser_ParseFile(fp, filename, &_PyParser_Grammar, start,
Guido van Rossuma110aa61994-08-29 12:50:44 +0000951 (char *)0, (char *)0, &err);
Guido van Rossuma110aa61994-08-29 12:50:44 +0000952 if (n == NULL)
953 err_input(&err);
954 return n;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000955}
956
Guido van Rossuma110aa61994-08-29 12:50:44 +0000957/* Simplified interface to parsestring -- return node or set exception */
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000958
Guido van Rossuma110aa61994-08-29 12:50:44 +0000959node *
Guido van Rossum82598051997-03-05 00:20:32 +0000960PyParser_SimpleParseString(str, start)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000961 char *str;
962 int start;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000963{
Guido van Rossuma110aa61994-08-29 12:50:44 +0000964 node *n;
965 perrdetail err;
Guido van Rossum82598051997-03-05 00:20:32 +0000966 n = PyParser_ParseString(str, &_PyParser_Grammar, start, &err);
Guido van Rossuma110aa61994-08-29 12:50:44 +0000967 if (n == NULL)
968 err_input(&err);
969 return n;
970}
971
972/* Set the error appropriate to the given input error code (see errcode.h) */
973
974static void
975err_input(err)
976 perrdetail *err;
977{
Guido van Rossum82598051997-03-05 00:20:32 +0000978 PyObject *v, *w;
Guido van Rossuma110aa61994-08-29 12:50:44 +0000979 char *msg = NULL;
Guido van Rossum82598051997-03-05 00:20:32 +0000980 v = Py_BuildValue("(ziiz)", err->filename,
Guido van Rossuma110aa61994-08-29 12:50:44 +0000981 err->lineno, err->offset, err->text);
982 if (err->text != NULL) {
983 free(err->text);
984 err->text = NULL;
985 }
986 switch (err->error) {
987 case E_SYNTAX:
988 msg = "invalid syntax";
989 break;
990 case E_TOKEN:
991 msg = "invalid token";
Guido van Rossuma110aa61994-08-29 12:50:44 +0000992 break;
993 case E_INTR:
Guido van Rossum82598051997-03-05 00:20:32 +0000994 PyErr_SetNone(PyExc_KeyboardInterrupt);
Barry Warsawc80baa31999-01-27 16:39:40 +0000995 Py_XDECREF(v);
Guido van Rossuma110aa61994-08-29 12:50:44 +0000996 return;
997 case E_NOMEM:
Guido van Rossum82598051997-03-05 00:20:32 +0000998 PyErr_NoMemory();
Barry Warsawc80baa31999-01-27 16:39:40 +0000999 Py_XDECREF(v);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001000 return;
1001 case E_EOF:
1002 msg = "unexpected EOF while parsing";
1003 break;
Guido van Rossum560e8ad1998-04-10 19:43:42 +00001004 case E_INDENT:
1005 msg = "inconsistent use of tabs and spaces in indentation";
1006 break;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001007 default:
1008 fprintf(stderr, "error=%d\n", err->error);
1009 msg = "unknown parsing error";
1010 break;
1011 }
Guido van Rossum82598051997-03-05 00:20:32 +00001012 w = Py_BuildValue("(sO)", msg, v);
1013 Py_XDECREF(v);
1014 PyErr_SetObject(PyExc_SyntaxError, w);
1015 Py_XDECREF(w);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001016}
1017
1018/* Print fatal error message and abort */
1019
1020void
Guido van Rossum82598051997-03-05 00:20:32 +00001021Py_FatalError(msg)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001022 char *msg;
1023{
Guido van Rossum83dd6c31994-09-29 09:38:33 +00001024 fprintf(stderr, "Fatal Python error: %s\n", msg);
Guido van Rossum8ae87c01995-01-26 00:40:38 +00001025#ifdef macintosh
1026 for (;;);
1027#endif
Guido van Rossum9b38a141996-09-11 23:12:24 +00001028#ifdef MS_WIN32
Guido van Rossum23c94461997-05-22 20:21:30 +00001029 OutputDebugString("Fatal Python error: ");
Guido van Rossuma44823b1995-03-14 15:01:17 +00001030 OutputDebugString(msg);
1031 OutputDebugString("\n");
Guido van Rossum0ba35361998-08-13 13:33:16 +00001032#ifdef _DEBUG
1033 DebugBreak();
Guido van Rossuma44823b1995-03-14 15:01:17 +00001034#endif
Guido van Rossum0ba35361998-08-13 13:33:16 +00001035#endif /* MS_WIN32 */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001036 abort();
1037}
1038
1039/* Clean up and exit */
1040
Guido van Rossuma110aa61994-08-29 12:50:44 +00001041#ifdef WITH_THREAD
Guido van Rossum49b56061998-10-01 20:42:43 +00001042#include "pythread.h"
Guido van Rossum82598051997-03-05 00:20:32 +00001043int _PyThread_Started = 0; /* Set by threadmodule.c and maybe others */
Guido van Rossumf9f2e821992-08-17 08:59:08 +00001044#endif
1045
Guido van Rossum2dcfc961998-10-01 16:01:57 +00001046#define NEXITFUNCS 32
Guido van Rossum1662dd51994-09-07 14:38:28 +00001047static void (*exitfuncs[NEXITFUNCS])();
1048static int nexitfuncs = 0;
1049
1050int Py_AtExit(func)
Guido van Rossum82598051997-03-05 00:20:32 +00001051 void (*func) Py_PROTO((void));
Guido van Rossum1662dd51994-09-07 14:38:28 +00001052{
1053 if (nexitfuncs >= NEXITFUNCS)
1054 return -1;
1055 exitfuncs[nexitfuncs++] = func;
1056 return 0;
1057}
1058
Guido van Rossumcc283f51997-08-05 02:22:03 +00001059static void
1060call_sys_exitfunc()
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001061{
Guido van Rossum82598051997-03-05 00:20:32 +00001062 PyObject *exitfunc = PySys_GetObject("exitfunc");
Guido van Rossum59bff391992-09-03 20:28:00 +00001063
1064 if (exitfunc) {
Guido van Rossumbf02fb21998-04-03 21:12:12 +00001065 PyObject *res, *f;
Guido van Rossum82598051997-03-05 00:20:32 +00001066 Py_INCREF(exitfunc);
1067 PySys_SetObject("exitfunc", (PyObject *)NULL);
Guido van Rossumbf02fb21998-04-03 21:12:12 +00001068 f = PySys_GetObject("stderr");
Guido van Rossum82598051997-03-05 00:20:32 +00001069 res = PyEval_CallObject(exitfunc, (PyObject *)NULL);
Guido van Rossum59bff391992-09-03 20:28:00 +00001070 if (res == NULL) {
Guido van Rossumbf02fb21998-04-03 21:12:12 +00001071 if (f)
1072 PyFile_WriteString("Error in sys.exitfunc:\n", f);
Guido van Rossum82598051997-03-05 00:20:32 +00001073 PyErr_Print();
Guido van Rossum59bff391992-09-03 20:28:00 +00001074 }
Guido van Rossum82598051997-03-05 00:20:32 +00001075 Py_DECREF(exitfunc);
Guido van Rossum59bff391992-09-03 20:28:00 +00001076 }
1077
Guido van Rossum0829c751998-02-28 04:31:39 +00001078 if (Py_FlushLine())
1079 PyErr_Clear();
Guido van Rossumcc283f51997-08-05 02:22:03 +00001080}
Guido van Rossum1662dd51994-09-07 14:38:28 +00001081
Guido van Rossumcc283f51997-08-05 02:22:03 +00001082static void
1083call_ll_exitfuncs()
1084{
Guido van Rossum1662dd51994-09-07 14:38:28 +00001085 while (nexitfuncs > 0)
1086 (*exitfuncs[--nexitfuncs])();
Guido van Rossum25ce5661997-08-02 03:10:38 +00001087
1088 fflush(stdout);
1089 fflush(stderr);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001090}
1091
1092void
Guido van Rossum82598051997-03-05 00:20:32 +00001093Py_Exit(sts)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001094 int sts;
1095{
Guido van Rossumcc283f51997-08-05 02:22:03 +00001096 Py_Finalize();
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001097
Jack Jansen66a89771995-10-27 13:22:14 +00001098#ifdef macintosh
1099 PyMac_Exit(sts);
1100#else
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001101 exit(sts);
Jack Jansen66a89771995-10-27 13:22:14 +00001102#endif
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001103}
1104
Guido van Rossumf1dc5661993-07-05 10:31:29 +00001105static void
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001106initsigs()
1107{
Guido van Rossuma110aa61994-08-29 12:50:44 +00001108#ifdef HAVE_SIGNAL_H
1109#ifdef SIGPIPE
1110 signal(SIGPIPE, SIG_IGN);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001111#endif
Guido van Rossuma110aa61994-08-29 12:50:44 +00001112#endif /* HAVE_SIGNAL_H */
Guido van Rossum82598051997-03-05 00:20:32 +00001113 PyOS_InitInterrupts(); /* May imply initsignal() */
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001114}
1115
Guido van Rossumaae0d321996-05-22 16:35:33 +00001116#ifdef Py_TRACE_REFS
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001117/* Ask a yes/no question */
1118
Guido van Rossum59bff391992-09-03 20:28:00 +00001119int
Guido van Rossum82598051997-03-05 00:20:32 +00001120_Py_AskYesNo(prompt)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001121 char *prompt;
1122{
1123 char buf[256];
1124
1125 printf("%s [ny] ", prompt);
1126 if (fgets(buf, sizeof buf, stdin) == NULL)
1127 return 0;
1128 return buf[0] == 'y' || buf[0] == 'Y';
1129}
1130#endif
1131
Guido van Rossuma110aa61994-08-29 12:50:44 +00001132#ifdef MPW
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001133
1134/* Check for file descriptor connected to interactive device.
1135 Pretend that stdin is always interactive, other files never. */
1136
1137int
1138isatty(fd)
1139 int fd;
1140{
1141 return fd == fileno(stdin);
1142}
1143
1144#endif
Guido van Rossum7433b121997-02-14 19:45:36 +00001145
1146/*
1147 * The file descriptor fd is considered ``interactive'' if either
1148 * a) isatty(fd) is TRUE, or
1149 * b) the -i flag was given, and the filename associated with
1150 * the descriptor is NULL or "<stdin>" or "???".
1151 */
1152int
1153Py_FdIsInteractive(fp, filename)
1154 FILE *fp;
1155 char *filename;
1156{
1157 if (isatty((int)fileno(fp)))
1158 return 1;
1159 if (!Py_InteractiveFlag)
1160 return 0;
1161 return (filename == NULL) ||
1162 (strcmp(filename, "<stdin>") == 0) ||
1163 (strcmp(filename, "???") == 0);
1164}