blob: 48b875e149b73d1f5fa2d9e96c0aaa90a5a8b78d [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);
39static PyObject *run_err_node(node *n, char *filename,
40 PyObject *globals, PyObject *locals);
41static PyObject *run_node(node *n, char *filename,
42 PyObject *globals, PyObject *locals);
43static PyObject *run_pyc_file(FILE *fp, char *filename,
44 PyObject *globals, PyObject *locals);
45static 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
59
Guido van Rossum82598051997-03-05 00:20:32 +000060int Py_DebugFlag; /* Needed by parser.c */
61int Py_VerboseFlag; /* Needed by import.c */
Guido van Rossum7433b121997-02-14 19:45:36 +000062int Py_InteractiveFlag; /* Needed by Py_FdIsInteractive() below */
Guido van Rossumdcc0c131997-08-29 22:32:42 +000063int Py_NoSiteFlag; /* Suppress 'import site' */
Barry Warsaw3ce09642000-05-02 19:18:59 +000064int Py_UseClassExceptionsFlag = 1; /* Needed by bltinmodule.c: deprecated */
Guido van Rossuma61691e1998-02-06 22:27:24 +000065int Py_FrozenFlag; /* Needed by getpath.c */
Guido van Rossumb16d1972000-05-01 17:55:15 +000066int Py_UnicodeFlag = 0; /* Needed by compile.c */
Guido van Rossum1984f1e1992-08-04 12:41:02 +000067
Guido van Rossum25ce5661997-08-02 03:10:38 +000068static int initialized = 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +000069
Thomas Wouters7e474022000-07-16 12:04:32 +000070/* API to access the initialized flag -- useful for esoteric use */
Guido van Rossume3c0d5e1997-08-22 04:20:13 +000071
72int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000073Py_IsInitialized(void)
Guido van Rossume3c0d5e1997-08-22 04:20:13 +000074{
75 return initialized;
76}
77
Guido van Rossum25ce5661997-08-02 03:10:38 +000078/* Global initializations. Can be undone by Py_Finalize(). Don't
79 call this twice without an intervening Py_Finalize() call. When
80 initializations fail, a fatal error is issued and the function does
81 not return. On return, the first thread and interpreter state have
82 been created.
Guido van Rossum1984f1e1992-08-04 12:41:02 +000083
Guido van Rossum25ce5661997-08-02 03:10:38 +000084 Locking: you must hold the interpreter lock while calling this.
85 (If the lock has not yet been initialized, that's equivalent to
86 having the lock, but you cannot use multiple threads.)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +000087
Guido van Rossum25ce5661997-08-02 03:10:38 +000088*/
Guido van Rossuma027efa1997-05-05 20:56:21 +000089
90void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000091Py_Initialize(void)
Guido van Rossuma027efa1997-05-05 20:56:21 +000092{
Guido van Rossuma027efa1997-05-05 20:56:21 +000093 PyInterpreterState *interp;
Guido van Rossum25ce5661997-08-02 03:10:38 +000094 PyThreadState *tstate;
95 PyObject *bimod, *sysmod;
Guido van Rossumad6dfda1997-07-19 19:17:22 +000096 char *p;
Guido van Rossumad6dfda1997-07-19 19:17:22 +000097
Guido van Rossumdcc0c131997-08-29 22:32:42 +000098 if (initialized)
Guido van Rossumaa615051997-08-20 22:40:18 +000099 return;
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000100 initialized = 1;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000101
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000102 if ((p = getenv("PYTHONDEBUG")) && *p != '\0')
Marc-André Lemburgdc3d6062000-08-25 21:00:46 +0000103 Py_DebugFlag = Py_DebugFlag ? Py_DebugFlag : 1;
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000104 if ((p = getenv("PYTHONVERBOSE")) && *p != '\0')
Marc-André Lemburgdc3d6062000-08-25 21:00:46 +0000105 Py_VerboseFlag = Py_VerboseFlag ? Py_VerboseFlag : 1;
Guido van Rossum562f5b11998-10-07 14:50:42 +0000106 if ((p = getenv("PYTHONOPTIMIZE")) && *p != '\0')
Marc-André Lemburgdc3d6062000-08-25 21:00:46 +0000107 Py_OptimizeFlag = Py_OptimizeFlag ? Py_OptimizeFlag : 1;
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000108
Guido van Rossuma027efa1997-05-05 20:56:21 +0000109 interp = PyInterpreterState_New();
110 if (interp == NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000111 Py_FatalError("Py_Initialize: can't make first interpreter");
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000112
Guido van Rossuma027efa1997-05-05 20:56:21 +0000113 tstate = PyThreadState_New(interp);
114 if (tstate == NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000115 Py_FatalError("Py_Initialize: can't make first thread");
Guido van Rossuma027efa1997-05-05 20:56:21 +0000116 (void) PyThreadState_Swap(tstate);
117
Guido van Rossum25ce5661997-08-02 03:10:38 +0000118 interp->modules = PyDict_New();
119 if (interp->modules == NULL)
120 Py_FatalError("Py_Initialize: can't make modules dictionary");
Guido van Rossuma027efa1997-05-05 20:56:21 +0000121
Guido van Rossumc94044c2000-03-10 23:03:54 +0000122 /* Init codec registry */
123 _PyCodecRegistry_Init();
124
125 /* Init Unicode implementation; relies on the codec registry */
126 _PyUnicode_Init();
127
Barry Warsawf242aa02000-05-25 23:09:49 +0000128 bimod = _PyBuiltin_Init();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000129 if (bimod == NULL)
130 Py_FatalError("Py_Initialize: can't initialize __builtin__");
Guido van Rossum4a1f39a1997-11-04 19:36:18 +0000131 interp->builtins = PyModule_GetDict(bimod);
132 Py_INCREF(interp->builtins);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000133
134 sysmod = _PySys_Init();
135 if (sysmod == NULL)
136 Py_FatalError("Py_Initialize: can't initialize sys");
137 interp->sysdict = PyModule_GetDict(sysmod);
138 Py_INCREF(interp->sysdict);
139 _PyImport_FixupExtension("sys", "sys");
Guido van Rossuma027efa1997-05-05 20:56:21 +0000140 PySys_SetPath(Py_GetPath());
Guido van Rossum25ce5661997-08-02 03:10:38 +0000141 PyDict_SetItemString(interp->sysdict, "modules",
142 interp->modules);
143
Guido van Rossum7c85ab81999-07-08 17:26:56 +0000144 _PyImport_Init();
145
Barry Warsawf242aa02000-05-25 23:09:49 +0000146 /* initialize builtin exceptions */
147 init_exceptions();
148
Barry Warsaw035574d1997-08-29 22:07:17 +0000149 /* phase 2 of builtins */
Barry Warsaw963b8711997-09-18 16:42:02 +0000150 _PyImport_FixupExtension("__builtin__", "__builtin__");
Barry Warsaw035574d1997-08-29 22:07:17 +0000151
Guido van Rossum25ce5661997-08-02 03:10:38 +0000152 initsigs(); /* Signal handling stuff, including initintr() */
153
154 initmain(); /* Module __main__ */
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000155 if (!Py_NoSiteFlag)
156 initsite(); /* Module site */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000157}
158
Guido van Rossum2edcf0d1998-12-15 16:12:00 +0000159#ifdef COUNT_ALLOCS
Tim Petersdbd9ba62000-07-09 03:09:57 +0000160extern void dump_counts(void);
Guido van Rossum2edcf0d1998-12-15 16:12:00 +0000161#endif
162
Guido van Rossum25ce5661997-08-02 03:10:38 +0000163/* Undo the effect of Py_Initialize().
164
165 Beware: if multiple interpreter and/or thread states exist, these
166 are not wiped out; only the current thread and interpreter state
167 are deleted. But since everything else is deleted, those other
168 interpreter and thread states should no longer be used.
169
170 (XXX We should do better, e.g. wipe out all interpreters and
171 threads.)
172
173 Locking: as above.
174
175*/
176
177void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000178Py_Finalize(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000179{
180 PyInterpreterState *interp;
181 PyThreadState *tstate;
182
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000183 if (!initialized)
Guido van Rossumaa615051997-08-20 22:40:18 +0000184 return;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000185
Tim Peters384fd102001-01-21 03:40:37 +0000186 /* The interpreter is still entirely intact at this point, and the
187 * exit funcs may be relying on that. In particular, if some thread
188 * or exit func is still waiting to do an import, the import machinery
189 * expects Py_IsInitialized() to return true. So don't say the
190 * interpreter is uninitialized until after the exit funcs have run.
191 * Note that Threading.py uses an exit func to do a join on all the
192 * threads created thru it, so this also protects pending imports in
193 * the threads created via Threading.
194 */
Guido van Rossum4cc462e1998-01-19 22:00:38 +0000195 call_sys_exitfunc();
Tim Peters384fd102001-01-21 03:40:37 +0000196 initialized = 0;
Guido van Rossum4cc462e1998-01-19 22:00:38 +0000197
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000198 /* Get current thread state and interpreter pointer */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000199 tstate = PyThreadState_Get();
200 interp = tstate->interp;
201
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000202 /* Disable signal handling */
203 PyOS_FiniInterrupts();
204
Guido van Rossumc94044c2000-03-10 23:03:54 +0000205 /* Cleanup Unicode implementation */
206 _PyUnicode_Fini();
207
208 /* Cleanup Codec registry */
209 _PyCodecRegistry_Fini();
210
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000211 /* Destroy all modules */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000212 PyImport_Cleanup();
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000213
Guido van Rossum1707aad1997-12-08 23:43:45 +0000214 /* Destroy the database used by _PyImport_{Fixup,Find}Extension */
215 _PyImport_Fini();
216
217 /* Debugging stuff */
218#ifdef COUNT_ALLOCS
219 dump_counts();
220#endif
221
222#ifdef Py_REF_DEBUG
223 fprintf(stderr, "[%ld refs]\n", _Py_RefTotal);
224#endif
225
226#ifdef Py_TRACE_REFS
Guido van Rossumeca47842000-04-27 23:44:15 +0000227 if (
228#ifdef MS_WINDOWS /* Only ask on Windows if env var set */
229 getenv("PYTHONDUMPREFS") &&
230#endif /* MS_WINDOWS */
231 _Py_AskYesNo("Print left references?")) {
Guido van Rossum1707aad1997-12-08 23:43:45 +0000232 _Py_PrintReferences(stderr);
233 }
234#endif /* Py_TRACE_REFS */
235
Barry Warsaw035574d1997-08-29 22:07:17 +0000236 /* Now we decref the exception classes. After this point nothing
237 can raise an exception. That's okay, because each Fini() method
238 below has been checked to make sure no exceptions are ever
239 raised.
240 */
Barry Warsawf242aa02000-05-25 23:09:49 +0000241 fini_exceptions();
242
243 /* Delete current thread */
244 PyInterpreterState_Clear(interp);
245 PyThreadState_Swap(NULL);
246 PyInterpreterState_Delete(interp);
247
Guido van Rossumcc283f51997-08-05 02:22:03 +0000248 PyMethod_Fini();
249 PyFrame_Fini();
250 PyCFunction_Fini();
251 PyTuple_Fini();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000252 PyString_Fini();
Guido van Rossumcc283f51997-08-05 02:22:03 +0000253 PyInt_Fini();
254 PyFloat_Fini();
255
256 /* XXX Still allocated:
257 - various static ad-hoc pointers to interned strings
258 - int and float free list blocks
259 - whatever various modules and libraries allocate
260 */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000261
262 PyGrammar_RemoveAccelerators(&_PyParser_Grammar);
Guido van Rossumcc283f51997-08-05 02:22:03 +0000263
264 call_ll_exitfuncs();
265
Guido van Rossumcc283f51997-08-05 02:22:03 +0000266#ifdef Py_TRACE_REFS
Guido van Rossumcc283f51997-08-05 02:22:03 +0000267 _Py_ResetReferences();
268#endif /* Py_TRACE_REFS */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000269}
270
271/* Create and initialize a new interpreter and thread, and return the
272 new thread. This requires that Py_Initialize() has been called
273 first.
274
275 Unsuccessful initialization yields a NULL pointer. Note that *no*
276 exception information is available even in this case -- the
277 exception information is held in the thread, and there is no
278 thread.
279
280 Locking: as above.
281
282*/
283
284PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000285Py_NewInterpreter(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000286{
287 PyInterpreterState *interp;
288 PyThreadState *tstate, *save_tstate;
289 PyObject *bimod, *sysmod;
290
291 if (!initialized)
292 Py_FatalError("Py_NewInterpreter: call Py_Initialize first");
293
294 interp = PyInterpreterState_New();
295 if (interp == NULL)
296 return NULL;
297
298 tstate = PyThreadState_New(interp);
299 if (tstate == NULL) {
300 PyInterpreterState_Delete(interp);
301 return NULL;
302 }
303
304 save_tstate = PyThreadState_Swap(tstate);
305
306 /* XXX The following is lax in error checking */
307
308 interp->modules = PyDict_New();
309
310 bimod = _PyImport_FindExtension("__builtin__", "__builtin__");
311 if (bimod != NULL) {
Guido van Rossum4a1f39a1997-11-04 19:36:18 +0000312 interp->builtins = PyModule_GetDict(bimod);
313 Py_INCREF(interp->builtins);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000314 }
315 sysmod = _PyImport_FindExtension("sys", "sys");
316 if (bimod != NULL && sysmod != NULL) {
317 interp->sysdict = PyModule_GetDict(sysmod);
318 Py_INCREF(interp->sysdict);
319 PySys_SetPath(Py_GetPath());
320 PyDict_SetItemString(interp->sysdict, "modules",
321 interp->modules);
322 initmain();
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000323 if (!Py_NoSiteFlag)
324 initsite();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000325 }
326
327 if (!PyErr_Occurred())
328 return tstate;
329
330 /* Oops, it didn't work. Undo it all. */
331
332 PyErr_Print();
333 PyThreadState_Clear(tstate);
334 PyThreadState_Swap(save_tstate);
335 PyThreadState_Delete(tstate);
336 PyInterpreterState_Delete(interp);
337
338 return NULL;
339}
340
341/* Delete an interpreter and its last thread. This requires that the
342 given thread state is current, that the thread has no remaining
343 frames, and that it is its interpreter's only remaining thread.
344 It is a fatal error to violate these constraints.
345
346 (Py_Finalize() doesn't have these constraints -- it zaps
347 everything, regardless.)
348
349 Locking: as above.
350
351*/
352
353void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000354Py_EndInterpreter(PyThreadState *tstate)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000355{
356 PyInterpreterState *interp = tstate->interp;
357
358 if (tstate != PyThreadState_Get())
359 Py_FatalError("Py_EndInterpreter: thread is not current");
360 if (tstate->frame != NULL)
361 Py_FatalError("Py_EndInterpreter: thread still has a frame");
362 if (tstate != interp->tstate_head || tstate->next != NULL)
363 Py_FatalError("Py_EndInterpreter: not the last thread");
364
365 PyImport_Cleanup();
366 PyInterpreterState_Clear(interp);
367 PyThreadState_Swap(NULL);
368 PyInterpreterState_Delete(interp);
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000369}
370
371static char *progname = "python";
372
373void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000374Py_SetProgramName(char *pn)
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000375{
376 if (pn && *pn)
377 progname = pn;
378}
379
380char *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000381Py_GetProgramName(void)
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000382{
383 return progname;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000384}
385
Guido van Rossuma61691e1998-02-06 22:27:24 +0000386static char *default_home = NULL;
387
388void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000389Py_SetPythonHome(char *home)
Guido van Rossuma61691e1998-02-06 22:27:24 +0000390{
391 default_home = home;
392}
393
394char *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000395Py_GetPythonHome(void)
Guido van Rossuma61691e1998-02-06 22:27:24 +0000396{
397 char *home = default_home;
398 if (home == NULL)
399 home = getenv("PYTHONHOME");
400 return home;
401}
402
Guido van Rossum6135a871995-01-09 17:53:26 +0000403/* Create __main__ module */
404
405static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000406initmain(void)
Guido van Rossum6135a871995-01-09 17:53:26 +0000407{
Guido van Rossum82598051997-03-05 00:20:32 +0000408 PyObject *m, *d;
409 m = PyImport_AddModule("__main__");
Guido van Rossum6135a871995-01-09 17:53:26 +0000410 if (m == NULL)
Guido van Rossum82598051997-03-05 00:20:32 +0000411 Py_FatalError("can't create __main__ module");
412 d = PyModule_GetDict(m);
413 if (PyDict_GetItemString(d, "__builtins__") == NULL) {
Guido van Rossum858cb731997-11-19 16:15:37 +0000414 PyObject *bimod = PyImport_ImportModule("__builtin__");
415 if (bimod == NULL ||
416 PyDict_SetItemString(d, "__builtins__", bimod) != 0)
Guido van Rossum82598051997-03-05 00:20:32 +0000417 Py_FatalError("can't add __builtins__ to __main__");
Barry Warsaw3d05b1a1999-01-29 21:30:22 +0000418 Py_DECREF(bimod);
Guido van Rossum6135a871995-01-09 17:53:26 +0000419 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000420}
421
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000422/* Import the site module (not into __main__ though) */
423
424static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000425initsite(void)
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000426{
427 PyObject *m, *f;
428 m = PyImport_ImportModule("site");
429 if (m == NULL) {
430 f = PySys_GetObject("stderr");
431 if (Py_VerboseFlag) {
432 PyFile_WriteString(
433 "'import site' failed; traceback:\n", f);
434 PyErr_Print();
435 }
436 else {
437 PyFile_WriteString(
438 "'import site' failed; use -v for traceback\n", f);
439 PyErr_Clear();
440 }
441 }
442 else {
443 Py_DECREF(m);
444 }
445}
446
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000447/* Parse input from a file and execute it */
448
449int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000450PyRun_AnyFile(FILE *fp, char *filename)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000451{
Guido van Rossum0df002c2000-08-27 19:21:52 +0000452 return PyRun_AnyFileEx(fp, filename, 0);
453}
454
455int
456PyRun_AnyFileEx(FILE *fp, char *filename, int closeit)
457{
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000458 if (filename == NULL)
459 filename = "???";
Guido van Rossum0df002c2000-08-27 19:21:52 +0000460 if (Py_FdIsInteractive(fp, filename)) {
461 int err = PyRun_InteractiveLoop(fp, filename);
462 if (closeit)
463 fclose(fp);
464 return err;
465 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000466 else
Guido van Rossum0df002c2000-08-27 19:21:52 +0000467 return PyRun_SimpleFileEx(fp, filename, closeit);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000468}
469
470int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000471PyRun_InteractiveLoop(FILE *fp, char *filename)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000472{
Guido van Rossum82598051997-03-05 00:20:32 +0000473 PyObject *v;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000474 int ret;
Guido van Rossum82598051997-03-05 00:20:32 +0000475 v = PySys_GetObject("ps1");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000476 if (v == NULL) {
Guido van Rossum82598051997-03-05 00:20:32 +0000477 PySys_SetObject("ps1", v = PyString_FromString(">>> "));
478 Py_XDECREF(v);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000479 }
Guido van Rossum82598051997-03-05 00:20:32 +0000480 v = PySys_GetObject("ps2");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000481 if (v == NULL) {
Guido van Rossum82598051997-03-05 00:20:32 +0000482 PySys_SetObject("ps2", v = PyString_FromString("... "));
483 Py_XDECREF(v);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000484 }
485 for (;;) {
Guido van Rossum82598051997-03-05 00:20:32 +0000486 ret = PyRun_InteractiveOne(fp, filename);
Guido van Rossumaae0d321996-05-22 16:35:33 +0000487#ifdef Py_REF_DEBUG
Guido van Rossum6f9e4331995-03-29 16:57:48 +0000488 fprintf(stderr, "[%ld refs]\n", _Py_RefTotal);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000489#endif
490 if (ret == E_EOF)
491 return 0;
492 /*
493 if (ret == E_NOMEM)
494 return -1;
495 */
496 }
497}
498
499int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000500PyRun_InteractiveOne(FILE *fp, char *filename)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000501{
Guido van Rossum82598051997-03-05 00:20:32 +0000502 PyObject *m, *d, *v, *w;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000503 node *n;
Guido van Rossuma110aa61994-08-29 12:50:44 +0000504 perrdetail err;
Guido van Rossumddc3fb51997-11-25 20:58:13 +0000505 char *ps1 = "", *ps2 = "";
Guido van Rossum82598051997-03-05 00:20:32 +0000506 v = PySys_GetObject("ps1");
Guido van Rossumddc3fb51997-11-25 20:58:13 +0000507 if (v != NULL) {
508 v = PyObject_Str(v);
509 if (v == NULL)
510 PyErr_Clear();
511 else if (PyString_Check(v))
512 ps1 = PyString_AsString(v);
513 }
Guido van Rossum82598051997-03-05 00:20:32 +0000514 w = PySys_GetObject("ps2");
Guido van Rossumddc3fb51997-11-25 20:58:13 +0000515 if (w != NULL) {
516 w = PyObject_Str(w);
517 if (w == NULL)
518 PyErr_Clear();
519 else if (PyString_Check(w))
520 ps2 = PyString_AsString(w);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000521 }
Guido van Rossum82598051997-03-05 00:20:32 +0000522 n = PyParser_ParseFile(fp, filename, &_PyParser_Grammar,
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000523 Py_single_input, ps1, ps2, &err);
Guido van Rossum82598051997-03-05 00:20:32 +0000524 Py_XDECREF(v);
525 Py_XDECREF(w);
Guido van Rossuma110aa61994-08-29 12:50:44 +0000526 if (n == NULL) {
527 if (err.error == E_EOF) {
528 if (err.text)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000529 PyMem_DEL(err.text);
Guido van Rossuma110aa61994-08-29 12:50:44 +0000530 return E_EOF;
531 }
532 err_input(&err);
Guido van Rossum82598051997-03-05 00:20:32 +0000533 PyErr_Print();
Guido van Rossuma110aa61994-08-29 12:50:44 +0000534 return err.error;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000535 }
Guido van Rossum82598051997-03-05 00:20:32 +0000536 m = PyImport_AddModule("__main__");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000537 if (m == NULL)
538 return -1;
Guido van Rossum82598051997-03-05 00:20:32 +0000539 d = PyModule_GetDict(m);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000540 v = run_node(n, filename, d, d);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000541 if (v == NULL) {
Guido van Rossum82598051997-03-05 00:20:32 +0000542 PyErr_Print();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000543 return -1;
544 }
Guido van Rossum82598051997-03-05 00:20:32 +0000545 Py_DECREF(v);
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000546 if (Py_FlushLine())
547 PyErr_Clear();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000548 return 0;
549}
550
551int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000552PyRun_SimpleFile(FILE *fp, char *filename)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000553{
Guido van Rossum0df002c2000-08-27 19:21:52 +0000554 return PyRun_SimpleFileEx(fp, filename, 0);
555}
556
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000557/* Check whether a file maybe a pyc file: Look at the extension,
558 the file type, and, if we may close it, at the first few bytes. */
559
560static int
561maybe_pyc_file(FILE *fp, char* filename, char* ext, int closeit)
562{
563 if (strcmp(ext, ".pyc") == 0 || strcmp(ext, ".pyo") == 0)
564 return 1;
565
566#ifdef macintosh
567 /* On a mac, we also assume a pyc file for types 'PYC ' and 'APPL' */
568 if (PyMac_getfiletype(filename) == 'PYC '
569 || PyMac_getfiletype(filename) == 'APPL')
570 return 1;
571#endif /* macintosh */
572
573 /* Only look into the file if we are allowed to close it, since
574 it then should also be seekable. */
575 if (closeit) {
576 /* Read only two bytes of the magic. If the file was opened in
577 text mode, the bytes 3 and 4 of the magic (\r\n) might not
578 be read as they are on disk. */
579 unsigned int halfmagic = PyImport_GetMagicNumber() & 0xFFFF;
580 unsigned char buf[2];
Tim Peters3e876562001-02-11 04:35:39 +0000581 /* Mess: In case of -x, the stream is NOT at its start now,
582 and ungetc() was used to push back the first newline,
Tim Peters6f5a4ef2001-02-17 22:02:34 +0000583 which makes the current stream position formally undefined,
584 and a x-platform nightmare.
585 Unfortunately, we have no direct way to know whether -x
586 was specified. So we use a terrible hack: if the current
587 stream position is not 0, we assume -x was specified, and
588 give up. Bug 132850 on SourceForge spells out the
589 hopelessness of trying anything else (fseek and ftell
590 don't work predictably x-platform for text-mode files).
Tim Peters3e876562001-02-11 04:35:39 +0000591 */
Tim Peters3e876562001-02-11 04:35:39 +0000592 int ispyc = 0;
Tim Peters6f5a4ef2001-02-17 22:02:34 +0000593 if (ftell(fp) == 0) {
594 if (fread(buf, 1, 2, fp) == 2 &&
595 ((unsigned int)buf[1]<<8 | buf[0]) == halfmagic)
596 ispyc = 1;
597 rewind(fp);
598 }
Tim Peters3e876562001-02-11 04:35:39 +0000599 return ispyc;
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000600 }
601 return 0;
602}
603
Guido van Rossum0df002c2000-08-27 19:21:52 +0000604int
605PyRun_SimpleFileEx(FILE *fp, char *filename, int closeit)
606{
Guido van Rossum82598051997-03-05 00:20:32 +0000607 PyObject *m, *d, *v;
Guido van Rossumfdef2711994-09-14 13:31:04 +0000608 char *ext;
609
Guido van Rossum82598051997-03-05 00:20:32 +0000610 m = PyImport_AddModule("__main__");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000611 if (m == NULL)
612 return -1;
Guido van Rossum82598051997-03-05 00:20:32 +0000613 d = PyModule_GetDict(m);
Guido van Rossumfdef2711994-09-14 13:31:04 +0000614 ext = filename + strlen(filename) - 4;
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000615 if (maybe_pyc_file(fp, filename, ext, closeit)) {
Guido van Rossumfdef2711994-09-14 13:31:04 +0000616 /* Try to run a pyc file. First, re-open in binary */
Guido van Rossum0df002c2000-08-27 19:21:52 +0000617 if (closeit)
618 fclose(fp);
Guido van Rossumfdef2711994-09-14 13:31:04 +0000619 if( (fp = fopen(filename, "rb")) == NULL ) {
620 fprintf(stderr, "python: Can't reopen .pyc file\n");
621 return -1;
622 }
Guido van Rossum2a7f58d1997-04-02 05:28:38 +0000623 /* Turn on optimization if a .pyo file is given */
624 if (strcmp(ext, ".pyo") == 0)
625 Py_OptimizeFlag = 1;
Guido van Rossumfdef2711994-09-14 13:31:04 +0000626 v = run_pyc_file(fp, filename, d, d);
627 } else {
Guido van Rossum0df002c2000-08-27 19:21:52 +0000628 v = PyRun_FileEx(fp, filename, Py_file_input, d, d, closeit);
Guido van Rossumfdef2711994-09-14 13:31:04 +0000629 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000630 if (v == NULL) {
Guido van Rossum82598051997-03-05 00:20:32 +0000631 PyErr_Print();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000632 return -1;
633 }
Guido van Rossum82598051997-03-05 00:20:32 +0000634 Py_DECREF(v);
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000635 if (Py_FlushLine())
636 PyErr_Clear();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000637 return 0;
638}
639
640int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000641PyRun_SimpleString(char *command)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000642{
Guido van Rossum82598051997-03-05 00:20:32 +0000643 PyObject *m, *d, *v;
644 m = PyImport_AddModule("__main__");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000645 if (m == NULL)
646 return -1;
Guido van Rossum82598051997-03-05 00:20:32 +0000647 d = PyModule_GetDict(m);
Guido van Rossumb05a5c71997-05-07 17:46:13 +0000648 v = PyRun_String(command, Py_file_input, d, d);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000649 if (v == NULL) {
Guido van Rossum82598051997-03-05 00:20:32 +0000650 PyErr_Print();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000651 return -1;
652 }
Guido van Rossum82598051997-03-05 00:20:32 +0000653 Py_DECREF(v);
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000654 if (Py_FlushLine())
655 PyErr_Clear();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000656 return 0;
657}
658
Barry Warsaw035574d1997-08-29 22:07:17 +0000659static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000660parse_syntax_error(PyObject *err, PyObject **message, char **filename,
661 int *lineno, int *offset, char **text)
Barry Warsaw035574d1997-08-29 22:07:17 +0000662{
663 long hold;
664 PyObject *v;
665
666 /* old style errors */
667 if (PyTuple_Check(err))
668 return PyArg_Parse(err, "(O(ziiz))", message, filename,
669 lineno, offset, text);
670
671 /* new style errors. `err' is an instance */
672
673 if (! (v = PyObject_GetAttrString(err, "msg")))
674 goto finally;
675 *message = v;
676
677 if (!(v = PyObject_GetAttrString(err, "filename")))
678 goto finally;
679 if (v == Py_None)
680 *filename = NULL;
681 else if (! (*filename = PyString_AsString(v)))
682 goto finally;
683
684 Py_DECREF(v);
685 if (!(v = PyObject_GetAttrString(err, "lineno")))
686 goto finally;
687 hold = PyInt_AsLong(v);
688 Py_DECREF(v);
689 v = NULL;
690 if (hold < 0 && PyErr_Occurred())
691 goto finally;
692 *lineno = (int)hold;
693
694 if (!(v = PyObject_GetAttrString(err, "offset")))
695 goto finally;
696 hold = PyInt_AsLong(v);
697 Py_DECREF(v);
698 v = NULL;
699 if (hold < 0 && PyErr_Occurred())
700 goto finally;
701 *offset = (int)hold;
702
703 if (!(v = PyObject_GetAttrString(err, "text")))
704 goto finally;
705 if (v == Py_None)
706 *text = NULL;
707 else if (! (*text = PyString_AsString(v)))
708 goto finally;
709 Py_DECREF(v);
710 return 1;
711
712finally:
713 Py_XDECREF(v);
714 return 0;
715}
716
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000717void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000718PyErr_Print(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000719{
Guido van Rossuma61691e1998-02-06 22:27:24 +0000720 PyErr_PrintEx(1);
721}
722
723void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000724PyErr_PrintEx(int set_sys_last_vars)
Guido van Rossuma61691e1998-02-06 22:27:24 +0000725{
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000726 int err = 0;
Guido van Rossum82598051997-03-05 00:20:32 +0000727 PyObject *exception, *v, *tb, *f;
728 PyErr_Fetch(&exception, &v, &tb);
Barry Warsaw035574d1997-08-29 22:07:17 +0000729 PyErr_NormalizeException(&exception, &v, &tb);
730
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000731 if (exception == NULL)
Guido van Rossum296b4751997-05-23 00:19:20 +0000732 return;
Barry Warsaw035574d1997-08-29 22:07:17 +0000733
Barry Warsaw36b8f941997-08-26 18:09:48 +0000734 if (PyErr_GivenExceptionMatches(exception, PyExc_SystemExit)) {
Guido van Rossum0829c751998-02-28 04:31:39 +0000735 if (Py_FlushLine())
736 PyErr_Clear();
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000737 fflush(stdout);
Guido van Rossum82598051997-03-05 00:20:32 +0000738 if (v == NULL || v == Py_None)
739 Py_Exit(0);
Barry Warsaw035574d1997-08-29 22:07:17 +0000740 if (PyInstance_Check(v)) {
741 /* we expect the error code to be store in the
742 `code' attribute
743 */
744 PyObject *code = PyObject_GetAttrString(v, "code");
745 if (code) {
746 Py_DECREF(v);
747 v = code;
Guido van Rossumaa9606f1997-10-03 13:53:28 +0000748 if (v == Py_None)
749 Py_Exit(0);
Barry Warsaw035574d1997-08-29 22:07:17 +0000750 }
751 /* if we failed to dig out the "code" attribute,
752 then just let the else clause below print the
753 error
754 */
755 }
Guido van Rossum82598051997-03-05 00:20:32 +0000756 if (PyInt_Check(v))
757 Py_Exit((int)PyInt_AsLong(v));
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000758 else {
Guido van Rossum3165fe61992-09-25 21:59:05 +0000759 /* OK to use real stderr here */
Guido van Rossum82598051997-03-05 00:20:32 +0000760 PyObject_Print(v, stderr, Py_PRINT_RAW);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000761 fprintf(stderr, "\n");
Guido van Rossum82598051997-03-05 00:20:32 +0000762 Py_Exit(1);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000763 }
764 }
Guido van Rossuma61691e1998-02-06 22:27:24 +0000765 if (set_sys_last_vars) {
766 PySys_SetObject("last_type", exception);
767 PySys_SetObject("last_value", v);
768 PySys_SetObject("last_traceback", tb);
769 }
Guido van Rossum82598051997-03-05 00:20:32 +0000770 f = PySys_GetObject("stderr");
Guido van Rossum3165fe61992-09-25 21:59:05 +0000771 if (f == NULL)
772 fprintf(stderr, "lost sys.stderr\n");
773 else {
Guido van Rossum0829c751998-02-28 04:31:39 +0000774 if (Py_FlushLine())
775 PyErr_Clear();
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000776 fflush(stdout);
Guido van Rossum0829c751998-02-28 04:31:39 +0000777 err = PyTraceBack_Print(tb, f);
Barry Warsaw36b8f941997-08-26 18:09:48 +0000778 if (err == 0 &&
779 PyErr_GivenExceptionMatches(exception, PyExc_SyntaxError))
780 {
Guido van Rossum82598051997-03-05 00:20:32 +0000781 PyObject *message;
Guido van Rossuma110aa61994-08-29 12:50:44 +0000782 char *filename, *text;
783 int lineno, offset;
Barry Warsaw035574d1997-08-29 22:07:17 +0000784 if (!parse_syntax_error(v, &message, &filename,
785 &lineno, &offset, &text))
Guido van Rossum82598051997-03-05 00:20:32 +0000786 PyErr_Clear();
Guido van Rossuma110aa61994-08-29 12:50:44 +0000787 else {
788 char buf[10];
Guido van Rossum82598051997-03-05 00:20:32 +0000789 PyFile_WriteString(" File \"", f);
Guido van Rossuma110aa61994-08-29 12:50:44 +0000790 if (filename == NULL)
Guido van Rossum82598051997-03-05 00:20:32 +0000791 PyFile_WriteString("<string>", f);
Guido van Rossuma110aa61994-08-29 12:50:44 +0000792 else
Guido van Rossum82598051997-03-05 00:20:32 +0000793 PyFile_WriteString(filename, f);
794 PyFile_WriteString("\", line ", f);
Guido van Rossuma110aa61994-08-29 12:50:44 +0000795 sprintf(buf, "%d", lineno);
Guido van Rossum82598051997-03-05 00:20:32 +0000796 PyFile_WriteString(buf, f);
797 PyFile_WriteString("\n", f);
Guido van Rossuma110aa61994-08-29 12:50:44 +0000798 if (text != NULL) {
Guido van Rossum798199d1994-09-19 08:08:50 +0000799 char *nl;
800 if (offset > 0 &&
Guido van Rossum478e7181997-05-06 15:24:59 +0000801 offset == (int)strlen(text))
Guido van Rossum798199d1994-09-19 08:08:50 +0000802 offset--;
803 for (;;) {
804 nl = strchr(text, '\n');
805 if (nl == NULL ||
806 nl-text >= offset)
807 break;
808 offset -= (nl+1-text);
809 text = nl+1;
810 }
Guido van Rossuma110aa61994-08-29 12:50:44 +0000811 while (*text == ' ' || *text == '\t') {
812 text++;
813 offset--;
814 }
Guido van Rossum82598051997-03-05 00:20:32 +0000815 PyFile_WriteString(" ", f);
816 PyFile_WriteString(text, f);
Guido van Rossuma110aa61994-08-29 12:50:44 +0000817 if (*text == '\0' ||
818 text[strlen(text)-1] != '\n')
Guido van Rossum82598051997-03-05 00:20:32 +0000819 PyFile_WriteString("\n", f);
820 PyFile_WriteString(" ", f);
Guido van Rossuma110aa61994-08-29 12:50:44 +0000821 offset--;
822 while (offset > 0) {
Guido van Rossum82598051997-03-05 00:20:32 +0000823 PyFile_WriteString(" ", f);
Guido van Rossuma110aa61994-08-29 12:50:44 +0000824 offset--;
825 }
Guido van Rossum82598051997-03-05 00:20:32 +0000826 PyFile_WriteString("^\n", f);
Guido van Rossuma110aa61994-08-29 12:50:44 +0000827 }
Guido van Rossum82598051997-03-05 00:20:32 +0000828 Py_INCREF(message);
829 Py_DECREF(v);
Guido van Rossuma110aa61994-08-29 12:50:44 +0000830 v = message;
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000831 /* Can't be bothered to check all those
832 PyFile_WriteString() calls */
833 if (PyErr_Occurred())
834 err = -1;
Guido van Rossuma110aa61994-08-29 12:50:44 +0000835 }
836 }
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000837 if (err) {
838 /* Don't do anything else */
839 }
840 else if (PyClass_Check(exception)) {
Barry Warsaw2f5f6a21997-09-16 21:42:03 +0000841 PyClassObject* exc = (PyClassObject*)exception;
842 PyObject* className = exc->cl_name;
843 PyObject* moduleName =
844 PyDict_GetItemString(exc->cl_dict, "__module__");
845
846 if (moduleName == NULL)
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000847 err = PyFile_WriteString("<unknown>", f);
Barry Warsaw2f5f6a21997-09-16 21:42:03 +0000848 else {
849 char* modstr = PyString_AsString(moduleName);
850 if (modstr && strcmp(modstr, "exceptions"))
851 {
852 err = PyFile_WriteString(modstr, f);
853 err += PyFile_WriteString(".", f);
854 }
855 }
856 if (err == 0) {
857 if (className == NULL)
858 err = PyFile_WriteString("<unknown>", f);
859 else
860 err = PyFile_WriteObject(className, f,
861 Py_PRINT_RAW);
862 }
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000863 }
864 else
865 err = PyFile_WriteObject(exception, f, Py_PRINT_RAW);
866 if (err == 0) {
867 if (v != NULL && v != Py_None) {
Barry Warsaw035574d1997-08-29 22:07:17 +0000868 PyObject *s = PyObject_Str(v);
869 /* only print colon if the str() of the
870 object is not the empty string
871 */
Guido van Rossumd6bf45b1997-09-05 19:11:53 +0000872 if (s == NULL)
873 err = -1;
874 else if (!PyString_Check(s) ||
875 PyString_GET_SIZE(s) != 0)
Barry Warsaw035574d1997-08-29 22:07:17 +0000876 err = PyFile_WriteString(": ", f);
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000877 if (err == 0)
Guido van Rossumd6bf45b1997-09-05 19:11:53 +0000878 err = PyFile_WriteObject(s, f, Py_PRINT_RAW);
879 Py_XDECREF(s);
Guido van Rossum262e1241995-02-07 15:30:45 +0000880 }
Guido van Rossum262e1241995-02-07 15:30:45 +0000881 }
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000882 if (err == 0)
883 err = PyFile_WriteString("\n", f);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000884 }
Guido van Rossum82598051997-03-05 00:20:32 +0000885 Py_XDECREF(exception);
886 Py_XDECREF(v);
887 Py_XDECREF(tb);
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000888 /* If an error happened here, don't show it.
889 XXX This is wrong, but too many callers rely on this behavior. */
890 if (err != 0)
891 PyErr_Clear();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000892}
893
Guido van Rossum82598051997-03-05 00:20:32 +0000894PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000895PyRun_String(char *str, int start, PyObject *globals, PyObject *locals)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000896{
Guido van Rossum82598051997-03-05 00:20:32 +0000897 return run_err_node(PyParser_SimpleParseString(str, start),
Guido van Rossuma110aa61994-08-29 12:50:44 +0000898 "<string>", globals, locals);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000899}
900
Guido van Rossum82598051997-03-05 00:20:32 +0000901PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000902PyRun_File(FILE *fp, char *filename, int start, PyObject *globals,
903 PyObject *locals)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000904{
Tim Peterse8682112000-08-27 20:18:17 +0000905 return PyRun_FileEx(fp, filename, start, globals, locals, 0);
Guido van Rossum0df002c2000-08-27 19:21:52 +0000906}
907
908PyObject *
909PyRun_FileEx(FILE *fp, char *filename, int start, PyObject *globals,
910 PyObject *locals, int closeit)
911{
912 node *n = PyParser_SimpleParseFile(fp, filename, start);
913 if (closeit)
914 fclose(fp);
915 return run_err_node(n, filename, globals, locals);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000916}
917
Guido van Rossum82598051997-03-05 00:20:32 +0000918static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000919run_err_node(node *n, char *filename, PyObject *globals, PyObject *locals)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000920{
Guido van Rossuma110aa61994-08-29 12:50:44 +0000921 if (n == NULL)
922 return NULL;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000923 return run_node(n, filename, globals, locals);
924}
925
Guido van Rossum82598051997-03-05 00:20:32 +0000926static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000927run_node(node *n, char *filename, PyObject *globals, PyObject *locals)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000928{
Guido van Rossum82598051997-03-05 00:20:32 +0000929 PyCodeObject *co;
930 PyObject *v;
931 co = PyNode_Compile(n, filename);
932 PyNode_Free(n);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000933 if (co == NULL)
934 return NULL;
Guido van Rossum82598051997-03-05 00:20:32 +0000935 v = PyEval_EvalCode(co, globals, locals);
936 Py_DECREF(co);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000937 return v;
938}
939
Guido van Rossum82598051997-03-05 00:20:32 +0000940static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000941run_pyc_file(FILE *fp, char *filename, PyObject *globals, PyObject *locals)
Guido van Rossumfdef2711994-09-14 13:31:04 +0000942{
Guido van Rossum82598051997-03-05 00:20:32 +0000943 PyCodeObject *co;
944 PyObject *v;
Guido van Rossumfdef2711994-09-14 13:31:04 +0000945 long magic;
Fred Drakee8de31c2000-08-31 05:38:39 +0000946 long PyImport_GetMagicNumber(void);
Guido van Rossumfdef2711994-09-14 13:31:04 +0000947
Guido van Rossum82598051997-03-05 00:20:32 +0000948 magic = PyMarshal_ReadLongFromFile(fp);
949 if (magic != PyImport_GetMagicNumber()) {
950 PyErr_SetString(PyExc_RuntimeError,
Guido van Rossumfdef2711994-09-14 13:31:04 +0000951 "Bad magic number in .pyc file");
952 return NULL;
953 }
Guido van Rossum82598051997-03-05 00:20:32 +0000954 (void) PyMarshal_ReadLongFromFile(fp);
Tim Petersd9b9ac82001-01-28 00:27:39 +0000955 v = PyMarshal_ReadLastObjectFromFile(fp);
Guido van Rossumfdef2711994-09-14 13:31:04 +0000956 fclose(fp);
Guido van Rossum82598051997-03-05 00:20:32 +0000957 if (v == NULL || !PyCode_Check(v)) {
958 Py_XDECREF(v);
959 PyErr_SetString(PyExc_RuntimeError,
Guido van Rossumfdef2711994-09-14 13:31:04 +0000960 "Bad code object in .pyc file");
961 return NULL;
962 }
Guido van Rossum82598051997-03-05 00:20:32 +0000963 co = (PyCodeObject *)v;
964 v = PyEval_EvalCode(co, globals, locals);
965 Py_DECREF(co);
Guido van Rossumfdef2711994-09-14 13:31:04 +0000966 return v;
967}
968
Guido van Rossum82598051997-03-05 00:20:32 +0000969PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000970Py_CompileString(char *str, char *filename, int start)
Guido van Rossum5b722181993-03-30 17:46:03 +0000971{
972 node *n;
Guido van Rossum82598051997-03-05 00:20:32 +0000973 PyCodeObject *co;
974 n = PyParser_SimpleParseString(str, start);
Guido van Rossuma110aa61994-08-29 12:50:44 +0000975 if (n == NULL)
Guido van Rossum5b722181993-03-30 17:46:03 +0000976 return NULL;
Guido van Rossum82598051997-03-05 00:20:32 +0000977 co = PyNode_Compile(n, filename);
978 PyNode_Free(n);
979 return (PyObject *)co;
Guido van Rossum5b722181993-03-30 17:46:03 +0000980}
981
Jeremy Hylton4b38da62001-02-02 18:19:15 +0000982struct symtable *
983Py_SymtableString(char *str, char *filename, int start)
984{
985 node *n;
986 struct symtable *st;
987 n = PyParser_SimpleParseString(str, start);
988 if (n == NULL)
989 return NULL;
990 st = PyNode_CompileSymtable(n, filename);
991 PyNode_Free(n);
992 return st;
993}
994
Guido van Rossuma110aa61994-08-29 12:50:44 +0000995/* Simplified interface to parsefile -- return node or set exception */
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000996
Guido van Rossuma110aa61994-08-29 12:50:44 +0000997node *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000998PyParser_SimpleParseFile(FILE *fp, char *filename, int start)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000999{
Guido van Rossuma110aa61994-08-29 12:50:44 +00001000 node *n;
1001 perrdetail err;
Guido van Rossum82598051997-03-05 00:20:32 +00001002 n = PyParser_ParseFile(fp, filename, &_PyParser_Grammar, start,
Guido van Rossuma110aa61994-08-29 12:50:44 +00001003 (char *)0, (char *)0, &err);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001004 if (n == NULL)
1005 err_input(&err);
1006 return n;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001007}
1008
Guido van Rossuma110aa61994-08-29 12:50:44 +00001009/* Simplified interface to parsestring -- return node or set exception */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001010
Guido van Rossuma110aa61994-08-29 12:50:44 +00001011node *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001012PyParser_SimpleParseString(char *str, int start)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001013{
Guido van Rossuma110aa61994-08-29 12:50:44 +00001014 node *n;
1015 perrdetail err;
Guido van Rossum82598051997-03-05 00:20:32 +00001016 n = PyParser_ParseString(str, &_PyParser_Grammar, start, &err);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001017 if (n == NULL)
1018 err_input(&err);
1019 return n;
1020}
1021
1022/* Set the error appropriate to the given input error code (see errcode.h) */
1023
1024static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001025err_input(perrdetail *err)
Guido van Rossuma110aa61994-08-29 12:50:44 +00001026{
Fred Drake85f36392000-07-11 17:53:00 +00001027 PyObject *v, *w, *errtype;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001028 char *msg = NULL;
Fred Drake85f36392000-07-11 17:53:00 +00001029 errtype = PyExc_SyntaxError;
Guido van Rossum82598051997-03-05 00:20:32 +00001030 v = Py_BuildValue("(ziiz)", err->filename,
Guido van Rossuma110aa61994-08-29 12:50:44 +00001031 err->lineno, err->offset, err->text);
1032 if (err->text != NULL) {
Guido van Rossumb18618d2000-05-03 23:44:39 +00001033 PyMem_DEL(err->text);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001034 err->text = NULL;
1035 }
1036 switch (err->error) {
1037 case E_SYNTAX:
Fred Drake85f36392000-07-11 17:53:00 +00001038 errtype = PyExc_IndentationError;
1039 if (err->expected == INDENT)
1040 msg = "expected an indented block";
1041 else if (err->token == INDENT)
1042 msg = "unexpected indent";
1043 else if (err->token == DEDENT)
1044 msg = "unexpected unindent";
1045 else {
1046 errtype = PyExc_SyntaxError;
1047 msg = "invalid syntax";
1048 }
Guido van Rossuma110aa61994-08-29 12:50:44 +00001049 break;
1050 case E_TOKEN:
1051 msg = "invalid token";
Guido van Rossuma110aa61994-08-29 12:50:44 +00001052 break;
1053 case E_INTR:
Guido van Rossum82598051997-03-05 00:20:32 +00001054 PyErr_SetNone(PyExc_KeyboardInterrupt);
Barry Warsawc80baa31999-01-27 16:39:40 +00001055 Py_XDECREF(v);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001056 return;
1057 case E_NOMEM:
Guido van Rossum82598051997-03-05 00:20:32 +00001058 PyErr_NoMemory();
Barry Warsawc80baa31999-01-27 16:39:40 +00001059 Py_XDECREF(v);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001060 return;
1061 case E_EOF:
1062 msg = "unexpected EOF while parsing";
1063 break;
Fred Drake85f36392000-07-11 17:53:00 +00001064 case E_TABSPACE:
1065 errtype = PyExc_TabError;
Guido van Rossum560e8ad1998-04-10 19:43:42 +00001066 msg = "inconsistent use of tabs and spaces in indentation";
1067 break;
Jeremy Hylton94988062000-06-20 19:10:44 +00001068 case E_OVERFLOW:
1069 msg = "expression too long";
1070 break;
Fred Drake85f36392000-07-11 17:53:00 +00001071 case E_DEDENT:
1072 errtype = PyExc_IndentationError;
1073 msg = "unindent does not match any outer indentation level";
1074 break;
1075 case E_TOODEEP:
1076 errtype = PyExc_IndentationError;
1077 msg = "too many levels of indentation";
1078 break;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001079 default:
1080 fprintf(stderr, "error=%d\n", err->error);
1081 msg = "unknown parsing error";
1082 break;
1083 }
Guido van Rossum82598051997-03-05 00:20:32 +00001084 w = Py_BuildValue("(sO)", msg, v);
Fred Drake85f36392000-07-11 17:53:00 +00001085 PyErr_SetObject(errtype, w);
Guido van Rossum82598051997-03-05 00:20:32 +00001086 Py_XDECREF(w);
Fred Drake83cb7972000-08-15 15:49:03 +00001087
1088 if (v != NULL) {
1089 PyObject *exc, *tb;
1090
1091 PyErr_Fetch(&errtype, &exc, &tb);
1092 PyErr_NormalizeException(&errtype, &exc, &tb);
1093 if (PyObject_SetAttrString(exc, "filename",
1094 PyTuple_GET_ITEM(v, 0)))
1095 PyErr_Clear();
1096 if (PyObject_SetAttrString(exc, "lineno",
1097 PyTuple_GET_ITEM(v, 1)))
1098 PyErr_Clear();
1099 if (PyObject_SetAttrString(exc, "offset",
1100 PyTuple_GET_ITEM(v, 2)))
1101 PyErr_Clear();
1102 Py_DECREF(v);
1103 PyErr_Restore(errtype, exc, tb);
1104 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001105}
1106
1107/* Print fatal error message and abort */
1108
1109void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001110Py_FatalError(char *msg)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001111{
Guido van Rossum83dd6c31994-09-29 09:38:33 +00001112 fprintf(stderr, "Fatal Python error: %s\n", msg);
Guido van Rossum8ae87c01995-01-26 00:40:38 +00001113#ifdef macintosh
1114 for (;;);
1115#endif
Guido van Rossum9b38a141996-09-11 23:12:24 +00001116#ifdef MS_WIN32
Guido van Rossum23c94461997-05-22 20:21:30 +00001117 OutputDebugString("Fatal Python error: ");
Guido van Rossuma44823b1995-03-14 15:01:17 +00001118 OutputDebugString(msg);
1119 OutputDebugString("\n");
Guido van Rossum0ba35361998-08-13 13:33:16 +00001120#ifdef _DEBUG
1121 DebugBreak();
Guido van Rossuma44823b1995-03-14 15:01:17 +00001122#endif
Guido van Rossum0ba35361998-08-13 13:33:16 +00001123#endif /* MS_WIN32 */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001124 abort();
1125}
1126
1127/* Clean up and exit */
1128
Guido van Rossuma110aa61994-08-29 12:50:44 +00001129#ifdef WITH_THREAD
Guido van Rossum49b56061998-10-01 20:42:43 +00001130#include "pythread.h"
Guido van Rossum82598051997-03-05 00:20:32 +00001131int _PyThread_Started = 0; /* Set by threadmodule.c and maybe others */
Guido van Rossumf9f2e821992-08-17 08:59:08 +00001132#endif
1133
Guido van Rossum2dcfc961998-10-01 16:01:57 +00001134#define NEXITFUNCS 32
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001135static void (*exitfuncs[NEXITFUNCS])(void);
Guido van Rossum1662dd51994-09-07 14:38:28 +00001136static int nexitfuncs = 0;
1137
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001138int Py_AtExit(void (*func)(void))
Guido van Rossum1662dd51994-09-07 14:38:28 +00001139{
1140 if (nexitfuncs >= NEXITFUNCS)
1141 return -1;
1142 exitfuncs[nexitfuncs++] = func;
1143 return 0;
1144}
1145
Guido van Rossumcc283f51997-08-05 02:22:03 +00001146static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001147call_sys_exitfunc(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001148{
Guido van Rossum82598051997-03-05 00:20:32 +00001149 PyObject *exitfunc = PySys_GetObject("exitfunc");
Guido van Rossum59bff391992-09-03 20:28:00 +00001150
1151 if (exitfunc) {
Guido van Rossumbf02fb21998-04-03 21:12:12 +00001152 PyObject *res, *f;
Guido van Rossum82598051997-03-05 00:20:32 +00001153 Py_INCREF(exitfunc);
1154 PySys_SetObject("exitfunc", (PyObject *)NULL);
Guido van Rossumbf02fb21998-04-03 21:12:12 +00001155 f = PySys_GetObject("stderr");
Guido van Rossum82598051997-03-05 00:20:32 +00001156 res = PyEval_CallObject(exitfunc, (PyObject *)NULL);
Guido van Rossum59bff391992-09-03 20:28:00 +00001157 if (res == NULL) {
Guido van Rossumbf02fb21998-04-03 21:12:12 +00001158 if (f)
1159 PyFile_WriteString("Error in sys.exitfunc:\n", f);
Guido van Rossum82598051997-03-05 00:20:32 +00001160 PyErr_Print();
Guido van Rossum59bff391992-09-03 20:28:00 +00001161 }
Guido van Rossum82598051997-03-05 00:20:32 +00001162 Py_DECREF(exitfunc);
Guido van Rossum59bff391992-09-03 20:28:00 +00001163 }
1164
Guido van Rossum0829c751998-02-28 04:31:39 +00001165 if (Py_FlushLine())
1166 PyErr_Clear();
Guido van Rossumcc283f51997-08-05 02:22:03 +00001167}
Guido van Rossum1662dd51994-09-07 14:38:28 +00001168
Guido van Rossumcc283f51997-08-05 02:22:03 +00001169static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001170call_ll_exitfuncs(void)
Guido van Rossumcc283f51997-08-05 02:22:03 +00001171{
Guido van Rossum1662dd51994-09-07 14:38:28 +00001172 while (nexitfuncs > 0)
1173 (*exitfuncs[--nexitfuncs])();
Guido van Rossum25ce5661997-08-02 03:10:38 +00001174
1175 fflush(stdout);
1176 fflush(stderr);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001177}
1178
1179void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001180Py_Exit(int sts)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001181{
Guido van Rossumcc283f51997-08-05 02:22:03 +00001182 Py_Finalize();
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001183
Jack Jansen66a89771995-10-27 13:22:14 +00001184#ifdef macintosh
1185 PyMac_Exit(sts);
1186#else
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001187 exit(sts);
Jack Jansen66a89771995-10-27 13:22:14 +00001188#endif
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001189}
1190
Guido van Rossumf1dc5661993-07-05 10:31:29 +00001191static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001192initsigs(void)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001193{
Guido van Rossuma110aa61994-08-29 12:50:44 +00001194#ifdef HAVE_SIGNAL_H
1195#ifdef SIGPIPE
1196 signal(SIGPIPE, SIG_IGN);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001197#endif
Guido van Rossuma110aa61994-08-29 12:50:44 +00001198#endif /* HAVE_SIGNAL_H */
Guido van Rossum82598051997-03-05 00:20:32 +00001199 PyOS_InitInterrupts(); /* May imply initsignal() */
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001200}
1201
Guido van Rossumaae0d321996-05-22 16:35:33 +00001202#ifdef Py_TRACE_REFS
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001203/* Ask a yes/no question */
1204
Guido van Rossum59bff391992-09-03 20:28:00 +00001205int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001206_Py_AskYesNo(char *prompt)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001207{
1208 char buf[256];
1209
1210 printf("%s [ny] ", prompt);
1211 if (fgets(buf, sizeof buf, stdin) == NULL)
1212 return 0;
1213 return buf[0] == 'y' || buf[0] == 'Y';
1214}
1215#endif
1216
Guido van Rossuma110aa61994-08-29 12:50:44 +00001217#ifdef MPW
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001218
1219/* Check for file descriptor connected to interactive device.
1220 Pretend that stdin is always interactive, other files never. */
1221
1222int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001223isatty(int fd)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001224{
1225 return fd == fileno(stdin);
1226}
1227
1228#endif
Guido van Rossum7433b121997-02-14 19:45:36 +00001229
1230/*
1231 * The file descriptor fd is considered ``interactive'' if either
1232 * a) isatty(fd) is TRUE, or
1233 * b) the -i flag was given, and the filename associated with
1234 * the descriptor is NULL or "<stdin>" or "???".
1235 */
1236int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001237Py_FdIsInteractive(FILE *fp, char *filename)
Guido van Rossum7433b121997-02-14 19:45:36 +00001238{
1239 if (isatty((int)fileno(fp)))
1240 return 1;
1241 if (!Py_InteractiveFlag)
1242 return 0;
1243 return (filename == NULL) ||
1244 (strcmp(filename, "<stdin>") == 0) ||
1245 (strcmp(filename, "???") == 0);
1246}
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001247
1248
1249#if defined(USE_STACKCHECK)
1250#if defined(WIN32) && defined(_MSC_VER)
1251
1252/* Stack checking for Microsoft C */
1253
1254#include <malloc.h>
1255#include <excpt.h>
1256
Fred Drakee8de31c2000-08-31 05:38:39 +00001257/*
1258 * Return non-zero when we run out of memory on the stack; zero otherwise.
1259 */
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001260int
Fred Drake399739f2000-08-31 05:52:44 +00001261PyOS_CheckStack(void)
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001262{
1263 __try {
1264 /* _alloca throws a stack overflow exception if there's
1265 not enough space left on the stack */
1266 _alloca(PYOS_STACK_MARGIN * sizeof(void*));
1267 return 0;
1268 } __except (EXCEPTION_EXECUTE_HANDLER) {
1269 /* just ignore all errors */
1270 }
1271 return 1;
1272}
1273
1274#endif /* WIN32 && _MSC_VER */
1275
1276/* Alternate implementations can be added here... */
1277
1278#endif /* USE_STACKCHECK */
Guido van Rossum6f256182000-09-16 16:32:19 +00001279
1280
1281/* Wrappers around sigaction() or signal(). */
1282
1283PyOS_sighandler_t
1284PyOS_getsig(int sig)
1285{
1286#ifdef HAVE_SIGACTION
1287 struct sigaction context;
1288 sigaction(sig, NULL, &context);
1289 return context.sa_handler;
1290#else
1291 PyOS_sighandler_t handler;
1292 handler = signal(sig, SIG_IGN);
1293 signal(sig, handler);
1294 return handler;
1295#endif
1296}
1297
1298PyOS_sighandler_t
1299PyOS_setsig(int sig, PyOS_sighandler_t handler)
1300{
1301#ifdef HAVE_SIGACTION
1302 struct sigaction context;
1303 PyOS_sighandler_t oldhandler;
1304 sigaction(sig, NULL, &context);
1305 oldhandler = context.sa_handler;
1306 context.sa_handler = handler;
1307 sigaction(sig, &context, NULL);
1308 return oldhandler;
1309#else
1310 return signal(sig, handler);
1311#endif
1312}