blob: edb8a1102aa09f6ddbcbf0de3320eb4244f642cf [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
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000802PyErr_PrintEx(int set_sys_last_vars)
Guido van Rossuma61691e1998-02-06 22:27:24 +0000803{
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000804 int err = 0;
Guido van Rossum82598051997-03-05 00:20:32 +0000805 PyObject *exception, *v, *tb, *f;
806 PyErr_Fetch(&exception, &v, &tb);
Barry Warsaw035574d1997-08-29 22:07:17 +0000807 PyErr_NormalizeException(&exception, &v, &tb);
808
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000809 if (exception == NULL)
Guido van Rossum296b4751997-05-23 00:19:20 +0000810 return;
Barry Warsaw035574d1997-08-29 22:07:17 +0000811
Barry Warsaw36b8f941997-08-26 18:09:48 +0000812 if (PyErr_GivenExceptionMatches(exception, PyExc_SystemExit)) {
Guido van Rossum0829c751998-02-28 04:31:39 +0000813 if (Py_FlushLine())
814 PyErr_Clear();
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000815 fflush(stdout);
Guido van Rossum82598051997-03-05 00:20:32 +0000816 if (v == NULL || v == Py_None)
817 Py_Exit(0);
Barry Warsaw035574d1997-08-29 22:07:17 +0000818 if (PyInstance_Check(v)) {
819 /* we expect the error code to be store in the
820 `code' attribute
821 */
822 PyObject *code = PyObject_GetAttrString(v, "code");
823 if (code) {
824 Py_DECREF(v);
825 v = code;
Guido van Rossumaa9606f1997-10-03 13:53:28 +0000826 if (v == Py_None)
827 Py_Exit(0);
Barry Warsaw035574d1997-08-29 22:07:17 +0000828 }
829 /* if we failed to dig out the "code" attribute,
830 then just let the else clause below print the
831 error
832 */
833 }
Guido van Rossum82598051997-03-05 00:20:32 +0000834 if (PyInt_Check(v))
835 Py_Exit((int)PyInt_AsLong(v));
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000836 else {
Guido van Rossum3165fe61992-09-25 21:59:05 +0000837 /* OK to use real stderr here */
Guido van Rossum82598051997-03-05 00:20:32 +0000838 PyObject_Print(v, stderr, Py_PRINT_RAW);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000839 fprintf(stderr, "\n");
Guido van Rossum82598051997-03-05 00:20:32 +0000840 Py_Exit(1);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000841 }
842 }
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 }
Guido van Rossum82598051997-03-05 00:20:32 +0000848 f = PySys_GetObject("stderr");
Guido van Rossum3165fe61992-09-25 21:59:05 +0000849 if (f == NULL)
850 fprintf(stderr, "lost sys.stderr\n");
851 else {
Guido van Rossum0829c751998-02-28 04:31:39 +0000852 if (Py_FlushLine())
853 PyErr_Clear();
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000854 fflush(stdout);
Guido van Rossum0829c751998-02-28 04:31:39 +0000855 err = PyTraceBack_Print(tb, f);
Barry Warsaw36b8f941997-08-26 18:09:48 +0000856 if (err == 0 &&
857 PyErr_GivenExceptionMatches(exception, PyExc_SyntaxError))
858 {
Guido van Rossum82598051997-03-05 00:20:32 +0000859 PyObject *message;
Guido van Rossuma110aa61994-08-29 12:50:44 +0000860 char *filename, *text;
861 int lineno, offset;
Barry Warsaw035574d1997-08-29 22:07:17 +0000862 if (!parse_syntax_error(v, &message, &filename,
863 &lineno, &offset, &text))
Guido van Rossum82598051997-03-05 00:20:32 +0000864 PyErr_Clear();
Guido van Rossuma110aa61994-08-29 12:50:44 +0000865 else {
866 char buf[10];
Guido van Rossum82598051997-03-05 00:20:32 +0000867 PyFile_WriteString(" File \"", f);
Guido van Rossuma110aa61994-08-29 12:50:44 +0000868 if (filename == NULL)
Guido van Rossum82598051997-03-05 00:20:32 +0000869 PyFile_WriteString("<string>", f);
Guido van Rossuma110aa61994-08-29 12:50:44 +0000870 else
Guido van Rossum82598051997-03-05 00:20:32 +0000871 PyFile_WriteString(filename, f);
872 PyFile_WriteString("\", line ", f);
Guido van Rossuma110aa61994-08-29 12:50:44 +0000873 sprintf(buf, "%d", lineno);
Guido van Rossum82598051997-03-05 00:20:32 +0000874 PyFile_WriteString(buf, f);
875 PyFile_WriteString("\n", f);
Jeremy Hylton9f1b9932001-02-28 07:07:43 +0000876 if (text != NULL)
877 print_error_text(f, offset, text);
Guido van Rossum82598051997-03-05 00:20:32 +0000878 Py_INCREF(message);
879 Py_DECREF(v);
Guido van Rossuma110aa61994-08-29 12:50:44 +0000880 v = message;
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000881 /* Can't be bothered to check all those
882 PyFile_WriteString() calls */
883 if (PyErr_Occurred())
884 err = -1;
Guido van Rossuma110aa61994-08-29 12:50:44 +0000885 }
886 }
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000887 if (err) {
888 /* Don't do anything else */
889 }
890 else if (PyClass_Check(exception)) {
Barry Warsaw2f5f6a21997-09-16 21:42:03 +0000891 PyClassObject* exc = (PyClassObject*)exception;
892 PyObject* className = exc->cl_name;
893 PyObject* moduleName =
894 PyDict_GetItemString(exc->cl_dict, "__module__");
895
896 if (moduleName == NULL)
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000897 err = PyFile_WriteString("<unknown>", f);
Barry Warsaw2f5f6a21997-09-16 21:42:03 +0000898 else {
899 char* modstr = PyString_AsString(moduleName);
900 if (modstr && strcmp(modstr, "exceptions"))
901 {
902 err = PyFile_WriteString(modstr, f);
903 err += PyFile_WriteString(".", f);
904 }
905 }
906 if (err == 0) {
907 if (className == NULL)
908 err = PyFile_WriteString("<unknown>", f);
909 else
910 err = PyFile_WriteObject(className, f,
911 Py_PRINT_RAW);
912 }
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000913 }
914 else
915 err = PyFile_WriteObject(exception, f, Py_PRINT_RAW);
916 if (err == 0) {
917 if (v != NULL && v != Py_None) {
Barry Warsaw035574d1997-08-29 22:07:17 +0000918 PyObject *s = PyObject_Str(v);
919 /* only print colon if the str() of the
920 object is not the empty string
921 */
Guido van Rossumd6bf45b1997-09-05 19:11:53 +0000922 if (s == NULL)
923 err = -1;
924 else if (!PyString_Check(s) ||
925 PyString_GET_SIZE(s) != 0)
Barry Warsaw035574d1997-08-29 22:07:17 +0000926 err = PyFile_WriteString(": ", f);
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000927 if (err == 0)
Guido van Rossumd6bf45b1997-09-05 19:11:53 +0000928 err = PyFile_WriteObject(s, f, Py_PRINT_RAW);
929 Py_XDECREF(s);
Guido van Rossum262e1241995-02-07 15:30:45 +0000930 }
Guido van Rossum262e1241995-02-07 15:30:45 +0000931 }
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000932 if (err == 0)
933 err = PyFile_WriteString("\n", f);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000934 }
Guido van Rossum82598051997-03-05 00:20:32 +0000935 Py_XDECREF(exception);
936 Py_XDECREF(v);
937 Py_XDECREF(tb);
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000938 /* If an error happened here, don't show it.
939 XXX This is wrong, but too many callers rely on this behavior. */
940 if (err != 0)
941 PyErr_Clear();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000942}
943
Guido van Rossum82598051997-03-05 00:20:32 +0000944PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000945PyRun_String(char *str, int start, PyObject *globals, PyObject *locals)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000946{
Guido van Rossum82598051997-03-05 00:20:32 +0000947 return run_err_node(PyParser_SimpleParseString(str, start),
Jeremy Hylton9f324e92001-03-01 22:59:14 +0000948 "<string>", globals, locals, NULL);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000949}
950
Guido van Rossum82598051997-03-05 00:20:32 +0000951PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000952PyRun_File(FILE *fp, char *filename, int start, PyObject *globals,
953 PyObject *locals)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000954{
Tim Peterse8682112000-08-27 20:18:17 +0000955 return PyRun_FileEx(fp, filename, start, globals, locals, 0);
Guido van Rossum0df002c2000-08-27 19:21:52 +0000956}
957
958PyObject *
959PyRun_FileEx(FILE *fp, char *filename, int start, PyObject *globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000960 PyObject *locals, int closeit)
Guido van Rossum0df002c2000-08-27 19:21:52 +0000961{
962 node *n = PyParser_SimpleParseFile(fp, filename, start);
963 if (closeit)
964 fclose(fp);
Jeremy Hylton9f324e92001-03-01 22:59:14 +0000965 return run_err_node(n, filename, globals, locals, NULL);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000966}
967
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000968PyObject *
969PyRun_StringFlags(char *str, int start, PyObject *globals, PyObject *locals,
970 PyCompilerFlags *flags)
971{
972 return run_err_node(PyParser_SimpleParseString(str, start),
973 "<string>", globals, locals, flags);
974}
975
976PyObject *
977PyRun_FileFlags(FILE *fp, char *filename, int start, PyObject *globals,
978 PyObject *locals, PyCompilerFlags *flags)
979{
980 return PyRun_FileExFlags(fp, filename, start, globals, locals, 0,
981 flags);
982}
983
984PyObject *
985PyRun_FileExFlags(FILE *fp, char *filename, int start, PyObject *globals,
986 PyObject *locals, int closeit, PyCompilerFlags *flags)
987{
988 node *n = PyParser_SimpleParseFile(fp, filename, start);
989 if (closeit)
990 fclose(fp);
991 return run_err_node(n, filename, globals, locals, flags);
992}
993
Guido van Rossum82598051997-03-05 00:20:32 +0000994static PyObject *
Jeremy Hylton9f324e92001-03-01 22:59:14 +0000995run_err_node(node *n, char *filename, PyObject *globals, PyObject *locals,
996 PyCompilerFlags *flags)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000997{
Guido van Rossuma110aa61994-08-29 12:50:44 +0000998 if (n == NULL)
999 return NULL;
Jeremy Hylton9f324e92001-03-01 22:59:14 +00001000 return run_node(n, filename, globals, locals, flags);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001001}
1002
Guido van Rossum82598051997-03-05 00:20:32 +00001003static PyObject *
Jeremy Hylton9f324e92001-03-01 22:59:14 +00001004run_node(node *n, char *filename, PyObject *globals, PyObject *locals,
1005 PyCompilerFlags *flags)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001006{
Guido van Rossum82598051997-03-05 00:20:32 +00001007 PyCodeObject *co;
1008 PyObject *v;
Jeremy Hylton9f324e92001-03-01 22:59:14 +00001009 co = PyNode_CompileFlags(n, filename, flags);
Guido van Rossum82598051997-03-05 00:20:32 +00001010 PyNode_Free(n);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001011 if (co == NULL)
1012 return NULL;
Guido van Rossum82598051997-03-05 00:20:32 +00001013 v = PyEval_EvalCode(co, globals, locals);
1014 Py_DECREF(co);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001015 return v;
1016}
1017
Guido van Rossum82598051997-03-05 00:20:32 +00001018static PyObject *
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001019run_pyc_file(FILE *fp, char *filename, PyObject *globals, PyObject *locals,
1020 PyCompilerFlags *flags)
Guido van Rossumfdef2711994-09-14 13:31:04 +00001021{
Guido van Rossum82598051997-03-05 00:20:32 +00001022 PyCodeObject *co;
1023 PyObject *v;
Guido van Rossumfdef2711994-09-14 13:31:04 +00001024 long magic;
Fred Drakee8de31c2000-08-31 05:38:39 +00001025 long PyImport_GetMagicNumber(void);
Guido van Rossumfdef2711994-09-14 13:31:04 +00001026
Guido van Rossum82598051997-03-05 00:20:32 +00001027 magic = PyMarshal_ReadLongFromFile(fp);
1028 if (magic != PyImport_GetMagicNumber()) {
1029 PyErr_SetString(PyExc_RuntimeError,
Guido van Rossumfdef2711994-09-14 13:31:04 +00001030 "Bad magic number in .pyc file");
1031 return NULL;
1032 }
Guido van Rossum82598051997-03-05 00:20:32 +00001033 (void) PyMarshal_ReadLongFromFile(fp);
Tim Petersd9b9ac82001-01-28 00:27:39 +00001034 v = PyMarshal_ReadLastObjectFromFile(fp);
Guido van Rossumfdef2711994-09-14 13:31:04 +00001035 fclose(fp);
Guido van Rossum82598051997-03-05 00:20:32 +00001036 if (v == NULL || !PyCode_Check(v)) {
1037 Py_XDECREF(v);
1038 PyErr_SetString(PyExc_RuntimeError,
Guido van Rossumfdef2711994-09-14 13:31:04 +00001039 "Bad code object in .pyc file");
1040 return NULL;
1041 }
Guido van Rossum82598051997-03-05 00:20:32 +00001042 co = (PyCodeObject *)v;
1043 v = PyEval_EvalCode(co, globals, locals);
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001044 if (v && flags) {
1045 if (co->co_flags & CO_NESTED)
1046 flags->cf_nested_scopes = 1;
1047 fprintf(stderr, "run_pyc_file: nested_scopes: %d\n",
1048 flags->cf_nested_scopes);
1049 }
Guido van Rossum82598051997-03-05 00:20:32 +00001050 Py_DECREF(co);
Guido van Rossumfdef2711994-09-14 13:31:04 +00001051 return v;
1052}
1053
Guido van Rossum82598051997-03-05 00:20:32 +00001054PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001055Py_CompileString(char *str, char *filename, int start)
Guido van Rossum5b722181993-03-30 17:46:03 +00001056{
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001057 return Py_CompileStringFlags(str, filename, start, NULL);
1058}
1059
1060PyObject *
1061Py_CompileStringFlags(char *str, char *filename, int start,
1062 PyCompilerFlags *flags)
1063{
Guido van Rossum5b722181993-03-30 17:46:03 +00001064 node *n;
Guido van Rossum82598051997-03-05 00:20:32 +00001065 PyCodeObject *co;
1066 n = PyParser_SimpleParseString(str, start);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001067 if (n == NULL)
Guido van Rossum5b722181993-03-30 17:46:03 +00001068 return NULL;
Guido van Rossum82598051997-03-05 00:20:32 +00001069 co = PyNode_Compile(n, filename);
1070 PyNode_Free(n);
1071 return (PyObject *)co;
Guido van Rossum5b722181993-03-30 17:46:03 +00001072}
1073
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001074struct symtable *
1075Py_SymtableString(char *str, char *filename, int start)
1076{
1077 node *n;
1078 struct symtable *st;
1079 n = PyParser_SimpleParseString(str, start);
1080 if (n == NULL)
1081 return NULL;
1082 st = PyNode_CompileSymtable(n, filename);
1083 PyNode_Free(n);
1084 return st;
1085}
1086
Guido van Rossuma110aa61994-08-29 12:50:44 +00001087/* Simplified interface to parsefile -- return node or set exception */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001088
Guido van Rossuma110aa61994-08-29 12:50:44 +00001089node *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001090PyParser_SimpleParseFile(FILE *fp, char *filename, int start)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001091{
Guido van Rossuma110aa61994-08-29 12:50:44 +00001092 node *n;
1093 perrdetail err;
Guido van Rossum82598051997-03-05 00:20:32 +00001094 n = PyParser_ParseFile(fp, filename, &_PyParser_Grammar, start,
Guido van Rossuma110aa61994-08-29 12:50:44 +00001095 (char *)0, (char *)0, &err);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001096 if (n == NULL)
1097 err_input(&err);
1098 return n;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001099}
1100
Guido van Rossuma110aa61994-08-29 12:50:44 +00001101/* Simplified interface to parsestring -- return node or set exception */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001102
Guido van Rossuma110aa61994-08-29 12:50:44 +00001103node *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001104PyParser_SimpleParseString(char *str, int start)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001105{
Guido van Rossuma110aa61994-08-29 12:50:44 +00001106 node *n;
1107 perrdetail err;
Guido van Rossum82598051997-03-05 00:20:32 +00001108 n = PyParser_ParseString(str, &_PyParser_Grammar, start, &err);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001109 if (n == NULL)
1110 err_input(&err);
1111 return n;
1112}
1113
1114/* Set the error appropriate to the given input error code (see errcode.h) */
1115
1116static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001117err_input(perrdetail *err)
Guido van Rossuma110aa61994-08-29 12:50:44 +00001118{
Fred Drake85f36392000-07-11 17:53:00 +00001119 PyObject *v, *w, *errtype;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001120 char *msg = NULL;
Fred Drake85f36392000-07-11 17:53:00 +00001121 errtype = PyExc_SyntaxError;
Guido van Rossum82598051997-03-05 00:20:32 +00001122 v = Py_BuildValue("(ziiz)", err->filename,
Guido van Rossuma110aa61994-08-29 12:50:44 +00001123 err->lineno, err->offset, err->text);
1124 if (err->text != NULL) {
Guido van Rossumb18618d2000-05-03 23:44:39 +00001125 PyMem_DEL(err->text);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001126 err->text = NULL;
1127 }
1128 switch (err->error) {
1129 case E_SYNTAX:
Fred Drake85f36392000-07-11 17:53:00 +00001130 errtype = PyExc_IndentationError;
1131 if (err->expected == INDENT)
1132 msg = "expected an indented block";
1133 else if (err->token == INDENT)
1134 msg = "unexpected indent";
1135 else if (err->token == DEDENT)
1136 msg = "unexpected unindent";
1137 else {
1138 errtype = PyExc_SyntaxError;
1139 msg = "invalid syntax";
1140 }
Guido van Rossuma110aa61994-08-29 12:50:44 +00001141 break;
1142 case E_TOKEN:
1143 msg = "invalid token";
Guido van Rossuma110aa61994-08-29 12:50:44 +00001144 break;
1145 case E_INTR:
Guido van Rossum82598051997-03-05 00:20:32 +00001146 PyErr_SetNone(PyExc_KeyboardInterrupt);
Barry Warsawc80baa31999-01-27 16:39:40 +00001147 Py_XDECREF(v);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001148 return;
1149 case E_NOMEM:
Guido van Rossum82598051997-03-05 00:20:32 +00001150 PyErr_NoMemory();
Barry Warsawc80baa31999-01-27 16:39:40 +00001151 Py_XDECREF(v);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001152 return;
1153 case E_EOF:
1154 msg = "unexpected EOF while parsing";
1155 break;
Fred Drake85f36392000-07-11 17:53:00 +00001156 case E_TABSPACE:
1157 errtype = PyExc_TabError;
Guido van Rossum560e8ad1998-04-10 19:43:42 +00001158 msg = "inconsistent use of tabs and spaces in indentation";
1159 break;
Jeremy Hylton94988062000-06-20 19:10:44 +00001160 case E_OVERFLOW:
1161 msg = "expression too long";
1162 break;
Fred Drake85f36392000-07-11 17:53:00 +00001163 case E_DEDENT:
1164 errtype = PyExc_IndentationError;
1165 msg = "unindent does not match any outer indentation level";
1166 break;
1167 case E_TOODEEP:
1168 errtype = PyExc_IndentationError;
1169 msg = "too many levels of indentation";
1170 break;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001171 default:
1172 fprintf(stderr, "error=%d\n", err->error);
1173 msg = "unknown parsing error";
1174 break;
1175 }
Guido van Rossum82598051997-03-05 00:20:32 +00001176 w = Py_BuildValue("(sO)", msg, v);
Fred Drake85f36392000-07-11 17:53:00 +00001177 PyErr_SetObject(errtype, w);
Guido van Rossum82598051997-03-05 00:20:32 +00001178 Py_XDECREF(w);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001179}
1180
1181/* Print fatal error message and abort */
1182
1183void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001184Py_FatalError(char *msg)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001185{
Guido van Rossum83dd6c31994-09-29 09:38:33 +00001186 fprintf(stderr, "Fatal Python error: %s\n", msg);
Guido van Rossum8ae87c01995-01-26 00:40:38 +00001187#ifdef macintosh
1188 for (;;);
1189#endif
Guido van Rossum9b38a141996-09-11 23:12:24 +00001190#ifdef MS_WIN32
Guido van Rossum23c94461997-05-22 20:21:30 +00001191 OutputDebugString("Fatal Python error: ");
Guido van Rossuma44823b1995-03-14 15:01:17 +00001192 OutputDebugString(msg);
1193 OutputDebugString("\n");
Guido van Rossum0ba35361998-08-13 13:33:16 +00001194#ifdef _DEBUG
1195 DebugBreak();
Guido van Rossuma44823b1995-03-14 15:01:17 +00001196#endif
Guido van Rossum0ba35361998-08-13 13:33:16 +00001197#endif /* MS_WIN32 */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001198 abort();
1199}
1200
1201/* Clean up and exit */
1202
Guido van Rossuma110aa61994-08-29 12:50:44 +00001203#ifdef WITH_THREAD
Guido van Rossum49b56061998-10-01 20:42:43 +00001204#include "pythread.h"
Guido van Rossum82598051997-03-05 00:20:32 +00001205int _PyThread_Started = 0; /* Set by threadmodule.c and maybe others */
Guido van Rossumf9f2e821992-08-17 08:59:08 +00001206#endif
1207
Guido van Rossum2dcfc961998-10-01 16:01:57 +00001208#define NEXITFUNCS 32
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001209static void (*exitfuncs[NEXITFUNCS])(void);
Guido van Rossum1662dd51994-09-07 14:38:28 +00001210static int nexitfuncs = 0;
1211
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001212int Py_AtExit(void (*func)(void))
Guido van Rossum1662dd51994-09-07 14:38:28 +00001213{
1214 if (nexitfuncs >= NEXITFUNCS)
1215 return -1;
1216 exitfuncs[nexitfuncs++] = func;
1217 return 0;
1218}
1219
Guido van Rossumcc283f51997-08-05 02:22:03 +00001220static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001221call_sys_exitfunc(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001222{
Guido van Rossum82598051997-03-05 00:20:32 +00001223 PyObject *exitfunc = PySys_GetObject("exitfunc");
Guido van Rossum59bff391992-09-03 20:28:00 +00001224
1225 if (exitfunc) {
Guido van Rossumbf02fb21998-04-03 21:12:12 +00001226 PyObject *res, *f;
Guido van Rossum82598051997-03-05 00:20:32 +00001227 Py_INCREF(exitfunc);
1228 PySys_SetObject("exitfunc", (PyObject *)NULL);
Guido van Rossumbf02fb21998-04-03 21:12:12 +00001229 f = PySys_GetObject("stderr");
Guido van Rossum82598051997-03-05 00:20:32 +00001230 res = PyEval_CallObject(exitfunc, (PyObject *)NULL);
Guido van Rossum59bff391992-09-03 20:28:00 +00001231 if (res == NULL) {
Guido van Rossumbf02fb21998-04-03 21:12:12 +00001232 if (f)
1233 PyFile_WriteString("Error in sys.exitfunc:\n", f);
Guido van Rossum82598051997-03-05 00:20:32 +00001234 PyErr_Print();
Guido van Rossum59bff391992-09-03 20:28:00 +00001235 }
Guido van Rossum82598051997-03-05 00:20:32 +00001236 Py_DECREF(exitfunc);
Guido van Rossum59bff391992-09-03 20:28:00 +00001237 }
1238
Guido van Rossum0829c751998-02-28 04:31:39 +00001239 if (Py_FlushLine())
1240 PyErr_Clear();
Guido van Rossumcc283f51997-08-05 02:22:03 +00001241}
Guido van Rossum1662dd51994-09-07 14:38:28 +00001242
Guido van Rossumcc283f51997-08-05 02:22:03 +00001243static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001244call_ll_exitfuncs(void)
Guido van Rossumcc283f51997-08-05 02:22:03 +00001245{
Guido van Rossum1662dd51994-09-07 14:38:28 +00001246 while (nexitfuncs > 0)
1247 (*exitfuncs[--nexitfuncs])();
Guido van Rossum25ce5661997-08-02 03:10:38 +00001248
1249 fflush(stdout);
1250 fflush(stderr);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001251}
1252
1253void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001254Py_Exit(int sts)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001255{
Guido van Rossumcc283f51997-08-05 02:22:03 +00001256 Py_Finalize();
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001257
Jack Jansen66a89771995-10-27 13:22:14 +00001258#ifdef macintosh
1259 PyMac_Exit(sts);
1260#else
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001261 exit(sts);
Jack Jansen66a89771995-10-27 13:22:14 +00001262#endif
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001263}
1264
Guido van Rossumf1dc5661993-07-05 10:31:29 +00001265static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001266initsigs(void)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001267{
Guido van Rossuma110aa61994-08-29 12:50:44 +00001268#ifdef HAVE_SIGNAL_H
1269#ifdef SIGPIPE
1270 signal(SIGPIPE, SIG_IGN);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001271#endif
Guido van Rossuma110aa61994-08-29 12:50:44 +00001272#endif /* HAVE_SIGNAL_H */
Guido van Rossum82598051997-03-05 00:20:32 +00001273 PyOS_InitInterrupts(); /* May imply initsignal() */
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001274}
1275
Guido van Rossumaae0d321996-05-22 16:35:33 +00001276#ifdef Py_TRACE_REFS
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001277/* Ask a yes/no question */
1278
Guido van Rossum59bff391992-09-03 20:28:00 +00001279int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001280_Py_AskYesNo(char *prompt)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001281{
1282 char buf[256];
1283
1284 printf("%s [ny] ", prompt);
1285 if (fgets(buf, sizeof buf, stdin) == NULL)
1286 return 0;
1287 return buf[0] == 'y' || buf[0] == 'Y';
1288}
1289#endif
1290
Guido van Rossuma110aa61994-08-29 12:50:44 +00001291#ifdef MPW
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001292
1293/* Check for file descriptor connected to interactive device.
1294 Pretend that stdin is always interactive, other files never. */
1295
1296int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001297isatty(int fd)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001298{
1299 return fd == fileno(stdin);
1300}
1301
1302#endif
Guido van Rossum7433b121997-02-14 19:45:36 +00001303
1304/*
1305 * The file descriptor fd is considered ``interactive'' if either
1306 * a) isatty(fd) is TRUE, or
1307 * b) the -i flag was given, and the filename associated with
1308 * the descriptor is NULL or "<stdin>" or "???".
1309 */
1310int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001311Py_FdIsInteractive(FILE *fp, char *filename)
Guido van Rossum7433b121997-02-14 19:45:36 +00001312{
1313 if (isatty((int)fileno(fp)))
1314 return 1;
1315 if (!Py_InteractiveFlag)
1316 return 0;
1317 return (filename == NULL) ||
1318 (strcmp(filename, "<stdin>") == 0) ||
1319 (strcmp(filename, "???") == 0);
1320}
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001321
1322
1323#if defined(USE_STACKCHECK)
1324#if defined(WIN32) && defined(_MSC_VER)
1325
1326/* Stack checking for Microsoft C */
1327
1328#include <malloc.h>
1329#include <excpt.h>
1330
Fred Drakee8de31c2000-08-31 05:38:39 +00001331/*
1332 * Return non-zero when we run out of memory on the stack; zero otherwise.
1333 */
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001334int
Fred Drake399739f2000-08-31 05:52:44 +00001335PyOS_CheckStack(void)
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001336{
1337 __try {
1338 /* _alloca throws a stack overflow exception if there's
1339 not enough space left on the stack */
1340 _alloca(PYOS_STACK_MARGIN * sizeof(void*));
1341 return 0;
1342 } __except (EXCEPTION_EXECUTE_HANDLER) {
1343 /* just ignore all errors */
1344 }
1345 return 1;
1346}
1347
1348#endif /* WIN32 && _MSC_VER */
1349
1350/* Alternate implementations can be added here... */
1351
1352#endif /* USE_STACKCHECK */
Guido van Rossum6f256182000-09-16 16:32:19 +00001353
1354
1355/* Wrappers around sigaction() or signal(). */
1356
1357PyOS_sighandler_t
1358PyOS_getsig(int sig)
1359{
1360#ifdef HAVE_SIGACTION
1361 struct sigaction context;
1362 sigaction(sig, NULL, &context);
1363 return context.sa_handler;
1364#else
1365 PyOS_sighandler_t handler;
1366 handler = signal(sig, SIG_IGN);
1367 signal(sig, handler);
1368 return handler;
1369#endif
1370}
1371
1372PyOS_sighandler_t
1373PyOS_setsig(int sig, PyOS_sighandler_t handler)
1374{
1375#ifdef HAVE_SIGACTION
1376 struct sigaction context;
1377 PyOS_sighandler_t oldhandler;
1378 sigaction(sig, NULL, &context);
1379 oldhandler = context.sa_handler;
1380 context.sa_handler = handler;
1381 sigaction(sig, &context, NULL);
1382 return oldhandler;
1383#else
1384 return signal(sig, handler);
1385#endif
1386}