blob: ed54ab386f0094946e195bb397be284b67adcfbf [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 Rossuma110aa61994-08-29 12:50:44 +000016#ifdef HAVE_SIGNAL_H
Guido van Rossuma9e7dc11992-10-18 18:53:57 +000017#include <signal.h>
Guido van Rossuma9e7dc11992-10-18 18:53:57 +000018#endif
19
Martin v. Löwis73d538b2003-03-05 15:13:47 +000020#ifdef HAVE_LANGINFO_H
21#include <locale.h>
22#include <langinfo.h>
23#endif
24
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000025#ifdef MS_WINDOWS
Guido van Rossuma44823b1995-03-14 15:01:17 +000026#undef BYTE
27#include "windows.h"
28#endif
29
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000030extern char *Py_GetPath(void);
Guido van Rossum1984f1e1992-08-04 12:41:02 +000031
Guido van Rossum82598051997-03-05 00:20:32 +000032extern grammar _PyParser_Grammar; /* From graminit.c */
Guido van Rossum1984f1e1992-08-04 12:41:02 +000033
Guido van Rossumb73cc041993-11-01 16:28:59 +000034/* Forward */
Tim Petersdbd9ba62000-07-09 03:09:57 +000035static void initmain(void);
36static void initsite(void);
Martin v. Löwis95292d62002-12-11 14:04:59 +000037static PyObject *run_err_node(node *, const char *, PyObject *, PyObject *,
Jeremy Hylton9f324e92001-03-01 22:59:14 +000038 PyCompilerFlags *);
Martin v. Löwis95292d62002-12-11 14:04:59 +000039static PyObject *run_node(node *, const char *, PyObject *, PyObject *,
Jeremy Hylton9f324e92001-03-01 22:59:14 +000040 PyCompilerFlags *);
Martin v. Löwis95292d62002-12-11 14:04:59 +000041static PyObject *run_pyc_file(FILE *, const char *, PyObject *, PyObject *,
Jeremy Hyltonbc320242001-03-22 02:47:58 +000042 PyCompilerFlags *);
Tim Petersdbd9ba62000-07-09 03:09:57 +000043static void err_input(perrdetail *);
44static void initsigs(void);
45static void call_sys_exitfunc(void);
46static void call_ll_exitfuncs(void);
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000047extern void _PyUnicode_Init(void);
48extern void _PyUnicode_Fini(void);
Guido van Rossumc94044c2000-03-10 23:03:54 +000049
Mark Hammond8d98d2c2003-04-19 15:41:53 +000050#ifdef WITH_THREAD
51extern void _PyGILState_Init(PyInterpreterState *, PyThreadState *);
52extern void _PyGILState_Fini(void);
53#endif /* WITH_THREAD */
54
Guido van Rossum82598051997-03-05 00:20:32 +000055int Py_DebugFlag; /* Needed by parser.c */
56int Py_VerboseFlag; /* Needed by import.c */
Guido van Rossum7433b121997-02-14 19:45:36 +000057int Py_InteractiveFlag; /* Needed by Py_FdIsInteractive() below */
Guido van Rossumdcc0c131997-08-29 22:32:42 +000058int Py_NoSiteFlag; /* Suppress 'import site' */
Barry Warsaw3ce09642000-05-02 19:18:59 +000059int Py_UseClassExceptionsFlag = 1; /* Needed by bltinmodule.c: deprecated */
Guido van Rossuma61691e1998-02-06 22:27:24 +000060int Py_FrozenFlag; /* Needed by getpath.c */
Guido van Rossumb16d1972000-05-01 17:55:15 +000061int Py_UnicodeFlag = 0; /* Needed by compile.c */
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +000062int Py_IgnoreEnvironmentFlag; /* e.g. PYTHONPATH, PYTHONHOME */
Tim Peters3caca232001-12-06 06:23:26 +000063/* _XXX Py_QnewFlag should go away in 2.3. It's true iff -Qnew is passed,
64 on the command line, and is used in 2.2 by ceval.c to make all "/" divisions
65 true divisions (which they will be in 2.3). */
66int _Py_QnewFlag = 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +000067
Mark Hammonda43fd0c2003-02-19 00:33:33 +000068/* Reference to 'warnings' module, to avoid importing it
Mark Hammondedd07732003-07-15 23:03:55 +000069 on the fly when the import lock may be held. See 683658/771097
Mark Hammonda43fd0c2003-02-19 00:33:33 +000070*/
Mark Hammondedd07732003-07-15 23:03:55 +000071static PyObject *warnings_module = NULL;
72
73/* Returns a borrowed reference to the 'warnings' module, or NULL.
74 If the module is returned, it is guaranteed to have been obtained
75 without acquiring the import lock
76*/
Martin v. Löwisa2c17c52003-08-09 09:47:11 +000077PyObject *PyModule_GetWarningsModule(void)
Mark Hammondedd07732003-07-15 23:03:55 +000078{
79 PyObject *typ, *val, *tb;
80 PyObject *all_modules;
81 /* If we managed to get the module at init time, just use it */
82 if (warnings_module)
83 return warnings_module;
84 /* If it wasn't available at init time, it may be available
85 now in sys.modules (common scenario is frozen apps: import
86 at init time fails, but the frozen init code sets up sys.path
87 correctly, then does an implicit import of warnings for us
88 */
89 /* Save and restore any exceptions */
90 PyErr_Fetch(&typ, &val, &tb);
91
Mark Hammond5f4e8ca2003-07-16 01:54:38 +000092 all_modules = PySys_GetObject("modules");
Mark Hammondedd07732003-07-15 23:03:55 +000093 if (all_modules) {
94 warnings_module = PyDict_GetItemString(all_modules, "warnings");
95 /* We keep a ref in the global */
96 Py_XINCREF(warnings_module);
97 }
98 PyErr_Restore(typ, val, tb);
99 return warnings_module;
100}
Mark Hammonda43fd0c2003-02-19 00:33:33 +0000101
Guido van Rossum25ce5661997-08-02 03:10:38 +0000102static int initialized = 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000103
Thomas Wouters7e474022000-07-16 12:04:32 +0000104/* API to access the initialized flag -- useful for esoteric use */
Guido van Rossume3c0d5e1997-08-22 04:20:13 +0000105
106int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000107Py_IsInitialized(void)
Guido van Rossume3c0d5e1997-08-22 04:20:13 +0000108{
109 return initialized;
110}
111
Guido van Rossum25ce5661997-08-02 03:10:38 +0000112/* Global initializations. Can be undone by Py_Finalize(). Don't
113 call this twice without an intervening Py_Finalize() call. When
114 initializations fail, a fatal error is issued and the function does
115 not return. On return, the first thread and interpreter state have
116 been created.
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000117
Guido van Rossum25ce5661997-08-02 03:10:38 +0000118 Locking: you must hold the interpreter lock while calling this.
119 (If the lock has not yet been initialized, that's equivalent to
120 having the lock, but you cannot use multiple threads.)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000121
Guido van Rossum25ce5661997-08-02 03:10:38 +0000122*/
Guido van Rossuma027efa1997-05-05 20:56:21 +0000123
Guido van Rossum9abaf4d2001-10-12 22:17:56 +0000124static int
125add_flag(int flag, const char *envs)
126{
127 int env = atoi(envs);
128 if (flag < env)
129 flag = env;
130 if (flag < 1)
131 flag = 1;
132 return flag;
133}
134
Guido van Rossuma027efa1997-05-05 20:56:21 +0000135void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000136Py_Initialize(void)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000137{
Guido van Rossuma027efa1997-05-05 20:56:21 +0000138 PyInterpreterState *interp;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000139 PyThreadState *tstate;
140 PyObject *bimod, *sysmod;
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000141 char *p;
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000142#if defined(Py_USING_UNICODE) && defined(HAVE_LANGINFO_H) && defined(CODESET)
143 char *codeset;
144 char *saved_locale;
145 PyObject *sys_stream, *sys_isatty;
146#endif
Guido van Rossum70d893a2001-08-16 08:21:42 +0000147 extern void _Py_ReadyTypes(void);
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000148
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000149 if (initialized)
Guido van Rossumaa615051997-08-20 22:40:18 +0000150 return;
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000151 initialized = 1;
Tim Petersd08e3822003-04-17 15:24:21 +0000152
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000153 if ((p = Py_GETENV("PYTHONDEBUG")) && *p != '\0')
Guido van Rossum9abaf4d2001-10-12 22:17:56 +0000154 Py_DebugFlag = add_flag(Py_DebugFlag, p);
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000155 if ((p = Py_GETENV("PYTHONVERBOSE")) && *p != '\0')
Guido van Rossum9abaf4d2001-10-12 22:17:56 +0000156 Py_VerboseFlag = add_flag(Py_VerboseFlag, p);
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000157 if ((p = Py_GETENV("PYTHONOPTIMIZE")) && *p != '\0')
Guido van Rossum9abaf4d2001-10-12 22:17:56 +0000158 Py_OptimizeFlag = add_flag(Py_OptimizeFlag, p);
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000159
Guido van Rossuma027efa1997-05-05 20:56:21 +0000160 interp = PyInterpreterState_New();
161 if (interp == NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000162 Py_FatalError("Py_Initialize: can't make first interpreter");
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000163
Guido van Rossuma027efa1997-05-05 20:56:21 +0000164 tstate = PyThreadState_New(interp);
165 if (tstate == NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000166 Py_FatalError("Py_Initialize: can't make first thread");
Guido van Rossuma027efa1997-05-05 20:56:21 +0000167 (void) PyThreadState_Swap(tstate);
168
Guido van Rossum70d893a2001-08-16 08:21:42 +0000169 _Py_ReadyTypes();
Guido van Rossum528b7eb2001-08-07 17:24:28 +0000170
Neal Norwitzb2501f42002-12-31 03:42:13 +0000171 if (!_PyFrame_Init())
Neal Norwitzc91ed402002-12-30 22:29:22 +0000172 Py_FatalError("Py_Initialize: can't init frames");
173
Neal Norwitzb2501f42002-12-31 03:42:13 +0000174 if (!_PyInt_Init())
Neal Norwitzc91ed402002-12-30 22:29:22 +0000175 Py_FatalError("Py_Initialize: can't init ints");
176
Guido van Rossum25ce5661997-08-02 03:10:38 +0000177 interp->modules = PyDict_New();
178 if (interp->modules == NULL)
179 Py_FatalError("Py_Initialize: can't make modules dictionary");
Guido van Rossuma027efa1997-05-05 20:56:21 +0000180
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000181#ifdef Py_USING_UNICODE
Guido van Rossumc94044c2000-03-10 23:03:54 +0000182 /* Init Unicode implementation; relies on the codec registry */
183 _PyUnicode_Init();
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000184#endif
Guido van Rossumc94044c2000-03-10 23:03:54 +0000185
Barry Warsawf242aa02000-05-25 23:09:49 +0000186 bimod = _PyBuiltin_Init();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000187 if (bimod == NULL)
188 Py_FatalError("Py_Initialize: can't initialize __builtin__");
Guido van Rossum4a1f39a1997-11-04 19:36:18 +0000189 interp->builtins = PyModule_GetDict(bimod);
190 Py_INCREF(interp->builtins);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000191
192 sysmod = _PySys_Init();
193 if (sysmod == NULL)
194 Py_FatalError("Py_Initialize: can't initialize sys");
195 interp->sysdict = PyModule_GetDict(sysmod);
196 Py_INCREF(interp->sysdict);
197 _PyImport_FixupExtension("sys", "sys");
Guido van Rossuma027efa1997-05-05 20:56:21 +0000198 PySys_SetPath(Py_GetPath());
Guido van Rossum25ce5661997-08-02 03:10:38 +0000199 PyDict_SetItemString(interp->sysdict, "modules",
200 interp->modules);
201
Guido van Rossum7c85ab81999-07-08 17:26:56 +0000202 _PyImport_Init();
203
Barry Warsawf242aa02000-05-25 23:09:49 +0000204 /* initialize builtin exceptions */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000205 _PyExc_Init();
Barry Warsaw5821bc52001-08-13 23:04:56 +0000206 _PyImport_FixupExtension("exceptions", "exceptions");
Barry Warsawf242aa02000-05-25 23:09:49 +0000207
Barry Warsaw035574d1997-08-29 22:07:17 +0000208 /* phase 2 of builtins */
Barry Warsaw963b8711997-09-18 16:42:02 +0000209 _PyImport_FixupExtension("__builtin__", "__builtin__");
Barry Warsaw035574d1997-08-29 22:07:17 +0000210
Just van Rossum52e14d62002-12-30 22:08:05 +0000211 _PyImportHooks_Init();
212
Guido van Rossum25ce5661997-08-02 03:10:38 +0000213 initsigs(); /* Signal handling stuff, including initintr() */
214
215 initmain(); /* Module __main__ */
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000216 if (!Py_NoSiteFlag)
217 initsite(); /* Module site */
Just van Rossum5bfba3a2003-02-25 20:25:12 +0000218
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000219 /* auto-thread-state API, if available */
220#ifdef WITH_THREAD
221 _PyGILState_Init(interp, tstate);
222#endif /* WITH_THREAD */
223
Mark Hammondedd07732003-07-15 23:03:55 +0000224 warnings_module = PyImport_ImportModule("warnings");
225 if (!warnings_module)
226 PyErr_Clear();
Martin v. Löwis73d538b2003-03-05 15:13:47 +0000227
228#if defined(Py_USING_UNICODE) && defined(HAVE_LANGINFO_H) && defined(CODESET)
229 /* On Unix, set the file system encoding according to the
230 user's preference, if the CODESET names a well-known
231 Python codec, and Py_FileSystemDefaultEncoding isn't
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000232 initialized by other means. Also set the encoding of
233 stdin and stdout if these are terminals. */
234
Martin v. Löwisf56d0152003-11-13 07:43:21 +0000235 saved_locale = strdup(setlocale(LC_CTYPE, NULL));
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000236 setlocale(LC_CTYPE, "");
237 codeset = nl_langinfo(CODESET);
238 if (codeset && *codeset) {
239 PyObject *enc = PyCodec_Encoder(codeset);
240 if (enc) {
241 codeset = strdup(codeset);
242 Py_DECREF(enc);
243 } else {
244 codeset = NULL;
245 PyErr_Clear();
Martin v. Löwis73d538b2003-03-05 15:13:47 +0000246 }
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000247 } else
248 codeset = NULL;
249 setlocale(LC_CTYPE, saved_locale);
Martin v. Löwisf56d0152003-11-13 07:43:21 +0000250 free(saved_locale);
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000251
252 if (codeset) {
Michael W. Hudson68debc92003-08-11 12:20:24 +0000253 sys_stream = PySys_GetObject("stdin");
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000254 sys_isatty = PyObject_CallMethod(sys_stream, "isatty", "");
255 if (!sys_isatty)
256 PyErr_Clear();
257 if(sys_isatty && PyObject_IsTrue(sys_isatty)) {
258 if (!PyFile_SetEncoding(sys_stream, codeset))
259 Py_FatalError("Cannot set codeset of stdin");
260 }
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000261 Py_XDECREF(sys_isatty);
262
263 sys_stream = PySys_GetObject("stdout");
264 sys_isatty = PyObject_CallMethod(sys_stream, "isatty", "");
265 if (!sys_isatty)
266 PyErr_Clear();
267 if(sys_isatty && PyObject_IsTrue(sys_isatty)) {
268 if (!PyFile_SetEncoding(sys_stream, codeset))
269 Py_FatalError("Cannot set codeset of stdout");
270 }
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000271 Py_XDECREF(sys_isatty);
272
273 if (!Py_FileSystemDefaultEncoding)
274 Py_FileSystemDefaultEncoding = codeset;
275 else
276 free(codeset);
Martin v. Löwis73d538b2003-03-05 15:13:47 +0000277 }
278#endif
Guido van Rossum25ce5661997-08-02 03:10:38 +0000279}
280
Guido van Rossum2edcf0d1998-12-15 16:12:00 +0000281#ifdef COUNT_ALLOCS
Tim Petersdbd9ba62000-07-09 03:09:57 +0000282extern void dump_counts(void);
Guido van Rossum2edcf0d1998-12-15 16:12:00 +0000283#endif
284
Guido van Rossum25ce5661997-08-02 03:10:38 +0000285/* Undo the effect of Py_Initialize().
286
287 Beware: if multiple interpreter and/or thread states exist, these
288 are not wiped out; only the current thread and interpreter state
289 are deleted. But since everything else is deleted, those other
290 interpreter and thread states should no longer be used.
291
292 (XXX We should do better, e.g. wipe out all interpreters and
293 threads.)
294
295 Locking: as above.
296
297*/
298
299void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000300Py_Finalize(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000301{
302 PyInterpreterState *interp;
303 PyThreadState *tstate;
304
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000305 if (!initialized)
Guido van Rossumaa615051997-08-20 22:40:18 +0000306 return;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000307
Tim Peters384fd102001-01-21 03:40:37 +0000308 /* The interpreter is still entirely intact at this point, and the
309 * exit funcs may be relying on that. In particular, if some thread
310 * or exit func is still waiting to do an import, the import machinery
311 * expects Py_IsInitialized() to return true. So don't say the
312 * interpreter is uninitialized until after the exit funcs have run.
313 * Note that Threading.py uses an exit func to do a join on all the
314 * threads created thru it, so this also protects pending imports in
315 * the threads created via Threading.
316 */
Guido van Rossum4cc462e1998-01-19 22:00:38 +0000317 call_sys_exitfunc();
Tim Peters384fd102001-01-21 03:40:37 +0000318 initialized = 0;
Guido van Rossum4cc462e1998-01-19 22:00:38 +0000319
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000320 /* Get current thread state and interpreter pointer */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000321 tstate = PyThreadState_Get();
322 interp = tstate->interp;
323
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000324 /* Disable signal handling */
325 PyOS_FiniInterrupts();
326
Mark Hammonda43fd0c2003-02-19 00:33:33 +0000327 /* drop module references we saved */
Mark Hammondedd07732003-07-15 23:03:55 +0000328 Py_XDECREF(warnings_module);
329 warnings_module = NULL;
Mark Hammonda43fd0c2003-02-19 00:33:33 +0000330
Guido van Rossume13ddc92003-04-17 17:29:22 +0000331 /* Collect garbage. This may call finalizers; it's nice to call these
332 before all modules are destroyed. */
333 PyGC_Collect();
334
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000335 /* Destroy all modules */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000336 PyImport_Cleanup();
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000337
Guido van Rossume13ddc92003-04-17 17:29:22 +0000338 /* Collect final garbage. This disposes of cycles created by
339 new-style class definitions, for example. */
340 PyGC_Collect();
341
Guido van Rossum1707aad1997-12-08 23:43:45 +0000342 /* Destroy the database used by _PyImport_{Fixup,Find}Extension */
343 _PyImport_Fini();
344
345 /* Debugging stuff */
346#ifdef COUNT_ALLOCS
347 dump_counts();
348#endif
349
350#ifdef Py_REF_DEBUG
351 fprintf(stderr, "[%ld refs]\n", _Py_RefTotal);
352#endif
353
Tim Peters9cf25ce2003-04-17 15:21:01 +0000354#ifdef Py_TRACE_REFS
355 /* Display all objects still alive -- this can invoke arbitrary
356 * __repr__ overrides, so requires a mostly-intact interpreter.
357 * Alas, a lot of stuff may still be alive now that will be cleaned
358 * up later.
359 */
Tim Peters269b2a62003-04-17 19:52:29 +0000360 if (Py_GETENV("PYTHONDUMPREFS"))
Tim Peters9cf25ce2003-04-17 15:21:01 +0000361 _Py_PrintReferences(stderr);
Tim Peters9cf25ce2003-04-17 15:21:01 +0000362#endif /* Py_TRACE_REFS */
363
Barry Warsaw035574d1997-08-29 22:07:17 +0000364 /* Now we decref the exception classes. After this point nothing
365 can raise an exception. That's okay, because each Fini() method
366 below has been checked to make sure no exceptions are ever
367 raised.
368 */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000369 _PyExc_Fini();
Barry Warsawf242aa02000-05-25 23:09:49 +0000370
Mark Hammond6cb90292003-04-22 11:18:00 +0000371 /* Cleanup auto-thread-state */
372#ifdef WITH_THREAD
373 _PyGILState_Fini();
374#endif /* WITH_THREAD */
375
Guido van Rossumd922fa42003-04-15 14:10:09 +0000376 /* Clear interpreter state */
Barry Warsawf242aa02000-05-25 23:09:49 +0000377 PyInterpreterState_Clear(interp);
Guido van Rossumd922fa42003-04-15 14:10:09 +0000378
Guido van Rossumd922fa42003-04-15 14:10:09 +0000379 /* Delete current thread */
Barry Warsawf242aa02000-05-25 23:09:49 +0000380 PyThreadState_Swap(NULL);
381 PyInterpreterState_Delete(interp);
382
Guido van Rossumd922fa42003-04-15 14:10:09 +0000383 /* Sundry finalizers */
Guido van Rossumcc283f51997-08-05 02:22:03 +0000384 PyMethod_Fini();
385 PyFrame_Fini();
386 PyCFunction_Fini();
387 PyTuple_Fini();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000388 PyString_Fini();
Guido van Rossumcc283f51997-08-05 02:22:03 +0000389 PyInt_Fini();
390 PyFloat_Fini();
391
Marc-André Lemburg95de5c12002-04-08 08:19:36 +0000392#ifdef Py_USING_UNICODE
393 /* Cleanup Unicode implementation */
394 _PyUnicode_Fini();
395#endif
396
Guido van Rossumcc283f51997-08-05 02:22:03 +0000397 /* XXX Still allocated:
398 - various static ad-hoc pointers to interned strings
399 - int and float free list blocks
400 - whatever various modules and libraries allocate
401 */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000402
403 PyGrammar_RemoveAccelerators(&_PyParser_Grammar);
Guido van Rossumcc283f51997-08-05 02:22:03 +0000404
Tim Peters269b2a62003-04-17 19:52:29 +0000405#ifdef Py_TRACE_REFS
406 /* Display addresses (& refcnts) of all objects still alive.
407 * An address can be used to find the repr of the object, printed
408 * above by _Py_PrintReferences.
409 */
410 if (Py_GETENV("PYTHONDUMPREFS"))
411 _Py_PrintReferenceAddresses(stderr);
412#endif /* Py_TRACE_REFS */
Tim Peters0e871182002-04-13 08:29:14 +0000413#ifdef PYMALLOC_DEBUG
414 if (Py_GETENV("PYTHONMALLOCSTATS"))
415 _PyObject_DebugMallocStats();
416#endif
417
Guido van Rossumcc283f51997-08-05 02:22:03 +0000418 call_ll_exitfuncs();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000419}
420
421/* Create and initialize a new interpreter and thread, and return the
422 new thread. This requires that Py_Initialize() has been called
423 first.
424
425 Unsuccessful initialization yields a NULL pointer. Note that *no*
426 exception information is available even in this case -- the
427 exception information is held in the thread, and there is no
428 thread.
429
430 Locking: as above.
431
432*/
433
434PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000435Py_NewInterpreter(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000436{
437 PyInterpreterState *interp;
438 PyThreadState *tstate, *save_tstate;
439 PyObject *bimod, *sysmod;
440
441 if (!initialized)
442 Py_FatalError("Py_NewInterpreter: call Py_Initialize first");
443
444 interp = PyInterpreterState_New();
445 if (interp == NULL)
446 return NULL;
447
448 tstate = PyThreadState_New(interp);
449 if (tstate == NULL) {
450 PyInterpreterState_Delete(interp);
451 return NULL;
452 }
453
454 save_tstate = PyThreadState_Swap(tstate);
455
456 /* XXX The following is lax in error checking */
457
458 interp->modules = PyDict_New();
459
460 bimod = _PyImport_FindExtension("__builtin__", "__builtin__");
461 if (bimod != NULL) {
Guido van Rossum4a1f39a1997-11-04 19:36:18 +0000462 interp->builtins = PyModule_GetDict(bimod);
463 Py_INCREF(interp->builtins);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000464 }
465 sysmod = _PyImport_FindExtension("sys", "sys");
466 if (bimod != NULL && sysmod != NULL) {
467 interp->sysdict = PyModule_GetDict(sysmod);
468 Py_INCREF(interp->sysdict);
469 PySys_SetPath(Py_GetPath());
470 PyDict_SetItemString(interp->sysdict, "modules",
471 interp->modules);
Martin v. Löwisd7ceb222003-01-22 09:00:38 +0000472 _PyImportHooks_Init();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000473 initmain();
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000474 if (!Py_NoSiteFlag)
475 initsite();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000476 }
477
478 if (!PyErr_Occurred())
479 return tstate;
480
481 /* Oops, it didn't work. Undo it all. */
482
483 PyErr_Print();
484 PyThreadState_Clear(tstate);
485 PyThreadState_Swap(save_tstate);
486 PyThreadState_Delete(tstate);
487 PyInterpreterState_Delete(interp);
488
489 return NULL;
490}
491
492/* Delete an interpreter and its last thread. This requires that the
493 given thread state is current, that the thread has no remaining
494 frames, and that it is its interpreter's only remaining thread.
495 It is a fatal error to violate these constraints.
496
497 (Py_Finalize() doesn't have these constraints -- it zaps
498 everything, regardless.)
499
500 Locking: as above.
501
502*/
503
504void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000505Py_EndInterpreter(PyThreadState *tstate)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000506{
507 PyInterpreterState *interp = tstate->interp;
508
509 if (tstate != PyThreadState_Get())
510 Py_FatalError("Py_EndInterpreter: thread is not current");
511 if (tstate->frame != NULL)
512 Py_FatalError("Py_EndInterpreter: thread still has a frame");
513 if (tstate != interp->tstate_head || tstate->next != NULL)
514 Py_FatalError("Py_EndInterpreter: not the last thread");
515
516 PyImport_Cleanup();
517 PyInterpreterState_Clear(interp);
518 PyThreadState_Swap(NULL);
519 PyInterpreterState_Delete(interp);
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000520}
521
522static char *progname = "python";
523
524void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000525Py_SetProgramName(char *pn)
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000526{
527 if (pn && *pn)
528 progname = pn;
529}
530
531char *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000532Py_GetProgramName(void)
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000533{
534 return progname;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000535}
536
Guido van Rossuma61691e1998-02-06 22:27:24 +0000537static char *default_home = NULL;
538
539void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000540Py_SetPythonHome(char *home)
Guido van Rossuma61691e1998-02-06 22:27:24 +0000541{
542 default_home = home;
543}
544
545char *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000546Py_GetPythonHome(void)
Guido van Rossuma61691e1998-02-06 22:27:24 +0000547{
548 char *home = default_home;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000549 if (home == NULL && !Py_IgnoreEnvironmentFlag)
550 home = Py_GETENV("PYTHONHOME");
Guido van Rossuma61691e1998-02-06 22:27:24 +0000551 return home;
552}
553
Guido van Rossum6135a871995-01-09 17:53:26 +0000554/* Create __main__ module */
555
556static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000557initmain(void)
Guido van Rossum6135a871995-01-09 17:53:26 +0000558{
Guido van Rossum82598051997-03-05 00:20:32 +0000559 PyObject *m, *d;
560 m = PyImport_AddModule("__main__");
Guido van Rossum6135a871995-01-09 17:53:26 +0000561 if (m == NULL)
Guido van Rossum82598051997-03-05 00:20:32 +0000562 Py_FatalError("can't create __main__ module");
563 d = PyModule_GetDict(m);
564 if (PyDict_GetItemString(d, "__builtins__") == NULL) {
Guido van Rossum858cb731997-11-19 16:15:37 +0000565 PyObject *bimod = PyImport_ImportModule("__builtin__");
566 if (bimod == NULL ||
567 PyDict_SetItemString(d, "__builtins__", bimod) != 0)
Guido van Rossum82598051997-03-05 00:20:32 +0000568 Py_FatalError("can't add __builtins__ to __main__");
Barry Warsaw3d05b1a1999-01-29 21:30:22 +0000569 Py_DECREF(bimod);
Guido van Rossum6135a871995-01-09 17:53:26 +0000570 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000571}
572
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000573/* Import the site module (not into __main__ though) */
574
575static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000576initsite(void)
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000577{
578 PyObject *m, *f;
579 m = PyImport_ImportModule("site");
580 if (m == NULL) {
581 f = PySys_GetObject("stderr");
582 if (Py_VerboseFlag) {
583 PyFile_WriteString(
584 "'import site' failed; traceback:\n", f);
585 PyErr_Print();
586 }
587 else {
588 PyFile_WriteString(
589 "'import site' failed; use -v for traceback\n", f);
590 PyErr_Clear();
591 }
592 }
593 else {
594 Py_DECREF(m);
595 }
596}
597
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000598/* Parse input from a file and execute it */
599
600int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000601PyRun_AnyFile(FILE *fp, const char *filename)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000602{
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000603 return PyRun_AnyFileExFlags(fp, filename, 0, NULL);
604}
605
606int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000607PyRun_AnyFileFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000608{
609 return PyRun_AnyFileExFlags(fp, filename, 0, flags);
Guido van Rossum0df002c2000-08-27 19:21:52 +0000610}
611
612int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000613PyRun_AnyFileEx(FILE *fp, const char *filename, int closeit)
Guido van Rossum0df002c2000-08-27 19:21:52 +0000614{
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000615 return PyRun_AnyFileExFlags(fp, filename, closeit, NULL);
616}
617
618int
Tim Petersd08e3822003-04-17 15:24:21 +0000619PyRun_AnyFileExFlags(FILE *fp, const char *filename, int closeit,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000620 PyCompilerFlags *flags)
621{
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000622 if (filename == NULL)
623 filename = "???";
Guido van Rossum0df002c2000-08-27 19:21:52 +0000624 if (Py_FdIsInteractive(fp, filename)) {
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000625 int err = PyRun_InteractiveLoopFlags(fp, filename, flags);
Guido van Rossum0df002c2000-08-27 19:21:52 +0000626 if (closeit)
627 fclose(fp);
628 return err;
629 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000630 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000631 return PyRun_SimpleFileExFlags(fp, filename, closeit, flags);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000632}
633
634int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000635PyRun_InteractiveLoop(FILE *fp, const char *filename)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000636{
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000637 return PyRun_InteractiveLoopFlags(fp, filename, NULL);
638}
639
640int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000641PyRun_InteractiveLoopFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000642{
Guido van Rossum82598051997-03-05 00:20:32 +0000643 PyObject *v;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000644 int ret;
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000645 PyCompilerFlags local_flags;
Jeremy Hylton9f324e92001-03-01 22:59:14 +0000646
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000647 if (flags == NULL) {
648 flags = &local_flags;
Tim Peters5ba58662001-07-16 02:29:45 +0000649 local_flags.cf_flags = 0;
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000650 }
Guido van Rossum82598051997-03-05 00:20:32 +0000651 v = PySys_GetObject("ps1");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000652 if (v == NULL) {
Guido van Rossum82598051997-03-05 00:20:32 +0000653 PySys_SetObject("ps1", v = PyString_FromString(">>> "));
654 Py_XDECREF(v);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000655 }
Guido van Rossum82598051997-03-05 00:20:32 +0000656 v = PySys_GetObject("ps2");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000657 if (v == NULL) {
Guido van Rossum82598051997-03-05 00:20:32 +0000658 PySys_SetObject("ps2", v = PyString_FromString("... "));
659 Py_XDECREF(v);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000660 }
661 for (;;) {
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000662 ret = PyRun_InteractiveOneFlags(fp, filename, flags);
Guido van Rossumaae0d321996-05-22 16:35:33 +0000663#ifdef Py_REF_DEBUG
Guido van Rossum6f9e4331995-03-29 16:57:48 +0000664 fprintf(stderr, "[%ld refs]\n", _Py_RefTotal);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000665#endif
666 if (ret == E_EOF)
667 return 0;
668 /*
669 if (ret == E_NOMEM)
670 return -1;
671 */
672 }
673}
674
675int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000676PyRun_InteractiveOne(FILE *fp, const char *filename)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000677{
Jeremy Hylton9f324e92001-03-01 22:59:14 +0000678 return PyRun_InteractiveOneFlags(fp, filename, NULL);
679}
680
Neil Schemenauerc24ea082002-03-22 23:53:36 +0000681/* compute parser flags based on compiler flags */
Neil Schemenauerc24ea082002-03-22 23:53:36 +0000682#define PARSER_FLAGS(flags) \
Guido van Rossum4b499dd32003-02-13 22:07:59 +0000683 (((flags) && (flags)->cf_flags & PyCF_DONT_IMPLY_DEDENT) ? \
684 PyPARSE_DONT_IMPLY_DEDENT : 0)
Neil Schemenauerc24ea082002-03-22 23:53:36 +0000685
Jeremy Hylton9f324e92001-03-01 22:59:14 +0000686int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000687PyRun_InteractiveOneFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
Jeremy Hylton9f324e92001-03-01 22:59:14 +0000688{
Guido van Rossum82598051997-03-05 00:20:32 +0000689 PyObject *m, *d, *v, *w;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000690 node *n;
Guido van Rossuma110aa61994-08-29 12:50:44 +0000691 perrdetail err;
Guido van Rossumddc3fb51997-11-25 20:58:13 +0000692 char *ps1 = "", *ps2 = "";
Tim Petersfe2127d2001-07-16 05:37:24 +0000693
Guido van Rossum82598051997-03-05 00:20:32 +0000694 v = PySys_GetObject("ps1");
Guido van Rossumddc3fb51997-11-25 20:58:13 +0000695 if (v != NULL) {
696 v = PyObject_Str(v);
697 if (v == NULL)
698 PyErr_Clear();
699 else if (PyString_Check(v))
700 ps1 = PyString_AsString(v);
701 }
Guido van Rossum82598051997-03-05 00:20:32 +0000702 w = PySys_GetObject("ps2");
Guido van Rossumddc3fb51997-11-25 20:58:13 +0000703 if (w != NULL) {
704 w = PyObject_Str(w);
705 if (w == NULL)
706 PyErr_Clear();
707 else if (PyString_Check(w))
708 ps2 = PyString_AsString(w);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000709 }
Tim Petersfe2127d2001-07-16 05:37:24 +0000710 n = PyParser_ParseFileFlags(fp, filename, &_PyParser_Grammar,
711 Py_single_input, ps1, ps2, &err,
Neil Schemenauerc24ea082002-03-22 23:53:36 +0000712 PARSER_FLAGS(flags));
Guido van Rossum82598051997-03-05 00:20:32 +0000713 Py_XDECREF(v);
714 Py_XDECREF(w);
Guido van Rossuma110aa61994-08-29 12:50:44 +0000715 if (n == NULL) {
716 if (err.error == E_EOF) {
717 if (err.text)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000718 PyMem_DEL(err.text);
Guido van Rossuma110aa61994-08-29 12:50:44 +0000719 return E_EOF;
720 }
721 err_input(&err);
Guido van Rossum82598051997-03-05 00:20:32 +0000722 PyErr_Print();
Guido van Rossuma110aa61994-08-29 12:50:44 +0000723 return err.error;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000724 }
Guido van Rossum82598051997-03-05 00:20:32 +0000725 m = PyImport_AddModule("__main__");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000726 if (m == NULL)
727 return -1;
Guido van Rossum82598051997-03-05 00:20:32 +0000728 d = PyModule_GetDict(m);
Jeremy Hylton9f324e92001-03-01 22:59:14 +0000729 v = run_node(n, filename, d, d, flags);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000730 if (v == NULL) {
Guido van Rossum82598051997-03-05 00:20:32 +0000731 PyErr_Print();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000732 return -1;
733 }
Guido van Rossum82598051997-03-05 00:20:32 +0000734 Py_DECREF(v);
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000735 if (Py_FlushLine())
736 PyErr_Clear();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000737 return 0;
738}
739
740int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000741PyRun_SimpleFile(FILE *fp, const char *filename)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000742{
Guido van Rossum0df002c2000-08-27 19:21:52 +0000743 return PyRun_SimpleFileEx(fp, filename, 0);
744}
745
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000746/* Check whether a file maybe a pyc file: Look at the extension,
747 the file type, and, if we may close it, at the first few bytes. */
748
749static int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000750maybe_pyc_file(FILE *fp, const char* filename, const char* ext, int closeit)
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000751{
752 if (strcmp(ext, ".pyc") == 0 || strcmp(ext, ".pyo") == 0)
753 return 1;
754
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000755 /* Only look into the file if we are allowed to close it, since
756 it then should also be seekable. */
757 if (closeit) {
758 /* Read only two bytes of the magic. If the file was opened in
759 text mode, the bytes 3 and 4 of the magic (\r\n) might not
760 be read as they are on disk. */
761 unsigned int halfmagic = PyImport_GetMagicNumber() & 0xFFFF;
762 unsigned char buf[2];
Tim Peters3e876562001-02-11 04:35:39 +0000763 /* Mess: In case of -x, the stream is NOT at its start now,
764 and ungetc() was used to push back the first newline,
Tim Peters6f5a4ef2001-02-17 22:02:34 +0000765 which makes the current stream position formally undefined,
766 and a x-platform nightmare.
767 Unfortunately, we have no direct way to know whether -x
768 was specified. So we use a terrible hack: if the current
769 stream position is not 0, we assume -x was specified, and
770 give up. Bug 132850 on SourceForge spells out the
771 hopelessness of trying anything else (fseek and ftell
772 don't work predictably x-platform for text-mode files).
Tim Peters3e876562001-02-11 04:35:39 +0000773 */
Tim Peters3e876562001-02-11 04:35:39 +0000774 int ispyc = 0;
Tim Peters6f5a4ef2001-02-17 22:02:34 +0000775 if (ftell(fp) == 0) {
776 if (fread(buf, 1, 2, fp) == 2 &&
Tim Petersd08e3822003-04-17 15:24:21 +0000777 ((unsigned int)buf[1]<<8 | buf[0]) == halfmagic)
Tim Peters6f5a4ef2001-02-17 22:02:34 +0000778 ispyc = 1;
779 rewind(fp);
780 }
Tim Peters3e876562001-02-11 04:35:39 +0000781 return ispyc;
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000782 }
783 return 0;
Tim Petersd08e3822003-04-17 15:24:21 +0000784}
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000785
Guido van Rossum0df002c2000-08-27 19:21:52 +0000786int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000787PyRun_SimpleFileEx(FILE *fp, const char *filename, int closeit)
Guido van Rossum0df002c2000-08-27 19:21:52 +0000788{
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000789 return PyRun_SimpleFileExFlags(fp, filename, closeit, NULL);
790}
791
792int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000793PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000794 PyCompilerFlags *flags)
795{
Guido van Rossum82598051997-03-05 00:20:32 +0000796 PyObject *m, *d, *v;
Martin v. Löwis95292d62002-12-11 14:04:59 +0000797 const char *ext;
Guido van Rossumfdef2711994-09-14 13:31:04 +0000798
Guido van Rossum82598051997-03-05 00:20:32 +0000799 m = PyImport_AddModule("__main__");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000800 if (m == NULL)
801 return -1;
Guido van Rossum82598051997-03-05 00:20:32 +0000802 d = PyModule_GetDict(m);
Fred Drake8ed02042002-10-17 21:24:58 +0000803 if (PyDict_GetItemString(d, "__file__") == NULL) {
804 PyObject *f = PyString_FromString(filename);
805 if (f == NULL)
806 return -1;
807 if (PyDict_SetItemString(d, "__file__", f) < 0) {
808 Py_DECREF(f);
809 return -1;
810 }
811 Py_DECREF(f);
812 }
Guido van Rossumfdef2711994-09-14 13:31:04 +0000813 ext = filename + strlen(filename) - 4;
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000814 if (maybe_pyc_file(fp, filename, ext, closeit)) {
Guido van Rossumfdef2711994-09-14 13:31:04 +0000815 /* Try to run a pyc file. First, re-open in binary */
Guido van Rossum0df002c2000-08-27 19:21:52 +0000816 if (closeit)
817 fclose(fp);
Fred Drake8ed02042002-10-17 21:24:58 +0000818 if ((fp = fopen(filename, "rb")) == NULL) {
Guido van Rossumfdef2711994-09-14 13:31:04 +0000819 fprintf(stderr, "python: Can't reopen .pyc file\n");
820 return -1;
821 }
Guido van Rossum2a7f58d1997-04-02 05:28:38 +0000822 /* Turn on optimization if a .pyo file is given */
823 if (strcmp(ext, ".pyo") == 0)
824 Py_OptimizeFlag = 1;
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000825 v = run_pyc_file(fp, filename, d, d, flags);
Guido van Rossumfdef2711994-09-14 13:31:04 +0000826 } else {
Tim Petersd08e3822003-04-17 15:24:21 +0000827 v = PyRun_FileExFlags(fp, filename, Py_file_input, d, d,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000828 closeit, flags);
Guido van Rossumfdef2711994-09-14 13:31:04 +0000829 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000830 if (v == NULL) {
Guido van Rossum82598051997-03-05 00:20:32 +0000831 PyErr_Print();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000832 return -1;
833 }
Guido van Rossum82598051997-03-05 00:20:32 +0000834 Py_DECREF(v);
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000835 if (Py_FlushLine())
836 PyErr_Clear();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000837 return 0;
838}
839
840int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000841PyRun_SimpleString(const char *command)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000842{
Guido van Rossum393661d2001-08-31 17:40:15 +0000843 return PyRun_SimpleStringFlags(command, NULL);
844}
845
846int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000847PyRun_SimpleStringFlags(const char *command, PyCompilerFlags *flags)
Guido van Rossum393661d2001-08-31 17:40:15 +0000848{
Guido van Rossum82598051997-03-05 00:20:32 +0000849 PyObject *m, *d, *v;
850 m = PyImport_AddModule("__main__");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000851 if (m == NULL)
852 return -1;
Guido van Rossum82598051997-03-05 00:20:32 +0000853 d = PyModule_GetDict(m);
Guido van Rossum393661d2001-08-31 17:40:15 +0000854 v = PyRun_StringFlags(command, Py_file_input, d, d, flags);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000855 if (v == NULL) {
Guido van Rossum82598051997-03-05 00:20:32 +0000856 PyErr_Print();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000857 return -1;
858 }
Guido van Rossum82598051997-03-05 00:20:32 +0000859 Py_DECREF(v);
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000860 if (Py_FlushLine())
861 PyErr_Clear();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000862 return 0;
863}
864
Barry Warsaw035574d1997-08-29 22:07:17 +0000865static int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000866parse_syntax_error(PyObject *err, PyObject **message, const char **filename,
867 int *lineno, int *offset, const char **text)
Barry Warsaw035574d1997-08-29 22:07:17 +0000868{
869 long hold;
870 PyObject *v;
871
872 /* old style errors */
873 if (PyTuple_Check(err))
Neal Norwitz78293352002-04-01 01:41:20 +0000874 return PyArg_ParseTuple(err, "O(ziiz)", message, filename,
875 lineno, offset, text);
Barry Warsaw035574d1997-08-29 22:07:17 +0000876
877 /* new style errors. `err' is an instance */
878
879 if (! (v = PyObject_GetAttrString(err, "msg")))
880 goto finally;
881 *message = v;
882
883 if (!(v = PyObject_GetAttrString(err, "filename")))
884 goto finally;
885 if (v == Py_None)
886 *filename = NULL;
887 else if (! (*filename = PyString_AsString(v)))
888 goto finally;
889
890 Py_DECREF(v);
891 if (!(v = PyObject_GetAttrString(err, "lineno")))
892 goto finally;
893 hold = PyInt_AsLong(v);
894 Py_DECREF(v);
895 v = NULL;
896 if (hold < 0 && PyErr_Occurred())
897 goto finally;
898 *lineno = (int)hold;
899
900 if (!(v = PyObject_GetAttrString(err, "offset")))
901 goto finally;
Jeremy Hylton9f1b9932001-02-28 07:07:43 +0000902 if (v == Py_None) {
903 *offset = -1;
904 Py_DECREF(v);
905 v = NULL;
906 } else {
907 hold = PyInt_AsLong(v);
908 Py_DECREF(v);
909 v = NULL;
910 if (hold < 0 && PyErr_Occurred())
911 goto finally;
912 *offset = (int)hold;
913 }
Barry Warsaw035574d1997-08-29 22:07:17 +0000914
915 if (!(v = PyObject_GetAttrString(err, "text")))
916 goto finally;
917 if (v == Py_None)
918 *text = NULL;
919 else if (! (*text = PyString_AsString(v)))
920 goto finally;
921 Py_DECREF(v);
922 return 1;
923
924finally:
925 Py_XDECREF(v);
926 return 0;
927}
928
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000929void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000930PyErr_Print(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000931{
Guido van Rossuma61691e1998-02-06 22:27:24 +0000932 PyErr_PrintEx(1);
933}
934
Jeremy Hylton9f1b9932001-02-28 07:07:43 +0000935static void
Martin v. Löwis95292d62002-12-11 14:04:59 +0000936print_error_text(PyObject *f, int offset, const char *text)
Jeremy Hylton9f1b9932001-02-28 07:07:43 +0000937{
938 char *nl;
939 if (offset >= 0) {
940 if (offset > 0 && offset == (int)strlen(text))
941 offset--;
942 for (;;) {
943 nl = strchr(text, '\n');
944 if (nl == NULL || nl-text >= offset)
945 break;
946 offset -= (nl+1-text);
947 text = nl+1;
948 }
949 while (*text == ' ' || *text == '\t') {
950 text++;
951 offset--;
952 }
953 }
954 PyFile_WriteString(" ", f);
955 PyFile_WriteString(text, f);
956 if (*text == '\0' || text[strlen(text)-1] != '\n')
957 PyFile_WriteString("\n", f);
958 if (offset == -1)
959 return;
960 PyFile_WriteString(" ", f);
961 offset--;
962 while (offset > 0) {
963 PyFile_WriteString(" ", f);
964 offset--;
965 }
966 PyFile_WriteString("^\n", f);
967}
968
Guido van Rossum66e8e862001-03-23 17:54:43 +0000969static void
970handle_system_exit(void)
Ka-Ping Yee26fabb02001-03-23 15:36:41 +0000971{
Guido van Rossum66e8e862001-03-23 17:54:43 +0000972 PyObject *exception, *value, *tb;
Tim Peterscf615b52003-04-19 18:47:02 +0000973 int exitcode = 0;
974
Guido van Rossum66e8e862001-03-23 17:54:43 +0000975 PyErr_Fetch(&exception, &value, &tb);
Ka-Ping Yee26fabb02001-03-23 15:36:41 +0000976 if (Py_FlushLine())
977 PyErr_Clear();
978 fflush(stdout);
979 if (value == NULL || value == Py_None)
Tim Peterscf615b52003-04-19 18:47:02 +0000980 goto done;
Ka-Ping Yee26fabb02001-03-23 15:36:41 +0000981 if (PyInstance_Check(value)) {
982 /* The error code should be in the `code' attribute. */
983 PyObject *code = PyObject_GetAttrString(value, "code");
984 if (code) {
985 Py_DECREF(value);
986 value = code;
987 if (value == Py_None)
Tim Peterscf615b52003-04-19 18:47:02 +0000988 goto done;
Ka-Ping Yee26fabb02001-03-23 15:36:41 +0000989 }
990 /* If we failed to dig out the 'code' attribute,
991 just let the else clause below print the error. */
992 }
993 if (PyInt_Check(value))
Tim Peterscf615b52003-04-19 18:47:02 +0000994 exitcode = (int)PyInt_AsLong(value);
Ka-Ping Yee26fabb02001-03-23 15:36:41 +0000995 else {
996 PyObject_Print(value, stderr, Py_PRINT_RAW);
997 PySys_WriteStderr("\n");
Tim Peterscf615b52003-04-19 18:47:02 +0000998 exitcode = 1;
Ka-Ping Yee26fabb02001-03-23 15:36:41 +0000999 }
Tim Peterscf615b52003-04-19 18:47:02 +00001000 done:
1001 /* Restore and clear the exception info, in order to properly decref
1002 * the exception, value, and traceback. If we just exit instead,
1003 * these leak, which confuses PYTHONDUMPREFS output, and may prevent
1004 * some finalizers from running.
1005 */
1006 PyErr_Restore(exception, value, tb);
1007 PyErr_Clear();
1008 Py_Exit(exitcode);
1009 /* NOTREACHED */
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001010}
1011
1012void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001013PyErr_PrintEx(int set_sys_last_vars)
Guido van Rossuma61691e1998-02-06 22:27:24 +00001014{
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001015 PyObject *exception, *v, *tb, *hook;
Guido van Rossum66e8e862001-03-23 17:54:43 +00001016
1017 if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
1018 handle_system_exit();
1019 }
Guido van Rossum82598051997-03-05 00:20:32 +00001020 PyErr_Fetch(&exception, &v, &tb);
Barry Warsaw035574d1997-08-29 22:07:17 +00001021 PyErr_NormalizeException(&exception, &v, &tb);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001022 if (exception == NULL)
Guido van Rossum296b4751997-05-23 00:19:20 +00001023 return;
Guido van Rossuma61691e1998-02-06 22:27:24 +00001024 if (set_sys_last_vars) {
1025 PySys_SetObject("last_type", exception);
1026 PySys_SetObject("last_value", v);
1027 PySys_SetObject("last_traceback", tb);
1028 }
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001029 hook = PySys_GetObject("excepthook");
1030 if (hook) {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001031 PyObject *args = PyTuple_Pack(3,
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001032 exception, v ? v : Py_None, tb ? tb : Py_None);
1033 PyObject *result = PyEval_CallObject(hook, args);
1034 if (result == NULL) {
1035 PyObject *exception2, *v2, *tb2;
Guido van Rossum66e8e862001-03-23 17:54:43 +00001036 if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
1037 handle_system_exit();
1038 }
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001039 PyErr_Fetch(&exception2, &v2, &tb2);
1040 PyErr_NormalizeException(&exception2, &v2, &tb2);
1041 if (Py_FlushLine())
1042 PyErr_Clear();
1043 fflush(stdout);
1044 PySys_WriteStderr("Error in sys.excepthook:\n");
1045 PyErr_Display(exception2, v2, tb2);
1046 PySys_WriteStderr("\nOriginal exception was:\n");
1047 PyErr_Display(exception, v, tb);
Jeremy Hylton07028582001-12-07 15:35:35 +00001048 Py_XDECREF(exception2);
1049 Py_XDECREF(v2);
1050 Py_XDECREF(tb2);
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001051 }
1052 Py_XDECREF(result);
1053 Py_XDECREF(args);
1054 } else {
1055 PySys_WriteStderr("sys.excepthook is missing\n");
1056 PyErr_Display(exception, v, tb);
1057 }
1058 Py_XDECREF(exception);
1059 Py_XDECREF(v);
1060 Py_XDECREF(tb);
1061}
1062
1063void PyErr_Display(PyObject *exception, PyObject *value, PyObject *tb)
1064{
1065 int err = 0;
1066 PyObject *v = value;
1067 PyObject *f = PySys_GetObject("stderr");
Guido van Rossum3165fe61992-09-25 21:59:05 +00001068 if (f == NULL)
1069 fprintf(stderr, "lost sys.stderr\n");
1070 else {
Guido van Rossum0829c751998-02-28 04:31:39 +00001071 if (Py_FlushLine())
1072 PyErr_Clear();
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001073 fflush(stdout);
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001074 if (tb && tb != Py_None)
1075 err = PyTraceBack_Print(tb, f);
Barry Warsaw36b8f941997-08-26 18:09:48 +00001076 if (err == 0 &&
Martin v. Löwiscfeb3b62002-03-03 21:30:27 +00001077 PyObject_HasAttrString(v, "print_file_and_line"))
Barry Warsaw36b8f941997-08-26 18:09:48 +00001078 {
Guido van Rossum82598051997-03-05 00:20:32 +00001079 PyObject *message;
Martin v. Löwis95292d62002-12-11 14:04:59 +00001080 const char *filename, *text;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001081 int lineno, offset;
Barry Warsaw035574d1997-08-29 22:07:17 +00001082 if (!parse_syntax_error(v, &message, &filename,
1083 &lineno, &offset, &text))
Guido van Rossum82598051997-03-05 00:20:32 +00001084 PyErr_Clear();
Guido van Rossuma110aa61994-08-29 12:50:44 +00001085 else {
1086 char buf[10];
Guido van Rossum82598051997-03-05 00:20:32 +00001087 PyFile_WriteString(" File \"", f);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001088 if (filename == NULL)
Guido van Rossum82598051997-03-05 00:20:32 +00001089 PyFile_WriteString("<string>", f);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001090 else
Guido van Rossum82598051997-03-05 00:20:32 +00001091 PyFile_WriteString(filename, f);
1092 PyFile_WriteString("\", line ", f);
Jeremy Hylton518ab1c2001-11-28 20:42:20 +00001093 PyOS_snprintf(buf, sizeof(buf), "%d", lineno);
Guido van Rossum82598051997-03-05 00:20:32 +00001094 PyFile_WriteString(buf, f);
1095 PyFile_WriteString("\n", f);
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001096 if (text != NULL)
1097 print_error_text(f, offset, text);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001098 v = message;
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001099 /* Can't be bothered to check all those
1100 PyFile_WriteString() calls */
1101 if (PyErr_Occurred())
1102 err = -1;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001103 }
1104 }
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001105 if (err) {
1106 /* Don't do anything else */
1107 }
1108 else if (PyClass_Check(exception)) {
Barry Warsaw2f5f6a21997-09-16 21:42:03 +00001109 PyClassObject* exc = (PyClassObject*)exception;
1110 PyObject* className = exc->cl_name;
1111 PyObject* moduleName =
1112 PyDict_GetItemString(exc->cl_dict, "__module__");
1113
1114 if (moduleName == NULL)
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001115 err = PyFile_WriteString("<unknown>", f);
Barry Warsaw2f5f6a21997-09-16 21:42:03 +00001116 else {
1117 char* modstr = PyString_AsString(moduleName);
Tim Petersd08e3822003-04-17 15:24:21 +00001118 if (modstr && strcmp(modstr, "exceptions"))
Barry Warsaw2f5f6a21997-09-16 21:42:03 +00001119 {
1120 err = PyFile_WriteString(modstr, f);
1121 err += PyFile_WriteString(".", f);
1122 }
1123 }
1124 if (err == 0) {
1125 if (className == NULL)
1126 err = PyFile_WriteString("<unknown>", f);
1127 else
1128 err = PyFile_WriteObject(className, f,
1129 Py_PRINT_RAW);
1130 }
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001131 }
1132 else
1133 err = PyFile_WriteObject(exception, f, Py_PRINT_RAW);
1134 if (err == 0) {
1135 if (v != NULL && v != Py_None) {
Barry Warsaw035574d1997-08-29 22:07:17 +00001136 PyObject *s = PyObject_Str(v);
1137 /* only print colon if the str() of the
1138 object is not the empty string
1139 */
Guido van Rossumd6bf45b1997-09-05 19:11:53 +00001140 if (s == NULL)
1141 err = -1;
1142 else if (!PyString_Check(s) ||
1143 PyString_GET_SIZE(s) != 0)
Barry Warsaw035574d1997-08-29 22:07:17 +00001144 err = PyFile_WriteString(": ", f);
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001145 if (err == 0)
Guido van Rossumd6bf45b1997-09-05 19:11:53 +00001146 err = PyFile_WriteObject(s, f, Py_PRINT_RAW);
1147 Py_XDECREF(s);
Guido van Rossum262e1241995-02-07 15:30:45 +00001148 }
Guido van Rossum262e1241995-02-07 15:30:45 +00001149 }
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001150 if (err == 0)
1151 err = PyFile_WriteString("\n", f);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001152 }
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001153 /* If an error happened here, don't show it.
1154 XXX This is wrong, but too many callers rely on this behavior. */
1155 if (err != 0)
1156 PyErr_Clear();
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001157}
1158
Guido van Rossum82598051997-03-05 00:20:32 +00001159PyObject *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001160PyRun_String(const char *str, int start, PyObject *globals, PyObject *locals)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001161{
Guido van Rossum82598051997-03-05 00:20:32 +00001162 return run_err_node(PyParser_SimpleParseString(str, start),
Jeremy Hylton9f324e92001-03-01 22:59:14 +00001163 "<string>", globals, locals, NULL);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001164}
1165
Guido van Rossum82598051997-03-05 00:20:32 +00001166PyObject *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001167PyRun_File(FILE *fp, const char *filename, int start, PyObject *globals,
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001168 PyObject *locals)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001169{
Tim Peterse8682112000-08-27 20:18:17 +00001170 return PyRun_FileEx(fp, filename, start, globals, locals, 0);
Guido van Rossum0df002c2000-08-27 19:21:52 +00001171}
1172
1173PyObject *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001174PyRun_FileEx(FILE *fp, const char *filename, int start, PyObject *globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001175 PyObject *locals, int closeit)
Guido van Rossum0df002c2000-08-27 19:21:52 +00001176{
1177 node *n = PyParser_SimpleParseFile(fp, filename, start);
1178 if (closeit)
1179 fclose(fp);
Jeremy Hylton9f324e92001-03-01 22:59:14 +00001180 return run_err_node(n, filename, globals, locals, NULL);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001181}
1182
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001183PyObject *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001184PyRun_StringFlags(const char *str, int start, PyObject *globals, PyObject *locals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001185 PyCompilerFlags *flags)
1186{
Guido van Rossuma1b3a472001-07-16 16:51:33 +00001187 return run_err_node(PyParser_SimpleParseStringFlags(
Neil Schemenauerc24ea082002-03-22 23:53:36 +00001188 str, start, PARSER_FLAGS(flags)),
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001189 "<string>", globals, locals, flags);
1190}
1191
1192PyObject *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001193PyRun_FileFlags(FILE *fp, const char *filename, int start, PyObject *globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001194 PyObject *locals, PyCompilerFlags *flags)
1195{
1196 return PyRun_FileExFlags(fp, filename, start, globals, locals, 0,
Tim Petersd08e3822003-04-17 15:24:21 +00001197 flags);
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001198}
1199
1200PyObject *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001201PyRun_FileExFlags(FILE *fp, const char *filename, int start, PyObject *globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001202 PyObject *locals, int closeit, PyCompilerFlags *flags)
1203{
Tim Petersfe2127d2001-07-16 05:37:24 +00001204 node *n = PyParser_SimpleParseFileFlags(fp, filename, start,
Neil Schemenauerc24ea082002-03-22 23:53:36 +00001205 PARSER_FLAGS(flags));
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001206 if (closeit)
1207 fclose(fp);
1208 return run_err_node(n, filename, globals, locals, flags);
1209}
1210
Guido van Rossum82598051997-03-05 00:20:32 +00001211static PyObject *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001212run_err_node(node *n, const char *filename, PyObject *globals, PyObject *locals,
Jeremy Hylton9f324e92001-03-01 22:59:14 +00001213 PyCompilerFlags *flags)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001214{
Guido van Rossuma110aa61994-08-29 12:50:44 +00001215 if (n == NULL)
1216 return NULL;
Jeremy Hylton9f324e92001-03-01 22:59:14 +00001217 return run_node(n, filename, globals, locals, flags);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001218}
1219
Guido van Rossum82598051997-03-05 00:20:32 +00001220static PyObject *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001221run_node(node *n, const char *filename, PyObject *globals, PyObject *locals,
Jeremy Hylton9f324e92001-03-01 22:59:14 +00001222 PyCompilerFlags *flags)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001223{
Guido van Rossum82598051997-03-05 00:20:32 +00001224 PyCodeObject *co;
1225 PyObject *v;
Jeremy Hylton9f324e92001-03-01 22:59:14 +00001226 co = PyNode_CompileFlags(n, filename, flags);
Guido van Rossum82598051997-03-05 00:20:32 +00001227 PyNode_Free(n);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001228 if (co == NULL)
1229 return NULL;
Guido van Rossum82598051997-03-05 00:20:32 +00001230 v = PyEval_EvalCode(co, globals, locals);
1231 Py_DECREF(co);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001232 return v;
1233}
1234
Guido van Rossum82598051997-03-05 00:20:32 +00001235static PyObject *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001236run_pyc_file(FILE *fp, const char *filename, PyObject *globals, PyObject *locals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001237 PyCompilerFlags *flags)
Guido van Rossumfdef2711994-09-14 13:31:04 +00001238{
Guido van Rossum82598051997-03-05 00:20:32 +00001239 PyCodeObject *co;
1240 PyObject *v;
Guido van Rossumfdef2711994-09-14 13:31:04 +00001241 long magic;
Fred Drakee8de31c2000-08-31 05:38:39 +00001242 long PyImport_GetMagicNumber(void);
Guido van Rossumfdef2711994-09-14 13:31:04 +00001243
Guido van Rossum82598051997-03-05 00:20:32 +00001244 magic = PyMarshal_ReadLongFromFile(fp);
1245 if (magic != PyImport_GetMagicNumber()) {
1246 PyErr_SetString(PyExc_RuntimeError,
Guido van Rossumfdef2711994-09-14 13:31:04 +00001247 "Bad magic number in .pyc file");
1248 return NULL;
1249 }
Guido van Rossum82598051997-03-05 00:20:32 +00001250 (void) PyMarshal_ReadLongFromFile(fp);
Tim Petersd9b9ac82001-01-28 00:27:39 +00001251 v = PyMarshal_ReadLastObjectFromFile(fp);
Guido van Rossumfdef2711994-09-14 13:31:04 +00001252 fclose(fp);
Guido van Rossum82598051997-03-05 00:20:32 +00001253 if (v == NULL || !PyCode_Check(v)) {
1254 Py_XDECREF(v);
1255 PyErr_SetString(PyExc_RuntimeError,
Guido van Rossumfdef2711994-09-14 13:31:04 +00001256 "Bad code object in .pyc file");
1257 return NULL;
1258 }
Guido van Rossum82598051997-03-05 00:20:32 +00001259 co = (PyCodeObject *)v;
1260 v = PyEval_EvalCode(co, globals, locals);
Jeremy Hyltonb857ba22001-08-10 21:41:33 +00001261 if (v && flags)
1262 flags->cf_flags |= (co->co_flags & PyCF_MASK);
Guido van Rossum82598051997-03-05 00:20:32 +00001263 Py_DECREF(co);
Guido van Rossumfdef2711994-09-14 13:31:04 +00001264 return v;
1265}
1266
Guido van Rossum82598051997-03-05 00:20:32 +00001267PyObject *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001268Py_CompileString(const char *str, const char *filename, int start)
Guido van Rossum5b722181993-03-30 17:46:03 +00001269{
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001270 return Py_CompileStringFlags(str, filename, start, NULL);
1271}
1272
1273PyObject *
Tim Petersd08e3822003-04-17 15:24:21 +00001274Py_CompileStringFlags(const char *str, const char *filename, int start,
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001275 PyCompilerFlags *flags)
1276{
Guido van Rossum5b722181993-03-30 17:46:03 +00001277 node *n;
Guido van Rossum82598051997-03-05 00:20:32 +00001278 PyCodeObject *co;
Thomas Heller6b17abf2002-07-09 09:23:27 +00001279
1280 n = PyParser_SimpleParseStringFlagsFilename(str, filename, start,
1281 PARSER_FLAGS(flags));
Guido van Rossuma110aa61994-08-29 12:50:44 +00001282 if (n == NULL)
Guido van Rossum5b722181993-03-30 17:46:03 +00001283 return NULL;
Jeremy Hylton673a4fd2001-03-26 19:53:38 +00001284 co = PyNode_CompileFlags(n, filename, flags);
Guido van Rossum82598051997-03-05 00:20:32 +00001285 PyNode_Free(n);
1286 return (PyObject *)co;
Guido van Rossum5b722181993-03-30 17:46:03 +00001287}
1288
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001289struct symtable *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001290Py_SymtableString(const char *str, const char *filename, int start)
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001291{
1292 node *n;
1293 struct symtable *st;
Thomas Heller6b17abf2002-07-09 09:23:27 +00001294 n = PyParser_SimpleParseStringFlagsFilename(str, filename,
1295 start, 0);
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001296 if (n == NULL)
1297 return NULL;
1298 st = PyNode_CompileSymtable(n, filename);
1299 PyNode_Free(n);
1300 return st;
1301}
1302
Guido van Rossuma110aa61994-08-29 12:50:44 +00001303/* Simplified interface to parsefile -- return node or set exception */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001304
Guido van Rossuma110aa61994-08-29 12:50:44 +00001305node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001306PyParser_SimpleParseFileFlags(FILE *fp, const char *filename, int start, int flags)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001307{
Guido van Rossuma110aa61994-08-29 12:50:44 +00001308 node *n;
1309 perrdetail err;
Tim Petersfe2127d2001-07-16 05:37:24 +00001310 n = PyParser_ParseFileFlags(fp, filename, &_PyParser_Grammar, start,
1311 (char *)0, (char *)0, &err, flags);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001312 if (n == NULL)
1313 err_input(&err);
1314 return n;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001315}
1316
Tim Petersfe2127d2001-07-16 05:37:24 +00001317node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001318PyParser_SimpleParseFile(FILE *fp, const char *filename, int start)
Tim Petersfe2127d2001-07-16 05:37:24 +00001319{
1320 return PyParser_SimpleParseFileFlags(fp, filename, start, 0);
1321}
1322
Guido van Rossuma110aa61994-08-29 12:50:44 +00001323/* Simplified interface to parsestring -- return node or set exception */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001324
Guido van Rossuma110aa61994-08-29 12:50:44 +00001325node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001326PyParser_SimpleParseStringFlags(const char *str, int start, int flags)
Tim Petersfe2127d2001-07-16 05:37:24 +00001327{
1328 node *n;
1329 perrdetail err;
1330 n = PyParser_ParseStringFlags(str, &_PyParser_Grammar, start, &err,
1331 flags);
1332 if (n == NULL)
1333 err_input(&err);
1334 return n;
1335}
1336
1337node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001338PyParser_SimpleParseString(const char *str, int start)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001339{
Tim Petersfe2127d2001-07-16 05:37:24 +00001340 return PyParser_SimpleParseStringFlags(str, start, 0);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001341}
1342
Thomas Heller6b17abf2002-07-09 09:23:27 +00001343node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001344PyParser_SimpleParseStringFlagsFilename(const char *str, const char *filename,
Thomas Heller6b17abf2002-07-09 09:23:27 +00001345 int start, int flags)
1346{
1347 node *n;
1348 perrdetail err;
1349
Tim Petersd08e3822003-04-17 15:24:21 +00001350 n = PyParser_ParseStringFlagsFilename(str, filename,
Thomas Heller6b17abf2002-07-09 09:23:27 +00001351 &_PyParser_Grammar,
1352 start, &err, flags);
1353 if (n == NULL)
1354 err_input(&err);
1355 return n;
1356}
1357
1358node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001359PyParser_SimpleParseStringFilename(const char *str, const char *filename, int start)
Thomas Heller6b17abf2002-07-09 09:23:27 +00001360{
1361 return PyParser_SimpleParseStringFlagsFilename(str, filename,
1362 start, 0);
1363}
1364
Guido van Rossum66ebd912003-04-17 16:02:26 +00001365/* May want to move a more generalized form of this to parsetok.c or
1366 even parser modules. */
1367
1368void
1369PyParser_SetError(perrdetail *err)
1370{
1371 err_input(err);
1372}
1373
Guido van Rossuma110aa61994-08-29 12:50:44 +00001374/* Set the error appropriate to the given input error code (see errcode.h) */
1375
1376static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001377err_input(perrdetail *err)
Guido van Rossuma110aa61994-08-29 12:50:44 +00001378{
Fred Drake85f36392000-07-11 17:53:00 +00001379 PyObject *v, *w, *errtype;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001380 PyObject* u = NULL;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001381 char *msg = NULL;
Fred Drake85f36392000-07-11 17:53:00 +00001382 errtype = PyExc_SyntaxError;
Guido van Rossum82598051997-03-05 00:20:32 +00001383 v = Py_BuildValue("(ziiz)", err->filename,
Guido van Rossuma110aa61994-08-29 12:50:44 +00001384 err->lineno, err->offset, err->text);
1385 if (err->text != NULL) {
Guido van Rossumb18618d2000-05-03 23:44:39 +00001386 PyMem_DEL(err->text);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001387 err->text = NULL;
1388 }
1389 switch (err->error) {
1390 case E_SYNTAX:
Fred Drake85f36392000-07-11 17:53:00 +00001391 errtype = PyExc_IndentationError;
1392 if (err->expected == INDENT)
1393 msg = "expected an indented block";
1394 else if (err->token == INDENT)
1395 msg = "unexpected indent";
1396 else if (err->token == DEDENT)
1397 msg = "unexpected unindent";
1398 else {
1399 errtype = PyExc_SyntaxError;
1400 msg = "invalid syntax";
1401 }
Guido van Rossuma110aa61994-08-29 12:50:44 +00001402 break;
1403 case E_TOKEN:
1404 msg = "invalid token";
Guido van Rossuma110aa61994-08-29 12:50:44 +00001405 break;
Skip Montanaro118ec702002-08-15 01:20:16 +00001406 case E_EOFS:
1407 msg = "EOF while scanning triple-quoted string";
1408 break;
1409 case E_EOLS:
1410 msg = "EOL while scanning single-quoted string";
1411 break;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001412 case E_INTR:
Guido van Rossum82598051997-03-05 00:20:32 +00001413 PyErr_SetNone(PyExc_KeyboardInterrupt);
Barry Warsawc80baa31999-01-27 16:39:40 +00001414 Py_XDECREF(v);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001415 return;
1416 case E_NOMEM:
Guido van Rossum82598051997-03-05 00:20:32 +00001417 PyErr_NoMemory();
Barry Warsawc80baa31999-01-27 16:39:40 +00001418 Py_XDECREF(v);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001419 return;
1420 case E_EOF:
1421 msg = "unexpected EOF while parsing";
1422 break;
Fred Drake85f36392000-07-11 17:53:00 +00001423 case E_TABSPACE:
1424 errtype = PyExc_TabError;
Guido van Rossum560e8ad1998-04-10 19:43:42 +00001425 msg = "inconsistent use of tabs and spaces in indentation";
1426 break;
Jeremy Hylton94988062000-06-20 19:10:44 +00001427 case E_OVERFLOW:
1428 msg = "expression too long";
1429 break;
Fred Drake85f36392000-07-11 17:53:00 +00001430 case E_DEDENT:
1431 errtype = PyExc_IndentationError;
1432 msg = "unindent does not match any outer indentation level";
1433 break;
1434 case E_TOODEEP:
1435 errtype = PyExc_IndentationError;
1436 msg = "too many levels of indentation";
1437 break;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001438 case E_DECODE: { /* XXX */
1439 PyThreadState* tstate = PyThreadState_Get();
1440 PyObject* value = tstate->curexc_value;
1441 if (value != NULL) {
1442 u = PyObject_Repr(value);
1443 if (u != NULL) {
1444 msg = PyString_AsString(u);
1445 break;
1446 }
1447 }
1448 }
Guido van Rossuma110aa61994-08-29 12:50:44 +00001449 default:
1450 fprintf(stderr, "error=%d\n", err->error);
1451 msg = "unknown parsing error";
1452 break;
1453 }
Guido van Rossum82598051997-03-05 00:20:32 +00001454 w = Py_BuildValue("(sO)", msg, v);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001455 Py_XDECREF(u);
Guido van Rossum41318302001-03-23 04:01:07 +00001456 Py_XDECREF(v);
Fred Drake85f36392000-07-11 17:53:00 +00001457 PyErr_SetObject(errtype, w);
Guido van Rossum82598051997-03-05 00:20:32 +00001458 Py_XDECREF(w);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001459}
1460
1461/* Print fatal error message and abort */
1462
1463void
Tim Peters7c321a82002-07-09 02:57:01 +00001464Py_FatalError(const char *msg)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001465{
Guido van Rossum83dd6c31994-09-29 09:38:33 +00001466 fprintf(stderr, "Fatal Python error: %s\n", msg);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001467#ifdef MS_WINDOWS
Guido van Rossum23c94461997-05-22 20:21:30 +00001468 OutputDebugString("Fatal Python error: ");
Guido van Rossuma44823b1995-03-14 15:01:17 +00001469 OutputDebugString(msg);
1470 OutputDebugString("\n");
Guido van Rossum0ba35361998-08-13 13:33:16 +00001471#ifdef _DEBUG
1472 DebugBreak();
Guido van Rossuma44823b1995-03-14 15:01:17 +00001473#endif
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001474#endif /* MS_WINDOWS */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001475 abort();
1476}
1477
1478/* Clean up and exit */
1479
Guido van Rossuma110aa61994-08-29 12:50:44 +00001480#ifdef WITH_THREAD
Guido van Rossum49b56061998-10-01 20:42:43 +00001481#include "pythread.h"
Guido van Rossum82598051997-03-05 00:20:32 +00001482int _PyThread_Started = 0; /* Set by threadmodule.c and maybe others */
Guido van Rossumf9f2e821992-08-17 08:59:08 +00001483#endif
1484
Guido van Rossum2dcfc961998-10-01 16:01:57 +00001485#define NEXITFUNCS 32
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001486static void (*exitfuncs[NEXITFUNCS])(void);
Guido van Rossum1662dd51994-09-07 14:38:28 +00001487static int nexitfuncs = 0;
1488
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001489int Py_AtExit(void (*func)(void))
Guido van Rossum1662dd51994-09-07 14:38:28 +00001490{
1491 if (nexitfuncs >= NEXITFUNCS)
1492 return -1;
1493 exitfuncs[nexitfuncs++] = func;
1494 return 0;
1495}
1496
Guido van Rossumcc283f51997-08-05 02:22:03 +00001497static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001498call_sys_exitfunc(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001499{
Guido van Rossum82598051997-03-05 00:20:32 +00001500 PyObject *exitfunc = PySys_GetObject("exitfunc");
Guido van Rossum59bff391992-09-03 20:28:00 +00001501
1502 if (exitfunc) {
Fred Drake6a12d8d2001-03-23 17:34:02 +00001503 PyObject *res;
Guido van Rossum82598051997-03-05 00:20:32 +00001504 Py_INCREF(exitfunc);
1505 PySys_SetObject("exitfunc", (PyObject *)NULL);
1506 res = PyEval_CallObject(exitfunc, (PyObject *)NULL);
Guido van Rossum59bff391992-09-03 20:28:00 +00001507 if (res == NULL) {
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001508 if (!PyErr_ExceptionMatches(PyExc_SystemExit)) {
1509 PySys_WriteStderr("Error in sys.exitfunc:\n");
1510 }
Guido van Rossum82598051997-03-05 00:20:32 +00001511 PyErr_Print();
Guido van Rossum59bff391992-09-03 20:28:00 +00001512 }
Guido van Rossum82598051997-03-05 00:20:32 +00001513 Py_DECREF(exitfunc);
Guido van Rossum59bff391992-09-03 20:28:00 +00001514 }
1515
Guido van Rossum0829c751998-02-28 04:31:39 +00001516 if (Py_FlushLine())
1517 PyErr_Clear();
Guido van Rossumcc283f51997-08-05 02:22:03 +00001518}
Guido van Rossum1662dd51994-09-07 14:38:28 +00001519
Guido van Rossumcc283f51997-08-05 02:22:03 +00001520static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001521call_ll_exitfuncs(void)
Guido van Rossumcc283f51997-08-05 02:22:03 +00001522{
Guido van Rossum1662dd51994-09-07 14:38:28 +00001523 while (nexitfuncs > 0)
1524 (*exitfuncs[--nexitfuncs])();
Guido van Rossum25ce5661997-08-02 03:10:38 +00001525
1526 fflush(stdout);
1527 fflush(stderr);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001528}
1529
1530void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001531Py_Exit(int sts)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001532{
Guido van Rossumcc283f51997-08-05 02:22:03 +00001533 Py_Finalize();
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001534
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001535 exit(sts);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001536}
1537
Guido van Rossumf1dc5661993-07-05 10:31:29 +00001538static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001539initsigs(void)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001540{
Guido van Rossuma110aa61994-08-29 12:50:44 +00001541#ifdef HAVE_SIGNAL_H
1542#ifdef SIGPIPE
1543 signal(SIGPIPE, SIG_IGN);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001544#endif
Guido van Rossum70d893a2001-08-16 08:21:42 +00001545#ifdef SIGXFZ
1546 signal(SIGXFZ, SIG_IGN);
1547#endif
Jeremy Hylton1b0bf9b2002-04-23 20:31:01 +00001548#ifdef SIGXFSZ
1549 signal(SIGXFSZ, SIG_IGN);
1550#endif
Guido van Rossuma110aa61994-08-29 12:50:44 +00001551#endif /* HAVE_SIGNAL_H */
Guido van Rossum82598051997-03-05 00:20:32 +00001552 PyOS_InitInterrupts(); /* May imply initsignal() */
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001553}
1554
Guido van Rossum7433b121997-02-14 19:45:36 +00001555
1556/*
1557 * The file descriptor fd is considered ``interactive'' if either
1558 * a) isatty(fd) is TRUE, or
1559 * b) the -i flag was given, and the filename associated with
1560 * the descriptor is NULL or "<stdin>" or "???".
1561 */
1562int
Martin v. Löwis95292d62002-12-11 14:04:59 +00001563Py_FdIsInteractive(FILE *fp, const char *filename)
Guido van Rossum7433b121997-02-14 19:45:36 +00001564{
1565 if (isatty((int)fileno(fp)))
1566 return 1;
1567 if (!Py_InteractiveFlag)
1568 return 0;
1569 return (filename == NULL) ||
1570 (strcmp(filename, "<stdin>") == 0) ||
1571 (strcmp(filename, "???") == 0);
1572}
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001573
1574
Tim Petersd08e3822003-04-17 15:24:21 +00001575#if defined(USE_STACKCHECK)
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001576#if defined(WIN32) && defined(_MSC_VER)
1577
1578/* Stack checking for Microsoft C */
1579
1580#include <malloc.h>
1581#include <excpt.h>
1582
Fred Drakee8de31c2000-08-31 05:38:39 +00001583/*
1584 * Return non-zero when we run out of memory on the stack; zero otherwise.
1585 */
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001586int
Fred Drake399739f2000-08-31 05:52:44 +00001587PyOS_CheckStack(void)
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001588{
1589 __try {
Tim Peters92e4dd82002-10-05 01:47:34 +00001590 /* alloca throws a stack overflow exception if there's
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001591 not enough space left on the stack */
Tim Peters92e4dd82002-10-05 01:47:34 +00001592 alloca(PYOS_STACK_MARGIN * sizeof(void*));
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001593 return 0;
1594 } __except (EXCEPTION_EXECUTE_HANDLER) {
1595 /* just ignore all errors */
1596 }
1597 return 1;
1598}
1599
1600#endif /* WIN32 && _MSC_VER */
1601
1602/* Alternate implementations can be added here... */
1603
1604#endif /* USE_STACKCHECK */
Guido van Rossum6f256182000-09-16 16:32:19 +00001605
1606
1607/* Wrappers around sigaction() or signal(). */
1608
1609PyOS_sighandler_t
1610PyOS_getsig(int sig)
1611{
1612#ifdef HAVE_SIGACTION
1613 struct sigaction context;
Barry Warsawafeb2a42001-11-13 23:08:26 +00001614 /* Initialize context.sa_handler to SIG_ERR which makes about as
1615 * much sense as anything else. It should get overwritten if
1616 * sigaction actually succeeds and otherwise we avoid an
1617 * uninitialized memory read.
1618 */
1619 context.sa_handler = SIG_ERR;
Guido van Rossum6f256182000-09-16 16:32:19 +00001620 sigaction(sig, NULL, &context);
1621 return context.sa_handler;
1622#else
1623 PyOS_sighandler_t handler;
1624 handler = signal(sig, SIG_IGN);
1625 signal(sig, handler);
1626 return handler;
1627#endif
1628}
1629
1630PyOS_sighandler_t
1631PyOS_setsig(int sig, PyOS_sighandler_t handler)
1632{
1633#ifdef HAVE_SIGACTION
1634 struct sigaction context;
1635 PyOS_sighandler_t oldhandler;
Barry Warsawafeb2a42001-11-13 23:08:26 +00001636 /* Initialize context.sa_handler to SIG_ERR which makes about as
1637 * much sense as anything else. It should get overwritten if
1638 * sigaction actually succeeds and otherwise we avoid an
1639 * uninitialized memory read.
1640 */
1641 context.sa_handler = SIG_ERR;
Guido van Rossum6f256182000-09-16 16:32:19 +00001642 sigaction(sig, NULL, &context);
1643 oldhandler = context.sa_handler;
1644 context.sa_handler = handler;
1645 sigaction(sig, &context, NULL);
1646 return oldhandler;
1647#else
1648 return signal(sig, handler);
1649#endif
1650}