blob: 374060724ebc042843b394946d0ed00664d2e640 [file] [log] [blame]
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001
2/* Python interpreter top-level routines, including init/exit */
3
Guido van Rossum82598051997-03-05 00:20:32 +00004#include "Python.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +00005
6#include "grammar.h"
7#include "node.h"
Fred Drake85f36392000-07-11 17:53:00 +00008#include "token.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +00009#include "parsetok.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +000010#include "errcode.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +000011#include "compile.h"
Jeremy Hylton4b38da62001-02-02 18:19:15 +000012#include "symtable.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +000013#include "eval.h"
Guido van Rossumfdef2711994-09-14 13:31:04 +000014#include "marshal.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +000015
Guido van Rossum80bb9651996-12-05 23:27:02 +000016#ifdef HAVE_UNISTD_H
17#include <unistd.h>
18#endif
19
Guido van Rossuma110aa61994-08-29 12:50:44 +000020#ifdef HAVE_SIGNAL_H
Guido van Rossuma9e7dc11992-10-18 18:53:57 +000021#include <signal.h>
Guido van Rossuma9e7dc11992-10-18 18:53:57 +000022#endif
23
Guido van Rossum9b38a141996-09-11 23:12:24 +000024#ifdef MS_WIN32
Guido van Rossuma44823b1995-03-14 15:01:17 +000025#undef BYTE
26#include "windows.h"
27#endif
28
Jack Jansencbf630f2000-07-11 21:59:16 +000029#ifdef macintosh
30#include "macglue.h"
31#endif
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000032extern char *Py_GetPath(void);
Guido van Rossum1984f1e1992-08-04 12:41:02 +000033
Guido van Rossum82598051997-03-05 00:20:32 +000034extern grammar _PyParser_Grammar; /* From graminit.c */
Guido van Rossum1984f1e1992-08-04 12:41:02 +000035
Guido van Rossumb73cc041993-11-01 16:28:59 +000036/* Forward */
Tim Petersdbd9ba62000-07-09 03:09:57 +000037static void initmain(void);
38static void initsite(void);
Jeremy Hylton9f324e92001-03-01 22:59:14 +000039static PyObject *run_err_node(node *, char *, PyObject *, PyObject *,
40 PyCompilerFlags *);
41static PyObject *run_node(node *, char *, PyObject *, PyObject *,
42 PyCompilerFlags *);
Jeremy Hyltonbc320242001-03-22 02:47:58 +000043static PyObject *run_pyc_file(FILE *, char *, PyObject *, PyObject *,
44 PyCompilerFlags *);
Tim Petersdbd9ba62000-07-09 03:09:57 +000045static void err_input(perrdetail *);
46static void initsigs(void);
47static void call_sys_exitfunc(void);
48static void call_ll_exitfuncs(void);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +000049
Guido van Rossumbffd6832000-01-20 22:32:56 +000050#ifdef Py_TRACE_REFS
51int _Py_AskYesNo(char *prompt);
52#endif
53
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000054extern void _PyUnicode_Init(void);
55extern void _PyUnicode_Fini(void);
56extern void _PyCodecRegistry_Init(void);
57extern void _PyCodecRegistry_Fini(void);
Guido van Rossumc94044c2000-03-10 23:03:54 +000058
Guido van Rossum82598051997-03-05 00:20:32 +000059int Py_DebugFlag; /* Needed by parser.c */
60int Py_VerboseFlag; /* Needed by import.c */
Guido van Rossum7433b121997-02-14 19:45:36 +000061int Py_InteractiveFlag; /* Needed by Py_FdIsInteractive() below */
Guido van Rossumdcc0c131997-08-29 22:32:42 +000062int Py_NoSiteFlag; /* Suppress 'import site' */
Barry Warsaw3ce09642000-05-02 19:18:59 +000063int Py_UseClassExceptionsFlag = 1; /* Needed by bltinmodule.c: deprecated */
Guido van Rossuma61691e1998-02-06 22:27:24 +000064int Py_FrozenFlag; /* Needed by getpath.c */
Guido van Rossumb16d1972000-05-01 17:55:15 +000065int Py_UnicodeFlag = 0; /* Needed by compile.c */
Guido van Rossum1984f1e1992-08-04 12:41:02 +000066
Guido van Rossum25ce5661997-08-02 03:10:38 +000067static int initialized = 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +000068
Thomas Wouters7e474022000-07-16 12:04:32 +000069/* API to access the initialized flag -- useful for esoteric use */
Guido van Rossume3c0d5e1997-08-22 04:20:13 +000070
71int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000072Py_IsInitialized(void)
Guido van Rossume3c0d5e1997-08-22 04:20:13 +000073{
74 return initialized;
75}
76
Guido van Rossum25ce5661997-08-02 03:10:38 +000077/* Global initializations. Can be undone by Py_Finalize(). Don't
78 call this twice without an intervening Py_Finalize() call. When
79 initializations fail, a fatal error is issued and the function does
80 not return. On return, the first thread and interpreter state have
81 been created.
Guido van Rossum1984f1e1992-08-04 12:41:02 +000082
Guido van Rossum25ce5661997-08-02 03:10:38 +000083 Locking: you must hold the interpreter lock while calling this.
84 (If the lock has not yet been initialized, that's equivalent to
85 having the lock, but you cannot use multiple threads.)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +000086
Guido van Rossum25ce5661997-08-02 03:10:38 +000087*/
Guido van Rossuma027efa1997-05-05 20:56:21 +000088
89void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000090Py_Initialize(void)
Guido van Rossuma027efa1997-05-05 20:56:21 +000091{
Guido van Rossuma027efa1997-05-05 20:56:21 +000092 PyInterpreterState *interp;
Guido van Rossum25ce5661997-08-02 03:10:38 +000093 PyThreadState *tstate;
94 PyObject *bimod, *sysmod;
Guido van Rossumad6dfda1997-07-19 19:17:22 +000095 char *p;
Guido van Rossumad6dfda1997-07-19 19:17:22 +000096
Guido van Rossumdcc0c131997-08-29 22:32:42 +000097 if (initialized)
Guido van Rossumaa615051997-08-20 22:40:18 +000098 return;
Guido van Rossumdcc0c131997-08-29 22:32:42 +000099 initialized = 1;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000100
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000101 if ((p = getenv("PYTHONDEBUG")) && *p != '\0')
Marc-André Lemburgdc3d6062000-08-25 21:00:46 +0000102 Py_DebugFlag = Py_DebugFlag ? Py_DebugFlag : 1;
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000103 if ((p = getenv("PYTHONVERBOSE")) && *p != '\0')
Marc-André Lemburgdc3d6062000-08-25 21:00:46 +0000104 Py_VerboseFlag = Py_VerboseFlag ? Py_VerboseFlag : 1;
Guido van Rossum562f5b11998-10-07 14:50:42 +0000105 if ((p = getenv("PYTHONOPTIMIZE")) && *p != '\0')
Marc-André Lemburgdc3d6062000-08-25 21:00:46 +0000106 Py_OptimizeFlag = Py_OptimizeFlag ? Py_OptimizeFlag : 1;
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000107
Guido van Rossuma027efa1997-05-05 20:56:21 +0000108 interp = PyInterpreterState_New();
109 if (interp == NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000110 Py_FatalError("Py_Initialize: can't make first interpreter");
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000111
Guido van Rossuma027efa1997-05-05 20:56:21 +0000112 tstate = PyThreadState_New(interp);
113 if (tstate == NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000114 Py_FatalError("Py_Initialize: can't make first thread");
Guido van Rossuma027efa1997-05-05 20:56:21 +0000115 (void) PyThreadState_Swap(tstate);
116
Guido van Rossum25ce5661997-08-02 03:10:38 +0000117 interp->modules = PyDict_New();
118 if (interp->modules == NULL)
119 Py_FatalError("Py_Initialize: can't make modules dictionary");
Guido van Rossuma027efa1997-05-05 20:56:21 +0000120
Guido van Rossumc94044c2000-03-10 23:03:54 +0000121 /* Init codec registry */
122 _PyCodecRegistry_Init();
123
124 /* Init Unicode implementation; relies on the codec registry */
125 _PyUnicode_Init();
126
Barry Warsawf242aa02000-05-25 23:09:49 +0000127 bimod = _PyBuiltin_Init();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000128 if (bimod == NULL)
129 Py_FatalError("Py_Initialize: can't initialize __builtin__");
Guido van Rossum4a1f39a1997-11-04 19:36:18 +0000130 interp->builtins = PyModule_GetDict(bimod);
131 Py_INCREF(interp->builtins);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000132
133 sysmod = _PySys_Init();
134 if (sysmod == NULL)
135 Py_FatalError("Py_Initialize: can't initialize sys");
136 interp->sysdict = PyModule_GetDict(sysmod);
137 Py_INCREF(interp->sysdict);
138 _PyImport_FixupExtension("sys", "sys");
Guido van Rossuma027efa1997-05-05 20:56:21 +0000139 PySys_SetPath(Py_GetPath());
Guido van Rossum25ce5661997-08-02 03:10:38 +0000140 PyDict_SetItemString(interp->sysdict, "modules",
141 interp->modules);
142
Guido van Rossum7c85ab81999-07-08 17:26:56 +0000143 _PyImport_Init();
144
Barry Warsawf242aa02000-05-25 23:09:49 +0000145 /* initialize builtin exceptions */
146 init_exceptions();
147
Barry Warsaw035574d1997-08-29 22:07:17 +0000148 /* phase 2 of builtins */
Barry Warsaw963b8711997-09-18 16:42:02 +0000149 _PyImport_FixupExtension("__builtin__", "__builtin__");
Barry Warsaw035574d1997-08-29 22:07:17 +0000150
Guido van Rossum25ce5661997-08-02 03:10:38 +0000151 initsigs(); /* Signal handling stuff, including initintr() */
152
153 initmain(); /* Module __main__ */
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000154 if (!Py_NoSiteFlag)
155 initsite(); /* Module site */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000156}
157
Guido van Rossum2edcf0d1998-12-15 16:12:00 +0000158#ifdef COUNT_ALLOCS
Tim Petersdbd9ba62000-07-09 03:09:57 +0000159extern void dump_counts(void);
Guido van Rossum2edcf0d1998-12-15 16:12:00 +0000160#endif
161
Guido van Rossum25ce5661997-08-02 03:10:38 +0000162/* Undo the effect of Py_Initialize().
163
164 Beware: if multiple interpreter and/or thread states exist, these
165 are not wiped out; only the current thread and interpreter state
166 are deleted. But since everything else is deleted, those other
167 interpreter and thread states should no longer be used.
168
169 (XXX We should do better, e.g. wipe out all interpreters and
170 threads.)
171
172 Locking: as above.
173
174*/
175
176void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000177Py_Finalize(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000178{
179 PyInterpreterState *interp;
180 PyThreadState *tstate;
181
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000182 if (!initialized)
Guido van Rossumaa615051997-08-20 22:40:18 +0000183 return;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000184
Tim Peters384fd102001-01-21 03:40:37 +0000185 /* The interpreter is still entirely intact at this point, and the
186 * exit funcs may be relying on that. In particular, if some thread
187 * or exit func is still waiting to do an import, the import machinery
188 * expects Py_IsInitialized() to return true. So don't say the
189 * interpreter is uninitialized until after the exit funcs have run.
190 * Note that Threading.py uses an exit func to do a join on all the
191 * threads created thru it, so this also protects pending imports in
192 * the threads created via Threading.
193 */
Guido van Rossum4cc462e1998-01-19 22:00:38 +0000194 call_sys_exitfunc();
Tim Peters384fd102001-01-21 03:40:37 +0000195 initialized = 0;
Guido van Rossum4cc462e1998-01-19 22:00:38 +0000196
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000197 /* Get current thread state and interpreter pointer */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000198 tstate = PyThreadState_Get();
199 interp = tstate->interp;
200
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000201 /* Disable signal handling */
202 PyOS_FiniInterrupts();
203
Guido van Rossumc94044c2000-03-10 23:03:54 +0000204 /* Cleanup Unicode implementation */
205 _PyUnicode_Fini();
206
207 /* Cleanup Codec registry */
208 _PyCodecRegistry_Fini();
209
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000210 /* Destroy all modules */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000211 PyImport_Cleanup();
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000212
Guido van Rossum1707aad1997-12-08 23:43:45 +0000213 /* Destroy the database used by _PyImport_{Fixup,Find}Extension */
214 _PyImport_Fini();
215
216 /* Debugging stuff */
217#ifdef COUNT_ALLOCS
218 dump_counts();
219#endif
220
221#ifdef Py_REF_DEBUG
222 fprintf(stderr, "[%ld refs]\n", _Py_RefTotal);
223#endif
224
225#ifdef Py_TRACE_REFS
Guido van Rossumeca47842000-04-27 23:44:15 +0000226 if (
227#ifdef MS_WINDOWS /* Only ask on Windows if env var set */
228 getenv("PYTHONDUMPREFS") &&
229#endif /* MS_WINDOWS */
230 _Py_AskYesNo("Print left references?")) {
Guido van Rossum1707aad1997-12-08 23:43:45 +0000231 _Py_PrintReferences(stderr);
232 }
233#endif /* Py_TRACE_REFS */
234
Barry Warsaw035574d1997-08-29 22:07:17 +0000235 /* Now we decref the exception classes. After this point nothing
236 can raise an exception. That's okay, because each Fini() method
237 below has been checked to make sure no exceptions are ever
238 raised.
239 */
Barry Warsawf242aa02000-05-25 23:09:49 +0000240 fini_exceptions();
241
242 /* Delete current thread */
243 PyInterpreterState_Clear(interp);
244 PyThreadState_Swap(NULL);
245 PyInterpreterState_Delete(interp);
246
Guido van Rossumcc283f51997-08-05 02:22:03 +0000247 PyMethod_Fini();
248 PyFrame_Fini();
249 PyCFunction_Fini();
250 PyTuple_Fini();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000251 PyString_Fini();
Guido van Rossumcc283f51997-08-05 02:22:03 +0000252 PyInt_Fini();
253 PyFloat_Fini();
254
255 /* XXX Still allocated:
256 - various static ad-hoc pointers to interned strings
257 - int and float free list blocks
258 - whatever various modules and libraries allocate
259 */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000260
261 PyGrammar_RemoveAccelerators(&_PyParser_Grammar);
Guido van Rossumcc283f51997-08-05 02:22:03 +0000262
263 call_ll_exitfuncs();
264
Guido van Rossumcc283f51997-08-05 02:22:03 +0000265#ifdef Py_TRACE_REFS
Guido van Rossumcc283f51997-08-05 02:22:03 +0000266 _Py_ResetReferences();
267#endif /* Py_TRACE_REFS */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000268}
269
270/* Create and initialize a new interpreter and thread, and return the
271 new thread. This requires that Py_Initialize() has been called
272 first.
273
274 Unsuccessful initialization yields a NULL pointer. Note that *no*
275 exception information is available even in this case -- the
276 exception information is held in the thread, and there is no
277 thread.
278
279 Locking: as above.
280
281*/
282
283PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000284Py_NewInterpreter(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000285{
286 PyInterpreterState *interp;
287 PyThreadState *tstate, *save_tstate;
288 PyObject *bimod, *sysmod;
289
290 if (!initialized)
291 Py_FatalError("Py_NewInterpreter: call Py_Initialize first");
292
293 interp = PyInterpreterState_New();
294 if (interp == NULL)
295 return NULL;
296
297 tstate = PyThreadState_New(interp);
298 if (tstate == NULL) {
299 PyInterpreterState_Delete(interp);
300 return NULL;
301 }
302
303 save_tstate = PyThreadState_Swap(tstate);
304
305 /* XXX The following is lax in error checking */
306
307 interp->modules = PyDict_New();
308
309 bimod = _PyImport_FindExtension("__builtin__", "__builtin__");
310 if (bimod != NULL) {
Guido van Rossum4a1f39a1997-11-04 19:36:18 +0000311 interp->builtins = PyModule_GetDict(bimod);
312 Py_INCREF(interp->builtins);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000313 }
314 sysmod = _PyImport_FindExtension("sys", "sys");
315 if (bimod != NULL && sysmod != NULL) {
316 interp->sysdict = PyModule_GetDict(sysmod);
317 Py_INCREF(interp->sysdict);
318 PySys_SetPath(Py_GetPath());
319 PyDict_SetItemString(interp->sysdict, "modules",
320 interp->modules);
321 initmain();
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000322 if (!Py_NoSiteFlag)
323 initsite();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000324 }
325
326 if (!PyErr_Occurred())
327 return tstate;
328
329 /* Oops, it didn't work. Undo it all. */
330
331 PyErr_Print();
332 PyThreadState_Clear(tstate);
333 PyThreadState_Swap(save_tstate);
334 PyThreadState_Delete(tstate);
335 PyInterpreterState_Delete(interp);
336
337 return NULL;
338}
339
340/* Delete an interpreter and its last thread. This requires that the
341 given thread state is current, that the thread has no remaining
342 frames, and that it is its interpreter's only remaining thread.
343 It is a fatal error to violate these constraints.
344
345 (Py_Finalize() doesn't have these constraints -- it zaps
346 everything, regardless.)
347
348 Locking: as above.
349
350*/
351
352void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000353Py_EndInterpreter(PyThreadState *tstate)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000354{
355 PyInterpreterState *interp = tstate->interp;
356
357 if (tstate != PyThreadState_Get())
358 Py_FatalError("Py_EndInterpreter: thread is not current");
359 if (tstate->frame != NULL)
360 Py_FatalError("Py_EndInterpreter: thread still has a frame");
361 if (tstate != interp->tstate_head || tstate->next != NULL)
362 Py_FatalError("Py_EndInterpreter: not the last thread");
363
364 PyImport_Cleanup();
365 PyInterpreterState_Clear(interp);
366 PyThreadState_Swap(NULL);
367 PyInterpreterState_Delete(interp);
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000368}
369
370static char *progname = "python";
371
372void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000373Py_SetProgramName(char *pn)
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000374{
375 if (pn && *pn)
376 progname = pn;
377}
378
379char *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000380Py_GetProgramName(void)
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000381{
382 return progname;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000383}
384
Guido van Rossuma61691e1998-02-06 22:27:24 +0000385static char *default_home = NULL;
386
387void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000388Py_SetPythonHome(char *home)
Guido van Rossuma61691e1998-02-06 22:27:24 +0000389{
390 default_home = home;
391}
392
393char *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000394Py_GetPythonHome(void)
Guido van Rossuma61691e1998-02-06 22:27:24 +0000395{
396 char *home = default_home;
397 if (home == NULL)
398 home = getenv("PYTHONHOME");
399 return home;
400}
401
Guido van Rossum6135a871995-01-09 17:53:26 +0000402/* Create __main__ module */
403
404static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000405initmain(void)
Guido van Rossum6135a871995-01-09 17:53:26 +0000406{
Guido van Rossum82598051997-03-05 00:20:32 +0000407 PyObject *m, *d;
408 m = PyImport_AddModule("__main__");
Guido van Rossum6135a871995-01-09 17:53:26 +0000409 if (m == NULL)
Guido van Rossum82598051997-03-05 00:20:32 +0000410 Py_FatalError("can't create __main__ module");
411 d = PyModule_GetDict(m);
412 if (PyDict_GetItemString(d, "__builtins__") == NULL) {
Guido van Rossum858cb731997-11-19 16:15:37 +0000413 PyObject *bimod = PyImport_ImportModule("__builtin__");
414 if (bimod == NULL ||
415 PyDict_SetItemString(d, "__builtins__", bimod) != 0)
Guido van Rossum82598051997-03-05 00:20:32 +0000416 Py_FatalError("can't add __builtins__ to __main__");
Barry Warsaw3d05b1a1999-01-29 21:30:22 +0000417 Py_DECREF(bimod);
Guido van Rossum6135a871995-01-09 17:53:26 +0000418 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000419}
420
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000421/* Import the site module (not into __main__ though) */
422
423static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000424initsite(void)
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000425{
426 PyObject *m, *f;
427 m = PyImport_ImportModule("site");
428 if (m == NULL) {
429 f = PySys_GetObject("stderr");
430 if (Py_VerboseFlag) {
431 PyFile_WriteString(
432 "'import site' failed; traceback:\n", f);
433 PyErr_Print();
434 }
435 else {
436 PyFile_WriteString(
437 "'import site' failed; use -v for traceback\n", f);
438 PyErr_Clear();
439 }
440 }
441 else {
442 Py_DECREF(m);
443 }
444}
445
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000446/* Parse input from a file and execute it */
447
448int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000449PyRun_AnyFile(FILE *fp, char *filename)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000450{
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000451 return PyRun_AnyFileExFlags(fp, filename, 0, NULL);
452}
453
454int
455PyRun_AnyFileFlags(FILE *fp, char *filename, PyCompilerFlags *flags)
456{
457 return PyRun_AnyFileExFlags(fp, filename, 0, flags);
Guido van Rossum0df002c2000-08-27 19:21:52 +0000458}
459
460int
461PyRun_AnyFileEx(FILE *fp, char *filename, int closeit)
462{
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000463 return PyRun_AnyFileExFlags(fp, filename, closeit, NULL);
464}
465
466int
467PyRun_AnyFileExFlags(FILE *fp, char *filename, int closeit,
468 PyCompilerFlags *flags)
469{
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000470 if (filename == NULL)
471 filename = "???";
Guido van Rossum0df002c2000-08-27 19:21:52 +0000472 if (Py_FdIsInteractive(fp, filename)) {
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000473 int err = PyRun_InteractiveLoopFlags(fp, filename, flags);
Guido van Rossum0df002c2000-08-27 19:21:52 +0000474 if (closeit)
475 fclose(fp);
476 return err;
477 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000478 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000479 return PyRun_SimpleFileExFlags(fp, filename, closeit, flags);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000480}
481
482int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000483PyRun_InteractiveLoop(FILE *fp, char *filename)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000484{
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000485 return PyRun_InteractiveLoopFlags(fp, filename, NULL);
486}
487
488int
489PyRun_InteractiveLoopFlags(FILE *fp, char *filename, PyCompilerFlags *flags)
490{
Guido van Rossum82598051997-03-05 00:20:32 +0000491 PyObject *v;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000492 int ret;
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000493 PyCompilerFlags local_flags;
Jeremy Hylton9f324e92001-03-01 22:59:14 +0000494
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000495 if (flags == NULL) {
496 flags = &local_flags;
497 local_flags.cf_nested_scopes = 0;
498 }
Guido van Rossum82598051997-03-05 00:20:32 +0000499 v = PySys_GetObject("ps1");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000500 if (v == NULL) {
Guido van Rossum82598051997-03-05 00:20:32 +0000501 PySys_SetObject("ps1", v = PyString_FromString(">>> "));
502 Py_XDECREF(v);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000503 }
Guido van Rossum82598051997-03-05 00:20:32 +0000504 v = PySys_GetObject("ps2");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000505 if (v == NULL) {
Guido van Rossum82598051997-03-05 00:20:32 +0000506 PySys_SetObject("ps2", v = PyString_FromString("... "));
507 Py_XDECREF(v);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000508 }
509 for (;;) {
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000510 ret = PyRun_InteractiveOneFlags(fp, filename, flags);
Guido van Rossumaae0d321996-05-22 16:35:33 +0000511#ifdef Py_REF_DEBUG
Guido van Rossum6f9e4331995-03-29 16:57:48 +0000512 fprintf(stderr, "[%ld refs]\n", _Py_RefTotal);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000513#endif
514 if (ret == E_EOF)
515 return 0;
516 /*
517 if (ret == E_NOMEM)
518 return -1;
519 */
520 }
521}
522
523int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000524PyRun_InteractiveOne(FILE *fp, char *filename)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000525{
Jeremy Hylton9f324e92001-03-01 22:59:14 +0000526 return PyRun_InteractiveOneFlags(fp, filename, NULL);
527}
528
529int
530PyRun_InteractiveOneFlags(FILE *fp, char *filename, PyCompilerFlags *flags)
531{
Guido van Rossum82598051997-03-05 00:20:32 +0000532 PyObject *m, *d, *v, *w;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000533 node *n;
Guido van Rossuma110aa61994-08-29 12:50:44 +0000534 perrdetail err;
Guido van Rossumddc3fb51997-11-25 20:58:13 +0000535 char *ps1 = "", *ps2 = "";
Guido van Rossum82598051997-03-05 00:20:32 +0000536 v = PySys_GetObject("ps1");
Guido van Rossumddc3fb51997-11-25 20:58:13 +0000537 if (v != NULL) {
538 v = PyObject_Str(v);
539 if (v == NULL)
540 PyErr_Clear();
541 else if (PyString_Check(v))
542 ps1 = PyString_AsString(v);
543 }
Guido van Rossum82598051997-03-05 00:20:32 +0000544 w = PySys_GetObject("ps2");
Guido van Rossumddc3fb51997-11-25 20:58:13 +0000545 if (w != NULL) {
546 w = PyObject_Str(w);
547 if (w == NULL)
548 PyErr_Clear();
549 else if (PyString_Check(w))
550 ps2 = PyString_AsString(w);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000551 }
Guido van Rossum82598051997-03-05 00:20:32 +0000552 n = PyParser_ParseFile(fp, filename, &_PyParser_Grammar,
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000553 Py_single_input, ps1, ps2, &err);
Guido van Rossum82598051997-03-05 00:20:32 +0000554 Py_XDECREF(v);
555 Py_XDECREF(w);
Guido van Rossuma110aa61994-08-29 12:50:44 +0000556 if (n == NULL) {
557 if (err.error == E_EOF) {
558 if (err.text)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000559 PyMem_DEL(err.text);
Guido van Rossuma110aa61994-08-29 12:50:44 +0000560 return E_EOF;
561 }
562 err_input(&err);
Guido van Rossum82598051997-03-05 00:20:32 +0000563 PyErr_Print();
Guido van Rossuma110aa61994-08-29 12:50:44 +0000564 return err.error;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000565 }
Guido van Rossum82598051997-03-05 00:20:32 +0000566 m = PyImport_AddModule("__main__");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000567 if (m == NULL)
568 return -1;
Guido van Rossum82598051997-03-05 00:20:32 +0000569 d = PyModule_GetDict(m);
Jeremy Hylton9f324e92001-03-01 22:59:14 +0000570 v = run_node(n, filename, d, d, flags);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000571 if (v == NULL) {
Guido van Rossum82598051997-03-05 00:20:32 +0000572 PyErr_Print();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000573 return -1;
574 }
Guido van Rossum82598051997-03-05 00:20:32 +0000575 Py_DECREF(v);
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000576 if (Py_FlushLine())
577 PyErr_Clear();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000578 return 0;
579}
580
581int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000582PyRun_SimpleFile(FILE *fp, char *filename)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000583{
Guido van Rossum0df002c2000-08-27 19:21:52 +0000584 return PyRun_SimpleFileEx(fp, filename, 0);
585}
586
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000587/* Check whether a file maybe a pyc file: Look at the extension,
588 the file type, and, if we may close it, at the first few bytes. */
589
590static int
591maybe_pyc_file(FILE *fp, char* filename, char* ext, int closeit)
592{
593 if (strcmp(ext, ".pyc") == 0 || strcmp(ext, ".pyo") == 0)
594 return 1;
595
596#ifdef macintosh
597 /* On a mac, we also assume a pyc file for types 'PYC ' and 'APPL' */
598 if (PyMac_getfiletype(filename) == 'PYC '
599 || PyMac_getfiletype(filename) == 'APPL')
600 return 1;
601#endif /* macintosh */
602
603 /* Only look into the file if we are allowed to close it, since
604 it then should also be seekable. */
605 if (closeit) {
606 /* Read only two bytes of the magic. If the file was opened in
607 text mode, the bytes 3 and 4 of the magic (\r\n) might not
608 be read as they are on disk. */
609 unsigned int halfmagic = PyImport_GetMagicNumber() & 0xFFFF;
610 unsigned char buf[2];
Tim Peters3e876562001-02-11 04:35:39 +0000611 /* Mess: In case of -x, the stream is NOT at its start now,
612 and ungetc() was used to push back the first newline,
Tim Peters6f5a4ef2001-02-17 22:02:34 +0000613 which makes the current stream position formally undefined,
614 and a x-platform nightmare.
615 Unfortunately, we have no direct way to know whether -x
616 was specified. So we use a terrible hack: if the current
617 stream position is not 0, we assume -x was specified, and
618 give up. Bug 132850 on SourceForge spells out the
619 hopelessness of trying anything else (fseek and ftell
620 don't work predictably x-platform for text-mode files).
Tim Peters3e876562001-02-11 04:35:39 +0000621 */
Tim Peters3e876562001-02-11 04:35:39 +0000622 int ispyc = 0;
Tim Peters6f5a4ef2001-02-17 22:02:34 +0000623 if (ftell(fp) == 0) {
624 if (fread(buf, 1, 2, fp) == 2 &&
625 ((unsigned int)buf[1]<<8 | buf[0]) == halfmagic)
626 ispyc = 1;
627 rewind(fp);
628 }
Tim Peters3e876562001-02-11 04:35:39 +0000629 return ispyc;
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000630 }
631 return 0;
632}
633
Guido van Rossum0df002c2000-08-27 19:21:52 +0000634int
635PyRun_SimpleFileEx(FILE *fp, char *filename, int closeit)
636{
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000637 return PyRun_SimpleFileExFlags(fp, filename, closeit, NULL);
638}
639
640int
641PyRun_SimpleFileExFlags(FILE *fp, char *filename, int closeit,
642 PyCompilerFlags *flags)
643{
Guido van Rossum82598051997-03-05 00:20:32 +0000644 PyObject *m, *d, *v;
Guido van Rossumfdef2711994-09-14 13:31:04 +0000645 char *ext;
646
Guido van Rossum82598051997-03-05 00:20:32 +0000647 m = PyImport_AddModule("__main__");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000648 if (m == NULL)
649 return -1;
Guido van Rossum82598051997-03-05 00:20:32 +0000650 d = PyModule_GetDict(m);
Guido van Rossumfdef2711994-09-14 13:31:04 +0000651 ext = filename + strlen(filename) - 4;
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000652 if (maybe_pyc_file(fp, filename, ext, closeit)) {
Guido van Rossumfdef2711994-09-14 13:31:04 +0000653 /* Try to run a pyc file. First, re-open in binary */
Guido van Rossum0df002c2000-08-27 19:21:52 +0000654 if (closeit)
655 fclose(fp);
Guido van Rossumfdef2711994-09-14 13:31:04 +0000656 if( (fp = fopen(filename, "rb")) == NULL ) {
657 fprintf(stderr, "python: Can't reopen .pyc file\n");
658 return -1;
659 }
Guido van Rossum2a7f58d1997-04-02 05:28:38 +0000660 /* Turn on optimization if a .pyo file is given */
661 if (strcmp(ext, ".pyo") == 0)
662 Py_OptimizeFlag = 1;
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000663 v = run_pyc_file(fp, filename, d, d, flags);
Guido van Rossumfdef2711994-09-14 13:31:04 +0000664 } else {
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000665 v = PyRun_FileExFlags(fp, filename, Py_file_input, d, d,
666 closeit, flags);
Guido van Rossumfdef2711994-09-14 13:31:04 +0000667 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000668 if (v == NULL) {
Guido van Rossum82598051997-03-05 00:20:32 +0000669 PyErr_Print();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000670 return -1;
671 }
Guido van Rossum82598051997-03-05 00:20:32 +0000672 Py_DECREF(v);
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000673 if (Py_FlushLine())
674 PyErr_Clear();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000675 return 0;
676}
677
678int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000679PyRun_SimpleString(char *command)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000680{
Guido van Rossum82598051997-03-05 00:20:32 +0000681 PyObject *m, *d, *v;
682 m = PyImport_AddModule("__main__");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000683 if (m == NULL)
684 return -1;
Guido van Rossum82598051997-03-05 00:20:32 +0000685 d = PyModule_GetDict(m);
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000686 v = PyRun_String(command, Py_file_input, d, d);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000687 if (v == NULL) {
Guido van Rossum82598051997-03-05 00:20:32 +0000688 PyErr_Print();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000689 return -1;
690 }
Guido van Rossum82598051997-03-05 00:20:32 +0000691 Py_DECREF(v);
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000692 if (Py_FlushLine())
693 PyErr_Clear();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000694 return 0;
695}
696
Barry Warsaw035574d1997-08-29 22:07:17 +0000697static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000698parse_syntax_error(PyObject *err, PyObject **message, char **filename,
699 int *lineno, int *offset, char **text)
Barry Warsaw035574d1997-08-29 22:07:17 +0000700{
701 long hold;
702 PyObject *v;
703
704 /* old style errors */
705 if (PyTuple_Check(err))
706 return PyArg_Parse(err, "(O(ziiz))", message, filename,
707 lineno, offset, text);
708
709 /* new style errors. `err' is an instance */
710
711 if (! (v = PyObject_GetAttrString(err, "msg")))
712 goto finally;
713 *message = v;
714
715 if (!(v = PyObject_GetAttrString(err, "filename")))
716 goto finally;
717 if (v == Py_None)
718 *filename = NULL;
719 else if (! (*filename = PyString_AsString(v)))
720 goto finally;
721
722 Py_DECREF(v);
723 if (!(v = PyObject_GetAttrString(err, "lineno")))
724 goto finally;
725 hold = PyInt_AsLong(v);
726 Py_DECREF(v);
727 v = NULL;
728 if (hold < 0 && PyErr_Occurred())
729 goto finally;
730 *lineno = (int)hold;
731
732 if (!(v = PyObject_GetAttrString(err, "offset")))
733 goto finally;
Jeremy Hylton9f1b9932001-02-28 07:07:43 +0000734 if (v == Py_None) {
735 *offset = -1;
736 Py_DECREF(v);
737 v = NULL;
738 } else {
739 hold = PyInt_AsLong(v);
740 Py_DECREF(v);
741 v = NULL;
742 if (hold < 0 && PyErr_Occurred())
743 goto finally;
744 *offset = (int)hold;
745 }
Barry Warsaw035574d1997-08-29 22:07:17 +0000746
747 if (!(v = PyObject_GetAttrString(err, "text")))
748 goto finally;
749 if (v == Py_None)
750 *text = NULL;
751 else if (! (*text = PyString_AsString(v)))
752 goto finally;
753 Py_DECREF(v);
754 return 1;
755
756finally:
757 Py_XDECREF(v);
758 return 0;
759}
760
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000761void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000762PyErr_Print(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000763{
Guido van Rossuma61691e1998-02-06 22:27:24 +0000764 PyErr_PrintEx(1);
765}
766
Jeremy Hylton9f1b9932001-02-28 07:07:43 +0000767static void
768print_error_text(PyObject *f, int offset, char *text)
769{
770 char *nl;
771 if (offset >= 0) {
772 if (offset > 0 && offset == (int)strlen(text))
773 offset--;
774 for (;;) {
775 nl = strchr(text, '\n');
776 if (nl == NULL || nl-text >= offset)
777 break;
778 offset -= (nl+1-text);
779 text = nl+1;
780 }
781 while (*text == ' ' || *text == '\t') {
782 text++;
783 offset--;
784 }
785 }
786 PyFile_WriteString(" ", f);
787 PyFile_WriteString(text, f);
788 if (*text == '\0' || text[strlen(text)-1] != '\n')
789 PyFile_WriteString("\n", f);
790 if (offset == -1)
791 return;
792 PyFile_WriteString(" ", f);
793 offset--;
794 while (offset > 0) {
795 PyFile_WriteString(" ", f);
796 offset--;
797 }
798 PyFile_WriteString("^\n", f);
799}
800
Guido van Rossuma61691e1998-02-06 22:27:24 +0000801void
Ka-Ping Yee26fabb02001-03-23 15:36:41 +0000802PyRun_HandleSystemExit(PyObject* value)
803{
804 if (Py_FlushLine())
805 PyErr_Clear();
806 fflush(stdout);
807 if (value == NULL || value == Py_None)
808 Py_Exit(0);
809 if (PyInstance_Check(value)) {
810 /* The error code should be in the `code' attribute. */
811 PyObject *code = PyObject_GetAttrString(value, "code");
812 if (code) {
813 Py_DECREF(value);
814 value = code;
815 if (value == Py_None)
816 Py_Exit(0);
817 }
818 /* If we failed to dig out the 'code' attribute,
819 just let the else clause below print the error. */
820 }
821 if (PyInt_Check(value))
822 Py_Exit((int)PyInt_AsLong(value));
823 else {
824 PyObject_Print(value, stderr, Py_PRINT_RAW);
825 PySys_WriteStderr("\n");
826 Py_Exit(1);
827 }
828}
829
830void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000831PyErr_PrintEx(int set_sys_last_vars)
Guido van Rossuma61691e1998-02-06 22:27:24 +0000832{
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000833 PyObject *exception, *v, *tb, *hook;
Guido van Rossum82598051997-03-05 00:20:32 +0000834 PyErr_Fetch(&exception, &v, &tb);
Barry Warsaw035574d1997-08-29 22:07:17 +0000835 PyErr_NormalizeException(&exception, &v, &tb);
836
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000837 if (exception == NULL)
Guido van Rossum296b4751997-05-23 00:19:20 +0000838 return;
Barry Warsaw035574d1997-08-29 22:07:17 +0000839
Barry Warsaw36b8f941997-08-26 18:09:48 +0000840 if (PyErr_GivenExceptionMatches(exception, PyExc_SystemExit)) {
Ka-Ping Yee26fabb02001-03-23 15:36:41 +0000841 PyRun_HandleSystemExit(v);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000842 }
Guido van Rossuma61691e1998-02-06 22:27:24 +0000843 if (set_sys_last_vars) {
844 PySys_SetObject("last_type", exception);
845 PySys_SetObject("last_value", v);
846 PySys_SetObject("last_traceback", tb);
847 }
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000848 hook = PySys_GetObject("excepthook");
849 if (hook) {
850 PyObject *args = Py_BuildValue("(OOO)",
851 exception, v ? v : Py_None, tb ? tb : Py_None);
852 PyObject *result = PyEval_CallObject(hook, args);
853 if (result == NULL) {
854 PyObject *exception2, *v2, *tb2;
855 PyErr_Fetch(&exception2, &v2, &tb2);
856 PyErr_NormalizeException(&exception2, &v2, &tb2);
Ka-Ping Yee26fabb02001-03-23 15:36:41 +0000857 if (PyErr_GivenExceptionMatches(
858 exception2, PyExc_SystemExit)) {
859 PyRun_HandleSystemExit(v2);
860 }
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000861 if (Py_FlushLine())
862 PyErr_Clear();
863 fflush(stdout);
864 PySys_WriteStderr("Error in sys.excepthook:\n");
865 PyErr_Display(exception2, v2, tb2);
866 PySys_WriteStderr("\nOriginal exception was:\n");
867 PyErr_Display(exception, v, tb);
868 }
869 Py_XDECREF(result);
870 Py_XDECREF(args);
871 } else {
872 PySys_WriteStderr("sys.excepthook is missing\n");
873 PyErr_Display(exception, v, tb);
874 }
875 Py_XDECREF(exception);
876 Py_XDECREF(v);
877 Py_XDECREF(tb);
878}
879
880void PyErr_Display(PyObject *exception, PyObject *value, PyObject *tb)
881{
882 int err = 0;
883 PyObject *v = value;
884 PyObject *f = PySys_GetObject("stderr");
Guido van Rossum3165fe61992-09-25 21:59:05 +0000885 if (f == NULL)
886 fprintf(stderr, "lost sys.stderr\n");
887 else {
Guido van Rossum0829c751998-02-28 04:31:39 +0000888 if (Py_FlushLine())
889 PyErr_Clear();
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000890 fflush(stdout);
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +0000891 if (tb && tb != Py_None)
892 err = PyTraceBack_Print(tb, f);
Barry Warsaw36b8f941997-08-26 18:09:48 +0000893 if (err == 0 &&
894 PyErr_GivenExceptionMatches(exception, PyExc_SyntaxError))
895 {
Guido van Rossum82598051997-03-05 00:20:32 +0000896 PyObject *message;
Guido van Rossuma110aa61994-08-29 12:50:44 +0000897 char *filename, *text;
898 int lineno, offset;
Barry Warsaw035574d1997-08-29 22:07:17 +0000899 if (!parse_syntax_error(v, &message, &filename,
900 &lineno, &offset, &text))
Guido van Rossum82598051997-03-05 00:20:32 +0000901 PyErr_Clear();
Guido van Rossuma110aa61994-08-29 12:50:44 +0000902 else {
903 char buf[10];
Guido van Rossum82598051997-03-05 00:20:32 +0000904 PyFile_WriteString(" File \"", f);
Guido van Rossuma110aa61994-08-29 12:50:44 +0000905 if (filename == NULL)
Guido van Rossum82598051997-03-05 00:20:32 +0000906 PyFile_WriteString("<string>", f);
Guido van Rossuma110aa61994-08-29 12:50:44 +0000907 else
Guido van Rossum82598051997-03-05 00:20:32 +0000908 PyFile_WriteString(filename, f);
909 PyFile_WriteString("\", line ", f);
Guido van Rossuma110aa61994-08-29 12:50:44 +0000910 sprintf(buf, "%d", lineno);
Guido van Rossum82598051997-03-05 00:20:32 +0000911 PyFile_WriteString(buf, f);
912 PyFile_WriteString("\n", f);
Jeremy Hylton9f1b9932001-02-28 07:07:43 +0000913 if (text != NULL)
914 print_error_text(f, offset, text);
Guido van Rossuma110aa61994-08-29 12:50:44 +0000915 v = message;
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000916 /* Can't be bothered to check all those
917 PyFile_WriteString() calls */
918 if (PyErr_Occurred())
919 err = -1;
Guido van Rossuma110aa61994-08-29 12:50:44 +0000920 }
921 }
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000922 if (err) {
923 /* Don't do anything else */
924 }
925 else if (PyClass_Check(exception)) {
Barry Warsaw2f5f6a21997-09-16 21:42:03 +0000926 PyClassObject* exc = (PyClassObject*)exception;
927 PyObject* className = exc->cl_name;
928 PyObject* moduleName =
929 PyDict_GetItemString(exc->cl_dict, "__module__");
930
931 if (moduleName == NULL)
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000932 err = PyFile_WriteString("<unknown>", f);
Barry Warsaw2f5f6a21997-09-16 21:42:03 +0000933 else {
934 char* modstr = PyString_AsString(moduleName);
935 if (modstr && strcmp(modstr, "exceptions"))
936 {
937 err = PyFile_WriteString(modstr, f);
938 err += PyFile_WriteString(".", f);
939 }
940 }
941 if (err == 0) {
942 if (className == NULL)
943 err = PyFile_WriteString("<unknown>", f);
944 else
945 err = PyFile_WriteObject(className, f,
946 Py_PRINT_RAW);
947 }
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000948 }
949 else
950 err = PyFile_WriteObject(exception, f, Py_PRINT_RAW);
951 if (err == 0) {
952 if (v != NULL && v != Py_None) {
Barry Warsaw035574d1997-08-29 22:07:17 +0000953 PyObject *s = PyObject_Str(v);
954 /* only print colon if the str() of the
955 object is not the empty string
956 */
Guido van Rossumd6bf45b1997-09-05 19:11:53 +0000957 if (s == NULL)
958 err = -1;
959 else if (!PyString_Check(s) ||
960 PyString_GET_SIZE(s) != 0)
Barry Warsaw035574d1997-08-29 22:07:17 +0000961 err = PyFile_WriteString(": ", f);
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000962 if (err == 0)
Guido van Rossumd6bf45b1997-09-05 19:11:53 +0000963 err = PyFile_WriteObject(s, f, Py_PRINT_RAW);
964 Py_XDECREF(s);
Guido van Rossum262e1241995-02-07 15:30:45 +0000965 }
Guido van Rossum262e1241995-02-07 15:30:45 +0000966 }
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000967 if (err == 0)
968 err = PyFile_WriteString("\n", f);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000969 }
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000970 /* If an error happened here, don't show it.
971 XXX This is wrong, but too many callers rely on this behavior. */
972 if (err != 0)
973 PyErr_Clear();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000974}
975
Guido van Rossum82598051997-03-05 00:20:32 +0000976PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000977PyRun_String(char *str, int start, PyObject *globals, PyObject *locals)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000978{
Guido van Rossum82598051997-03-05 00:20:32 +0000979 return run_err_node(PyParser_SimpleParseString(str, start),
Jeremy Hylton9f324e92001-03-01 22:59:14 +0000980 "<string>", globals, locals, NULL);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000981}
982
Guido van Rossum82598051997-03-05 00:20:32 +0000983PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000984PyRun_File(FILE *fp, char *filename, int start, PyObject *globals,
985 PyObject *locals)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000986{
Tim Peterse8682112000-08-27 20:18:17 +0000987 return PyRun_FileEx(fp, filename, start, globals, locals, 0);
Guido van Rossum0df002c2000-08-27 19:21:52 +0000988}
989
990PyObject *
991PyRun_FileEx(FILE *fp, char *filename, int start, PyObject *globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000992 PyObject *locals, int closeit)
Guido van Rossum0df002c2000-08-27 19:21:52 +0000993{
994 node *n = PyParser_SimpleParseFile(fp, filename, start);
995 if (closeit)
996 fclose(fp);
Jeremy Hylton9f324e92001-03-01 22:59:14 +0000997 return run_err_node(n, filename, globals, locals, NULL);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000998}
999
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001000PyObject *
1001PyRun_StringFlags(char *str, int start, PyObject *globals, PyObject *locals,
1002 PyCompilerFlags *flags)
1003{
1004 return run_err_node(PyParser_SimpleParseString(str, start),
1005 "<string>", globals, locals, flags);
1006}
1007
1008PyObject *
1009PyRun_FileFlags(FILE *fp, char *filename, int start, PyObject *globals,
1010 PyObject *locals, PyCompilerFlags *flags)
1011{
1012 return PyRun_FileExFlags(fp, filename, start, globals, locals, 0,
1013 flags);
1014}
1015
1016PyObject *
1017PyRun_FileExFlags(FILE *fp, char *filename, int start, PyObject *globals,
1018 PyObject *locals, int closeit, PyCompilerFlags *flags)
1019{
1020 node *n = PyParser_SimpleParseFile(fp, filename, start);
1021 if (closeit)
1022 fclose(fp);
1023 return run_err_node(n, filename, globals, locals, flags);
1024}
1025
Guido van Rossum82598051997-03-05 00:20:32 +00001026static PyObject *
Jeremy Hylton9f324e92001-03-01 22:59:14 +00001027run_err_node(node *n, char *filename, PyObject *globals, PyObject *locals,
1028 PyCompilerFlags *flags)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001029{
Guido van Rossuma110aa61994-08-29 12:50:44 +00001030 if (n == NULL)
1031 return NULL;
Jeremy Hylton9f324e92001-03-01 22:59:14 +00001032 return run_node(n, filename, globals, locals, flags);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001033}
1034
Guido van Rossum82598051997-03-05 00:20:32 +00001035static PyObject *
Jeremy Hylton9f324e92001-03-01 22:59:14 +00001036run_node(node *n, char *filename, PyObject *globals, PyObject *locals,
1037 PyCompilerFlags *flags)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001038{
Guido van Rossum82598051997-03-05 00:20:32 +00001039 PyCodeObject *co;
1040 PyObject *v;
Jeremy Hylton9f324e92001-03-01 22:59:14 +00001041 co = PyNode_CompileFlags(n, filename, flags);
Guido van Rossum82598051997-03-05 00:20:32 +00001042 PyNode_Free(n);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001043 if (co == NULL)
1044 return NULL;
Guido van Rossum82598051997-03-05 00:20:32 +00001045 v = PyEval_EvalCode(co, globals, locals);
1046 Py_DECREF(co);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001047 return v;
1048}
1049
Guido van Rossum82598051997-03-05 00:20:32 +00001050static PyObject *
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001051run_pyc_file(FILE *fp, char *filename, PyObject *globals, PyObject *locals,
1052 PyCompilerFlags *flags)
Guido van Rossumfdef2711994-09-14 13:31:04 +00001053{
Guido van Rossum82598051997-03-05 00:20:32 +00001054 PyCodeObject *co;
1055 PyObject *v;
Guido van Rossumfdef2711994-09-14 13:31:04 +00001056 long magic;
Fred Drakee8de31c2000-08-31 05:38:39 +00001057 long PyImport_GetMagicNumber(void);
Guido van Rossumfdef2711994-09-14 13:31:04 +00001058
Guido van Rossum82598051997-03-05 00:20:32 +00001059 magic = PyMarshal_ReadLongFromFile(fp);
1060 if (magic != PyImport_GetMagicNumber()) {
1061 PyErr_SetString(PyExc_RuntimeError,
Guido van Rossumfdef2711994-09-14 13:31:04 +00001062 "Bad magic number in .pyc file");
1063 return NULL;
1064 }
Guido van Rossum82598051997-03-05 00:20:32 +00001065 (void) PyMarshal_ReadLongFromFile(fp);
Tim Petersd9b9ac82001-01-28 00:27:39 +00001066 v = PyMarshal_ReadLastObjectFromFile(fp);
Guido van Rossumfdef2711994-09-14 13:31:04 +00001067 fclose(fp);
Guido van Rossum82598051997-03-05 00:20:32 +00001068 if (v == NULL || !PyCode_Check(v)) {
1069 Py_XDECREF(v);
1070 PyErr_SetString(PyExc_RuntimeError,
Guido van Rossumfdef2711994-09-14 13:31:04 +00001071 "Bad code object in .pyc file");
1072 return NULL;
1073 }
Guido van Rossum82598051997-03-05 00:20:32 +00001074 co = (PyCodeObject *)v;
1075 v = PyEval_EvalCode(co, globals, locals);
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001076 if (v && flags) {
1077 if (co->co_flags & CO_NESTED)
1078 flags->cf_nested_scopes = 1;
1079 fprintf(stderr, "run_pyc_file: nested_scopes: %d\n",
1080 flags->cf_nested_scopes);
1081 }
Guido van Rossum82598051997-03-05 00:20:32 +00001082 Py_DECREF(co);
Guido van Rossumfdef2711994-09-14 13:31:04 +00001083 return v;
1084}
1085
Guido van Rossum82598051997-03-05 00:20:32 +00001086PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001087Py_CompileString(char *str, char *filename, int start)
Guido van Rossum5b722181993-03-30 17:46:03 +00001088{
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001089 return Py_CompileStringFlags(str, filename, start, NULL);
1090}
1091
1092PyObject *
1093Py_CompileStringFlags(char *str, char *filename, int start,
1094 PyCompilerFlags *flags)
1095{
Guido van Rossum5b722181993-03-30 17:46:03 +00001096 node *n;
Guido van Rossum82598051997-03-05 00:20:32 +00001097 PyCodeObject *co;
1098 n = PyParser_SimpleParseString(str, start);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001099 if (n == NULL)
Guido van Rossum5b722181993-03-30 17:46:03 +00001100 return NULL;
Guido van Rossum82598051997-03-05 00:20:32 +00001101 co = PyNode_Compile(n, filename);
1102 PyNode_Free(n);
1103 return (PyObject *)co;
Guido van Rossum5b722181993-03-30 17:46:03 +00001104}
1105
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001106struct symtable *
1107Py_SymtableString(char *str, char *filename, int start)
1108{
1109 node *n;
1110 struct symtable *st;
1111 n = PyParser_SimpleParseString(str, start);
1112 if (n == NULL)
1113 return NULL;
1114 st = PyNode_CompileSymtable(n, filename);
1115 PyNode_Free(n);
1116 return st;
1117}
1118
Guido van Rossuma110aa61994-08-29 12:50:44 +00001119/* Simplified interface to parsefile -- return node or set exception */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001120
Guido van Rossuma110aa61994-08-29 12:50:44 +00001121node *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001122PyParser_SimpleParseFile(FILE *fp, char *filename, int start)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001123{
Guido van Rossuma110aa61994-08-29 12:50:44 +00001124 node *n;
1125 perrdetail err;
Guido van Rossum82598051997-03-05 00:20:32 +00001126 n = PyParser_ParseFile(fp, filename, &_PyParser_Grammar, start,
Guido van Rossuma110aa61994-08-29 12:50:44 +00001127 (char *)0, (char *)0, &err);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001128 if (n == NULL)
1129 err_input(&err);
1130 return n;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001131}
1132
Guido van Rossuma110aa61994-08-29 12:50:44 +00001133/* Simplified interface to parsestring -- return node or set exception */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001134
Guido van Rossuma110aa61994-08-29 12:50:44 +00001135node *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001136PyParser_SimpleParseString(char *str, int start)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001137{
Guido van Rossuma110aa61994-08-29 12:50:44 +00001138 node *n;
1139 perrdetail err;
Guido van Rossum82598051997-03-05 00:20:32 +00001140 n = PyParser_ParseString(str, &_PyParser_Grammar, start, &err);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001141 if (n == NULL)
1142 err_input(&err);
1143 return n;
1144}
1145
1146/* Set the error appropriate to the given input error code (see errcode.h) */
1147
1148static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001149err_input(perrdetail *err)
Guido van Rossuma110aa61994-08-29 12:50:44 +00001150{
Fred Drake85f36392000-07-11 17:53:00 +00001151 PyObject *v, *w, *errtype;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001152 char *msg = NULL;
Fred Drake85f36392000-07-11 17:53:00 +00001153 errtype = PyExc_SyntaxError;
Guido van Rossum82598051997-03-05 00:20:32 +00001154 v = Py_BuildValue("(ziiz)", err->filename,
Guido van Rossuma110aa61994-08-29 12:50:44 +00001155 err->lineno, err->offset, err->text);
1156 if (err->text != NULL) {
Guido van Rossumb18618d2000-05-03 23:44:39 +00001157 PyMem_DEL(err->text);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001158 err->text = NULL;
1159 }
1160 switch (err->error) {
1161 case E_SYNTAX:
Fred Drake85f36392000-07-11 17:53:00 +00001162 errtype = PyExc_IndentationError;
1163 if (err->expected == INDENT)
1164 msg = "expected an indented block";
1165 else if (err->token == INDENT)
1166 msg = "unexpected indent";
1167 else if (err->token == DEDENT)
1168 msg = "unexpected unindent";
1169 else {
1170 errtype = PyExc_SyntaxError;
1171 msg = "invalid syntax";
1172 }
Guido van Rossuma110aa61994-08-29 12:50:44 +00001173 break;
1174 case E_TOKEN:
1175 msg = "invalid token";
Guido van Rossuma110aa61994-08-29 12:50:44 +00001176 break;
1177 case E_INTR:
Guido van Rossum82598051997-03-05 00:20:32 +00001178 PyErr_SetNone(PyExc_KeyboardInterrupt);
Barry Warsawc80baa31999-01-27 16:39:40 +00001179 Py_XDECREF(v);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001180 return;
1181 case E_NOMEM:
Guido van Rossum82598051997-03-05 00:20:32 +00001182 PyErr_NoMemory();
Barry Warsawc80baa31999-01-27 16:39:40 +00001183 Py_XDECREF(v);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001184 return;
1185 case E_EOF:
1186 msg = "unexpected EOF while parsing";
1187 break;
Fred Drake85f36392000-07-11 17:53:00 +00001188 case E_TABSPACE:
1189 errtype = PyExc_TabError;
Guido van Rossum560e8ad1998-04-10 19:43:42 +00001190 msg = "inconsistent use of tabs and spaces in indentation";
1191 break;
Jeremy Hylton94988062000-06-20 19:10:44 +00001192 case E_OVERFLOW:
1193 msg = "expression too long";
1194 break;
Fred Drake85f36392000-07-11 17:53:00 +00001195 case E_DEDENT:
1196 errtype = PyExc_IndentationError;
1197 msg = "unindent does not match any outer indentation level";
1198 break;
1199 case E_TOODEEP:
1200 errtype = PyExc_IndentationError;
1201 msg = "too many levels of indentation";
1202 break;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001203 default:
1204 fprintf(stderr, "error=%d\n", err->error);
1205 msg = "unknown parsing error";
1206 break;
1207 }
Guido van Rossum82598051997-03-05 00:20:32 +00001208 w = Py_BuildValue("(sO)", msg, v);
Guido van Rossum41318302001-03-23 04:01:07 +00001209 Py_XDECREF(v);
Fred Drake85f36392000-07-11 17:53:00 +00001210 PyErr_SetObject(errtype, w);
Guido van Rossum82598051997-03-05 00:20:32 +00001211 Py_XDECREF(w);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001212}
1213
1214/* Print fatal error message and abort */
1215
1216void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001217Py_FatalError(char *msg)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001218{
Guido van Rossum83dd6c31994-09-29 09:38:33 +00001219 fprintf(stderr, "Fatal Python error: %s\n", msg);
Guido van Rossum8ae87c01995-01-26 00:40:38 +00001220#ifdef macintosh
1221 for (;;);
1222#endif
Guido van Rossum9b38a141996-09-11 23:12:24 +00001223#ifdef MS_WIN32
Guido van Rossum23c94461997-05-22 20:21:30 +00001224 OutputDebugString("Fatal Python error: ");
Guido van Rossuma44823b1995-03-14 15:01:17 +00001225 OutputDebugString(msg);
1226 OutputDebugString("\n");
Guido van Rossum0ba35361998-08-13 13:33:16 +00001227#ifdef _DEBUG
1228 DebugBreak();
Guido van Rossuma44823b1995-03-14 15:01:17 +00001229#endif
Guido van Rossum0ba35361998-08-13 13:33:16 +00001230#endif /* MS_WIN32 */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001231 abort();
1232}
1233
1234/* Clean up and exit */
1235
Guido van Rossuma110aa61994-08-29 12:50:44 +00001236#ifdef WITH_THREAD
Guido van Rossum49b56061998-10-01 20:42:43 +00001237#include "pythread.h"
Guido van Rossum82598051997-03-05 00:20:32 +00001238int _PyThread_Started = 0; /* Set by threadmodule.c and maybe others */
Guido van Rossumf9f2e821992-08-17 08:59:08 +00001239#endif
1240
Guido van Rossum2dcfc961998-10-01 16:01:57 +00001241#define NEXITFUNCS 32
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001242static void (*exitfuncs[NEXITFUNCS])(void);
Guido van Rossum1662dd51994-09-07 14:38:28 +00001243static int nexitfuncs = 0;
1244
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001245int Py_AtExit(void (*func)(void))
Guido van Rossum1662dd51994-09-07 14:38:28 +00001246{
1247 if (nexitfuncs >= NEXITFUNCS)
1248 return -1;
1249 exitfuncs[nexitfuncs++] = func;
1250 return 0;
1251}
1252
Guido van Rossumcc283f51997-08-05 02:22:03 +00001253static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001254call_sys_exitfunc(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001255{
Guido van Rossum82598051997-03-05 00:20:32 +00001256 PyObject *exitfunc = PySys_GetObject("exitfunc");
Guido van Rossum59bff391992-09-03 20:28:00 +00001257
1258 if (exitfunc) {
Guido van Rossumbf02fb21998-04-03 21:12:12 +00001259 PyObject *res, *f;
Guido van Rossum82598051997-03-05 00:20:32 +00001260 Py_INCREF(exitfunc);
1261 PySys_SetObject("exitfunc", (PyObject *)NULL);
1262 res = PyEval_CallObject(exitfunc, (PyObject *)NULL);
Guido van Rossum59bff391992-09-03 20:28:00 +00001263 if (res == NULL) {
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001264 if (!PyErr_ExceptionMatches(PyExc_SystemExit)) {
1265 PySys_WriteStderr("Error in sys.exitfunc:\n");
1266 }
Guido van Rossum82598051997-03-05 00:20:32 +00001267 PyErr_Print();
Guido van Rossum59bff391992-09-03 20:28:00 +00001268 }
Guido van Rossum82598051997-03-05 00:20:32 +00001269 Py_DECREF(exitfunc);
Guido van Rossum59bff391992-09-03 20:28:00 +00001270 }
1271
Guido van Rossum0829c751998-02-28 04:31:39 +00001272 if (Py_FlushLine())
1273 PyErr_Clear();
Guido van Rossumcc283f51997-08-05 02:22:03 +00001274}
Guido van Rossum1662dd51994-09-07 14:38:28 +00001275
Guido van Rossumcc283f51997-08-05 02:22:03 +00001276static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001277call_ll_exitfuncs(void)
Guido van Rossumcc283f51997-08-05 02:22:03 +00001278{
Guido van Rossum1662dd51994-09-07 14:38:28 +00001279 while (nexitfuncs > 0)
1280 (*exitfuncs[--nexitfuncs])();
Guido van Rossum25ce5661997-08-02 03:10:38 +00001281
1282 fflush(stdout);
1283 fflush(stderr);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001284}
1285
1286void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001287Py_Exit(int sts)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001288{
Guido van Rossumcc283f51997-08-05 02:22:03 +00001289 Py_Finalize();
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001290
Jack Jansen66a89771995-10-27 13:22:14 +00001291#ifdef macintosh
1292 PyMac_Exit(sts);
1293#else
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001294 exit(sts);
Jack Jansen66a89771995-10-27 13:22:14 +00001295#endif
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001296}
1297
Guido van Rossumf1dc5661993-07-05 10:31:29 +00001298static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001299initsigs(void)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001300{
Guido van Rossuma110aa61994-08-29 12:50:44 +00001301#ifdef HAVE_SIGNAL_H
1302#ifdef SIGPIPE
1303 signal(SIGPIPE, SIG_IGN);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001304#endif
Guido van Rossuma110aa61994-08-29 12:50:44 +00001305#endif /* HAVE_SIGNAL_H */
Guido van Rossum82598051997-03-05 00:20:32 +00001306 PyOS_InitInterrupts(); /* May imply initsignal() */
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001307}
1308
Guido van Rossumaae0d321996-05-22 16:35:33 +00001309#ifdef Py_TRACE_REFS
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001310/* Ask a yes/no question */
1311
Guido van Rossum59bff391992-09-03 20:28:00 +00001312int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001313_Py_AskYesNo(char *prompt)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001314{
1315 char buf[256];
1316
1317 printf("%s [ny] ", prompt);
1318 if (fgets(buf, sizeof buf, stdin) == NULL)
1319 return 0;
1320 return buf[0] == 'y' || buf[0] == 'Y';
1321}
1322#endif
1323
Guido van Rossuma110aa61994-08-29 12:50:44 +00001324#ifdef MPW
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001325
1326/* Check for file descriptor connected to interactive device.
1327 Pretend that stdin is always interactive, other files never. */
1328
1329int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001330isatty(int fd)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001331{
1332 return fd == fileno(stdin);
1333}
1334
1335#endif
Guido van Rossum7433b121997-02-14 19:45:36 +00001336
1337/*
1338 * The file descriptor fd is considered ``interactive'' if either
1339 * a) isatty(fd) is TRUE, or
1340 * b) the -i flag was given, and the filename associated with
1341 * the descriptor is NULL or "<stdin>" or "???".
1342 */
1343int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001344Py_FdIsInteractive(FILE *fp, char *filename)
Guido van Rossum7433b121997-02-14 19:45:36 +00001345{
1346 if (isatty((int)fileno(fp)))
1347 return 1;
1348 if (!Py_InteractiveFlag)
1349 return 0;
1350 return (filename == NULL) ||
1351 (strcmp(filename, "<stdin>") == 0) ||
1352 (strcmp(filename, "???") == 0);
1353}
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001354
1355
1356#if defined(USE_STACKCHECK)
1357#if defined(WIN32) && defined(_MSC_VER)
1358
1359/* Stack checking for Microsoft C */
1360
1361#include <malloc.h>
1362#include <excpt.h>
1363
Fred Drakee8de31c2000-08-31 05:38:39 +00001364/*
1365 * Return non-zero when we run out of memory on the stack; zero otherwise.
1366 */
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001367int
Fred Drake399739f2000-08-31 05:52:44 +00001368PyOS_CheckStack(void)
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001369{
1370 __try {
1371 /* _alloca throws a stack overflow exception if there's
1372 not enough space left on the stack */
1373 _alloca(PYOS_STACK_MARGIN * sizeof(void*));
1374 return 0;
1375 } __except (EXCEPTION_EXECUTE_HANDLER) {
1376 /* just ignore all errors */
1377 }
1378 return 1;
1379}
1380
1381#endif /* WIN32 && _MSC_VER */
1382
1383/* Alternate implementations can be added here... */
1384
1385#endif /* USE_STACKCHECK */
Guido van Rossum6f256182000-09-16 16:32:19 +00001386
1387
1388/* Wrappers around sigaction() or signal(). */
1389
1390PyOS_sighandler_t
1391PyOS_getsig(int sig)
1392{
1393#ifdef HAVE_SIGACTION
1394 struct sigaction context;
1395 sigaction(sig, NULL, &context);
1396 return context.sa_handler;
1397#else
1398 PyOS_sighandler_t handler;
1399 handler = signal(sig, SIG_IGN);
1400 signal(sig, handler);
1401 return handler;
1402#endif
1403}
1404
1405PyOS_sighandler_t
1406PyOS_setsig(int sig, PyOS_sighandler_t handler)
1407{
1408#ifdef HAVE_SIGACTION
1409 struct sigaction context;
1410 PyOS_sighandler_t oldhandler;
1411 sigaction(sig, NULL, &context);
1412 oldhandler = context.sa_handler;
1413 context.sa_handler = handler;
1414 sigaction(sig, &context, NULL);
1415 return oldhandler;
1416#else
1417 return signal(sig, handler);
1418#endif
1419}