blob: f7c282089d0d5361a5ff33f06da62f17a2876f82 [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 *);
43static PyObject *run_pyc_file(FILE *, char *, PyObject *, PyObject *);
Tim Petersdbd9ba62000-07-09 03:09:57 +000044static void err_input(perrdetail *);
45static void initsigs(void);
46static void call_sys_exitfunc(void);
47static void call_ll_exitfuncs(void);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +000048
Guido van Rossumbffd6832000-01-20 22:32:56 +000049#ifdef Py_TRACE_REFS
50int _Py_AskYesNo(char *prompt);
51#endif
52
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000053extern void _PyUnicode_Init(void);
54extern void _PyUnicode_Fini(void);
55extern void _PyCodecRegistry_Init(void);
56extern void _PyCodecRegistry_Fini(void);
Guido van Rossumc94044c2000-03-10 23:03:54 +000057
Guido van Rossum82598051997-03-05 00:20:32 +000058int Py_DebugFlag; /* Needed by parser.c */
59int Py_VerboseFlag; /* Needed by import.c */
Guido van Rossum7433b121997-02-14 19:45:36 +000060int Py_InteractiveFlag; /* Needed by Py_FdIsInteractive() below */
Guido van Rossumdcc0c131997-08-29 22:32:42 +000061int Py_NoSiteFlag; /* Suppress 'import site' */
Barry Warsaw3ce09642000-05-02 19:18:59 +000062int Py_UseClassExceptionsFlag = 1; /* Needed by bltinmodule.c: deprecated */
Guido van Rossuma61691e1998-02-06 22:27:24 +000063int Py_FrozenFlag; /* Needed by getpath.c */
Guido van Rossumb16d1972000-05-01 17:55:15 +000064int Py_UnicodeFlag = 0; /* Needed by compile.c */
Guido van Rossum1984f1e1992-08-04 12:41:02 +000065
Guido van Rossum25ce5661997-08-02 03:10:38 +000066static int initialized = 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +000067
Thomas Wouters7e474022000-07-16 12:04:32 +000068/* API to access the initialized flag -- useful for esoteric use */
Guido van Rossume3c0d5e1997-08-22 04:20:13 +000069
70int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000071Py_IsInitialized(void)
Guido van Rossume3c0d5e1997-08-22 04:20:13 +000072{
73 return initialized;
74}
75
Guido van Rossum25ce5661997-08-02 03:10:38 +000076/* Global initializations. Can be undone by Py_Finalize(). Don't
77 call this twice without an intervening Py_Finalize() call. When
78 initializations fail, a fatal error is issued and the function does
79 not return. On return, the first thread and interpreter state have
80 been created.
Guido van Rossum1984f1e1992-08-04 12:41:02 +000081
Guido van Rossum25ce5661997-08-02 03:10:38 +000082 Locking: you must hold the interpreter lock while calling this.
83 (If the lock has not yet been initialized, that's equivalent to
84 having the lock, but you cannot use multiple threads.)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +000085
Guido van Rossum25ce5661997-08-02 03:10:38 +000086*/
Guido van Rossuma027efa1997-05-05 20:56:21 +000087
88void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000089Py_Initialize(void)
Guido van Rossuma027efa1997-05-05 20:56:21 +000090{
Guido van Rossuma027efa1997-05-05 20:56:21 +000091 PyInterpreterState *interp;
Guido van Rossum25ce5661997-08-02 03:10:38 +000092 PyThreadState *tstate;
93 PyObject *bimod, *sysmod;
Guido van Rossumad6dfda1997-07-19 19:17:22 +000094 char *p;
Guido van Rossumad6dfda1997-07-19 19:17:22 +000095
Guido van Rossumdcc0c131997-08-29 22:32:42 +000096 if (initialized)
Guido van Rossumaa615051997-08-20 22:40:18 +000097 return;
Guido van Rossumdcc0c131997-08-29 22:32:42 +000098 initialized = 1;
Guido van Rossum25ce5661997-08-02 03:10:38 +000099
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000100 if ((p = getenv("PYTHONDEBUG")) && *p != '\0')
Marc-André Lemburgdc3d6062000-08-25 21:00:46 +0000101 Py_DebugFlag = Py_DebugFlag ? Py_DebugFlag : 1;
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000102 if ((p = getenv("PYTHONVERBOSE")) && *p != '\0')
Marc-André Lemburgdc3d6062000-08-25 21:00:46 +0000103 Py_VerboseFlag = Py_VerboseFlag ? Py_VerboseFlag : 1;
Guido van Rossum562f5b11998-10-07 14:50:42 +0000104 if ((p = getenv("PYTHONOPTIMIZE")) && *p != '\0')
Marc-André Lemburgdc3d6062000-08-25 21:00:46 +0000105 Py_OptimizeFlag = Py_OptimizeFlag ? Py_OptimizeFlag : 1;
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000106
Guido van Rossuma027efa1997-05-05 20:56:21 +0000107 interp = PyInterpreterState_New();
108 if (interp == NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000109 Py_FatalError("Py_Initialize: can't make first interpreter");
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000110
Guido van Rossuma027efa1997-05-05 20:56:21 +0000111 tstate = PyThreadState_New(interp);
112 if (tstate == NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000113 Py_FatalError("Py_Initialize: can't make first thread");
Guido van Rossuma027efa1997-05-05 20:56:21 +0000114 (void) PyThreadState_Swap(tstate);
115
Guido van Rossum25ce5661997-08-02 03:10:38 +0000116 interp->modules = PyDict_New();
117 if (interp->modules == NULL)
118 Py_FatalError("Py_Initialize: can't make modules dictionary");
Guido van Rossuma027efa1997-05-05 20:56:21 +0000119
Guido van Rossumc94044c2000-03-10 23:03:54 +0000120 /* Init codec registry */
121 _PyCodecRegistry_Init();
122
123 /* Init Unicode implementation; relies on the codec registry */
124 _PyUnicode_Init();
125
Barry Warsawf242aa02000-05-25 23:09:49 +0000126 bimod = _PyBuiltin_Init();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000127 if (bimod == NULL)
128 Py_FatalError("Py_Initialize: can't initialize __builtin__");
Guido van Rossum4a1f39a1997-11-04 19:36:18 +0000129 interp->builtins = PyModule_GetDict(bimod);
130 Py_INCREF(interp->builtins);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000131
132 sysmod = _PySys_Init();
133 if (sysmod == NULL)
134 Py_FatalError("Py_Initialize: can't initialize sys");
135 interp->sysdict = PyModule_GetDict(sysmod);
136 Py_INCREF(interp->sysdict);
137 _PyImport_FixupExtension("sys", "sys");
Guido van Rossuma027efa1997-05-05 20:56:21 +0000138 PySys_SetPath(Py_GetPath());
Guido van Rossum25ce5661997-08-02 03:10:38 +0000139 PyDict_SetItemString(interp->sysdict, "modules",
140 interp->modules);
141
Guido van Rossum7c85ab81999-07-08 17:26:56 +0000142 _PyImport_Init();
143
Barry Warsawf242aa02000-05-25 23:09:49 +0000144 /* initialize builtin exceptions */
145 init_exceptions();
146
Barry Warsaw035574d1997-08-29 22:07:17 +0000147 /* phase 2 of builtins */
Barry Warsaw963b8711997-09-18 16:42:02 +0000148 _PyImport_FixupExtension("__builtin__", "__builtin__");
Barry Warsaw035574d1997-08-29 22:07:17 +0000149
Guido van Rossum25ce5661997-08-02 03:10:38 +0000150 initsigs(); /* Signal handling stuff, including initintr() */
151
152 initmain(); /* Module __main__ */
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000153 if (!Py_NoSiteFlag)
154 initsite(); /* Module site */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000155}
156
Guido van Rossum2edcf0d1998-12-15 16:12:00 +0000157#ifdef COUNT_ALLOCS
Tim Petersdbd9ba62000-07-09 03:09:57 +0000158extern void dump_counts(void);
Guido van Rossum2edcf0d1998-12-15 16:12:00 +0000159#endif
160
Guido van Rossum25ce5661997-08-02 03:10:38 +0000161/* Undo the effect of Py_Initialize().
162
163 Beware: if multiple interpreter and/or thread states exist, these
164 are not wiped out; only the current thread and interpreter state
165 are deleted. But since everything else is deleted, those other
166 interpreter and thread states should no longer be used.
167
168 (XXX We should do better, e.g. wipe out all interpreters and
169 threads.)
170
171 Locking: as above.
172
173*/
174
175void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000176Py_Finalize(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000177{
178 PyInterpreterState *interp;
179 PyThreadState *tstate;
180
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000181 if (!initialized)
Guido van Rossumaa615051997-08-20 22:40:18 +0000182 return;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000183
Tim Peters384fd102001-01-21 03:40:37 +0000184 /* The interpreter is still entirely intact at this point, and the
185 * exit funcs may be relying on that. In particular, if some thread
186 * or exit func is still waiting to do an import, the import machinery
187 * expects Py_IsInitialized() to return true. So don't say the
188 * interpreter is uninitialized until after the exit funcs have run.
189 * Note that Threading.py uses an exit func to do a join on all the
190 * threads created thru it, so this also protects pending imports in
191 * the threads created via Threading.
192 */
Guido van Rossum4cc462e1998-01-19 22:00:38 +0000193 call_sys_exitfunc();
Tim Peters384fd102001-01-21 03:40:37 +0000194 initialized = 0;
Guido van Rossum4cc462e1998-01-19 22:00:38 +0000195
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000196 /* Get current thread state and interpreter pointer */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000197 tstate = PyThreadState_Get();
198 interp = tstate->interp;
199
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000200 /* Disable signal handling */
201 PyOS_FiniInterrupts();
202
Guido van Rossumc94044c2000-03-10 23:03:54 +0000203 /* Cleanup Unicode implementation */
204 _PyUnicode_Fini();
205
206 /* Cleanup Codec registry */
207 _PyCodecRegistry_Fini();
208
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000209 /* Destroy all modules */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000210 PyImport_Cleanup();
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000211
Guido van Rossum1707aad1997-12-08 23:43:45 +0000212 /* Destroy the database used by _PyImport_{Fixup,Find}Extension */
213 _PyImport_Fini();
214
215 /* Debugging stuff */
216#ifdef COUNT_ALLOCS
217 dump_counts();
218#endif
219
220#ifdef Py_REF_DEBUG
221 fprintf(stderr, "[%ld refs]\n", _Py_RefTotal);
222#endif
223
224#ifdef Py_TRACE_REFS
Guido van Rossumeca47842000-04-27 23:44:15 +0000225 if (
226#ifdef MS_WINDOWS /* Only ask on Windows if env var set */
227 getenv("PYTHONDUMPREFS") &&
228#endif /* MS_WINDOWS */
229 _Py_AskYesNo("Print left references?")) {
Guido van Rossum1707aad1997-12-08 23:43:45 +0000230 _Py_PrintReferences(stderr);
231 }
232#endif /* Py_TRACE_REFS */
233
Barry Warsaw035574d1997-08-29 22:07:17 +0000234 /* Now we decref the exception classes. After this point nothing
235 can raise an exception. That's okay, because each Fini() method
236 below has been checked to make sure no exceptions are ever
237 raised.
238 */
Barry Warsawf242aa02000-05-25 23:09:49 +0000239 fini_exceptions();
240
241 /* Delete current thread */
242 PyInterpreterState_Clear(interp);
243 PyThreadState_Swap(NULL);
244 PyInterpreterState_Delete(interp);
245
Guido van Rossumcc283f51997-08-05 02:22:03 +0000246 PyMethod_Fini();
247 PyFrame_Fini();
248 PyCFunction_Fini();
249 PyTuple_Fini();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000250 PyString_Fini();
Guido van Rossumcc283f51997-08-05 02:22:03 +0000251 PyInt_Fini();
252 PyFloat_Fini();
253
254 /* XXX Still allocated:
255 - various static ad-hoc pointers to interned strings
256 - int and float free list blocks
257 - whatever various modules and libraries allocate
258 */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000259
260 PyGrammar_RemoveAccelerators(&_PyParser_Grammar);
Guido van Rossumcc283f51997-08-05 02:22:03 +0000261
262 call_ll_exitfuncs();
263
Guido van Rossumcc283f51997-08-05 02:22:03 +0000264#ifdef Py_TRACE_REFS
Guido van Rossumcc283f51997-08-05 02:22:03 +0000265 _Py_ResetReferences();
266#endif /* Py_TRACE_REFS */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000267}
268
269/* Create and initialize a new interpreter and thread, and return the
270 new thread. This requires that Py_Initialize() has been called
271 first.
272
273 Unsuccessful initialization yields a NULL pointer. Note that *no*
274 exception information is available even in this case -- the
275 exception information is held in the thread, and there is no
276 thread.
277
278 Locking: as above.
279
280*/
281
282PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000283Py_NewInterpreter(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000284{
285 PyInterpreterState *interp;
286 PyThreadState *tstate, *save_tstate;
287 PyObject *bimod, *sysmod;
288
289 if (!initialized)
290 Py_FatalError("Py_NewInterpreter: call Py_Initialize first");
291
292 interp = PyInterpreterState_New();
293 if (interp == NULL)
294 return NULL;
295
296 tstate = PyThreadState_New(interp);
297 if (tstate == NULL) {
298 PyInterpreterState_Delete(interp);
299 return NULL;
300 }
301
302 save_tstate = PyThreadState_Swap(tstate);
303
304 /* XXX The following is lax in error checking */
305
306 interp->modules = PyDict_New();
307
308 bimod = _PyImport_FindExtension("__builtin__", "__builtin__");
309 if (bimod != NULL) {
Guido van Rossum4a1f39a1997-11-04 19:36:18 +0000310 interp->builtins = PyModule_GetDict(bimod);
311 Py_INCREF(interp->builtins);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000312 }
313 sysmod = _PyImport_FindExtension("sys", "sys");
314 if (bimod != NULL && sysmod != NULL) {
315 interp->sysdict = PyModule_GetDict(sysmod);
316 Py_INCREF(interp->sysdict);
317 PySys_SetPath(Py_GetPath());
318 PyDict_SetItemString(interp->sysdict, "modules",
319 interp->modules);
320 initmain();
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000321 if (!Py_NoSiteFlag)
322 initsite();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000323 }
324
325 if (!PyErr_Occurred())
326 return tstate;
327
328 /* Oops, it didn't work. Undo it all. */
329
330 PyErr_Print();
331 PyThreadState_Clear(tstate);
332 PyThreadState_Swap(save_tstate);
333 PyThreadState_Delete(tstate);
334 PyInterpreterState_Delete(interp);
335
336 return NULL;
337}
338
339/* Delete an interpreter and its last thread. This requires that the
340 given thread state is current, that the thread has no remaining
341 frames, and that it is its interpreter's only remaining thread.
342 It is a fatal error to violate these constraints.
343
344 (Py_Finalize() doesn't have these constraints -- it zaps
345 everything, regardless.)
346
347 Locking: as above.
348
349*/
350
351void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000352Py_EndInterpreter(PyThreadState *tstate)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000353{
354 PyInterpreterState *interp = tstate->interp;
355
356 if (tstate != PyThreadState_Get())
357 Py_FatalError("Py_EndInterpreter: thread is not current");
358 if (tstate->frame != NULL)
359 Py_FatalError("Py_EndInterpreter: thread still has a frame");
360 if (tstate != interp->tstate_head || tstate->next != NULL)
361 Py_FatalError("Py_EndInterpreter: not the last thread");
362
363 PyImport_Cleanup();
364 PyInterpreterState_Clear(interp);
365 PyThreadState_Swap(NULL);
366 PyInterpreterState_Delete(interp);
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000367}
368
369static char *progname = "python";
370
371void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000372Py_SetProgramName(char *pn)
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000373{
374 if (pn && *pn)
375 progname = pn;
376}
377
378char *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000379Py_GetProgramName(void)
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000380{
381 return progname;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000382}
383
Guido van Rossuma61691e1998-02-06 22:27:24 +0000384static char *default_home = NULL;
385
386void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000387Py_SetPythonHome(char *home)
Guido van Rossuma61691e1998-02-06 22:27:24 +0000388{
389 default_home = home;
390}
391
392char *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000393Py_GetPythonHome(void)
Guido van Rossuma61691e1998-02-06 22:27:24 +0000394{
395 char *home = default_home;
396 if (home == NULL)
397 home = getenv("PYTHONHOME");
398 return home;
399}
400
Guido van Rossum6135a871995-01-09 17:53:26 +0000401/* Create __main__ module */
402
403static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000404initmain(void)
Guido van Rossum6135a871995-01-09 17:53:26 +0000405{
Guido van Rossum82598051997-03-05 00:20:32 +0000406 PyObject *m, *d;
407 m = PyImport_AddModule("__main__");
Guido van Rossum6135a871995-01-09 17:53:26 +0000408 if (m == NULL)
Guido van Rossum82598051997-03-05 00:20:32 +0000409 Py_FatalError("can't create __main__ module");
410 d = PyModule_GetDict(m);
411 if (PyDict_GetItemString(d, "__builtins__") == NULL) {
Guido van Rossum858cb731997-11-19 16:15:37 +0000412 PyObject *bimod = PyImport_ImportModule("__builtin__");
413 if (bimod == NULL ||
414 PyDict_SetItemString(d, "__builtins__", bimod) != 0)
Guido van Rossum82598051997-03-05 00:20:32 +0000415 Py_FatalError("can't add __builtins__ to __main__");
Barry Warsaw3d05b1a1999-01-29 21:30:22 +0000416 Py_DECREF(bimod);
Guido van Rossum6135a871995-01-09 17:53:26 +0000417 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000418}
419
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000420/* Import the site module (not into __main__ though) */
421
422static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000423initsite(void)
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000424{
425 PyObject *m, *f;
426 m = PyImport_ImportModule("site");
427 if (m == NULL) {
428 f = PySys_GetObject("stderr");
429 if (Py_VerboseFlag) {
430 PyFile_WriteString(
431 "'import site' failed; traceback:\n", f);
432 PyErr_Print();
433 }
434 else {
435 PyFile_WriteString(
436 "'import site' failed; use -v for traceback\n", f);
437 PyErr_Clear();
438 }
439 }
440 else {
441 Py_DECREF(m);
442 }
443}
444
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000445/* Parse input from a file and execute it */
446
447int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000448PyRun_AnyFile(FILE *fp, char *filename)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000449{
Guido van Rossum0df002c2000-08-27 19:21:52 +0000450 return PyRun_AnyFileEx(fp, filename, 0);
451}
452
453int
454PyRun_AnyFileEx(FILE *fp, char *filename, int closeit)
455{
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000456 if (filename == NULL)
457 filename = "???";
Guido van Rossum0df002c2000-08-27 19:21:52 +0000458 if (Py_FdIsInteractive(fp, filename)) {
459 int err = PyRun_InteractiveLoop(fp, filename);
460 if (closeit)
461 fclose(fp);
462 return err;
463 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000464 else
Guido van Rossum0df002c2000-08-27 19:21:52 +0000465 return PyRun_SimpleFileEx(fp, filename, closeit);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000466}
467
468int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000469PyRun_InteractiveLoop(FILE *fp, char *filename)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000470{
Guido van Rossum82598051997-03-05 00:20:32 +0000471 PyObject *v;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000472 int ret;
Jeremy Hylton9f324e92001-03-01 22:59:14 +0000473 PyCompilerFlags flags;
474
475 flags.cf_nested_scopes = 0;
Guido van Rossum82598051997-03-05 00:20:32 +0000476 v = PySys_GetObject("ps1");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000477 if (v == NULL) {
Guido van Rossum82598051997-03-05 00:20:32 +0000478 PySys_SetObject("ps1", v = PyString_FromString(">>> "));
479 Py_XDECREF(v);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000480 }
Guido van Rossum82598051997-03-05 00:20:32 +0000481 v = PySys_GetObject("ps2");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000482 if (v == NULL) {
Guido van Rossum82598051997-03-05 00:20:32 +0000483 PySys_SetObject("ps2", v = PyString_FromString("... "));
484 Py_XDECREF(v);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000485 }
486 for (;;) {
Jeremy Hylton9f324e92001-03-01 22:59:14 +0000487 ret = PyRun_InteractiveOneFlags(fp, filename, &flags);
Guido van Rossumaae0d321996-05-22 16:35:33 +0000488#ifdef Py_REF_DEBUG
Guido van Rossum6f9e4331995-03-29 16:57:48 +0000489 fprintf(stderr, "[%ld refs]\n", _Py_RefTotal);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000490#endif
491 if (ret == E_EOF)
492 return 0;
493 /*
494 if (ret == E_NOMEM)
495 return -1;
496 */
497 }
498}
499
500int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000501PyRun_InteractiveOne(FILE *fp, char *filename)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000502{
Jeremy Hylton9f324e92001-03-01 22:59:14 +0000503 return PyRun_InteractiveOneFlags(fp, filename, NULL);
504}
505
506int
507PyRun_InteractiveOneFlags(FILE *fp, char *filename, PyCompilerFlags *flags)
508{
Guido van Rossum82598051997-03-05 00:20:32 +0000509 PyObject *m, *d, *v, *w;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000510 node *n;
Guido van Rossuma110aa61994-08-29 12:50:44 +0000511 perrdetail err;
Guido van Rossumddc3fb51997-11-25 20:58:13 +0000512 char *ps1 = "", *ps2 = "";
Guido van Rossum82598051997-03-05 00:20:32 +0000513 v = PySys_GetObject("ps1");
Guido van Rossumddc3fb51997-11-25 20:58:13 +0000514 if (v != NULL) {
515 v = PyObject_Str(v);
516 if (v == NULL)
517 PyErr_Clear();
518 else if (PyString_Check(v))
519 ps1 = PyString_AsString(v);
520 }
Guido van Rossum82598051997-03-05 00:20:32 +0000521 w = PySys_GetObject("ps2");
Guido van Rossumddc3fb51997-11-25 20:58:13 +0000522 if (w != NULL) {
523 w = PyObject_Str(w);
524 if (w == NULL)
525 PyErr_Clear();
526 else if (PyString_Check(w))
527 ps2 = PyString_AsString(w);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000528 }
Guido van Rossum82598051997-03-05 00:20:32 +0000529 n = PyParser_ParseFile(fp, filename, &_PyParser_Grammar,
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000530 Py_single_input, ps1, ps2, &err);
Guido van Rossum82598051997-03-05 00:20:32 +0000531 Py_XDECREF(v);
532 Py_XDECREF(w);
Guido van Rossuma110aa61994-08-29 12:50:44 +0000533 if (n == NULL) {
534 if (err.error == E_EOF) {
535 if (err.text)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000536 PyMem_DEL(err.text);
Guido van Rossuma110aa61994-08-29 12:50:44 +0000537 return E_EOF;
538 }
539 err_input(&err);
Guido van Rossum82598051997-03-05 00:20:32 +0000540 PyErr_Print();
Guido van Rossuma110aa61994-08-29 12:50:44 +0000541 return err.error;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000542 }
Guido van Rossum82598051997-03-05 00:20:32 +0000543 m = PyImport_AddModule("__main__");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000544 if (m == NULL)
545 return -1;
Guido van Rossum82598051997-03-05 00:20:32 +0000546 d = PyModule_GetDict(m);
Jeremy Hylton9f324e92001-03-01 22:59:14 +0000547 v = run_node(n, filename, d, d, flags);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000548 if (v == NULL) {
Guido van Rossum82598051997-03-05 00:20:32 +0000549 PyErr_Print();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000550 return -1;
551 }
Guido van Rossum82598051997-03-05 00:20:32 +0000552 Py_DECREF(v);
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000553 if (Py_FlushLine())
554 PyErr_Clear();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000555 return 0;
556}
557
558int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000559PyRun_SimpleFile(FILE *fp, char *filename)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000560{
Guido van Rossum0df002c2000-08-27 19:21:52 +0000561 return PyRun_SimpleFileEx(fp, filename, 0);
562}
563
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000564/* Check whether a file maybe a pyc file: Look at the extension,
565 the file type, and, if we may close it, at the first few bytes. */
566
567static int
568maybe_pyc_file(FILE *fp, char* filename, char* ext, int closeit)
569{
570 if (strcmp(ext, ".pyc") == 0 || strcmp(ext, ".pyo") == 0)
571 return 1;
572
573#ifdef macintosh
574 /* On a mac, we also assume a pyc file for types 'PYC ' and 'APPL' */
575 if (PyMac_getfiletype(filename) == 'PYC '
576 || PyMac_getfiletype(filename) == 'APPL')
577 return 1;
578#endif /* macintosh */
579
580 /* Only look into the file if we are allowed to close it, since
581 it then should also be seekable. */
582 if (closeit) {
583 /* Read only two bytes of the magic. If the file was opened in
584 text mode, the bytes 3 and 4 of the magic (\r\n) might not
585 be read as they are on disk. */
586 unsigned int halfmagic = PyImport_GetMagicNumber() & 0xFFFF;
587 unsigned char buf[2];
Tim Peters3e876562001-02-11 04:35:39 +0000588 /* Mess: In case of -x, the stream is NOT at its start now,
589 and ungetc() was used to push back the first newline,
Tim Peters6f5a4ef2001-02-17 22:02:34 +0000590 which makes the current stream position formally undefined,
591 and a x-platform nightmare.
592 Unfortunately, we have no direct way to know whether -x
593 was specified. So we use a terrible hack: if the current
594 stream position is not 0, we assume -x was specified, and
595 give up. Bug 132850 on SourceForge spells out the
596 hopelessness of trying anything else (fseek and ftell
597 don't work predictably x-platform for text-mode files).
Tim Peters3e876562001-02-11 04:35:39 +0000598 */
Tim Peters3e876562001-02-11 04:35:39 +0000599 int ispyc = 0;
Tim Peters6f5a4ef2001-02-17 22:02:34 +0000600 if (ftell(fp) == 0) {
601 if (fread(buf, 1, 2, fp) == 2 &&
602 ((unsigned int)buf[1]<<8 | buf[0]) == halfmagic)
603 ispyc = 1;
604 rewind(fp);
605 }
Tim Peters3e876562001-02-11 04:35:39 +0000606 return ispyc;
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000607 }
608 return 0;
609}
610
Guido van Rossum0df002c2000-08-27 19:21:52 +0000611int
612PyRun_SimpleFileEx(FILE *fp, char *filename, int closeit)
613{
Guido van Rossum82598051997-03-05 00:20:32 +0000614 PyObject *m, *d, *v;
Guido van Rossumfdef2711994-09-14 13:31:04 +0000615 char *ext;
616
Guido van Rossum82598051997-03-05 00:20:32 +0000617 m = PyImport_AddModule("__main__");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000618 if (m == NULL)
619 return -1;
Guido van Rossum82598051997-03-05 00:20:32 +0000620 d = PyModule_GetDict(m);
Guido van Rossumfdef2711994-09-14 13:31:04 +0000621 ext = filename + strlen(filename) - 4;
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000622 if (maybe_pyc_file(fp, filename, ext, closeit)) {
Guido van Rossumfdef2711994-09-14 13:31:04 +0000623 /* Try to run a pyc file. First, re-open in binary */
Guido van Rossum0df002c2000-08-27 19:21:52 +0000624 if (closeit)
625 fclose(fp);
Guido van Rossumfdef2711994-09-14 13:31:04 +0000626 if( (fp = fopen(filename, "rb")) == NULL ) {
627 fprintf(stderr, "python: Can't reopen .pyc file\n");
628 return -1;
629 }
Guido van Rossum2a7f58d1997-04-02 05:28:38 +0000630 /* Turn on optimization if a .pyo file is given */
631 if (strcmp(ext, ".pyo") == 0)
632 Py_OptimizeFlag = 1;
Guido van Rossumfdef2711994-09-14 13:31:04 +0000633 v = run_pyc_file(fp, filename, d, d);
634 } else {
Guido van Rossum0df002c2000-08-27 19:21:52 +0000635 v = PyRun_FileEx(fp, filename, Py_file_input, d, d, closeit);
Guido van Rossumfdef2711994-09-14 13:31:04 +0000636 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000637 if (v == NULL) {
Guido van Rossum82598051997-03-05 00:20:32 +0000638 PyErr_Print();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000639 return -1;
640 }
Guido van Rossum82598051997-03-05 00:20:32 +0000641 Py_DECREF(v);
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000642 if (Py_FlushLine())
643 PyErr_Clear();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000644 return 0;
645}
646
647int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000648PyRun_SimpleString(char *command)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000649{
Guido van Rossum82598051997-03-05 00:20:32 +0000650 PyObject *m, *d, *v;
651 m = PyImport_AddModule("__main__");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000652 if (m == NULL)
653 return -1;
Guido van Rossum82598051997-03-05 00:20:32 +0000654 d = PyModule_GetDict(m);
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000655 v = PyRun_String(command, Py_file_input, d, d);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000656 if (v == NULL) {
Guido van Rossum82598051997-03-05 00:20:32 +0000657 PyErr_Print();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000658 return -1;
659 }
Guido van Rossum82598051997-03-05 00:20:32 +0000660 Py_DECREF(v);
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000661 if (Py_FlushLine())
662 PyErr_Clear();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000663 return 0;
664}
665
Barry Warsaw035574d1997-08-29 22:07:17 +0000666static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000667parse_syntax_error(PyObject *err, PyObject **message, char **filename,
668 int *lineno, int *offset, char **text)
Barry Warsaw035574d1997-08-29 22:07:17 +0000669{
670 long hold;
671 PyObject *v;
672
673 /* old style errors */
674 if (PyTuple_Check(err))
675 return PyArg_Parse(err, "(O(ziiz))", message, filename,
676 lineno, offset, text);
677
678 /* new style errors. `err' is an instance */
679
680 if (! (v = PyObject_GetAttrString(err, "msg")))
681 goto finally;
682 *message = v;
683
684 if (!(v = PyObject_GetAttrString(err, "filename")))
685 goto finally;
686 if (v == Py_None)
687 *filename = NULL;
688 else if (! (*filename = PyString_AsString(v)))
689 goto finally;
690
691 Py_DECREF(v);
692 if (!(v = PyObject_GetAttrString(err, "lineno")))
693 goto finally;
694 hold = PyInt_AsLong(v);
695 Py_DECREF(v);
696 v = NULL;
697 if (hold < 0 && PyErr_Occurred())
698 goto finally;
699 *lineno = (int)hold;
700
701 if (!(v = PyObject_GetAttrString(err, "offset")))
702 goto finally;
Jeremy Hylton9f1b9932001-02-28 07:07:43 +0000703 if (v == Py_None) {
704 *offset = -1;
705 Py_DECREF(v);
706 v = NULL;
707 } else {
708 hold = PyInt_AsLong(v);
709 Py_DECREF(v);
710 v = NULL;
711 if (hold < 0 && PyErr_Occurred())
712 goto finally;
713 *offset = (int)hold;
714 }
Barry Warsaw035574d1997-08-29 22:07:17 +0000715
716 if (!(v = PyObject_GetAttrString(err, "text")))
717 goto finally;
718 if (v == Py_None)
719 *text = NULL;
720 else if (! (*text = PyString_AsString(v)))
721 goto finally;
722 Py_DECREF(v);
723 return 1;
724
725finally:
726 Py_XDECREF(v);
727 return 0;
728}
729
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000730void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000731PyErr_Print(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000732{
Guido van Rossuma61691e1998-02-06 22:27:24 +0000733 PyErr_PrintEx(1);
734}
735
Jeremy Hylton9f1b9932001-02-28 07:07:43 +0000736static void
737print_error_text(PyObject *f, int offset, char *text)
738{
739 char *nl;
740 if (offset >= 0) {
741 if (offset > 0 && offset == (int)strlen(text))
742 offset--;
743 for (;;) {
744 nl = strchr(text, '\n');
745 if (nl == NULL || nl-text >= offset)
746 break;
747 offset -= (nl+1-text);
748 text = nl+1;
749 }
750 while (*text == ' ' || *text == '\t') {
751 text++;
752 offset--;
753 }
754 }
755 PyFile_WriteString(" ", f);
756 PyFile_WriteString(text, f);
757 if (*text == '\0' || text[strlen(text)-1] != '\n')
758 PyFile_WriteString("\n", f);
759 if (offset == -1)
760 return;
761 PyFile_WriteString(" ", f);
762 offset--;
763 while (offset > 0) {
764 PyFile_WriteString(" ", f);
765 offset--;
766 }
767 PyFile_WriteString("^\n", f);
768}
769
Guido van Rossuma61691e1998-02-06 22:27:24 +0000770void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000771PyErr_PrintEx(int set_sys_last_vars)
Guido van Rossuma61691e1998-02-06 22:27:24 +0000772{
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000773 int err = 0;
Guido van Rossum82598051997-03-05 00:20:32 +0000774 PyObject *exception, *v, *tb, *f;
775 PyErr_Fetch(&exception, &v, &tb);
Barry Warsaw035574d1997-08-29 22:07:17 +0000776 PyErr_NormalizeException(&exception, &v, &tb);
777
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000778 if (exception == NULL)
Guido van Rossum296b4751997-05-23 00:19:20 +0000779 return;
Barry Warsaw035574d1997-08-29 22:07:17 +0000780
Barry Warsaw36b8f941997-08-26 18:09:48 +0000781 if (PyErr_GivenExceptionMatches(exception, PyExc_SystemExit)) {
Guido van Rossum0829c751998-02-28 04:31:39 +0000782 if (Py_FlushLine())
783 PyErr_Clear();
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000784 fflush(stdout);
Guido van Rossum82598051997-03-05 00:20:32 +0000785 if (v == NULL || v == Py_None)
786 Py_Exit(0);
Barry Warsaw035574d1997-08-29 22:07:17 +0000787 if (PyInstance_Check(v)) {
788 /* we expect the error code to be store in the
789 `code' attribute
790 */
791 PyObject *code = PyObject_GetAttrString(v, "code");
792 if (code) {
793 Py_DECREF(v);
794 v = code;
Guido van Rossumaa9606f1997-10-03 13:53:28 +0000795 if (v == Py_None)
796 Py_Exit(0);
Barry Warsaw035574d1997-08-29 22:07:17 +0000797 }
798 /* if we failed to dig out the "code" attribute,
799 then just let the else clause below print the
800 error
801 */
802 }
Guido van Rossum82598051997-03-05 00:20:32 +0000803 if (PyInt_Check(v))
804 Py_Exit((int)PyInt_AsLong(v));
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000805 else {
Guido van Rossum3165fe61992-09-25 21:59:05 +0000806 /* OK to use real stderr here */
Guido van Rossum82598051997-03-05 00:20:32 +0000807 PyObject_Print(v, stderr, Py_PRINT_RAW);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000808 fprintf(stderr, "\n");
Guido van Rossum82598051997-03-05 00:20:32 +0000809 Py_Exit(1);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000810 }
811 }
Guido van Rossuma61691e1998-02-06 22:27:24 +0000812 if (set_sys_last_vars) {
813 PySys_SetObject("last_type", exception);
814 PySys_SetObject("last_value", v);
815 PySys_SetObject("last_traceback", tb);
816 }
Guido van Rossum82598051997-03-05 00:20:32 +0000817 f = PySys_GetObject("stderr");
Guido van Rossum3165fe61992-09-25 21:59:05 +0000818 if (f == NULL)
819 fprintf(stderr, "lost sys.stderr\n");
820 else {
Guido van Rossum0829c751998-02-28 04:31:39 +0000821 if (Py_FlushLine())
822 PyErr_Clear();
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000823 fflush(stdout);
Guido van Rossum0829c751998-02-28 04:31:39 +0000824 err = PyTraceBack_Print(tb, f);
Barry Warsaw36b8f941997-08-26 18:09:48 +0000825 if (err == 0 &&
826 PyErr_GivenExceptionMatches(exception, PyExc_SyntaxError))
827 {
Guido van Rossum82598051997-03-05 00:20:32 +0000828 PyObject *message;
Guido van Rossuma110aa61994-08-29 12:50:44 +0000829 char *filename, *text;
830 int lineno, offset;
Barry Warsaw035574d1997-08-29 22:07:17 +0000831 if (!parse_syntax_error(v, &message, &filename,
832 &lineno, &offset, &text))
Guido van Rossum82598051997-03-05 00:20:32 +0000833 PyErr_Clear();
Guido van Rossuma110aa61994-08-29 12:50:44 +0000834 else {
835 char buf[10];
Guido van Rossum82598051997-03-05 00:20:32 +0000836 PyFile_WriteString(" File \"", f);
Guido van Rossuma110aa61994-08-29 12:50:44 +0000837 if (filename == NULL)
Guido van Rossum82598051997-03-05 00:20:32 +0000838 PyFile_WriteString("<string>", f);
Guido van Rossuma110aa61994-08-29 12:50:44 +0000839 else
Guido van Rossum82598051997-03-05 00:20:32 +0000840 PyFile_WriteString(filename, f);
841 PyFile_WriteString("\", line ", f);
Guido van Rossuma110aa61994-08-29 12:50:44 +0000842 sprintf(buf, "%d", lineno);
Guido van Rossum82598051997-03-05 00:20:32 +0000843 PyFile_WriteString(buf, f);
844 PyFile_WriteString("\n", f);
Jeremy Hylton9f1b9932001-02-28 07:07:43 +0000845 if (text != NULL)
846 print_error_text(f, offset, text);
Guido van Rossum82598051997-03-05 00:20:32 +0000847 Py_INCREF(message);
848 Py_DECREF(v);
Guido van Rossuma110aa61994-08-29 12:50:44 +0000849 v = message;
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000850 /* Can't be bothered to check all those
851 PyFile_WriteString() calls */
852 if (PyErr_Occurred())
853 err = -1;
Guido van Rossuma110aa61994-08-29 12:50:44 +0000854 }
855 }
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000856 if (err) {
857 /* Don't do anything else */
858 }
859 else if (PyClass_Check(exception)) {
Barry Warsaw2f5f6a21997-09-16 21:42:03 +0000860 PyClassObject* exc = (PyClassObject*)exception;
861 PyObject* className = exc->cl_name;
862 PyObject* moduleName =
863 PyDict_GetItemString(exc->cl_dict, "__module__");
864
865 if (moduleName == NULL)
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000866 err = PyFile_WriteString("<unknown>", f);
Barry Warsaw2f5f6a21997-09-16 21:42:03 +0000867 else {
868 char* modstr = PyString_AsString(moduleName);
869 if (modstr && strcmp(modstr, "exceptions"))
870 {
871 err = PyFile_WriteString(modstr, f);
872 err += PyFile_WriteString(".", f);
873 }
874 }
875 if (err == 0) {
876 if (className == NULL)
877 err = PyFile_WriteString("<unknown>", f);
878 else
879 err = PyFile_WriteObject(className, f,
880 Py_PRINT_RAW);
881 }
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000882 }
883 else
884 err = PyFile_WriteObject(exception, f, Py_PRINT_RAW);
885 if (err == 0) {
886 if (v != NULL && v != Py_None) {
Barry Warsaw035574d1997-08-29 22:07:17 +0000887 PyObject *s = PyObject_Str(v);
888 /* only print colon if the str() of the
889 object is not the empty string
890 */
Guido van Rossumd6bf45b1997-09-05 19:11:53 +0000891 if (s == NULL)
892 err = -1;
893 else if (!PyString_Check(s) ||
894 PyString_GET_SIZE(s) != 0)
Barry Warsaw035574d1997-08-29 22:07:17 +0000895 err = PyFile_WriteString(": ", f);
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000896 if (err == 0)
Guido van Rossumd6bf45b1997-09-05 19:11:53 +0000897 err = PyFile_WriteObject(s, f, Py_PRINT_RAW);
898 Py_XDECREF(s);
Guido van Rossum262e1241995-02-07 15:30:45 +0000899 }
Guido van Rossum262e1241995-02-07 15:30:45 +0000900 }
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000901 if (err == 0)
902 err = PyFile_WriteString("\n", f);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000903 }
Guido van Rossum82598051997-03-05 00:20:32 +0000904 Py_XDECREF(exception);
905 Py_XDECREF(v);
906 Py_XDECREF(tb);
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000907 /* If an error happened here, don't show it.
908 XXX This is wrong, but too many callers rely on this behavior. */
909 if (err != 0)
910 PyErr_Clear();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000911}
912
Guido van Rossum82598051997-03-05 00:20:32 +0000913PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000914PyRun_String(char *str, int start, PyObject *globals, PyObject *locals)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000915{
Guido van Rossum82598051997-03-05 00:20:32 +0000916 return run_err_node(PyParser_SimpleParseString(str, start),
Jeremy Hylton9f324e92001-03-01 22:59:14 +0000917 "<string>", globals, locals, NULL);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000918}
919
Guido van Rossum82598051997-03-05 00:20:32 +0000920PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000921PyRun_File(FILE *fp, char *filename, int start, PyObject *globals,
922 PyObject *locals)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000923{
Tim Peterse8682112000-08-27 20:18:17 +0000924 return PyRun_FileEx(fp, filename, start, globals, locals, 0);
Guido van Rossum0df002c2000-08-27 19:21:52 +0000925}
926
927PyObject *
928PyRun_FileEx(FILE *fp, char *filename, int start, PyObject *globals,
929 PyObject *locals, int closeit)
930{
931 node *n = PyParser_SimpleParseFile(fp, filename, start);
932 if (closeit)
933 fclose(fp);
Jeremy Hylton9f324e92001-03-01 22:59:14 +0000934 return run_err_node(n, filename, globals, locals, NULL);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000935}
936
Guido van Rossum82598051997-03-05 00:20:32 +0000937static PyObject *
Jeremy Hylton9f324e92001-03-01 22:59:14 +0000938run_err_node(node *n, char *filename, PyObject *globals, PyObject *locals,
939 PyCompilerFlags *flags)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000940{
Guido van Rossuma110aa61994-08-29 12:50:44 +0000941 if (n == NULL)
942 return NULL;
Jeremy Hylton9f324e92001-03-01 22:59:14 +0000943 return run_node(n, filename, globals, locals, flags);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000944}
945
Guido van Rossum82598051997-03-05 00:20:32 +0000946static PyObject *
Jeremy Hylton9f324e92001-03-01 22:59:14 +0000947run_node(node *n, char *filename, PyObject *globals, PyObject *locals,
948 PyCompilerFlags *flags)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000949{
Guido van Rossum82598051997-03-05 00:20:32 +0000950 PyCodeObject *co;
951 PyObject *v;
Jeremy Hylton9f324e92001-03-01 22:59:14 +0000952 /* XXX pass sess->ss_nested_scopes to PyNode_Compile */
953 co = PyNode_CompileFlags(n, filename, flags);
Guido van Rossum82598051997-03-05 00:20:32 +0000954 PyNode_Free(n);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000955 if (co == NULL)
956 return NULL;
Guido van Rossum82598051997-03-05 00:20:32 +0000957 v = PyEval_EvalCode(co, globals, locals);
958 Py_DECREF(co);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000959 return v;
960}
961
Guido van Rossum82598051997-03-05 00:20:32 +0000962static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000963run_pyc_file(FILE *fp, char *filename, PyObject *globals, PyObject *locals)
Guido van Rossumfdef2711994-09-14 13:31:04 +0000964{
Guido van Rossum82598051997-03-05 00:20:32 +0000965 PyCodeObject *co;
966 PyObject *v;
Guido van Rossumfdef2711994-09-14 13:31:04 +0000967 long magic;
Fred Drakee8de31c2000-08-31 05:38:39 +0000968 long PyImport_GetMagicNumber(void);
Guido van Rossumfdef2711994-09-14 13:31:04 +0000969
Guido van Rossum82598051997-03-05 00:20:32 +0000970 magic = PyMarshal_ReadLongFromFile(fp);
971 if (magic != PyImport_GetMagicNumber()) {
972 PyErr_SetString(PyExc_RuntimeError,
Guido van Rossumfdef2711994-09-14 13:31:04 +0000973 "Bad magic number in .pyc file");
974 return NULL;
975 }
Guido van Rossum82598051997-03-05 00:20:32 +0000976 (void) PyMarshal_ReadLongFromFile(fp);
Tim Petersd9b9ac82001-01-28 00:27:39 +0000977 v = PyMarshal_ReadLastObjectFromFile(fp);
Guido van Rossumfdef2711994-09-14 13:31:04 +0000978 fclose(fp);
Guido van Rossum82598051997-03-05 00:20:32 +0000979 if (v == NULL || !PyCode_Check(v)) {
980 Py_XDECREF(v);
981 PyErr_SetString(PyExc_RuntimeError,
Guido van Rossumfdef2711994-09-14 13:31:04 +0000982 "Bad code object in .pyc file");
983 return NULL;
984 }
Guido van Rossum82598051997-03-05 00:20:32 +0000985 co = (PyCodeObject *)v;
986 v = PyEval_EvalCode(co, globals, locals);
987 Py_DECREF(co);
Guido van Rossumfdef2711994-09-14 13:31:04 +0000988 return v;
989}
990
Guido van Rossum82598051997-03-05 00:20:32 +0000991PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000992Py_CompileString(char *str, char *filename, int start)
Guido van Rossum5b722181993-03-30 17:46:03 +0000993{
994 node *n;
Guido van Rossum82598051997-03-05 00:20:32 +0000995 PyCodeObject *co;
996 n = PyParser_SimpleParseString(str, start);
Guido van Rossuma110aa61994-08-29 12:50:44 +0000997 if (n == NULL)
Guido van Rossum5b722181993-03-30 17:46:03 +0000998 return NULL;
Guido van Rossum82598051997-03-05 00:20:32 +0000999 co = PyNode_Compile(n, filename);
1000 PyNode_Free(n);
1001 return (PyObject *)co;
Guido van Rossum5b722181993-03-30 17:46:03 +00001002}
1003
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001004struct symtable *
1005Py_SymtableString(char *str, char *filename, int start)
1006{
1007 node *n;
1008 struct symtable *st;
1009 n = PyParser_SimpleParseString(str, start);
1010 if (n == NULL)
1011 return NULL;
1012 st = PyNode_CompileSymtable(n, filename);
1013 PyNode_Free(n);
1014 return st;
1015}
1016
Guido van Rossuma110aa61994-08-29 12:50:44 +00001017/* Simplified interface to parsefile -- return node or set exception */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001018
Guido van Rossuma110aa61994-08-29 12:50:44 +00001019node *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001020PyParser_SimpleParseFile(FILE *fp, char *filename, int start)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001021{
Guido van Rossuma110aa61994-08-29 12:50:44 +00001022 node *n;
1023 perrdetail err;
Guido van Rossum82598051997-03-05 00:20:32 +00001024 n = PyParser_ParseFile(fp, filename, &_PyParser_Grammar, start,
Guido van Rossuma110aa61994-08-29 12:50:44 +00001025 (char *)0, (char *)0, &err);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001026 if (n == NULL)
1027 err_input(&err);
1028 return n;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001029}
1030
Guido van Rossuma110aa61994-08-29 12:50:44 +00001031/* Simplified interface to parsestring -- return node or set exception */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001032
Guido van Rossuma110aa61994-08-29 12:50:44 +00001033node *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001034PyParser_SimpleParseString(char *str, int start)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001035{
Guido van Rossuma110aa61994-08-29 12:50:44 +00001036 node *n;
1037 perrdetail err;
Guido van Rossum82598051997-03-05 00:20:32 +00001038 n = PyParser_ParseString(str, &_PyParser_Grammar, start, &err);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001039 if (n == NULL)
1040 err_input(&err);
1041 return n;
1042}
1043
1044/* Set the error appropriate to the given input error code (see errcode.h) */
1045
1046static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001047err_input(perrdetail *err)
Guido van Rossuma110aa61994-08-29 12:50:44 +00001048{
Fred Drake85f36392000-07-11 17:53:00 +00001049 PyObject *v, *w, *errtype;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001050 char *msg = NULL;
Fred Drake85f36392000-07-11 17:53:00 +00001051 errtype = PyExc_SyntaxError;
Guido van Rossum82598051997-03-05 00:20:32 +00001052 v = Py_BuildValue("(ziiz)", err->filename,
Guido van Rossuma110aa61994-08-29 12:50:44 +00001053 err->lineno, err->offset, err->text);
1054 if (err->text != NULL) {
Guido van Rossumb18618d2000-05-03 23:44:39 +00001055 PyMem_DEL(err->text);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001056 err->text = NULL;
1057 }
1058 switch (err->error) {
1059 case E_SYNTAX:
Fred Drake85f36392000-07-11 17:53:00 +00001060 errtype = PyExc_IndentationError;
1061 if (err->expected == INDENT)
1062 msg = "expected an indented block";
1063 else if (err->token == INDENT)
1064 msg = "unexpected indent";
1065 else if (err->token == DEDENT)
1066 msg = "unexpected unindent";
1067 else {
1068 errtype = PyExc_SyntaxError;
1069 msg = "invalid syntax";
1070 }
Guido van Rossuma110aa61994-08-29 12:50:44 +00001071 break;
1072 case E_TOKEN:
1073 msg = "invalid token";
Guido van Rossuma110aa61994-08-29 12:50:44 +00001074 break;
1075 case E_INTR:
Guido van Rossum82598051997-03-05 00:20:32 +00001076 PyErr_SetNone(PyExc_KeyboardInterrupt);
Barry Warsawc80baa31999-01-27 16:39:40 +00001077 Py_XDECREF(v);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001078 return;
1079 case E_NOMEM:
Guido van Rossum82598051997-03-05 00:20:32 +00001080 PyErr_NoMemory();
Barry Warsawc80baa31999-01-27 16:39:40 +00001081 Py_XDECREF(v);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001082 return;
1083 case E_EOF:
1084 msg = "unexpected EOF while parsing";
1085 break;
Fred Drake85f36392000-07-11 17:53:00 +00001086 case E_TABSPACE:
1087 errtype = PyExc_TabError;
Guido van Rossum560e8ad1998-04-10 19:43:42 +00001088 msg = "inconsistent use of tabs and spaces in indentation";
1089 break;
Jeremy Hylton94988062000-06-20 19:10:44 +00001090 case E_OVERFLOW:
1091 msg = "expression too long";
1092 break;
Fred Drake85f36392000-07-11 17:53:00 +00001093 case E_DEDENT:
1094 errtype = PyExc_IndentationError;
1095 msg = "unindent does not match any outer indentation level";
1096 break;
1097 case E_TOODEEP:
1098 errtype = PyExc_IndentationError;
1099 msg = "too many levels of indentation";
1100 break;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001101 default:
1102 fprintf(stderr, "error=%d\n", err->error);
1103 msg = "unknown parsing error";
1104 break;
1105 }
Guido van Rossum82598051997-03-05 00:20:32 +00001106 w = Py_BuildValue("(sO)", msg, v);
Fred Drake85f36392000-07-11 17:53:00 +00001107 PyErr_SetObject(errtype, w);
Guido van Rossum82598051997-03-05 00:20:32 +00001108 Py_XDECREF(w);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001109}
1110
1111/* Print fatal error message and abort */
1112
1113void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001114Py_FatalError(char *msg)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001115{
Guido van Rossum83dd6c31994-09-29 09:38:33 +00001116 fprintf(stderr, "Fatal Python error: %s\n", msg);
Guido van Rossum8ae87c01995-01-26 00:40:38 +00001117#ifdef macintosh
1118 for (;;);
1119#endif
Guido van Rossum9b38a141996-09-11 23:12:24 +00001120#ifdef MS_WIN32
Guido van Rossum23c94461997-05-22 20:21:30 +00001121 OutputDebugString("Fatal Python error: ");
Guido van Rossuma44823b1995-03-14 15:01:17 +00001122 OutputDebugString(msg);
1123 OutputDebugString("\n");
Guido van Rossum0ba35361998-08-13 13:33:16 +00001124#ifdef _DEBUG
1125 DebugBreak();
Guido van Rossuma44823b1995-03-14 15:01:17 +00001126#endif
Guido van Rossum0ba35361998-08-13 13:33:16 +00001127#endif /* MS_WIN32 */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001128 abort();
1129}
1130
1131/* Clean up and exit */
1132
Guido van Rossuma110aa61994-08-29 12:50:44 +00001133#ifdef WITH_THREAD
Guido van Rossum49b56061998-10-01 20:42:43 +00001134#include "pythread.h"
Guido van Rossum82598051997-03-05 00:20:32 +00001135int _PyThread_Started = 0; /* Set by threadmodule.c and maybe others */
Guido van Rossumf9f2e821992-08-17 08:59:08 +00001136#endif
1137
Guido van Rossum2dcfc961998-10-01 16:01:57 +00001138#define NEXITFUNCS 32
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001139static void (*exitfuncs[NEXITFUNCS])(void);
Guido van Rossum1662dd51994-09-07 14:38:28 +00001140static int nexitfuncs = 0;
1141
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001142int Py_AtExit(void (*func)(void))
Guido van Rossum1662dd51994-09-07 14:38:28 +00001143{
1144 if (nexitfuncs >= NEXITFUNCS)
1145 return -1;
1146 exitfuncs[nexitfuncs++] = func;
1147 return 0;
1148}
1149
Guido van Rossumcc283f51997-08-05 02:22:03 +00001150static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001151call_sys_exitfunc(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001152{
Guido van Rossum82598051997-03-05 00:20:32 +00001153 PyObject *exitfunc = PySys_GetObject("exitfunc");
Guido van Rossum59bff391992-09-03 20:28:00 +00001154
1155 if (exitfunc) {
Guido van Rossumbf02fb21998-04-03 21:12:12 +00001156 PyObject *res, *f;
Guido van Rossum82598051997-03-05 00:20:32 +00001157 Py_INCREF(exitfunc);
1158 PySys_SetObject("exitfunc", (PyObject *)NULL);
Guido van Rossumbf02fb21998-04-03 21:12:12 +00001159 f = PySys_GetObject("stderr");
Guido van Rossum82598051997-03-05 00:20:32 +00001160 res = PyEval_CallObject(exitfunc, (PyObject *)NULL);
Guido van Rossum59bff391992-09-03 20:28:00 +00001161 if (res == NULL) {
Guido van Rossumbf02fb21998-04-03 21:12:12 +00001162 if (f)
1163 PyFile_WriteString("Error in sys.exitfunc:\n", f);
Guido van Rossum82598051997-03-05 00:20:32 +00001164 PyErr_Print();
Guido van Rossum59bff391992-09-03 20:28:00 +00001165 }
Guido van Rossum82598051997-03-05 00:20:32 +00001166 Py_DECREF(exitfunc);
Guido van Rossum59bff391992-09-03 20:28:00 +00001167 }
1168
Guido van Rossum0829c751998-02-28 04:31:39 +00001169 if (Py_FlushLine())
1170 PyErr_Clear();
Guido van Rossumcc283f51997-08-05 02:22:03 +00001171}
Guido van Rossum1662dd51994-09-07 14:38:28 +00001172
Guido van Rossumcc283f51997-08-05 02:22:03 +00001173static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001174call_ll_exitfuncs(void)
Guido van Rossumcc283f51997-08-05 02:22:03 +00001175{
Guido van Rossum1662dd51994-09-07 14:38:28 +00001176 while (nexitfuncs > 0)
1177 (*exitfuncs[--nexitfuncs])();
Guido van Rossum25ce5661997-08-02 03:10:38 +00001178
1179 fflush(stdout);
1180 fflush(stderr);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001181}
1182
1183void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001184Py_Exit(int sts)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001185{
Guido van Rossumcc283f51997-08-05 02:22:03 +00001186 Py_Finalize();
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001187
Jack Jansen66a89771995-10-27 13:22:14 +00001188#ifdef macintosh
1189 PyMac_Exit(sts);
1190#else
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001191 exit(sts);
Jack Jansen66a89771995-10-27 13:22:14 +00001192#endif
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001193}
1194
Guido van Rossumf1dc5661993-07-05 10:31:29 +00001195static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001196initsigs(void)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001197{
Guido van Rossuma110aa61994-08-29 12:50:44 +00001198#ifdef HAVE_SIGNAL_H
1199#ifdef SIGPIPE
1200 signal(SIGPIPE, SIG_IGN);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001201#endif
Guido van Rossuma110aa61994-08-29 12:50:44 +00001202#endif /* HAVE_SIGNAL_H */
Guido van Rossum82598051997-03-05 00:20:32 +00001203 PyOS_InitInterrupts(); /* May imply initsignal() */
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001204}
1205
Guido van Rossumaae0d321996-05-22 16:35:33 +00001206#ifdef Py_TRACE_REFS
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001207/* Ask a yes/no question */
1208
Guido van Rossum59bff391992-09-03 20:28:00 +00001209int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001210_Py_AskYesNo(char *prompt)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001211{
1212 char buf[256];
1213
1214 printf("%s [ny] ", prompt);
1215 if (fgets(buf, sizeof buf, stdin) == NULL)
1216 return 0;
1217 return buf[0] == 'y' || buf[0] == 'Y';
1218}
1219#endif
1220
Guido van Rossuma110aa61994-08-29 12:50:44 +00001221#ifdef MPW
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001222
1223/* Check for file descriptor connected to interactive device.
1224 Pretend that stdin is always interactive, other files never. */
1225
1226int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001227isatty(int fd)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001228{
1229 return fd == fileno(stdin);
1230}
1231
1232#endif
Guido van Rossum7433b121997-02-14 19:45:36 +00001233
1234/*
1235 * The file descriptor fd is considered ``interactive'' if either
1236 * a) isatty(fd) is TRUE, or
1237 * b) the -i flag was given, and the filename associated with
1238 * the descriptor is NULL or "<stdin>" or "???".
1239 */
1240int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001241Py_FdIsInteractive(FILE *fp, char *filename)
Guido van Rossum7433b121997-02-14 19:45:36 +00001242{
1243 if (isatty((int)fileno(fp)))
1244 return 1;
1245 if (!Py_InteractiveFlag)
1246 return 0;
1247 return (filename == NULL) ||
1248 (strcmp(filename, "<stdin>") == 0) ||
1249 (strcmp(filename, "???") == 0);
1250}
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001251
1252
1253#if defined(USE_STACKCHECK)
1254#if defined(WIN32) && defined(_MSC_VER)
1255
1256/* Stack checking for Microsoft C */
1257
1258#include <malloc.h>
1259#include <excpt.h>
1260
Fred Drakee8de31c2000-08-31 05:38:39 +00001261/*
1262 * Return non-zero when we run out of memory on the stack; zero otherwise.
1263 */
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001264int
Fred Drake399739f2000-08-31 05:52:44 +00001265PyOS_CheckStack(void)
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001266{
1267 __try {
1268 /* _alloca throws a stack overflow exception if there's
1269 not enough space left on the stack */
1270 _alloca(PYOS_STACK_MARGIN * sizeof(void*));
1271 return 0;
1272 } __except (EXCEPTION_EXECUTE_HANDLER) {
1273 /* just ignore all errors */
1274 }
1275 return 1;
1276}
1277
1278#endif /* WIN32 && _MSC_VER */
1279
1280/* Alternate implementations can be added here... */
1281
1282#endif /* USE_STACKCHECK */
Guido van Rossum6f256182000-09-16 16:32:19 +00001283
1284
1285/* Wrappers around sigaction() or signal(). */
1286
1287PyOS_sighandler_t
1288PyOS_getsig(int sig)
1289{
1290#ifdef HAVE_SIGACTION
1291 struct sigaction context;
1292 sigaction(sig, NULL, &context);
1293 return context.sa_handler;
1294#else
1295 PyOS_sighandler_t handler;
1296 handler = signal(sig, SIG_IGN);
1297 signal(sig, handler);
1298 return handler;
1299#endif
1300}
1301
1302PyOS_sighandler_t
1303PyOS_setsig(int sig, PyOS_sighandler_t handler)
1304{
1305#ifdef HAVE_SIGACTION
1306 struct sigaction context;
1307 PyOS_sighandler_t oldhandler;
1308 sigaction(sig, NULL, &context);
1309 oldhandler = context.sa_handler;
1310 context.sa_handler = handler;
1311 sigaction(sig, &context, NULL);
1312 return oldhandler;
1313#else
1314 return signal(sig, handler);
1315#endif
1316}