blob: c09a3ff901522d4360d45c50ccc91495f2457d6c [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
Jack Jansencbf630f2000-07-11 21:59:16 +000030#ifdef macintosh
31#include "macglue.h"
32#endif
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000033extern char *Py_GetPath(void);
Guido van Rossum1984f1e1992-08-04 12:41:02 +000034
Guido van Rossum82598051997-03-05 00:20:32 +000035extern grammar _PyParser_Grammar; /* From graminit.c */
Guido van Rossum1984f1e1992-08-04 12:41:02 +000036
Guido van Rossumb73cc041993-11-01 16:28:59 +000037/* Forward */
Tim Petersdbd9ba62000-07-09 03:09:57 +000038static void initmain(void);
39static void initsite(void);
Martin v. Löwis95292d62002-12-11 14:04:59 +000040static PyObject *run_err_node(node *, const char *, PyObject *, PyObject *,
Jeremy Hylton9f324e92001-03-01 22:59:14 +000041 PyCompilerFlags *);
Martin v. Löwis95292d62002-12-11 14:04:59 +000042static PyObject *run_node(node *, const char *, PyObject *, PyObject *,
Jeremy Hylton9f324e92001-03-01 22:59:14 +000043 PyCompilerFlags *);
Martin v. Löwis95292d62002-12-11 14:04:59 +000044static PyObject *run_pyc_file(FILE *, const char *, PyObject *, PyObject *,
Jeremy Hyltonbc320242001-03-22 02:47:58 +000045 PyCompilerFlags *);
Tim Petersdbd9ba62000-07-09 03:09:57 +000046static void err_input(perrdetail *);
47static void initsigs(void);
48static void call_sys_exitfunc(void);
49static void call_ll_exitfuncs(void);
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000050extern void _PyUnicode_Init(void);
51extern void _PyUnicode_Fini(void);
Guido van Rossumc94044c2000-03-10 23:03:54 +000052
Mark Hammond8d98d2c2003-04-19 15:41:53 +000053#ifdef WITH_THREAD
54extern void _PyGILState_Init(PyInterpreterState *, PyThreadState *);
55extern void _PyGILState_Fini(void);
56#endif /* WITH_THREAD */
57
Guido van Rossum82598051997-03-05 00:20:32 +000058int Py_DebugFlag; /* Needed by parser.c */
59int Py_VerboseFlag; /* Needed by import.c */
Guido van Rossum7433b121997-02-14 19:45:36 +000060int Py_InteractiveFlag; /* Needed by Py_FdIsInteractive() below */
Guido van Rossumdcc0c131997-08-29 22:32:42 +000061int Py_NoSiteFlag; /* Suppress 'import site' */
Barry Warsaw3ce09642000-05-02 19:18:59 +000062int Py_UseClassExceptionsFlag = 1; /* Needed by bltinmodule.c: deprecated */
Guido van Rossuma61691e1998-02-06 22:27:24 +000063int Py_FrozenFlag; /* Needed by getpath.c */
Guido van Rossumb16d1972000-05-01 17:55:15 +000064int Py_UnicodeFlag = 0; /* Needed by compile.c */
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +000065int Py_IgnoreEnvironmentFlag; /* e.g. PYTHONPATH, PYTHONHOME */
Tim Peters3caca232001-12-06 06:23:26 +000066/* _XXX Py_QnewFlag should go away in 2.3. It's true iff -Qnew is passed,
67 on the command line, and is used in 2.2 by ceval.c to make all "/" divisions
68 true divisions (which they will be in 2.3). */
69int _Py_QnewFlag = 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +000070
Mark Hammonda43fd0c2003-02-19 00:33:33 +000071/* Reference to 'warnings' module, to avoid importing it
Mark Hammondedd07732003-07-15 23:03:55 +000072 on the fly when the import lock may be held. See 683658/771097
Mark Hammonda43fd0c2003-02-19 00:33:33 +000073*/
Mark Hammondedd07732003-07-15 23:03:55 +000074static PyObject *warnings_module = NULL;
75
76/* Returns a borrowed reference to the 'warnings' module, or NULL.
77 If the module is returned, it is guaranteed to have been obtained
78 without acquiring the import lock
79*/
Martin v. Löwisa2c17c52003-08-09 09:47:11 +000080PyObject *PyModule_GetWarningsModule(void)
Mark Hammondedd07732003-07-15 23:03:55 +000081{
82 PyObject *typ, *val, *tb;
83 PyObject *all_modules;
84 /* If we managed to get the module at init time, just use it */
85 if (warnings_module)
86 return warnings_module;
87 /* If it wasn't available at init time, it may be available
88 now in sys.modules (common scenario is frozen apps: import
89 at init time fails, but the frozen init code sets up sys.path
90 correctly, then does an implicit import of warnings for us
91 */
92 /* Save and restore any exceptions */
93 PyErr_Fetch(&typ, &val, &tb);
94
Mark Hammond5f4e8ca2003-07-16 01:54:38 +000095 all_modules = PySys_GetObject("modules");
Mark Hammondedd07732003-07-15 23:03:55 +000096 if (all_modules) {
97 warnings_module = PyDict_GetItemString(all_modules, "warnings");
98 /* We keep a ref in the global */
99 Py_XINCREF(warnings_module);
100 }
101 PyErr_Restore(typ, val, tb);
102 return warnings_module;
103}
Mark Hammonda43fd0c2003-02-19 00:33:33 +0000104
Guido van Rossum25ce5661997-08-02 03:10:38 +0000105static int initialized = 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000106
Thomas Wouters7e474022000-07-16 12:04:32 +0000107/* API to access the initialized flag -- useful for esoteric use */
Guido van Rossume3c0d5e1997-08-22 04:20:13 +0000108
109int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000110Py_IsInitialized(void)
Guido van Rossume3c0d5e1997-08-22 04:20:13 +0000111{
112 return initialized;
113}
114
Guido van Rossum25ce5661997-08-02 03:10:38 +0000115/* Global initializations. Can be undone by Py_Finalize(). Don't
116 call this twice without an intervening Py_Finalize() call. When
117 initializations fail, a fatal error is issued and the function does
118 not return. On return, the first thread and interpreter state have
119 been created.
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000120
Guido van Rossum25ce5661997-08-02 03:10:38 +0000121 Locking: you must hold the interpreter lock while calling this.
122 (If the lock has not yet been initialized, that's equivalent to
123 having the lock, but you cannot use multiple threads.)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000124
Guido van Rossum25ce5661997-08-02 03:10:38 +0000125*/
Guido van Rossuma027efa1997-05-05 20:56:21 +0000126
Guido van Rossum9abaf4d2001-10-12 22:17:56 +0000127static int
128add_flag(int flag, const char *envs)
129{
130 int env = atoi(envs);
131 if (flag < env)
132 flag = env;
133 if (flag < 1)
134 flag = 1;
135 return flag;
136}
137
Guido van Rossuma027efa1997-05-05 20:56:21 +0000138void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000139Py_Initialize(void)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000140{
Guido van Rossuma027efa1997-05-05 20:56:21 +0000141 PyInterpreterState *interp;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000142 PyThreadState *tstate;
143 PyObject *bimod, *sysmod;
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000144 char *p;
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000145#if defined(Py_USING_UNICODE) && defined(HAVE_LANGINFO_H) && defined(CODESET)
146 char *codeset;
147 char *saved_locale;
148 PyObject *sys_stream, *sys_isatty;
149#endif
Guido van Rossum70d893a2001-08-16 08:21:42 +0000150 extern void _Py_ReadyTypes(void);
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000151
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000152 if (initialized)
Guido van Rossumaa615051997-08-20 22:40:18 +0000153 return;
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000154 initialized = 1;
Tim Petersd08e3822003-04-17 15:24:21 +0000155
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000156 if ((p = Py_GETENV("PYTHONDEBUG")) && *p != '\0')
Guido van Rossum9abaf4d2001-10-12 22:17:56 +0000157 Py_DebugFlag = add_flag(Py_DebugFlag, p);
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000158 if ((p = Py_GETENV("PYTHONVERBOSE")) && *p != '\0')
Guido van Rossum9abaf4d2001-10-12 22:17:56 +0000159 Py_VerboseFlag = add_flag(Py_VerboseFlag, p);
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000160 if ((p = Py_GETENV("PYTHONOPTIMIZE")) && *p != '\0')
Guido van Rossum9abaf4d2001-10-12 22:17:56 +0000161 Py_OptimizeFlag = add_flag(Py_OptimizeFlag, p);
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000162
Guido van Rossuma027efa1997-05-05 20:56:21 +0000163 interp = PyInterpreterState_New();
164 if (interp == NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000165 Py_FatalError("Py_Initialize: can't make first interpreter");
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000166
Guido van Rossuma027efa1997-05-05 20:56:21 +0000167 tstate = PyThreadState_New(interp);
168 if (tstate == NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000169 Py_FatalError("Py_Initialize: can't make first thread");
Guido van Rossuma027efa1997-05-05 20:56:21 +0000170 (void) PyThreadState_Swap(tstate);
171
Guido van Rossum70d893a2001-08-16 08:21:42 +0000172 _Py_ReadyTypes();
Guido van Rossum528b7eb2001-08-07 17:24:28 +0000173
Neal Norwitzb2501f42002-12-31 03:42:13 +0000174 if (!_PyFrame_Init())
Neal Norwitzc91ed402002-12-30 22:29:22 +0000175 Py_FatalError("Py_Initialize: can't init frames");
176
Neal Norwitzb2501f42002-12-31 03:42:13 +0000177 if (!_PyInt_Init())
Neal Norwitzc91ed402002-12-30 22:29:22 +0000178 Py_FatalError("Py_Initialize: can't init ints");
179
Guido van Rossum25ce5661997-08-02 03:10:38 +0000180 interp->modules = PyDict_New();
181 if (interp->modules == NULL)
182 Py_FatalError("Py_Initialize: can't make modules dictionary");
Guido van Rossuma027efa1997-05-05 20:56:21 +0000183
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000184#ifdef Py_USING_UNICODE
Guido van Rossumc94044c2000-03-10 23:03:54 +0000185 /* Init Unicode implementation; relies on the codec registry */
186 _PyUnicode_Init();
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000187#endif
Guido van Rossumc94044c2000-03-10 23:03:54 +0000188
Barry Warsawf242aa02000-05-25 23:09:49 +0000189 bimod = _PyBuiltin_Init();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000190 if (bimod == NULL)
191 Py_FatalError("Py_Initialize: can't initialize __builtin__");
Guido van Rossum4a1f39a1997-11-04 19:36:18 +0000192 interp->builtins = PyModule_GetDict(bimod);
193 Py_INCREF(interp->builtins);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000194
195 sysmod = _PySys_Init();
196 if (sysmod == NULL)
197 Py_FatalError("Py_Initialize: can't initialize sys");
198 interp->sysdict = PyModule_GetDict(sysmod);
199 Py_INCREF(interp->sysdict);
200 _PyImport_FixupExtension("sys", "sys");
Guido van Rossuma027efa1997-05-05 20:56:21 +0000201 PySys_SetPath(Py_GetPath());
Guido van Rossum25ce5661997-08-02 03:10:38 +0000202 PyDict_SetItemString(interp->sysdict, "modules",
203 interp->modules);
204
Guido van Rossum7c85ab81999-07-08 17:26:56 +0000205 _PyImport_Init();
206
Barry Warsawf242aa02000-05-25 23:09:49 +0000207 /* initialize builtin exceptions */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000208 _PyExc_Init();
Barry Warsaw5821bc52001-08-13 23:04:56 +0000209 _PyImport_FixupExtension("exceptions", "exceptions");
Barry Warsawf242aa02000-05-25 23:09:49 +0000210
Barry Warsaw035574d1997-08-29 22:07:17 +0000211 /* phase 2 of builtins */
Barry Warsaw963b8711997-09-18 16:42:02 +0000212 _PyImport_FixupExtension("__builtin__", "__builtin__");
Barry Warsaw035574d1997-08-29 22:07:17 +0000213
Just van Rossum52e14d62002-12-30 22:08:05 +0000214 _PyImportHooks_Init();
215
Guido van Rossum25ce5661997-08-02 03:10:38 +0000216 initsigs(); /* Signal handling stuff, including initintr() */
217
218 initmain(); /* Module __main__ */
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000219 if (!Py_NoSiteFlag)
220 initsite(); /* Module site */
Just van Rossum5bfba3a2003-02-25 20:25:12 +0000221
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000222 /* auto-thread-state API, if available */
223#ifdef WITH_THREAD
224 _PyGILState_Init(interp, tstate);
225#endif /* WITH_THREAD */
226
Mark Hammondedd07732003-07-15 23:03:55 +0000227 warnings_module = PyImport_ImportModule("warnings");
228 if (!warnings_module)
229 PyErr_Clear();
Martin v. Löwis73d538b2003-03-05 15:13:47 +0000230
231#if defined(Py_USING_UNICODE) && defined(HAVE_LANGINFO_H) && defined(CODESET)
232 /* On Unix, set the file system encoding according to the
233 user's preference, if the CODESET names a well-known
234 Python codec, and Py_FileSystemDefaultEncoding isn't
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000235 initialized by other means. Also set the encoding of
236 stdin and stdout if these are terminals. */
237
Martin v. Löwisf56d0152003-11-13 07:43:21 +0000238 saved_locale = strdup(setlocale(LC_CTYPE, NULL));
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000239 setlocale(LC_CTYPE, "");
240 codeset = nl_langinfo(CODESET);
241 if (codeset && *codeset) {
242 PyObject *enc = PyCodec_Encoder(codeset);
243 if (enc) {
244 codeset = strdup(codeset);
245 Py_DECREF(enc);
246 } else {
247 codeset = NULL;
248 PyErr_Clear();
Martin v. Löwis73d538b2003-03-05 15:13:47 +0000249 }
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000250 } else
251 codeset = NULL;
252 setlocale(LC_CTYPE, saved_locale);
Martin v. Löwisf56d0152003-11-13 07:43:21 +0000253 free(saved_locale);
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000254
255 if (codeset) {
Michael W. Hudson68debc92003-08-11 12:20:24 +0000256 sys_stream = PySys_GetObject("stdin");
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000257 sys_isatty = PyObject_CallMethod(sys_stream, "isatty", "");
258 if (!sys_isatty)
259 PyErr_Clear();
260 if(sys_isatty && PyObject_IsTrue(sys_isatty)) {
261 if (!PyFile_SetEncoding(sys_stream, codeset))
262 Py_FatalError("Cannot set codeset of stdin");
263 }
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000264 Py_XDECREF(sys_isatty);
265
266 sys_stream = PySys_GetObject("stdout");
267 sys_isatty = PyObject_CallMethod(sys_stream, "isatty", "");
268 if (!sys_isatty)
269 PyErr_Clear();
270 if(sys_isatty && PyObject_IsTrue(sys_isatty)) {
271 if (!PyFile_SetEncoding(sys_stream, codeset))
272 Py_FatalError("Cannot set codeset of stdout");
273 }
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000274 Py_XDECREF(sys_isatty);
275
276 if (!Py_FileSystemDefaultEncoding)
277 Py_FileSystemDefaultEncoding = codeset;
278 else
279 free(codeset);
Martin v. Löwis73d538b2003-03-05 15:13:47 +0000280 }
281#endif
Guido van Rossum25ce5661997-08-02 03:10:38 +0000282}
283
Guido van Rossum2edcf0d1998-12-15 16:12:00 +0000284#ifdef COUNT_ALLOCS
Tim Petersdbd9ba62000-07-09 03:09:57 +0000285extern void dump_counts(void);
Guido van Rossum2edcf0d1998-12-15 16:12:00 +0000286#endif
287
Guido van Rossum25ce5661997-08-02 03:10:38 +0000288/* Undo the effect of Py_Initialize().
289
290 Beware: if multiple interpreter and/or thread states exist, these
291 are not wiped out; only the current thread and interpreter state
292 are deleted. But since everything else is deleted, those other
293 interpreter and thread states should no longer be used.
294
295 (XXX We should do better, e.g. wipe out all interpreters and
296 threads.)
297
298 Locking: as above.
299
300*/
301
302void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000303Py_Finalize(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000304{
305 PyInterpreterState *interp;
306 PyThreadState *tstate;
307
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000308 if (!initialized)
Guido van Rossumaa615051997-08-20 22:40:18 +0000309 return;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000310
Tim Peters384fd102001-01-21 03:40:37 +0000311 /* The interpreter is still entirely intact at this point, and the
312 * exit funcs may be relying on that. In particular, if some thread
313 * or exit func is still waiting to do an import, the import machinery
314 * expects Py_IsInitialized() to return true. So don't say the
315 * interpreter is uninitialized until after the exit funcs have run.
316 * Note that Threading.py uses an exit func to do a join on all the
317 * threads created thru it, so this also protects pending imports in
318 * the threads created via Threading.
319 */
Guido van Rossum4cc462e1998-01-19 22:00:38 +0000320 call_sys_exitfunc();
Tim Peters384fd102001-01-21 03:40:37 +0000321 initialized = 0;
Guido van Rossum4cc462e1998-01-19 22:00:38 +0000322
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000323 /* Get current thread state and interpreter pointer */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000324 tstate = PyThreadState_Get();
325 interp = tstate->interp;
326
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000327 /* Disable signal handling */
328 PyOS_FiniInterrupts();
329
Mark Hammonda43fd0c2003-02-19 00:33:33 +0000330 /* drop module references we saved */
Mark Hammondedd07732003-07-15 23:03:55 +0000331 Py_XDECREF(warnings_module);
332 warnings_module = NULL;
Mark Hammonda43fd0c2003-02-19 00:33:33 +0000333
Guido van Rossume13ddc92003-04-17 17:29:22 +0000334 /* Collect garbage. This may call finalizers; it's nice to call these
335 before all modules are destroyed. */
336 PyGC_Collect();
337
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000338 /* Destroy all modules */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000339 PyImport_Cleanup();
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000340
Guido van Rossume13ddc92003-04-17 17:29:22 +0000341 /* Collect final garbage. This disposes of cycles created by
342 new-style class definitions, for example. */
343 PyGC_Collect();
344
Guido van Rossum1707aad1997-12-08 23:43:45 +0000345 /* Destroy the database used by _PyImport_{Fixup,Find}Extension */
346 _PyImport_Fini();
347
348 /* Debugging stuff */
349#ifdef COUNT_ALLOCS
350 dump_counts();
351#endif
352
353#ifdef Py_REF_DEBUG
354 fprintf(stderr, "[%ld refs]\n", _Py_RefTotal);
355#endif
356
Tim Peters9cf25ce2003-04-17 15:21:01 +0000357#ifdef Py_TRACE_REFS
358 /* Display all objects still alive -- this can invoke arbitrary
359 * __repr__ overrides, so requires a mostly-intact interpreter.
360 * Alas, a lot of stuff may still be alive now that will be cleaned
361 * up later.
362 */
Tim Peters269b2a62003-04-17 19:52:29 +0000363 if (Py_GETENV("PYTHONDUMPREFS"))
Tim Peters9cf25ce2003-04-17 15:21:01 +0000364 _Py_PrintReferences(stderr);
Tim Peters9cf25ce2003-04-17 15:21:01 +0000365#endif /* Py_TRACE_REFS */
366
Barry Warsaw035574d1997-08-29 22:07:17 +0000367 /* Now we decref the exception classes. After this point nothing
368 can raise an exception. That's okay, because each Fini() method
369 below has been checked to make sure no exceptions are ever
370 raised.
371 */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000372 _PyExc_Fini();
Barry Warsawf242aa02000-05-25 23:09:49 +0000373
Mark Hammond6cb90292003-04-22 11:18:00 +0000374 /* Cleanup auto-thread-state */
375#ifdef WITH_THREAD
376 _PyGILState_Fini();
377#endif /* WITH_THREAD */
378
Guido van Rossumd922fa42003-04-15 14:10:09 +0000379 /* Clear interpreter state */
Barry Warsawf242aa02000-05-25 23:09:49 +0000380 PyInterpreterState_Clear(interp);
Guido van Rossumd922fa42003-04-15 14:10:09 +0000381
Guido van Rossumd922fa42003-04-15 14:10:09 +0000382 /* Delete current thread */
Barry Warsawf242aa02000-05-25 23:09:49 +0000383 PyThreadState_Swap(NULL);
384 PyInterpreterState_Delete(interp);
385
Guido van Rossumd922fa42003-04-15 14:10:09 +0000386 /* Sundry finalizers */
Guido van Rossumcc283f51997-08-05 02:22:03 +0000387 PyMethod_Fini();
388 PyFrame_Fini();
389 PyCFunction_Fini();
390 PyTuple_Fini();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000391 PyString_Fini();
Guido van Rossumcc283f51997-08-05 02:22:03 +0000392 PyInt_Fini();
393 PyFloat_Fini();
394
Marc-André Lemburg95de5c12002-04-08 08:19:36 +0000395#ifdef Py_USING_UNICODE
396 /* Cleanup Unicode implementation */
397 _PyUnicode_Fini();
398#endif
399
Guido van Rossumcc283f51997-08-05 02:22:03 +0000400 /* XXX Still allocated:
401 - various static ad-hoc pointers to interned strings
402 - int and float free list blocks
403 - whatever various modules and libraries allocate
404 */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000405
406 PyGrammar_RemoveAccelerators(&_PyParser_Grammar);
Guido van Rossumcc283f51997-08-05 02:22:03 +0000407
Tim Peters269b2a62003-04-17 19:52:29 +0000408#ifdef Py_TRACE_REFS
409 /* Display addresses (& refcnts) of all objects still alive.
410 * An address can be used to find the repr of the object, printed
411 * above by _Py_PrintReferences.
412 */
413 if (Py_GETENV("PYTHONDUMPREFS"))
414 _Py_PrintReferenceAddresses(stderr);
415#endif /* Py_TRACE_REFS */
Tim Peters0e871182002-04-13 08:29:14 +0000416#ifdef PYMALLOC_DEBUG
417 if (Py_GETENV("PYTHONMALLOCSTATS"))
418 _PyObject_DebugMallocStats();
419#endif
420
Guido van Rossumcc283f51997-08-05 02:22:03 +0000421 call_ll_exitfuncs();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000422}
423
424/* Create and initialize a new interpreter and thread, and return the
425 new thread. This requires that Py_Initialize() has been called
426 first.
427
428 Unsuccessful initialization yields a NULL pointer. Note that *no*
429 exception information is available even in this case -- the
430 exception information is held in the thread, and there is no
431 thread.
432
433 Locking: as above.
434
435*/
436
437PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000438Py_NewInterpreter(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000439{
440 PyInterpreterState *interp;
441 PyThreadState *tstate, *save_tstate;
442 PyObject *bimod, *sysmod;
443
444 if (!initialized)
445 Py_FatalError("Py_NewInterpreter: call Py_Initialize first");
446
447 interp = PyInterpreterState_New();
448 if (interp == NULL)
449 return NULL;
450
451 tstate = PyThreadState_New(interp);
452 if (tstate == NULL) {
453 PyInterpreterState_Delete(interp);
454 return NULL;
455 }
456
457 save_tstate = PyThreadState_Swap(tstate);
458
459 /* XXX The following is lax in error checking */
460
461 interp->modules = PyDict_New();
462
463 bimod = _PyImport_FindExtension("__builtin__", "__builtin__");
464 if (bimod != NULL) {
Guido van Rossum4a1f39a1997-11-04 19:36:18 +0000465 interp->builtins = PyModule_GetDict(bimod);
466 Py_INCREF(interp->builtins);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000467 }
468 sysmod = _PyImport_FindExtension("sys", "sys");
469 if (bimod != NULL && sysmod != NULL) {
470 interp->sysdict = PyModule_GetDict(sysmod);
471 Py_INCREF(interp->sysdict);
472 PySys_SetPath(Py_GetPath());
473 PyDict_SetItemString(interp->sysdict, "modules",
474 interp->modules);
Martin v. Löwisd7ceb222003-01-22 09:00:38 +0000475 _PyImportHooks_Init();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000476 initmain();
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000477 if (!Py_NoSiteFlag)
478 initsite();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000479 }
480
481 if (!PyErr_Occurred())
482 return tstate;
483
484 /* Oops, it didn't work. Undo it all. */
485
486 PyErr_Print();
487 PyThreadState_Clear(tstate);
488 PyThreadState_Swap(save_tstate);
489 PyThreadState_Delete(tstate);
490 PyInterpreterState_Delete(interp);
491
492 return NULL;
493}
494
495/* Delete an interpreter and its last thread. This requires that the
496 given thread state is current, that the thread has no remaining
497 frames, and that it is its interpreter's only remaining thread.
498 It is a fatal error to violate these constraints.
499
500 (Py_Finalize() doesn't have these constraints -- it zaps
501 everything, regardless.)
502
503 Locking: as above.
504
505*/
506
507void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000508Py_EndInterpreter(PyThreadState *tstate)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000509{
510 PyInterpreterState *interp = tstate->interp;
511
512 if (tstate != PyThreadState_Get())
513 Py_FatalError("Py_EndInterpreter: thread is not current");
514 if (tstate->frame != NULL)
515 Py_FatalError("Py_EndInterpreter: thread still has a frame");
516 if (tstate != interp->tstate_head || tstate->next != NULL)
517 Py_FatalError("Py_EndInterpreter: not the last thread");
518
519 PyImport_Cleanup();
520 PyInterpreterState_Clear(interp);
521 PyThreadState_Swap(NULL);
522 PyInterpreterState_Delete(interp);
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000523}
524
525static char *progname = "python";
526
527void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000528Py_SetProgramName(char *pn)
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000529{
530 if (pn && *pn)
531 progname = pn;
532}
533
534char *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000535Py_GetProgramName(void)
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000536{
537 return progname;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000538}
539
Guido van Rossuma61691e1998-02-06 22:27:24 +0000540static char *default_home = NULL;
541
542void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000543Py_SetPythonHome(char *home)
Guido van Rossuma61691e1998-02-06 22:27:24 +0000544{
545 default_home = home;
546}
547
548char *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000549Py_GetPythonHome(void)
Guido van Rossuma61691e1998-02-06 22:27:24 +0000550{
551 char *home = default_home;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000552 if (home == NULL && !Py_IgnoreEnvironmentFlag)
553 home = Py_GETENV("PYTHONHOME");
Guido van Rossuma61691e1998-02-06 22:27:24 +0000554 return home;
555}
556
Guido van Rossum6135a871995-01-09 17:53:26 +0000557/* Create __main__ module */
558
559static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000560initmain(void)
Guido van Rossum6135a871995-01-09 17:53:26 +0000561{
Guido van Rossum82598051997-03-05 00:20:32 +0000562 PyObject *m, *d;
563 m = PyImport_AddModule("__main__");
Guido van Rossum6135a871995-01-09 17:53:26 +0000564 if (m == NULL)
Guido van Rossum82598051997-03-05 00:20:32 +0000565 Py_FatalError("can't create __main__ module");
566 d = PyModule_GetDict(m);
567 if (PyDict_GetItemString(d, "__builtins__") == NULL) {
Guido van Rossum858cb731997-11-19 16:15:37 +0000568 PyObject *bimod = PyImport_ImportModule("__builtin__");
569 if (bimod == NULL ||
570 PyDict_SetItemString(d, "__builtins__", bimod) != 0)
Guido van Rossum82598051997-03-05 00:20:32 +0000571 Py_FatalError("can't add __builtins__ to __main__");
Barry Warsaw3d05b1a1999-01-29 21:30:22 +0000572 Py_DECREF(bimod);
Guido van Rossum6135a871995-01-09 17:53:26 +0000573 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000574}
575
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000576/* Import the site module (not into __main__ though) */
577
578static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000579initsite(void)
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000580{
581 PyObject *m, *f;
582 m = PyImport_ImportModule("site");
583 if (m == NULL) {
584 f = PySys_GetObject("stderr");
585 if (Py_VerboseFlag) {
586 PyFile_WriteString(
587 "'import site' failed; traceback:\n", f);
588 PyErr_Print();
589 }
590 else {
591 PyFile_WriteString(
592 "'import site' failed; use -v for traceback\n", f);
593 PyErr_Clear();
594 }
595 }
596 else {
597 Py_DECREF(m);
598 }
599}
600
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000601/* Parse input from a file and execute it */
602
603int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000604PyRun_AnyFile(FILE *fp, const char *filename)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000605{
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000606 return PyRun_AnyFileExFlags(fp, filename, 0, NULL);
607}
608
609int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000610PyRun_AnyFileFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000611{
612 return PyRun_AnyFileExFlags(fp, filename, 0, flags);
Guido van Rossum0df002c2000-08-27 19:21:52 +0000613}
614
615int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000616PyRun_AnyFileEx(FILE *fp, const char *filename, int closeit)
Guido van Rossum0df002c2000-08-27 19:21:52 +0000617{
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000618 return PyRun_AnyFileExFlags(fp, filename, closeit, NULL);
619}
620
621int
Tim Petersd08e3822003-04-17 15:24:21 +0000622PyRun_AnyFileExFlags(FILE *fp, const char *filename, int closeit,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000623 PyCompilerFlags *flags)
624{
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000625 if (filename == NULL)
626 filename = "???";
Guido van Rossum0df002c2000-08-27 19:21:52 +0000627 if (Py_FdIsInteractive(fp, filename)) {
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000628 int err = PyRun_InteractiveLoopFlags(fp, filename, flags);
Guido van Rossum0df002c2000-08-27 19:21:52 +0000629 if (closeit)
630 fclose(fp);
631 return err;
632 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000633 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000634 return PyRun_SimpleFileExFlags(fp, filename, closeit, flags);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000635}
636
637int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000638PyRun_InteractiveLoop(FILE *fp, const char *filename)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000639{
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000640 return PyRun_InteractiveLoopFlags(fp, filename, NULL);
641}
642
643int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000644PyRun_InteractiveLoopFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000645{
Guido van Rossum82598051997-03-05 00:20:32 +0000646 PyObject *v;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000647 int ret;
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000648 PyCompilerFlags local_flags;
Jeremy Hylton9f324e92001-03-01 22:59:14 +0000649
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000650 if (flags == NULL) {
651 flags = &local_flags;
Tim Peters5ba58662001-07-16 02:29:45 +0000652 local_flags.cf_flags = 0;
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000653 }
Guido van Rossum82598051997-03-05 00:20:32 +0000654 v = PySys_GetObject("ps1");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000655 if (v == NULL) {
Guido van Rossum82598051997-03-05 00:20:32 +0000656 PySys_SetObject("ps1", v = PyString_FromString(">>> "));
657 Py_XDECREF(v);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000658 }
Guido van Rossum82598051997-03-05 00:20:32 +0000659 v = PySys_GetObject("ps2");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000660 if (v == NULL) {
Guido van Rossum82598051997-03-05 00:20:32 +0000661 PySys_SetObject("ps2", v = PyString_FromString("... "));
662 Py_XDECREF(v);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000663 }
664 for (;;) {
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000665 ret = PyRun_InteractiveOneFlags(fp, filename, flags);
Guido van Rossumaae0d321996-05-22 16:35:33 +0000666#ifdef Py_REF_DEBUG
Guido van Rossum6f9e4331995-03-29 16:57:48 +0000667 fprintf(stderr, "[%ld refs]\n", _Py_RefTotal);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000668#endif
669 if (ret == E_EOF)
670 return 0;
671 /*
672 if (ret == E_NOMEM)
673 return -1;
674 */
675 }
676}
677
678int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000679PyRun_InteractiveOne(FILE *fp, const char *filename)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000680{
Jeremy Hylton9f324e92001-03-01 22:59:14 +0000681 return PyRun_InteractiveOneFlags(fp, filename, NULL);
682}
683
Neil Schemenauerc24ea082002-03-22 23:53:36 +0000684/* compute parser flags based on compiler flags */
Neil Schemenauerc24ea082002-03-22 23:53:36 +0000685#define PARSER_FLAGS(flags) \
Guido van Rossum4b499dd32003-02-13 22:07:59 +0000686 (((flags) && (flags)->cf_flags & PyCF_DONT_IMPLY_DEDENT) ? \
687 PyPARSE_DONT_IMPLY_DEDENT : 0)
Neil Schemenauerc24ea082002-03-22 23:53:36 +0000688
Jeremy Hylton9f324e92001-03-01 22:59:14 +0000689int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000690PyRun_InteractiveOneFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
Jeremy Hylton9f324e92001-03-01 22:59:14 +0000691{
Guido van Rossum82598051997-03-05 00:20:32 +0000692 PyObject *m, *d, *v, *w;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000693 node *n;
Guido van Rossuma110aa61994-08-29 12:50:44 +0000694 perrdetail err;
Guido van Rossumddc3fb51997-11-25 20:58:13 +0000695 char *ps1 = "", *ps2 = "";
Tim Petersfe2127d2001-07-16 05:37:24 +0000696
Guido van Rossum82598051997-03-05 00:20:32 +0000697 v = PySys_GetObject("ps1");
Guido van Rossumddc3fb51997-11-25 20:58:13 +0000698 if (v != NULL) {
699 v = PyObject_Str(v);
700 if (v == NULL)
701 PyErr_Clear();
702 else if (PyString_Check(v))
703 ps1 = PyString_AsString(v);
704 }
Guido van Rossum82598051997-03-05 00:20:32 +0000705 w = PySys_GetObject("ps2");
Guido van Rossumddc3fb51997-11-25 20:58:13 +0000706 if (w != NULL) {
707 w = PyObject_Str(w);
708 if (w == NULL)
709 PyErr_Clear();
710 else if (PyString_Check(w))
711 ps2 = PyString_AsString(w);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000712 }
Tim Petersfe2127d2001-07-16 05:37:24 +0000713 n = PyParser_ParseFileFlags(fp, filename, &_PyParser_Grammar,
714 Py_single_input, ps1, ps2, &err,
Neil Schemenauerc24ea082002-03-22 23:53:36 +0000715 PARSER_FLAGS(flags));
Guido van Rossum82598051997-03-05 00:20:32 +0000716 Py_XDECREF(v);
717 Py_XDECREF(w);
Guido van Rossuma110aa61994-08-29 12:50:44 +0000718 if (n == NULL) {
719 if (err.error == E_EOF) {
720 if (err.text)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000721 PyMem_DEL(err.text);
Guido van Rossuma110aa61994-08-29 12:50:44 +0000722 return E_EOF;
723 }
724 err_input(&err);
Guido van Rossum82598051997-03-05 00:20:32 +0000725 PyErr_Print();
Guido van Rossuma110aa61994-08-29 12:50:44 +0000726 return err.error;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000727 }
Guido van Rossum82598051997-03-05 00:20:32 +0000728 m = PyImport_AddModule("__main__");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000729 if (m == NULL)
730 return -1;
Guido van Rossum82598051997-03-05 00:20:32 +0000731 d = PyModule_GetDict(m);
Jeremy Hylton9f324e92001-03-01 22:59:14 +0000732 v = run_node(n, filename, d, d, flags);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000733 if (v == NULL) {
Guido van Rossum82598051997-03-05 00:20:32 +0000734 PyErr_Print();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000735 return -1;
736 }
Guido van Rossum82598051997-03-05 00:20:32 +0000737 Py_DECREF(v);
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000738 if (Py_FlushLine())
739 PyErr_Clear();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000740 return 0;
741}
742
743int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000744PyRun_SimpleFile(FILE *fp, const char *filename)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000745{
Guido van Rossum0df002c2000-08-27 19:21:52 +0000746 return PyRun_SimpleFileEx(fp, filename, 0);
747}
748
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000749/* Check whether a file maybe a pyc file: Look at the extension,
750 the file type, and, if we may close it, at the first few bytes. */
751
752static int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000753maybe_pyc_file(FILE *fp, const char* filename, const char* ext, int closeit)
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000754{
755 if (strcmp(ext, ".pyc") == 0 || strcmp(ext, ".pyo") == 0)
756 return 1;
757
758#ifdef macintosh
759 /* On a mac, we also assume a pyc file for types 'PYC ' and 'APPL' */
Jack Jansen72f3b7a2002-12-13 15:23:10 +0000760 if (PyMac_getfiletype((char *)filename) == 'PYC '
761 || PyMac_getfiletype((char *)filename) == 'APPL')
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000762 return 1;
763#endif /* macintosh */
764
765 /* Only look into the file if we are allowed to close it, since
766 it then should also be seekable. */
767 if (closeit) {
768 /* Read only two bytes of the magic. If the file was opened in
769 text mode, the bytes 3 and 4 of the magic (\r\n) might not
770 be read as they are on disk. */
771 unsigned int halfmagic = PyImport_GetMagicNumber() & 0xFFFF;
772 unsigned char buf[2];
Tim Peters3e876562001-02-11 04:35:39 +0000773 /* Mess: In case of -x, the stream is NOT at its start now,
774 and ungetc() was used to push back the first newline,
Tim Peters6f5a4ef2001-02-17 22:02:34 +0000775 which makes the current stream position formally undefined,
776 and a x-platform nightmare.
777 Unfortunately, we have no direct way to know whether -x
778 was specified. So we use a terrible hack: if the current
779 stream position is not 0, we assume -x was specified, and
780 give up. Bug 132850 on SourceForge spells out the
781 hopelessness of trying anything else (fseek and ftell
782 don't work predictably x-platform for text-mode files).
Tim Peters3e876562001-02-11 04:35:39 +0000783 */
Tim Peters3e876562001-02-11 04:35:39 +0000784 int ispyc = 0;
Tim Peters6f5a4ef2001-02-17 22:02:34 +0000785 if (ftell(fp) == 0) {
786 if (fread(buf, 1, 2, fp) == 2 &&
Tim Petersd08e3822003-04-17 15:24:21 +0000787 ((unsigned int)buf[1]<<8 | buf[0]) == halfmagic)
Tim Peters6f5a4ef2001-02-17 22:02:34 +0000788 ispyc = 1;
789 rewind(fp);
790 }
Tim Peters3e876562001-02-11 04:35:39 +0000791 return ispyc;
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000792 }
793 return 0;
Tim Petersd08e3822003-04-17 15:24:21 +0000794}
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000795
Guido van Rossum0df002c2000-08-27 19:21:52 +0000796int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000797PyRun_SimpleFileEx(FILE *fp, const char *filename, int closeit)
Guido van Rossum0df002c2000-08-27 19:21:52 +0000798{
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000799 return PyRun_SimpleFileExFlags(fp, filename, closeit, NULL);
800}
801
802int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000803PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000804 PyCompilerFlags *flags)
805{
Guido van Rossum82598051997-03-05 00:20:32 +0000806 PyObject *m, *d, *v;
Martin v. Löwis95292d62002-12-11 14:04:59 +0000807 const char *ext;
Guido van Rossumfdef2711994-09-14 13:31:04 +0000808
Guido van Rossum82598051997-03-05 00:20:32 +0000809 m = PyImport_AddModule("__main__");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000810 if (m == NULL)
811 return -1;
Guido van Rossum82598051997-03-05 00:20:32 +0000812 d = PyModule_GetDict(m);
Fred Drake8ed02042002-10-17 21:24:58 +0000813 if (PyDict_GetItemString(d, "__file__") == NULL) {
814 PyObject *f = PyString_FromString(filename);
815 if (f == NULL)
816 return -1;
817 if (PyDict_SetItemString(d, "__file__", f) < 0) {
818 Py_DECREF(f);
819 return -1;
820 }
821 Py_DECREF(f);
822 }
Guido van Rossumfdef2711994-09-14 13:31:04 +0000823 ext = filename + strlen(filename) - 4;
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000824 if (maybe_pyc_file(fp, filename, ext, closeit)) {
Guido van Rossumfdef2711994-09-14 13:31:04 +0000825 /* Try to run a pyc file. First, re-open in binary */
Guido van Rossum0df002c2000-08-27 19:21:52 +0000826 if (closeit)
827 fclose(fp);
Fred Drake8ed02042002-10-17 21:24:58 +0000828 if ((fp = fopen(filename, "rb")) == NULL) {
Guido van Rossumfdef2711994-09-14 13:31:04 +0000829 fprintf(stderr, "python: Can't reopen .pyc file\n");
830 return -1;
831 }
Guido van Rossum2a7f58d1997-04-02 05:28:38 +0000832 /* Turn on optimization if a .pyo file is given */
833 if (strcmp(ext, ".pyo") == 0)
834 Py_OptimizeFlag = 1;
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000835 v = run_pyc_file(fp, filename, d, d, flags);
Guido van Rossumfdef2711994-09-14 13:31:04 +0000836 } else {
Tim Petersd08e3822003-04-17 15:24:21 +0000837 v = PyRun_FileExFlags(fp, filename, Py_file_input, d, d,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000838 closeit, flags);
Guido van Rossumfdef2711994-09-14 13:31:04 +0000839 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000840 if (v == NULL) {
Guido van Rossum82598051997-03-05 00:20:32 +0000841 PyErr_Print();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000842 return -1;
843 }
Guido van Rossum82598051997-03-05 00:20:32 +0000844 Py_DECREF(v);
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000845 if (Py_FlushLine())
846 PyErr_Clear();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000847 return 0;
848}
849
850int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000851PyRun_SimpleString(const char *command)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000852{
Guido van Rossum393661d2001-08-31 17:40:15 +0000853 return PyRun_SimpleStringFlags(command, NULL);
854}
855
856int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000857PyRun_SimpleStringFlags(const char *command, PyCompilerFlags *flags)
Guido van Rossum393661d2001-08-31 17:40:15 +0000858{
Guido van Rossum82598051997-03-05 00:20:32 +0000859 PyObject *m, *d, *v;
860 m = PyImport_AddModule("__main__");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000861 if (m == NULL)
862 return -1;
Guido van Rossum82598051997-03-05 00:20:32 +0000863 d = PyModule_GetDict(m);
Guido van Rossum393661d2001-08-31 17:40:15 +0000864 v = PyRun_StringFlags(command, Py_file_input, d, d, flags);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000865 if (v == NULL) {
Guido van Rossum82598051997-03-05 00:20:32 +0000866 PyErr_Print();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000867 return -1;
868 }
Guido van Rossum82598051997-03-05 00:20:32 +0000869 Py_DECREF(v);
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000870 if (Py_FlushLine())
871 PyErr_Clear();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000872 return 0;
873}
874
Barry Warsaw035574d1997-08-29 22:07:17 +0000875static int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000876parse_syntax_error(PyObject *err, PyObject **message, const char **filename,
877 int *lineno, int *offset, const char **text)
Barry Warsaw035574d1997-08-29 22:07:17 +0000878{
879 long hold;
880 PyObject *v;
881
882 /* old style errors */
883 if (PyTuple_Check(err))
Neal Norwitz78293352002-04-01 01:41:20 +0000884 return PyArg_ParseTuple(err, "O(ziiz)", message, filename,
885 lineno, offset, text);
Barry Warsaw035574d1997-08-29 22:07:17 +0000886
887 /* new style errors. `err' is an instance */
888
889 if (! (v = PyObject_GetAttrString(err, "msg")))
890 goto finally;
891 *message = v;
892
893 if (!(v = PyObject_GetAttrString(err, "filename")))
894 goto finally;
895 if (v == Py_None)
896 *filename = NULL;
897 else if (! (*filename = PyString_AsString(v)))
898 goto finally;
899
900 Py_DECREF(v);
901 if (!(v = PyObject_GetAttrString(err, "lineno")))
902 goto finally;
903 hold = PyInt_AsLong(v);
904 Py_DECREF(v);
905 v = NULL;
906 if (hold < 0 && PyErr_Occurred())
907 goto finally;
908 *lineno = (int)hold;
909
910 if (!(v = PyObject_GetAttrString(err, "offset")))
911 goto finally;
Jeremy Hylton9f1b9932001-02-28 07:07:43 +0000912 if (v == Py_None) {
913 *offset = -1;
914 Py_DECREF(v);
915 v = NULL;
916 } else {
917 hold = PyInt_AsLong(v);
918 Py_DECREF(v);
919 v = NULL;
920 if (hold < 0 && PyErr_Occurred())
921 goto finally;
922 *offset = (int)hold;
923 }
Barry Warsaw035574d1997-08-29 22:07:17 +0000924
925 if (!(v = PyObject_GetAttrString(err, "text")))
926 goto finally;
927 if (v == Py_None)
928 *text = NULL;
929 else if (! (*text = PyString_AsString(v)))
930 goto finally;
931 Py_DECREF(v);
932 return 1;
933
934finally:
935 Py_XDECREF(v);
936 return 0;
937}
938
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000939void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000940PyErr_Print(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000941{
Guido van Rossuma61691e1998-02-06 22:27:24 +0000942 PyErr_PrintEx(1);
943}
944
Jeremy Hylton9f1b9932001-02-28 07:07:43 +0000945static void
Martin v. Löwis95292d62002-12-11 14:04:59 +0000946print_error_text(PyObject *f, int offset, const char *text)
Jeremy Hylton9f1b9932001-02-28 07:07:43 +0000947{
948 char *nl;
949 if (offset >= 0) {
950 if (offset > 0 && offset == (int)strlen(text))
951 offset--;
952 for (;;) {
953 nl = strchr(text, '\n');
954 if (nl == NULL || nl-text >= offset)
955 break;
956 offset -= (nl+1-text);
957 text = nl+1;
958 }
959 while (*text == ' ' || *text == '\t') {
960 text++;
961 offset--;
962 }
963 }
964 PyFile_WriteString(" ", f);
965 PyFile_WriteString(text, f);
966 if (*text == '\0' || text[strlen(text)-1] != '\n')
967 PyFile_WriteString("\n", f);
968 if (offset == -1)
969 return;
970 PyFile_WriteString(" ", f);
971 offset--;
972 while (offset > 0) {
973 PyFile_WriteString(" ", f);
974 offset--;
975 }
976 PyFile_WriteString("^\n", f);
977}
978
Guido van Rossum66e8e862001-03-23 17:54:43 +0000979static void
980handle_system_exit(void)
Ka-Ping Yee26fabb02001-03-23 15:36:41 +0000981{
Guido van Rossum66e8e862001-03-23 17:54:43 +0000982 PyObject *exception, *value, *tb;
Tim Peterscf615b52003-04-19 18:47:02 +0000983 int exitcode = 0;
984
Guido van Rossum66e8e862001-03-23 17:54:43 +0000985 PyErr_Fetch(&exception, &value, &tb);
Ka-Ping Yee26fabb02001-03-23 15:36:41 +0000986 if (Py_FlushLine())
987 PyErr_Clear();
988 fflush(stdout);
989 if (value == NULL || value == Py_None)
Tim Peterscf615b52003-04-19 18:47:02 +0000990 goto done;
Ka-Ping Yee26fabb02001-03-23 15:36:41 +0000991 if (PyInstance_Check(value)) {
992 /* The error code should be in the `code' attribute. */
993 PyObject *code = PyObject_GetAttrString(value, "code");
994 if (code) {
995 Py_DECREF(value);
996 value = code;
997 if (value == Py_None)
Tim Peterscf615b52003-04-19 18:47:02 +0000998 goto done;
Ka-Ping Yee26fabb02001-03-23 15:36:41 +0000999 }
1000 /* If we failed to dig out the 'code' attribute,
1001 just let the else clause below print the error. */
1002 }
1003 if (PyInt_Check(value))
Tim Peterscf615b52003-04-19 18:47:02 +00001004 exitcode = (int)PyInt_AsLong(value);
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001005 else {
1006 PyObject_Print(value, stderr, Py_PRINT_RAW);
1007 PySys_WriteStderr("\n");
Tim Peterscf615b52003-04-19 18:47:02 +00001008 exitcode = 1;
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001009 }
Tim Peterscf615b52003-04-19 18:47:02 +00001010 done:
1011 /* Restore and clear the exception info, in order to properly decref
1012 * the exception, value, and traceback. If we just exit instead,
1013 * these leak, which confuses PYTHONDUMPREFS output, and may prevent
1014 * some finalizers from running.
1015 */
1016 PyErr_Restore(exception, value, tb);
1017 PyErr_Clear();
1018 Py_Exit(exitcode);
1019 /* NOTREACHED */
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001020}
1021
1022void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001023PyErr_PrintEx(int set_sys_last_vars)
Guido van Rossuma61691e1998-02-06 22:27:24 +00001024{
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001025 PyObject *exception, *v, *tb, *hook;
Guido van Rossum66e8e862001-03-23 17:54:43 +00001026
1027 if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
1028 handle_system_exit();
1029 }
Guido van Rossum82598051997-03-05 00:20:32 +00001030 PyErr_Fetch(&exception, &v, &tb);
Barry Warsaw035574d1997-08-29 22:07:17 +00001031 PyErr_NormalizeException(&exception, &v, &tb);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001032 if (exception == NULL)
Guido van Rossum296b4751997-05-23 00:19:20 +00001033 return;
Guido van Rossuma61691e1998-02-06 22:27:24 +00001034 if (set_sys_last_vars) {
1035 PySys_SetObject("last_type", exception);
1036 PySys_SetObject("last_value", v);
1037 PySys_SetObject("last_traceback", tb);
1038 }
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001039 hook = PySys_GetObject("excepthook");
1040 if (hook) {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001041 PyObject *args = PyTuple_Pack(3,
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001042 exception, v ? v : Py_None, tb ? tb : Py_None);
1043 PyObject *result = PyEval_CallObject(hook, args);
1044 if (result == NULL) {
1045 PyObject *exception2, *v2, *tb2;
Guido van Rossum66e8e862001-03-23 17:54:43 +00001046 if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
1047 handle_system_exit();
1048 }
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001049 PyErr_Fetch(&exception2, &v2, &tb2);
1050 PyErr_NormalizeException(&exception2, &v2, &tb2);
1051 if (Py_FlushLine())
1052 PyErr_Clear();
1053 fflush(stdout);
1054 PySys_WriteStderr("Error in sys.excepthook:\n");
1055 PyErr_Display(exception2, v2, tb2);
1056 PySys_WriteStderr("\nOriginal exception was:\n");
1057 PyErr_Display(exception, v, tb);
Jeremy Hylton07028582001-12-07 15:35:35 +00001058 Py_XDECREF(exception2);
1059 Py_XDECREF(v2);
1060 Py_XDECREF(tb2);
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001061 }
1062 Py_XDECREF(result);
1063 Py_XDECREF(args);
1064 } else {
1065 PySys_WriteStderr("sys.excepthook is missing\n");
1066 PyErr_Display(exception, v, tb);
1067 }
1068 Py_XDECREF(exception);
1069 Py_XDECREF(v);
1070 Py_XDECREF(tb);
1071}
1072
1073void PyErr_Display(PyObject *exception, PyObject *value, PyObject *tb)
1074{
1075 int err = 0;
1076 PyObject *v = value;
1077 PyObject *f = PySys_GetObject("stderr");
Guido van Rossum3165fe61992-09-25 21:59:05 +00001078 if (f == NULL)
1079 fprintf(stderr, "lost sys.stderr\n");
1080 else {
Guido van Rossum0829c751998-02-28 04:31:39 +00001081 if (Py_FlushLine())
1082 PyErr_Clear();
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001083 fflush(stdout);
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001084 if (tb && tb != Py_None)
1085 err = PyTraceBack_Print(tb, f);
Barry Warsaw36b8f941997-08-26 18:09:48 +00001086 if (err == 0 &&
Martin v. Löwiscfeb3b62002-03-03 21:30:27 +00001087 PyObject_HasAttrString(v, "print_file_and_line"))
Barry Warsaw36b8f941997-08-26 18:09:48 +00001088 {
Guido van Rossum82598051997-03-05 00:20:32 +00001089 PyObject *message;
Martin v. Löwis95292d62002-12-11 14:04:59 +00001090 const char *filename, *text;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001091 int lineno, offset;
Barry Warsaw035574d1997-08-29 22:07:17 +00001092 if (!parse_syntax_error(v, &message, &filename,
1093 &lineno, &offset, &text))
Guido van Rossum82598051997-03-05 00:20:32 +00001094 PyErr_Clear();
Guido van Rossuma110aa61994-08-29 12:50:44 +00001095 else {
1096 char buf[10];
Guido van Rossum82598051997-03-05 00:20:32 +00001097 PyFile_WriteString(" File \"", f);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001098 if (filename == NULL)
Guido van Rossum82598051997-03-05 00:20:32 +00001099 PyFile_WriteString("<string>", f);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001100 else
Guido van Rossum82598051997-03-05 00:20:32 +00001101 PyFile_WriteString(filename, f);
1102 PyFile_WriteString("\", line ", f);
Jeremy Hylton518ab1c2001-11-28 20:42:20 +00001103 PyOS_snprintf(buf, sizeof(buf), "%d", lineno);
Guido van Rossum82598051997-03-05 00:20:32 +00001104 PyFile_WriteString(buf, f);
1105 PyFile_WriteString("\n", f);
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001106 if (text != NULL)
1107 print_error_text(f, offset, text);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001108 v = message;
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001109 /* Can't be bothered to check all those
1110 PyFile_WriteString() calls */
1111 if (PyErr_Occurred())
1112 err = -1;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001113 }
1114 }
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001115 if (err) {
1116 /* Don't do anything else */
1117 }
1118 else if (PyClass_Check(exception)) {
Barry Warsaw2f5f6a21997-09-16 21:42:03 +00001119 PyClassObject* exc = (PyClassObject*)exception;
1120 PyObject* className = exc->cl_name;
1121 PyObject* moduleName =
1122 PyDict_GetItemString(exc->cl_dict, "__module__");
1123
1124 if (moduleName == NULL)
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001125 err = PyFile_WriteString("<unknown>", f);
Barry Warsaw2f5f6a21997-09-16 21:42:03 +00001126 else {
1127 char* modstr = PyString_AsString(moduleName);
Tim Petersd08e3822003-04-17 15:24:21 +00001128 if (modstr && strcmp(modstr, "exceptions"))
Barry Warsaw2f5f6a21997-09-16 21:42:03 +00001129 {
1130 err = PyFile_WriteString(modstr, f);
1131 err += PyFile_WriteString(".", f);
1132 }
1133 }
1134 if (err == 0) {
1135 if (className == NULL)
1136 err = PyFile_WriteString("<unknown>", f);
1137 else
1138 err = PyFile_WriteObject(className, f,
1139 Py_PRINT_RAW);
1140 }
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001141 }
1142 else
1143 err = PyFile_WriteObject(exception, f, Py_PRINT_RAW);
1144 if (err == 0) {
1145 if (v != NULL && v != Py_None) {
Barry Warsaw035574d1997-08-29 22:07:17 +00001146 PyObject *s = PyObject_Str(v);
1147 /* only print colon if the str() of the
1148 object is not the empty string
1149 */
Guido van Rossumd6bf45b1997-09-05 19:11:53 +00001150 if (s == NULL)
1151 err = -1;
1152 else if (!PyString_Check(s) ||
1153 PyString_GET_SIZE(s) != 0)
Barry Warsaw035574d1997-08-29 22:07:17 +00001154 err = PyFile_WriteString(": ", f);
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001155 if (err == 0)
Guido van Rossumd6bf45b1997-09-05 19:11:53 +00001156 err = PyFile_WriteObject(s, f, Py_PRINT_RAW);
1157 Py_XDECREF(s);
Guido van Rossum262e1241995-02-07 15:30:45 +00001158 }
Guido van Rossum262e1241995-02-07 15:30:45 +00001159 }
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001160 if (err == 0)
1161 err = PyFile_WriteString("\n", f);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001162 }
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001163 /* If an error happened here, don't show it.
1164 XXX This is wrong, but too many callers rely on this behavior. */
1165 if (err != 0)
1166 PyErr_Clear();
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001167}
1168
Guido van Rossum82598051997-03-05 00:20:32 +00001169PyObject *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001170PyRun_String(const char *str, int start, PyObject *globals, PyObject *locals)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001171{
Guido van Rossum82598051997-03-05 00:20:32 +00001172 return run_err_node(PyParser_SimpleParseString(str, start),
Jeremy Hylton9f324e92001-03-01 22:59:14 +00001173 "<string>", globals, locals, NULL);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001174}
1175
Guido van Rossum82598051997-03-05 00:20:32 +00001176PyObject *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001177PyRun_File(FILE *fp, const char *filename, int start, PyObject *globals,
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001178 PyObject *locals)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001179{
Tim Peterse8682112000-08-27 20:18:17 +00001180 return PyRun_FileEx(fp, filename, start, globals, locals, 0);
Guido van Rossum0df002c2000-08-27 19:21:52 +00001181}
1182
1183PyObject *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001184PyRun_FileEx(FILE *fp, const char *filename, int start, PyObject *globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001185 PyObject *locals, int closeit)
Guido van Rossum0df002c2000-08-27 19:21:52 +00001186{
1187 node *n = PyParser_SimpleParseFile(fp, filename, start);
1188 if (closeit)
1189 fclose(fp);
Jeremy Hylton9f324e92001-03-01 22:59:14 +00001190 return run_err_node(n, filename, globals, locals, NULL);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001191}
1192
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001193PyObject *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001194PyRun_StringFlags(const char *str, int start, PyObject *globals, PyObject *locals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001195 PyCompilerFlags *flags)
1196{
Guido van Rossuma1b3a472001-07-16 16:51:33 +00001197 return run_err_node(PyParser_SimpleParseStringFlags(
Neil Schemenauerc24ea082002-03-22 23:53:36 +00001198 str, start, PARSER_FLAGS(flags)),
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001199 "<string>", globals, locals, flags);
1200}
1201
1202PyObject *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001203PyRun_FileFlags(FILE *fp, const char *filename, int start, PyObject *globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001204 PyObject *locals, PyCompilerFlags *flags)
1205{
1206 return PyRun_FileExFlags(fp, filename, start, globals, locals, 0,
Tim Petersd08e3822003-04-17 15:24:21 +00001207 flags);
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001208}
1209
1210PyObject *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001211PyRun_FileExFlags(FILE *fp, const char *filename, int start, PyObject *globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001212 PyObject *locals, int closeit, PyCompilerFlags *flags)
1213{
Tim Petersfe2127d2001-07-16 05:37:24 +00001214 node *n = PyParser_SimpleParseFileFlags(fp, filename, start,
Neil Schemenauerc24ea082002-03-22 23:53:36 +00001215 PARSER_FLAGS(flags));
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001216 if (closeit)
1217 fclose(fp);
1218 return run_err_node(n, filename, globals, locals, flags);
1219}
1220
Guido van Rossum82598051997-03-05 00:20:32 +00001221static PyObject *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001222run_err_node(node *n, const char *filename, PyObject *globals, PyObject *locals,
Jeremy Hylton9f324e92001-03-01 22:59:14 +00001223 PyCompilerFlags *flags)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001224{
Guido van Rossuma110aa61994-08-29 12:50:44 +00001225 if (n == NULL)
1226 return NULL;
Jeremy Hylton9f324e92001-03-01 22:59:14 +00001227 return run_node(n, filename, globals, locals, flags);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001228}
1229
Guido van Rossum82598051997-03-05 00:20:32 +00001230static PyObject *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001231run_node(node *n, const char *filename, PyObject *globals, PyObject *locals,
Jeremy Hylton9f324e92001-03-01 22:59:14 +00001232 PyCompilerFlags *flags)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001233{
Guido van Rossum82598051997-03-05 00:20:32 +00001234 PyCodeObject *co;
1235 PyObject *v;
Jeremy Hylton9f324e92001-03-01 22:59:14 +00001236 co = PyNode_CompileFlags(n, filename, flags);
Guido van Rossum82598051997-03-05 00:20:32 +00001237 PyNode_Free(n);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001238 if (co == NULL)
1239 return NULL;
Guido van Rossum82598051997-03-05 00:20:32 +00001240 v = PyEval_EvalCode(co, globals, locals);
1241 Py_DECREF(co);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001242 return v;
1243}
1244
Guido van Rossum82598051997-03-05 00:20:32 +00001245static PyObject *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001246run_pyc_file(FILE *fp, const char *filename, PyObject *globals, PyObject *locals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001247 PyCompilerFlags *flags)
Guido van Rossumfdef2711994-09-14 13:31:04 +00001248{
Guido van Rossum82598051997-03-05 00:20:32 +00001249 PyCodeObject *co;
1250 PyObject *v;
Guido van Rossumfdef2711994-09-14 13:31:04 +00001251 long magic;
Fred Drakee8de31c2000-08-31 05:38:39 +00001252 long PyImport_GetMagicNumber(void);
Guido van Rossumfdef2711994-09-14 13:31:04 +00001253
Guido van Rossum82598051997-03-05 00:20:32 +00001254 magic = PyMarshal_ReadLongFromFile(fp);
1255 if (magic != PyImport_GetMagicNumber()) {
1256 PyErr_SetString(PyExc_RuntimeError,
Guido van Rossumfdef2711994-09-14 13:31:04 +00001257 "Bad magic number in .pyc file");
1258 return NULL;
1259 }
Guido van Rossum82598051997-03-05 00:20:32 +00001260 (void) PyMarshal_ReadLongFromFile(fp);
Tim Petersd9b9ac82001-01-28 00:27:39 +00001261 v = PyMarshal_ReadLastObjectFromFile(fp);
Guido van Rossumfdef2711994-09-14 13:31:04 +00001262 fclose(fp);
Guido van Rossum82598051997-03-05 00:20:32 +00001263 if (v == NULL || !PyCode_Check(v)) {
1264 Py_XDECREF(v);
1265 PyErr_SetString(PyExc_RuntimeError,
Guido van Rossumfdef2711994-09-14 13:31:04 +00001266 "Bad code object in .pyc file");
1267 return NULL;
1268 }
Guido van Rossum82598051997-03-05 00:20:32 +00001269 co = (PyCodeObject *)v;
1270 v = PyEval_EvalCode(co, globals, locals);
Jeremy Hyltonb857ba22001-08-10 21:41:33 +00001271 if (v && flags)
1272 flags->cf_flags |= (co->co_flags & PyCF_MASK);
Guido van Rossum82598051997-03-05 00:20:32 +00001273 Py_DECREF(co);
Guido van Rossumfdef2711994-09-14 13:31:04 +00001274 return v;
1275}
1276
Guido van Rossum82598051997-03-05 00:20:32 +00001277PyObject *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001278Py_CompileString(const char *str, const char *filename, int start)
Guido van Rossum5b722181993-03-30 17:46:03 +00001279{
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001280 return Py_CompileStringFlags(str, filename, start, NULL);
1281}
1282
1283PyObject *
Tim Petersd08e3822003-04-17 15:24:21 +00001284Py_CompileStringFlags(const char *str, const char *filename, int start,
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001285 PyCompilerFlags *flags)
1286{
Guido van Rossum5b722181993-03-30 17:46:03 +00001287 node *n;
Guido van Rossum82598051997-03-05 00:20:32 +00001288 PyCodeObject *co;
Thomas Heller6b17abf2002-07-09 09:23:27 +00001289
1290 n = PyParser_SimpleParseStringFlagsFilename(str, filename, start,
1291 PARSER_FLAGS(flags));
Guido van Rossuma110aa61994-08-29 12:50:44 +00001292 if (n == NULL)
Guido van Rossum5b722181993-03-30 17:46:03 +00001293 return NULL;
Jeremy Hylton673a4fd2001-03-26 19:53:38 +00001294 co = PyNode_CompileFlags(n, filename, flags);
Guido van Rossum82598051997-03-05 00:20:32 +00001295 PyNode_Free(n);
1296 return (PyObject *)co;
Guido van Rossum5b722181993-03-30 17:46:03 +00001297}
1298
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001299struct symtable *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001300Py_SymtableString(const char *str, const char *filename, int start)
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001301{
1302 node *n;
1303 struct symtable *st;
Thomas Heller6b17abf2002-07-09 09:23:27 +00001304 n = PyParser_SimpleParseStringFlagsFilename(str, filename,
1305 start, 0);
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001306 if (n == NULL)
1307 return NULL;
1308 st = PyNode_CompileSymtable(n, filename);
1309 PyNode_Free(n);
1310 return st;
1311}
1312
Guido van Rossuma110aa61994-08-29 12:50:44 +00001313/* Simplified interface to parsefile -- return node or set exception */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001314
Guido van Rossuma110aa61994-08-29 12:50:44 +00001315node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001316PyParser_SimpleParseFileFlags(FILE *fp, const char *filename, int start, int flags)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001317{
Guido van Rossuma110aa61994-08-29 12:50:44 +00001318 node *n;
1319 perrdetail err;
Tim Petersfe2127d2001-07-16 05:37:24 +00001320 n = PyParser_ParseFileFlags(fp, filename, &_PyParser_Grammar, start,
1321 (char *)0, (char *)0, &err, flags);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001322 if (n == NULL)
1323 err_input(&err);
1324 return n;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001325}
1326
Tim Petersfe2127d2001-07-16 05:37:24 +00001327node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001328PyParser_SimpleParseFile(FILE *fp, const char *filename, int start)
Tim Petersfe2127d2001-07-16 05:37:24 +00001329{
1330 return PyParser_SimpleParseFileFlags(fp, filename, start, 0);
1331}
1332
Guido van Rossuma110aa61994-08-29 12:50:44 +00001333/* Simplified interface to parsestring -- return node or set exception */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001334
Guido van Rossuma110aa61994-08-29 12:50:44 +00001335node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001336PyParser_SimpleParseStringFlags(const char *str, int start, int flags)
Tim Petersfe2127d2001-07-16 05:37:24 +00001337{
1338 node *n;
1339 perrdetail err;
1340 n = PyParser_ParseStringFlags(str, &_PyParser_Grammar, start, &err,
1341 flags);
1342 if (n == NULL)
1343 err_input(&err);
1344 return n;
1345}
1346
1347node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001348PyParser_SimpleParseString(const char *str, int start)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001349{
Tim Petersfe2127d2001-07-16 05:37:24 +00001350 return PyParser_SimpleParseStringFlags(str, start, 0);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001351}
1352
Thomas Heller6b17abf2002-07-09 09:23:27 +00001353node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001354PyParser_SimpleParseStringFlagsFilename(const char *str, const char *filename,
Thomas Heller6b17abf2002-07-09 09:23:27 +00001355 int start, int flags)
1356{
1357 node *n;
1358 perrdetail err;
1359
Tim Petersd08e3822003-04-17 15:24:21 +00001360 n = PyParser_ParseStringFlagsFilename(str, filename,
Thomas Heller6b17abf2002-07-09 09:23:27 +00001361 &_PyParser_Grammar,
1362 start, &err, flags);
1363 if (n == NULL)
1364 err_input(&err);
1365 return n;
1366}
1367
1368node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001369PyParser_SimpleParseStringFilename(const char *str, const char *filename, int start)
Thomas Heller6b17abf2002-07-09 09:23:27 +00001370{
1371 return PyParser_SimpleParseStringFlagsFilename(str, filename,
1372 start, 0);
1373}
1374
Guido van Rossum66ebd912003-04-17 16:02:26 +00001375/* May want to move a more generalized form of this to parsetok.c or
1376 even parser modules. */
1377
1378void
1379PyParser_SetError(perrdetail *err)
1380{
1381 err_input(err);
1382}
1383
Guido van Rossuma110aa61994-08-29 12:50:44 +00001384/* Set the error appropriate to the given input error code (see errcode.h) */
1385
1386static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001387err_input(perrdetail *err)
Guido van Rossuma110aa61994-08-29 12:50:44 +00001388{
Fred Drake85f36392000-07-11 17:53:00 +00001389 PyObject *v, *w, *errtype;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001390 PyObject* u = NULL;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001391 char *msg = NULL;
Fred Drake85f36392000-07-11 17:53:00 +00001392 errtype = PyExc_SyntaxError;
Guido van Rossum82598051997-03-05 00:20:32 +00001393 v = Py_BuildValue("(ziiz)", err->filename,
Guido van Rossuma110aa61994-08-29 12:50:44 +00001394 err->lineno, err->offset, err->text);
1395 if (err->text != NULL) {
Guido van Rossumb18618d2000-05-03 23:44:39 +00001396 PyMem_DEL(err->text);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001397 err->text = NULL;
1398 }
1399 switch (err->error) {
1400 case E_SYNTAX:
Fred Drake85f36392000-07-11 17:53:00 +00001401 errtype = PyExc_IndentationError;
1402 if (err->expected == INDENT)
1403 msg = "expected an indented block";
1404 else if (err->token == INDENT)
1405 msg = "unexpected indent";
1406 else if (err->token == DEDENT)
1407 msg = "unexpected unindent";
1408 else {
1409 errtype = PyExc_SyntaxError;
1410 msg = "invalid syntax";
1411 }
Guido van Rossuma110aa61994-08-29 12:50:44 +00001412 break;
1413 case E_TOKEN:
1414 msg = "invalid token";
Guido van Rossuma110aa61994-08-29 12:50:44 +00001415 break;
Skip Montanaro118ec702002-08-15 01:20:16 +00001416 case E_EOFS:
1417 msg = "EOF while scanning triple-quoted string";
1418 break;
1419 case E_EOLS:
1420 msg = "EOL while scanning single-quoted string";
1421 break;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001422 case E_INTR:
Guido van Rossum82598051997-03-05 00:20:32 +00001423 PyErr_SetNone(PyExc_KeyboardInterrupt);
Barry Warsawc80baa31999-01-27 16:39:40 +00001424 Py_XDECREF(v);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001425 return;
1426 case E_NOMEM:
Guido van Rossum82598051997-03-05 00:20:32 +00001427 PyErr_NoMemory();
Barry Warsawc80baa31999-01-27 16:39:40 +00001428 Py_XDECREF(v);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001429 return;
1430 case E_EOF:
1431 msg = "unexpected EOF while parsing";
1432 break;
Fred Drake85f36392000-07-11 17:53:00 +00001433 case E_TABSPACE:
1434 errtype = PyExc_TabError;
Guido van Rossum560e8ad1998-04-10 19:43:42 +00001435 msg = "inconsistent use of tabs and spaces in indentation";
1436 break;
Jeremy Hylton94988062000-06-20 19:10:44 +00001437 case E_OVERFLOW:
1438 msg = "expression too long";
1439 break;
Fred Drake85f36392000-07-11 17:53:00 +00001440 case E_DEDENT:
1441 errtype = PyExc_IndentationError;
1442 msg = "unindent does not match any outer indentation level";
1443 break;
1444 case E_TOODEEP:
1445 errtype = PyExc_IndentationError;
1446 msg = "too many levels of indentation";
1447 break;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001448 case E_DECODE: { /* XXX */
1449 PyThreadState* tstate = PyThreadState_Get();
1450 PyObject* value = tstate->curexc_value;
1451 if (value != NULL) {
1452 u = PyObject_Repr(value);
1453 if (u != NULL) {
1454 msg = PyString_AsString(u);
1455 break;
1456 }
1457 }
1458 }
Guido van Rossuma110aa61994-08-29 12:50:44 +00001459 default:
1460 fprintf(stderr, "error=%d\n", err->error);
1461 msg = "unknown parsing error";
1462 break;
1463 }
Guido van Rossum82598051997-03-05 00:20:32 +00001464 w = Py_BuildValue("(sO)", msg, v);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001465 Py_XDECREF(u);
Guido van Rossum41318302001-03-23 04:01:07 +00001466 Py_XDECREF(v);
Fred Drake85f36392000-07-11 17:53:00 +00001467 PyErr_SetObject(errtype, w);
Guido van Rossum82598051997-03-05 00:20:32 +00001468 Py_XDECREF(w);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001469}
1470
1471/* Print fatal error message and abort */
1472
1473void
Tim Peters7c321a82002-07-09 02:57:01 +00001474Py_FatalError(const char *msg)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001475{
Guido van Rossum83dd6c31994-09-29 09:38:33 +00001476 fprintf(stderr, "Fatal Python error: %s\n", msg);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001477#ifdef MS_WINDOWS
Guido van Rossum23c94461997-05-22 20:21:30 +00001478 OutputDebugString("Fatal Python error: ");
Guido van Rossuma44823b1995-03-14 15:01:17 +00001479 OutputDebugString(msg);
1480 OutputDebugString("\n");
Guido van Rossum0ba35361998-08-13 13:33:16 +00001481#ifdef _DEBUG
1482 DebugBreak();
Guido van Rossuma44823b1995-03-14 15:01:17 +00001483#endif
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001484#endif /* MS_WINDOWS */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001485 abort();
1486}
1487
1488/* Clean up and exit */
1489
Guido van Rossuma110aa61994-08-29 12:50:44 +00001490#ifdef WITH_THREAD
Guido van Rossum49b56061998-10-01 20:42:43 +00001491#include "pythread.h"
Guido van Rossum82598051997-03-05 00:20:32 +00001492int _PyThread_Started = 0; /* Set by threadmodule.c and maybe others */
Guido van Rossumf9f2e821992-08-17 08:59:08 +00001493#endif
1494
Guido van Rossum2dcfc961998-10-01 16:01:57 +00001495#define NEXITFUNCS 32
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001496static void (*exitfuncs[NEXITFUNCS])(void);
Guido van Rossum1662dd51994-09-07 14:38:28 +00001497static int nexitfuncs = 0;
1498
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001499int Py_AtExit(void (*func)(void))
Guido van Rossum1662dd51994-09-07 14:38:28 +00001500{
1501 if (nexitfuncs >= NEXITFUNCS)
1502 return -1;
1503 exitfuncs[nexitfuncs++] = func;
1504 return 0;
1505}
1506
Guido van Rossumcc283f51997-08-05 02:22:03 +00001507static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001508call_sys_exitfunc(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001509{
Guido van Rossum82598051997-03-05 00:20:32 +00001510 PyObject *exitfunc = PySys_GetObject("exitfunc");
Guido van Rossum59bff391992-09-03 20:28:00 +00001511
1512 if (exitfunc) {
Fred Drake6a12d8d2001-03-23 17:34:02 +00001513 PyObject *res;
Guido van Rossum82598051997-03-05 00:20:32 +00001514 Py_INCREF(exitfunc);
1515 PySys_SetObject("exitfunc", (PyObject *)NULL);
1516 res = PyEval_CallObject(exitfunc, (PyObject *)NULL);
Guido van Rossum59bff391992-09-03 20:28:00 +00001517 if (res == NULL) {
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001518 if (!PyErr_ExceptionMatches(PyExc_SystemExit)) {
1519 PySys_WriteStderr("Error in sys.exitfunc:\n");
1520 }
Guido van Rossum82598051997-03-05 00:20:32 +00001521 PyErr_Print();
Guido van Rossum59bff391992-09-03 20:28:00 +00001522 }
Guido van Rossum82598051997-03-05 00:20:32 +00001523 Py_DECREF(exitfunc);
Guido van Rossum59bff391992-09-03 20:28:00 +00001524 }
1525
Guido van Rossum0829c751998-02-28 04:31:39 +00001526 if (Py_FlushLine())
1527 PyErr_Clear();
Guido van Rossumcc283f51997-08-05 02:22:03 +00001528}
Guido van Rossum1662dd51994-09-07 14:38:28 +00001529
Guido van Rossumcc283f51997-08-05 02:22:03 +00001530static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001531call_ll_exitfuncs(void)
Guido van Rossumcc283f51997-08-05 02:22:03 +00001532{
Guido van Rossum1662dd51994-09-07 14:38:28 +00001533 while (nexitfuncs > 0)
1534 (*exitfuncs[--nexitfuncs])();
Guido van Rossum25ce5661997-08-02 03:10:38 +00001535
1536 fflush(stdout);
1537 fflush(stderr);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001538}
1539
1540void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001541Py_Exit(int sts)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001542{
Guido van Rossumcc283f51997-08-05 02:22:03 +00001543 Py_Finalize();
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001544
Jack Jansen66a89771995-10-27 13:22:14 +00001545#ifdef macintosh
1546 PyMac_Exit(sts);
1547#else
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001548 exit(sts);
Jack Jansen66a89771995-10-27 13:22:14 +00001549#endif
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001550}
1551
Guido van Rossumf1dc5661993-07-05 10:31:29 +00001552static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001553initsigs(void)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001554{
Guido van Rossuma110aa61994-08-29 12:50:44 +00001555#ifdef HAVE_SIGNAL_H
1556#ifdef SIGPIPE
1557 signal(SIGPIPE, SIG_IGN);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001558#endif
Guido van Rossum70d893a2001-08-16 08:21:42 +00001559#ifdef SIGXFZ
1560 signal(SIGXFZ, SIG_IGN);
1561#endif
Jeremy Hylton1b0bf9b2002-04-23 20:31:01 +00001562#ifdef SIGXFSZ
1563 signal(SIGXFSZ, SIG_IGN);
1564#endif
Guido van Rossuma110aa61994-08-29 12:50:44 +00001565#endif /* HAVE_SIGNAL_H */
Guido van Rossum82598051997-03-05 00:20:32 +00001566 PyOS_InitInterrupts(); /* May imply initsignal() */
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001567}
1568
Guido van Rossum7433b121997-02-14 19:45:36 +00001569
1570/*
1571 * The file descriptor fd is considered ``interactive'' if either
1572 * a) isatty(fd) is TRUE, or
1573 * b) the -i flag was given, and the filename associated with
1574 * the descriptor is NULL or "<stdin>" or "???".
1575 */
1576int
Martin v. Löwis95292d62002-12-11 14:04:59 +00001577Py_FdIsInteractive(FILE *fp, const char *filename)
Guido van Rossum7433b121997-02-14 19:45:36 +00001578{
1579 if (isatty((int)fileno(fp)))
1580 return 1;
1581 if (!Py_InteractiveFlag)
1582 return 0;
1583 return (filename == NULL) ||
1584 (strcmp(filename, "<stdin>") == 0) ||
1585 (strcmp(filename, "???") == 0);
1586}
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001587
1588
Tim Petersd08e3822003-04-17 15:24:21 +00001589#if defined(USE_STACKCHECK)
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001590#if defined(WIN32) && defined(_MSC_VER)
1591
1592/* Stack checking for Microsoft C */
1593
1594#include <malloc.h>
1595#include <excpt.h>
1596
Fred Drakee8de31c2000-08-31 05:38:39 +00001597/*
1598 * Return non-zero when we run out of memory on the stack; zero otherwise.
1599 */
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001600int
Fred Drake399739f2000-08-31 05:52:44 +00001601PyOS_CheckStack(void)
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001602{
1603 __try {
Tim Peters92e4dd82002-10-05 01:47:34 +00001604 /* alloca throws a stack overflow exception if there's
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001605 not enough space left on the stack */
Tim Peters92e4dd82002-10-05 01:47:34 +00001606 alloca(PYOS_STACK_MARGIN * sizeof(void*));
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001607 return 0;
1608 } __except (EXCEPTION_EXECUTE_HANDLER) {
1609 /* just ignore all errors */
1610 }
1611 return 1;
1612}
1613
1614#endif /* WIN32 && _MSC_VER */
1615
1616/* Alternate implementations can be added here... */
1617
1618#endif /* USE_STACKCHECK */
Guido van Rossum6f256182000-09-16 16:32:19 +00001619
1620
1621/* Wrappers around sigaction() or signal(). */
1622
1623PyOS_sighandler_t
1624PyOS_getsig(int sig)
1625{
1626#ifdef HAVE_SIGACTION
1627 struct sigaction context;
Barry Warsawafeb2a42001-11-13 23:08:26 +00001628 /* Initialize context.sa_handler to SIG_ERR which makes about as
1629 * much sense as anything else. It should get overwritten if
1630 * sigaction actually succeeds and otherwise we avoid an
1631 * uninitialized memory read.
1632 */
1633 context.sa_handler = SIG_ERR;
Guido van Rossum6f256182000-09-16 16:32:19 +00001634 sigaction(sig, NULL, &context);
1635 return context.sa_handler;
1636#else
1637 PyOS_sighandler_t handler;
1638 handler = signal(sig, SIG_IGN);
1639 signal(sig, handler);
1640 return handler;
1641#endif
1642}
1643
1644PyOS_sighandler_t
1645PyOS_setsig(int sig, PyOS_sighandler_t handler)
1646{
1647#ifdef HAVE_SIGACTION
1648 struct sigaction context;
1649 PyOS_sighandler_t oldhandler;
Barry Warsawafeb2a42001-11-13 23:08:26 +00001650 /* Initialize context.sa_handler to SIG_ERR which makes about as
1651 * much sense as anything else. It should get overwritten if
1652 * sigaction actually succeeds and otherwise we avoid an
1653 * uninitialized memory read.
1654 */
1655 context.sa_handler = SIG_ERR;
Guido van Rossum6f256182000-09-16 16:32:19 +00001656 sigaction(sig, NULL, &context);
1657 oldhandler = context.sa_handler;
1658 context.sa_handler = handler;
1659 sigaction(sig, &context, NULL);
1660 return oldhandler;
1661#else
1662 return signal(sig, handler);
1663#endif
1664}