| 
									
										
										
										
											1992-08-04 12:41:02 +00:00
										 |  |  | /***********************************************************
 | 
					
						
							| 
									
										
										
										
											1995-01-04 19:12:13 +00:00
										 |  |  | Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, | 
					
						
							|  |  |  | The Netherlands. | 
					
						
							| 
									
										
										
										
											1992-08-04 12:41:02 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |                         All Rights Reserved | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-10-25 14:44:06 +00:00
										 |  |  | Permission to use, copy, modify, and distribute this software and its | 
					
						
							|  |  |  | documentation for any purpose and without fee is hereby granted, | 
					
						
							| 
									
										
										
										
											1992-08-04 12:41:02 +00:00
										 |  |  | provided that the above copyright notice appear in all copies and that | 
					
						
							| 
									
										
										
										
											1996-10-25 14:44:06 +00:00
										 |  |  | both that copyright notice and this permission notice appear in | 
					
						
							| 
									
										
										
										
											1992-08-04 12:41:02 +00:00
										 |  |  | supporting documentation, and that the names of Stichting Mathematisch | 
					
						
							| 
									
										
										
										
											1996-10-25 14:44:06 +00:00
										 |  |  | Centrum or CWI or Corporation for National Research Initiatives or | 
					
						
							|  |  |  | CNRI not be used in advertising or publicity pertaining to | 
					
						
							|  |  |  | distribution of the software without specific, written prior | 
					
						
							|  |  |  | permission. | 
					
						
							| 
									
										
										
										
											1992-08-04 12:41:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-10-25 14:44:06 +00:00
										 |  |  | While CWI is the initial source for this software, a modified version | 
					
						
							|  |  |  | is made available by the Corporation for National Research Initiatives | 
					
						
							|  |  |  | (CNRI) at the Internet address ftp://ftp.python.org.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH | 
					
						
							|  |  |  | REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF | 
					
						
							|  |  |  | MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH | 
					
						
							|  |  |  | CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL | 
					
						
							|  |  |  | DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR | 
					
						
							|  |  |  | PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER | 
					
						
							|  |  |  | TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR | 
					
						
							|  |  |  | PERFORMANCE OF THIS SOFTWARE. | 
					
						
							| 
									
										
										
										
											1992-08-04 12:41:02 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | ******************************************************************/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Python interpreter top-level routines, including init/exit */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-03-05 00:20:32 +00:00
										 |  |  | #include "Python.h"
 | 
					
						
							| 
									
										
										
										
											1992-08-04 12:41:02 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include "grammar.h"
 | 
					
						
							|  |  |  | #include "node.h"
 | 
					
						
							|  |  |  | #include "parsetok.h"
 | 
					
						
							|  |  |  | #include "errcode.h"
 | 
					
						
							|  |  |  | #include "compile.h"
 | 
					
						
							| 
									
										
										
										
											1992-08-05 19:58:53 +00:00
										 |  |  | #include "eval.h"
 | 
					
						
							| 
									
										
										
										
											1994-09-14 13:31:04 +00:00
										 |  |  | #include "marshal.h"
 | 
					
						
							| 
									
										
										
										
											1992-08-04 12:41:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-12-05 23:27:02 +00:00
										 |  |  | #ifdef HAVE_UNISTD_H
 | 
					
						
							|  |  |  | #include <unistd.h>
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1994-08-29 12:50:44 +00:00
										 |  |  | #ifdef HAVE_SIGNAL_H
 | 
					
						
							| 
									
										
										
										
											1992-10-18 18:53:57 +00:00
										 |  |  | #include <signal.h>
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-09-11 23:12:24 +00:00
										 |  |  | #ifdef MS_WIN32
 | 
					
						
							| 
									
										
										
										
											1995-03-14 15:01:17 +00:00
										 |  |  | #undef BYTE
 | 
					
						
							|  |  |  | #include "windows.h"
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-03-05 00:20:32 +00:00
										 |  |  | extern char *Py_GetPath(); | 
					
						
							| 
									
										
										
										
											1992-08-04 12:41:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-03-05 00:20:32 +00:00
										 |  |  | extern grammar _PyParser_Grammar; /* From graminit.c */ | 
					
						
							| 
									
										
										
										
											1992-08-04 12:41:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1993-11-01 16:28:59 +00:00
										 |  |  | /* Forward */ | 
					
						
							| 
									
										
										
										
											1997-03-05 00:20:32 +00:00
										 |  |  | static void initmain Py_PROTO((void)); | 
					
						
							| 
									
										
										
										
											1997-08-29 22:32:42 +00:00
										 |  |  | static void initsite Py_PROTO((void)); | 
					
						
							| 
									
										
										
										
											1997-03-05 00:20:32 +00:00
										 |  |  | static PyObject *run_err_node Py_PROTO((node *n, char *filename, | 
					
						
							|  |  |  | 				   PyObject *globals, PyObject *locals)); | 
					
						
							|  |  |  | static PyObject *run_node Py_PROTO((node *n, char *filename, | 
					
						
							|  |  |  | 			       PyObject *globals, PyObject *locals)); | 
					
						
							|  |  |  | static PyObject *run_pyc_file Py_PROTO((FILE *fp, char *filename, | 
					
						
							|  |  |  | 				   PyObject *globals, PyObject *locals)); | 
					
						
							|  |  |  | static void err_input Py_PROTO((perrdetail *)); | 
					
						
							|  |  |  | static void initsigs Py_PROTO((void)); | 
					
						
							| 
									
										
										
										
											1997-08-05 02:22:03 +00:00
										 |  |  | static void call_sys_exitfunc Py_PROTO((void)); | 
					
						
							|  |  |  | static void call_ll_exitfuncs Py_PROTO((void)); | 
					
						
							| 
									
										
										
										
											1997-03-05 00:20:32 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | int Py_DebugFlag; /* Needed by parser.c */ | 
					
						
							|  |  |  | int Py_VerboseFlag; /* Needed by import.c */ | 
					
						
							| 
									
										
										
										
											1997-02-14 19:45:36 +00:00
										 |  |  | int Py_InteractiveFlag; /* Needed by Py_FdIsInteractive() below */ | 
					
						
							| 
									
										
										
										
											1997-08-29 22:32:42 +00:00
										 |  |  | int Py_NoSiteFlag; /* Suppress 'import site' */ | 
					
						
							| 
									
										
										
										
											1997-10-03 19:46:02 +00:00
										 |  |  | int Py_UseClassExceptionsFlag = 1; /* Needed by bltinmodule.c */ | 
					
						
							| 
									
										
										
										
											1998-02-06 22:27:24 +00:00
										 |  |  | int Py_FrozenFlag; /* Needed by getpath.c */ | 
					
						
							| 
									
										
										
										
											1992-08-04 12:41:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-08-02 03:10:38 +00:00
										 |  |  | static int initialized = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-08-22 04:20:13 +00:00
										 |  |  | /* API to access the initialized flag -- useful for eroteric use */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int | 
					
						
							|  |  |  | Py_IsInitialized() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	return initialized; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-08-02 03:10:38 +00:00
										 |  |  | /* Global initializations.  Can be undone by Py_Finalize().  Don't
 | 
					
						
							|  |  |  |    call this twice without an intervening Py_Finalize() call.  When | 
					
						
							|  |  |  |    initializations fail, a fatal error is issued and the function does | 
					
						
							|  |  |  |    not return.  On return, the first thread and interpreter state have | 
					
						
							|  |  |  |    been created. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    Locking: you must hold the interpreter lock while calling this. | 
					
						
							|  |  |  |    (If the lock has not yet been initialized, that's equivalent to | 
					
						
							|  |  |  |    having the lock, but you cannot use multiple threads.) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											1992-08-04 12:41:02 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | void | 
					
						
							| 
									
										
										
										
											1997-08-02 03:10:38 +00:00
										 |  |  | Py_Initialize() | 
					
						
							| 
									
										
										
										
											1992-08-04 12:41:02 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											1997-08-02 03:10:38 +00:00
										 |  |  | 	PyInterpreterState *interp; | 
					
						
							|  |  |  | 	PyThreadState *tstate; | 
					
						
							|  |  |  | 	PyObject *bimod, *sysmod; | 
					
						
							|  |  |  | 	char *p; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-08-29 22:32:42 +00:00
										 |  |  | 	if (initialized) | 
					
						
							| 
									
										
										
										
											1997-08-20 22:40:18 +00:00
										 |  |  | 		return; | 
					
						
							| 
									
										
										
										
											1997-08-29 22:32:42 +00:00
										 |  |  | 	initialized = 1; | 
					
						
							| 
									
										
										
										
											1992-08-04 12:41:02 +00:00
										 |  |  | 	 | 
					
						
							| 
									
										
										
										
											1997-08-02 03:10:38 +00:00
										 |  |  | 	if ((p = getenv("PYTHONDEBUG")) && *p != '\0') | 
					
						
							|  |  |  | 		Py_DebugFlag = 1; | 
					
						
							|  |  |  | 	if ((p = getenv("PYTHONVERBOSE")) && *p != '\0') | 
					
						
							|  |  |  | 		Py_VerboseFlag = 1; | 
					
						
							| 
									
										
										
										
											1998-10-07 14:50:42 +00:00
										 |  |  | 	if ((p = getenv("PYTHONOPTIMIZE")) && *p != '\0') | 
					
						
							|  |  |  | 		Py_OptimizeFlag = 1; | 
					
						
							| 
									
										
										
										
											1997-08-02 03:10:38 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	interp = PyInterpreterState_New(); | 
					
						
							|  |  |  | 	if (interp == NULL) | 
					
						
							|  |  |  | 		Py_FatalError("Py_Initialize: can't make first interpreter"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	tstate = PyThreadState_New(interp); | 
					
						
							|  |  |  | 	if (tstate == NULL) | 
					
						
							|  |  |  | 		Py_FatalError("Py_Initialize: can't make first thread"); | 
					
						
							|  |  |  | 	(void) PyThreadState_Swap(tstate); | 
					
						
							| 
									
										
										
										
											1992-08-04 12:41:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-08-02 03:10:38 +00:00
										 |  |  | 	interp->modules = PyDict_New(); | 
					
						
							|  |  |  | 	if (interp->modules == NULL) | 
					
						
							|  |  |  | 		Py_FatalError("Py_Initialize: can't make modules dictionary"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-08-29 22:07:17 +00:00
										 |  |  | 	bimod = _PyBuiltin_Init_1(); | 
					
						
							| 
									
										
										
										
											1997-08-02 03:10:38 +00:00
										 |  |  | 	if (bimod == NULL) | 
					
						
							|  |  |  | 		Py_FatalError("Py_Initialize: can't initialize __builtin__"); | 
					
						
							| 
									
										
										
										
											1997-11-04 19:36:18 +00:00
										 |  |  | 	interp->builtins = PyModule_GetDict(bimod); | 
					
						
							|  |  |  | 	Py_INCREF(interp->builtins); | 
					
						
							| 
									
										
										
										
											1997-08-02 03:10:38 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	sysmod = _PySys_Init(); | 
					
						
							|  |  |  | 	if (sysmod == NULL) | 
					
						
							|  |  |  | 		Py_FatalError("Py_Initialize: can't initialize sys"); | 
					
						
							|  |  |  | 	interp->sysdict = PyModule_GetDict(sysmod); | 
					
						
							|  |  |  | 	Py_INCREF(interp->sysdict); | 
					
						
							|  |  |  | 	_PyImport_FixupExtension("sys", "sys"); | 
					
						
							| 
									
										
										
										
											1997-03-05 00:20:32 +00:00
										 |  |  | 	PySys_SetPath(Py_GetPath()); | 
					
						
							| 
									
										
										
										
											1997-08-02 03:10:38 +00:00
										 |  |  | 	PyDict_SetItemString(interp->sysdict, "modules", | 
					
						
							|  |  |  | 			     interp->modules); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-08-29 22:07:17 +00:00
										 |  |  | 	/* phase 2 of builtins */ | 
					
						
							| 
									
										
										
										
											1997-11-04 19:36:18 +00:00
										 |  |  | 	_PyBuiltin_Init_2(interp->builtins); | 
					
						
							| 
									
										
										
										
											1997-09-18 16:42:02 +00:00
										 |  |  | 	_PyImport_FixupExtension("__builtin__", "__builtin__"); | 
					
						
							| 
									
										
										
										
											1997-08-29 22:07:17 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-08-02 03:10:38 +00:00
										 |  |  | 	_PyImport_Init(); | 
					
						
							| 
									
										
										
										
											1992-10-18 18:53:57 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	initsigs(); /* Signal handling stuff, including initintr() */ | 
					
						
							| 
									
										
										
										
											1995-01-09 17:53:26 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-08-02 03:10:38 +00:00
										 |  |  | 	initmain(); /* Module __main__ */ | 
					
						
							| 
									
										
										
										
											1997-08-29 22:32:42 +00:00
										 |  |  | 	if (!Py_NoSiteFlag) | 
					
						
							|  |  |  | 		initsite(); /* Module site */ | 
					
						
							| 
									
										
										
										
											1995-01-09 17:53:26 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-08-02 03:10:38 +00:00
										 |  |  | /* Undo the effect of Py_Initialize().
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    Beware: if multiple interpreter and/or thread states exist, these | 
					
						
							|  |  |  |    are not wiped out; only the current thread and interpreter state | 
					
						
							|  |  |  |    are deleted.  But since everything else is deleted, those other | 
					
						
							|  |  |  |    interpreter and thread states should no longer be used. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    (XXX We should do better, e.g. wipe out all interpreters and | 
					
						
							|  |  |  |    threads.) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    Locking: as above. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											1997-05-05 20:56:21 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | void | 
					
						
							| 
									
										
										
										
											1997-08-02 03:10:38 +00:00
										 |  |  | Py_Finalize() | 
					
						
							| 
									
										
										
										
											1997-05-05 20:56:21 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											1997-08-02 03:10:38 +00:00
										 |  |  | 	PyInterpreterState *interp; | 
					
						
							| 
									
										
										
										
											1997-05-05 20:56:21 +00:00
										 |  |  | 	PyThreadState *tstate; | 
					
						
							| 
									
										
										
										
											1997-08-02 03:10:38 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-08-29 22:32:42 +00:00
										 |  |  | 	if (!initialized) | 
					
						
							| 
									
										
										
										
											1997-08-20 22:40:18 +00:00
										 |  |  | 		return; | 
					
						
							| 
									
										
										
										
											1997-08-29 22:32:42 +00:00
										 |  |  | 	initialized = 0; | 
					
						
							| 
									
										
										
										
											1997-08-02 03:10:38 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-01-19 22:00:38 +00:00
										 |  |  | 	call_sys_exitfunc(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-11-03 21:58:47 +00:00
										 |  |  | 	/* Get current thread state and interpreter pointer */ | 
					
						
							| 
									
										
										
										
											1997-08-02 03:10:38 +00:00
										 |  |  | 	tstate = PyThreadState_Get(); | 
					
						
							|  |  |  | 	interp = tstate->interp; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-11-03 21:58:47 +00:00
										 |  |  | 	/* Disable signal handling */ | 
					
						
							|  |  |  | 	PyOS_FiniInterrupts(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* Destroy PyExc_MemoryErrorInst */ | 
					
						
							|  |  |  | 	_PyBuiltin_Fini_1(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* Destroy all modules */ | 
					
						
							| 
									
										
										
										
											1997-08-02 03:10:38 +00:00
										 |  |  | 	PyImport_Cleanup(); | 
					
						
							| 
									
										
										
										
											1997-11-03 21:58:47 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-12-08 23:43:45 +00:00
										 |  |  | 	/* Destroy the database used by _PyImport_{Fixup,Find}Extension */ | 
					
						
							|  |  |  | 	_PyImport_Fini(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* Debugging stuff */ | 
					
						
							|  |  |  | #ifdef COUNT_ALLOCS
 | 
					
						
							|  |  |  | 	dump_counts(); | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifdef Py_REF_DEBUG
 | 
					
						
							|  |  |  | 	fprintf(stderr, "[%ld refs]\n", _Py_RefTotal); | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifdef Py_TRACE_REFS
 | 
					
						
							|  |  |  | 	if (_Py_AskYesNo("Print left references?")) { | 
					
						
							|  |  |  | 		_Py_PrintReferences(stderr); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | #endif /* Py_TRACE_REFS */
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-11-04 19:36:18 +00:00
										 |  |  | 	/* Delete current thread */ | 
					
						
							| 
									
										
										
										
											1997-08-02 03:10:38 +00:00
										 |  |  | 	PyInterpreterState_Clear(interp); | 
					
						
							|  |  |  | 	PyThreadState_Swap(NULL); | 
					
						
							|  |  |  | 	PyInterpreterState_Delete(interp); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-08-29 22:07:17 +00:00
										 |  |  | 	/* Now we decref the exception classes.  After this point nothing
 | 
					
						
							|  |  |  | 	   can raise an exception.  That's okay, because each Fini() method | 
					
						
							|  |  |  | 	   below has been checked to make sure no exceptions are ever | 
					
						
							|  |  |  | 	   raised. | 
					
						
							|  |  |  | 	*/ | 
					
						
							|  |  |  | 	_PyBuiltin_Fini_2(); | 
					
						
							| 
									
										
										
										
											1997-08-05 02:22:03 +00:00
										 |  |  | 	PyMethod_Fini(); | 
					
						
							|  |  |  | 	PyFrame_Fini(); | 
					
						
							|  |  |  | 	PyCFunction_Fini(); | 
					
						
							|  |  |  | 	PyTuple_Fini(); | 
					
						
							| 
									
										
										
										
											1997-08-02 03:10:38 +00:00
										 |  |  | 	PyString_Fini(); | 
					
						
							| 
									
										
										
										
											1997-08-05 02:22:03 +00:00
										 |  |  | 	PyInt_Fini(); | 
					
						
							|  |  |  | 	PyFloat_Fini(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* XXX Still allocated:
 | 
					
						
							|  |  |  | 	   - various static ad-hoc pointers to interned strings | 
					
						
							|  |  |  | 	   - int and float free list blocks | 
					
						
							|  |  |  | 	   - whatever various modules and libraries allocate | 
					
						
							|  |  |  | 	*/ | 
					
						
							| 
									
										
										
										
											1997-08-02 03:10:38 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	PyGrammar_RemoveAccelerators(&_PyParser_Grammar); | 
					
						
							| 
									
										
										
										
											1997-08-05 02:22:03 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	call_ll_exitfuncs(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifdef Py_TRACE_REFS
 | 
					
						
							|  |  |  | 	_Py_ResetReferences(); | 
					
						
							|  |  |  | #endif /* Py_TRACE_REFS */
 | 
					
						
							| 
									
										
										
										
											1997-08-02 03:10:38 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Create and initialize a new interpreter and thread, and return the
 | 
					
						
							|  |  |  |    new thread.  This requires that Py_Initialize() has been called | 
					
						
							|  |  |  |    first. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    Unsuccessful initialization yields a NULL pointer.  Note that *no* | 
					
						
							|  |  |  |    exception information is available even in this case -- the | 
					
						
							|  |  |  |    exception information is held in the thread, and there is no | 
					
						
							|  |  |  |    thread. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    Locking: as above. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | PyThreadState * | 
					
						
							|  |  |  | Py_NewInterpreter() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											1997-05-05 20:56:21 +00:00
										 |  |  | 	PyInterpreterState *interp; | 
					
						
							| 
									
										
										
										
											1997-08-02 03:10:38 +00:00
										 |  |  | 	PyThreadState *tstate, *save_tstate; | 
					
						
							|  |  |  | 	PyObject *bimod, *sysmod; | 
					
						
							| 
									
										
										
										
											1997-07-19 19:17:22 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-08-02 03:10:38 +00:00
										 |  |  | 	if (!initialized) | 
					
						
							|  |  |  | 		Py_FatalError("Py_NewInterpreter: call Py_Initialize first"); | 
					
						
							| 
									
										
										
										
											1997-07-19 19:17:22 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-05-05 20:56:21 +00:00
										 |  |  | 	interp = PyInterpreterState_New(); | 
					
						
							|  |  |  | 	if (interp == NULL) | 
					
						
							| 
									
										
										
										
											1997-08-02 03:10:38 +00:00
										 |  |  | 		return NULL; | 
					
						
							| 
									
										
										
										
											1997-07-19 19:17:22 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-05-05 20:56:21 +00:00
										 |  |  | 	tstate = PyThreadState_New(interp); | 
					
						
							| 
									
										
										
										
											1997-08-02 03:10:38 +00:00
										 |  |  | 	if (tstate == NULL) { | 
					
						
							|  |  |  | 		PyInterpreterState_Delete(interp); | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											1997-05-05 20:56:21 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-08-02 03:10:38 +00:00
										 |  |  | 	save_tstate = PyThreadState_Swap(tstate); | 
					
						
							| 
									
										
										
										
											1997-05-05 20:56:21 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-08-02 03:10:38 +00:00
										 |  |  | 	/* XXX The following is lax in error checking */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	interp->modules = PyDict_New(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	bimod = _PyImport_FindExtension("__builtin__", "__builtin__"); | 
					
						
							|  |  |  | 	if (bimod != NULL) { | 
					
						
							| 
									
										
										
										
											1997-11-04 19:36:18 +00:00
										 |  |  | 		interp->builtins = PyModule_GetDict(bimod); | 
					
						
							|  |  |  | 		Py_INCREF(interp->builtins); | 
					
						
							| 
									
										
										
										
											1997-08-02 03:10:38 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	sysmod = _PyImport_FindExtension("sys", "sys"); | 
					
						
							|  |  |  | 	if (bimod != NULL && sysmod != NULL) { | 
					
						
							|  |  |  | 		interp->sysdict = PyModule_GetDict(sysmod); | 
					
						
							|  |  |  | 		Py_INCREF(interp->sysdict); | 
					
						
							|  |  |  | 		PySys_SetPath(Py_GetPath()); | 
					
						
							|  |  |  | 		PyDict_SetItemString(interp->sysdict, "modules", | 
					
						
							|  |  |  | 				     interp->modules); | 
					
						
							|  |  |  | 		initmain(); | 
					
						
							| 
									
										
										
										
											1997-08-29 22:32:42 +00:00
										 |  |  | 		if (!Py_NoSiteFlag) | 
					
						
							|  |  |  | 			initsite(); | 
					
						
							| 
									
										
										
										
											1997-08-02 03:10:38 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (!PyErr_Occurred()) | 
					
						
							|  |  |  | 		return tstate; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* Oops, it didn't work.  Undo it all. */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	PyErr_Print(); | 
					
						
							|  |  |  | 	PyThreadState_Clear(tstate); | 
					
						
							|  |  |  | 	PyThreadState_Swap(save_tstate); | 
					
						
							|  |  |  | 	PyThreadState_Delete(tstate); | 
					
						
							|  |  |  | 	PyInterpreterState_Delete(interp); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return NULL; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Delete an interpreter and its last thread.  This requires that the
 | 
					
						
							|  |  |  |    given thread state is current, that the thread has no remaining | 
					
						
							|  |  |  |    frames, and that it is its interpreter's only remaining thread. | 
					
						
							|  |  |  |    It is a fatal error to violate these constraints. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    (Py_Finalize() doesn't have these constraints -- it zaps | 
					
						
							|  |  |  |    everything, regardless.) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    Locking: as above. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void | 
					
						
							|  |  |  | Py_EndInterpreter(tstate) | 
					
						
							|  |  |  | 	PyThreadState *tstate; | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	PyInterpreterState *interp = tstate->interp; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (tstate != PyThreadState_Get()) | 
					
						
							|  |  |  | 		Py_FatalError("Py_EndInterpreter: thread is not current"); | 
					
						
							|  |  |  | 	if (tstate->frame != NULL) | 
					
						
							|  |  |  | 		Py_FatalError("Py_EndInterpreter: thread still has a frame"); | 
					
						
							|  |  |  | 	if (tstate != interp->tstate_head || tstate->next != NULL) | 
					
						
							|  |  |  | 		Py_FatalError("Py_EndInterpreter: not the last thread"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	PyImport_Cleanup(); | 
					
						
							|  |  |  | 	PyInterpreterState_Clear(interp); | 
					
						
							|  |  |  | 	PyThreadState_Swap(NULL); | 
					
						
							|  |  |  | 	PyInterpreterState_Delete(interp); | 
					
						
							| 
									
										
										
										
											1997-07-19 19:17:22 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static char *progname = "python"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void | 
					
						
							|  |  |  | Py_SetProgramName(pn) | 
					
						
							|  |  |  | 	char *pn; | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	if (pn && *pn) | 
					
						
							|  |  |  | 		progname = pn; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | char * | 
					
						
							|  |  |  | Py_GetProgramName() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	return progname; | 
					
						
							| 
									
										
										
										
											1997-05-05 20:56:21 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-02-06 22:27:24 +00:00
										 |  |  | static char *default_home = NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void | 
					
						
							|  |  |  | Py_SetPythonHome(home) | 
					
						
							|  |  |  | 	char *home; | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	default_home = home; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | char * | 
					
						
							|  |  |  | Py_GetPythonHome() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	char *home = default_home; | 
					
						
							|  |  |  | 	if (home == NULL) | 
					
						
							|  |  |  | 		home = getenv("PYTHONHOME"); | 
					
						
							|  |  |  | 	return home; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1995-01-09 17:53:26 +00:00
										 |  |  | /* Create __main__ module */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void | 
					
						
							|  |  |  | initmain() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											1997-03-05 00:20:32 +00:00
										 |  |  | 	PyObject *m, *d; | 
					
						
							|  |  |  | 	m = PyImport_AddModule("__main__"); | 
					
						
							| 
									
										
										
										
											1995-01-09 17:53:26 +00:00
										 |  |  | 	if (m == NULL) | 
					
						
							| 
									
										
										
										
											1997-03-05 00:20:32 +00:00
										 |  |  | 		Py_FatalError("can't create __main__ module"); | 
					
						
							|  |  |  | 	d = PyModule_GetDict(m); | 
					
						
							|  |  |  | 	if (PyDict_GetItemString(d, "__builtins__") == NULL) { | 
					
						
							| 
									
										
										
										
											1997-11-19 16:15:37 +00:00
										 |  |  | 		PyObject *bimod = PyImport_ImportModule("__builtin__"); | 
					
						
							|  |  |  | 		if (bimod == NULL || | 
					
						
							|  |  |  | 		    PyDict_SetItemString(d, "__builtins__", bimod) != 0) | 
					
						
							| 
									
										
										
										
											1997-03-05 00:20:32 +00:00
										 |  |  | 			Py_FatalError("can't add __builtins__ to __main__"); | 
					
						
							| 
									
										
										
										
											1995-01-09 17:53:26 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											1992-08-04 12:41:02 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-08-29 22:32:42 +00:00
										 |  |  | /* Import the site module (not into __main__ though) */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void | 
					
						
							|  |  |  | initsite() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	PyObject *m, *f; | 
					
						
							|  |  |  | 	m = PyImport_ImportModule("site"); | 
					
						
							|  |  |  | 	if (m == NULL) { | 
					
						
							|  |  |  | 		f = PySys_GetObject("stderr"); | 
					
						
							|  |  |  | 		if (Py_VerboseFlag) { | 
					
						
							|  |  |  | 			PyFile_WriteString( | 
					
						
							|  |  |  | 				"'import site' failed; traceback:\n", f); | 
					
						
							|  |  |  | 			PyErr_Print(); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		else { | 
					
						
							|  |  |  | 			PyFile_WriteString( | 
					
						
							|  |  |  | 			  "'import site' failed; use -v for traceback\n", f); | 
					
						
							|  |  |  | 			PyErr_Clear(); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	else { | 
					
						
							|  |  |  | 		Py_DECREF(m); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1992-08-04 12:41:02 +00:00
										 |  |  | /* Parse input from a file and execute it */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int | 
					
						
							| 
									
										
										
										
											1997-03-05 00:20:32 +00:00
										 |  |  | PyRun_AnyFile(fp, filename) | 
					
						
							| 
									
										
										
										
											1992-08-04 12:41:02 +00:00
										 |  |  | 	FILE *fp; | 
					
						
							|  |  |  | 	char *filename; | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	if (filename == NULL) | 
					
						
							|  |  |  | 		filename = "???"; | 
					
						
							| 
									
										
										
										
											1997-02-14 19:45:36 +00:00
										 |  |  | 	if (Py_FdIsInteractive(fp, filename)) | 
					
						
							| 
									
										
										
										
											1997-03-05 00:20:32 +00:00
										 |  |  | 		return PyRun_InteractiveLoop(fp, filename); | 
					
						
							| 
									
										
										
										
											1992-08-04 12:41:02 +00:00
										 |  |  | 	else | 
					
						
							| 
									
										
										
										
											1997-03-05 00:20:32 +00:00
										 |  |  | 		return PyRun_SimpleFile(fp, filename); | 
					
						
							| 
									
										
										
										
											1992-08-04 12:41:02 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int | 
					
						
							| 
									
										
										
										
											1997-03-05 00:20:32 +00:00
										 |  |  | PyRun_InteractiveLoop(fp, filename) | 
					
						
							| 
									
										
										
										
											1992-08-04 12:41:02 +00:00
										 |  |  | 	FILE *fp; | 
					
						
							|  |  |  | 	char *filename; | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											1997-03-05 00:20:32 +00:00
										 |  |  | 	PyObject *v; | 
					
						
							| 
									
										
										
										
											1992-08-04 12:41:02 +00:00
										 |  |  | 	int ret; | 
					
						
							| 
									
										
										
										
											1997-03-05 00:20:32 +00:00
										 |  |  | 	v = PySys_GetObject("ps1"); | 
					
						
							| 
									
										
										
										
											1992-08-04 12:41:02 +00:00
										 |  |  | 	if (v == NULL) { | 
					
						
							| 
									
										
										
										
											1997-03-05 00:20:32 +00:00
										 |  |  | 		PySys_SetObject("ps1", v = PyString_FromString(">>> ")); | 
					
						
							|  |  |  | 		Py_XDECREF(v); | 
					
						
							| 
									
										
										
										
											1992-08-04 12:41:02 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											1997-03-05 00:20:32 +00:00
										 |  |  | 	v = PySys_GetObject("ps2"); | 
					
						
							| 
									
										
										
										
											1992-08-04 12:41:02 +00:00
										 |  |  | 	if (v == NULL) { | 
					
						
							| 
									
										
										
										
											1997-03-05 00:20:32 +00:00
										 |  |  | 		PySys_SetObject("ps2", v = PyString_FromString("... ")); | 
					
						
							|  |  |  | 		Py_XDECREF(v); | 
					
						
							| 
									
										
										
										
											1992-08-04 12:41:02 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	for (;;) { | 
					
						
							| 
									
										
										
										
											1997-03-05 00:20:32 +00:00
										 |  |  | 		ret = PyRun_InteractiveOne(fp, filename); | 
					
						
							| 
									
										
										
										
											1996-05-22 16:35:33 +00:00
										 |  |  | #ifdef Py_REF_DEBUG
 | 
					
						
							| 
									
										
										
										
											1995-03-29 16:57:48 +00:00
										 |  |  | 		fprintf(stderr, "[%ld refs]\n", _Py_RefTotal); | 
					
						
							| 
									
										
										
										
											1992-08-04 12:41:02 +00:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | 		if (ret == E_EOF) | 
					
						
							|  |  |  | 			return 0; | 
					
						
							|  |  |  | 		/*
 | 
					
						
							|  |  |  | 		if (ret == E_NOMEM) | 
					
						
							|  |  |  | 			return -1; | 
					
						
							|  |  |  | 		*/ | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int | 
					
						
							| 
									
										
										
										
											1997-03-05 00:20:32 +00:00
										 |  |  | PyRun_InteractiveOne(fp, filename) | 
					
						
							| 
									
										
										
										
											1992-08-04 12:41:02 +00:00
										 |  |  | 	FILE *fp; | 
					
						
							|  |  |  | 	char *filename; | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											1997-03-05 00:20:32 +00:00
										 |  |  | 	PyObject *m, *d, *v, *w; | 
					
						
							| 
									
										
										
										
											1992-08-04 12:41:02 +00:00
										 |  |  | 	node *n; | 
					
						
							| 
									
										
										
										
											1994-08-29 12:50:44 +00:00
										 |  |  | 	perrdetail err; | 
					
						
							| 
									
										
										
										
											1997-11-25 20:58:13 +00:00
										 |  |  | 	char *ps1 = "", *ps2 = ""; | 
					
						
							| 
									
										
										
										
											1997-03-05 00:20:32 +00:00
										 |  |  | 	v = PySys_GetObject("ps1"); | 
					
						
							| 
									
										
										
										
											1997-11-25 20:58:13 +00:00
										 |  |  | 	if (v != NULL) { | 
					
						
							|  |  |  | 		v = PyObject_Str(v); | 
					
						
							|  |  |  | 		if (v == NULL) | 
					
						
							|  |  |  | 			PyErr_Clear(); | 
					
						
							|  |  |  | 		else if (PyString_Check(v)) | 
					
						
							|  |  |  | 			ps1 = PyString_AsString(v); | 
					
						
							| 
									
										
										
										
											1992-08-04 12:41:02 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											1997-11-25 20:58:13 +00:00
										 |  |  | 	w = PySys_GetObject("ps2"); | 
					
						
							|  |  |  | 	if (w != NULL) { | 
					
						
							|  |  |  | 		w = PyObject_Str(w); | 
					
						
							|  |  |  | 		if (w == NULL) | 
					
						
							|  |  |  | 			PyErr_Clear(); | 
					
						
							|  |  |  | 		else if (PyString_Check(w)) | 
					
						
							|  |  |  | 			ps2 = PyString_AsString(w); | 
					
						
							| 
									
										
										
										
											1992-08-04 12:41:02 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											1997-03-05 00:20:32 +00:00
										 |  |  | 	n = PyParser_ParseFile(fp, filename, &_PyParser_Grammar, | 
					
						
							| 
									
										
										
										
											1997-05-07 17:46:13 +00:00
										 |  |  | 			       Py_single_input, ps1, ps2, &err); | 
					
						
							| 
									
										
										
										
											1997-03-05 00:20:32 +00:00
										 |  |  | 	Py_XDECREF(v); | 
					
						
							|  |  |  | 	Py_XDECREF(w); | 
					
						
							| 
									
										
										
										
											1994-08-29 12:50:44 +00:00
										 |  |  | 	if (n == NULL) { | 
					
						
							|  |  |  | 		if (err.error == E_EOF) { | 
					
						
							|  |  |  | 			if (err.text) | 
					
						
							|  |  |  | 				free(err.text); | 
					
						
							|  |  |  | 			return E_EOF; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		err_input(&err); | 
					
						
							| 
									
										
										
										
											1997-03-05 00:20:32 +00:00
										 |  |  | 		PyErr_Print(); | 
					
						
							| 
									
										
										
										
											1994-08-29 12:50:44 +00:00
										 |  |  | 		return err.error; | 
					
						
							| 
									
										
										
										
											1992-08-04 12:41:02 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											1997-03-05 00:20:32 +00:00
										 |  |  | 	m = PyImport_AddModule("__main__"); | 
					
						
							| 
									
										
										
										
											1992-08-04 12:41:02 +00:00
										 |  |  | 	if (m == NULL) | 
					
						
							|  |  |  | 		return -1; | 
					
						
							| 
									
										
										
										
											1997-03-05 00:20:32 +00:00
										 |  |  | 	d = PyModule_GetDict(m); | 
					
						
							| 
									
										
										
										
											1992-08-04 12:41:02 +00:00
										 |  |  | 	v = run_node(n, filename, d, d); | 
					
						
							|  |  |  | 	if (v == NULL) { | 
					
						
							| 
									
										
										
										
											1997-03-05 00:20:32 +00:00
										 |  |  | 		PyErr_Print(); | 
					
						
							| 
									
										
										
										
											1992-08-04 12:41:02 +00:00
										 |  |  | 		return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											1997-03-05 00:20:32 +00:00
										 |  |  | 	Py_DECREF(v); | 
					
						
							| 
									
										
										
										
											1997-05-22 22:35:04 +00:00
										 |  |  | 	if (Py_FlushLine()) | 
					
						
							|  |  |  | 		PyErr_Clear(); | 
					
						
							| 
									
										
										
										
											1992-08-04 12:41:02 +00:00
										 |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int | 
					
						
							| 
									
										
										
										
											1997-03-05 00:20:32 +00:00
										 |  |  | PyRun_SimpleFile(fp, filename) | 
					
						
							| 
									
										
										
										
											1992-08-04 12:41:02 +00:00
										 |  |  | 	FILE *fp; | 
					
						
							|  |  |  | 	char *filename; | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											1997-03-05 00:20:32 +00:00
										 |  |  | 	PyObject *m, *d, *v; | 
					
						
							| 
									
										
										
										
											1994-09-14 13:31:04 +00:00
										 |  |  | 	char *ext; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-03-05 00:20:32 +00:00
										 |  |  | 	m = PyImport_AddModule("__main__"); | 
					
						
							| 
									
										
										
										
											1992-08-04 12:41:02 +00:00
										 |  |  | 	if (m == NULL) | 
					
						
							|  |  |  | 		return -1; | 
					
						
							| 
									
										
										
										
											1997-03-05 00:20:32 +00:00
										 |  |  | 	d = PyModule_GetDict(m); | 
					
						
							| 
									
										
										
										
											1994-09-14 13:31:04 +00:00
										 |  |  | 	ext = filename + strlen(filename) - 4; | 
					
						
							| 
									
										
										
										
											1997-04-02 05:28:38 +00:00
										 |  |  | 	if (strcmp(ext, ".pyc") == 0 || strcmp(ext, ".pyo") == 0 | 
					
						
							| 
									
										
										
										
											1995-02-13 11:44:56 +00:00
										 |  |  | #ifdef macintosh
 | 
					
						
							|  |  |  | 	/* On a mac, we also assume a pyc file for types 'PYC ' and 'APPL' */ | 
					
						
							| 
									
										
										
										
											1997-04-02 05:28:38 +00:00
										 |  |  | 	    || getfiletype(filename) == 'PYC ' | 
					
						
							|  |  |  | 	    || getfiletype(filename) == 'APPL' | 
					
						
							| 
									
										
										
										
											1995-02-13 11:44:56 +00:00
										 |  |  | #endif /* macintosh */
 | 
					
						
							| 
									
										
										
										
											1997-04-02 05:28:38 +00:00
										 |  |  | 		) { | 
					
						
							| 
									
										
										
										
											1994-09-14 13:31:04 +00:00
										 |  |  | 		/* Try to run a pyc file. First, re-open in binary */ | 
					
						
							| 
									
										
										
										
											1994-12-14 12:58:37 +00:00
										 |  |  | 		/* Don't close, done in main: fclose(fp); */ | 
					
						
							| 
									
										
										
										
											1994-09-14 13:31:04 +00:00
										 |  |  | 		if( (fp = fopen(filename, "rb")) == NULL ) { | 
					
						
							|  |  |  | 			fprintf(stderr, "python: Can't reopen .pyc file\n"); | 
					
						
							|  |  |  | 			return -1; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											1997-04-02 05:28:38 +00:00
										 |  |  | 		/* Turn on optimization if a .pyo file is given */ | 
					
						
							|  |  |  | 		if (strcmp(ext, ".pyo") == 0) | 
					
						
							|  |  |  | 			Py_OptimizeFlag = 1; | 
					
						
							| 
									
										
										
										
											1994-09-14 13:31:04 +00:00
										 |  |  | 		v = run_pyc_file(fp, filename, d, d); | 
					
						
							|  |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											1997-05-07 17:46:13 +00:00
										 |  |  | 		v = PyRun_File(fp, filename, Py_file_input, d, d); | 
					
						
							| 
									
										
										
										
											1994-09-14 13:31:04 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											1992-08-04 12:41:02 +00:00
										 |  |  | 	if (v == NULL) { | 
					
						
							| 
									
										
										
										
											1997-03-05 00:20:32 +00:00
										 |  |  | 		PyErr_Print(); | 
					
						
							| 
									
										
										
										
											1992-08-04 12:41:02 +00:00
										 |  |  | 		return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											1997-03-05 00:20:32 +00:00
										 |  |  | 	Py_DECREF(v); | 
					
						
							| 
									
										
										
										
											1997-05-22 22:35:04 +00:00
										 |  |  | 	if (Py_FlushLine()) | 
					
						
							|  |  |  | 		PyErr_Clear(); | 
					
						
							| 
									
										
										
										
											1992-08-04 12:41:02 +00:00
										 |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int | 
					
						
							| 
									
										
										
										
											1997-03-05 00:20:32 +00:00
										 |  |  | PyRun_SimpleString(command) | 
					
						
							| 
									
										
										
										
											1992-08-04 12:41:02 +00:00
										 |  |  | 	char *command; | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											1997-03-05 00:20:32 +00:00
										 |  |  | 	PyObject *m, *d, *v; | 
					
						
							|  |  |  | 	m = PyImport_AddModule("__main__"); | 
					
						
							| 
									
										
										
										
											1992-08-04 12:41:02 +00:00
										 |  |  | 	if (m == NULL) | 
					
						
							|  |  |  | 		return -1; | 
					
						
							| 
									
										
										
										
											1997-03-05 00:20:32 +00:00
										 |  |  | 	d = PyModule_GetDict(m); | 
					
						
							| 
									
										
										
										
											1997-05-07 17:46:13 +00:00
										 |  |  | 	v = PyRun_String(command, Py_file_input, d, d); | 
					
						
							| 
									
										
										
										
											1992-08-04 12:41:02 +00:00
										 |  |  | 	if (v == NULL) { | 
					
						
							| 
									
										
										
										
											1997-03-05 00:20:32 +00:00
										 |  |  | 		PyErr_Print(); | 
					
						
							| 
									
										
										
										
											1992-08-04 12:41:02 +00:00
										 |  |  | 		return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											1997-03-05 00:20:32 +00:00
										 |  |  | 	Py_DECREF(v); | 
					
						
							| 
									
										
										
										
											1997-05-22 22:35:04 +00:00
										 |  |  | 	if (Py_FlushLine()) | 
					
						
							|  |  |  | 		PyErr_Clear(); | 
					
						
							| 
									
										
										
										
											1992-08-04 12:41:02 +00:00
										 |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-08-29 22:07:17 +00:00
										 |  |  | static int | 
					
						
							|  |  |  | parse_syntax_error(err, message, filename, lineno, offset, text) | 
					
						
							|  |  |  |      PyObject* err; | 
					
						
							|  |  |  |      PyObject** message; | 
					
						
							|  |  |  |      char** filename; | 
					
						
							|  |  |  |      int* lineno; | 
					
						
							|  |  |  |      int* offset; | 
					
						
							|  |  |  |      char** text; | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	long hold; | 
					
						
							|  |  |  | 	PyObject *v; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* old style errors */ | 
					
						
							|  |  |  | 	if (PyTuple_Check(err)) | 
					
						
							|  |  |  | 		return PyArg_Parse(err, "(O(ziiz))", message, filename, | 
					
						
							|  |  |  | 				   lineno, offset, text); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* new style errors.  `err' is an instance */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (! (v = PyObject_GetAttrString(err, "msg"))) | 
					
						
							|  |  |  | 		goto finally; | 
					
						
							|  |  |  | 	*message = v; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (!(v = PyObject_GetAttrString(err, "filename"))) | 
					
						
							|  |  |  | 		goto finally; | 
					
						
							|  |  |  | 	if (v == Py_None) | 
					
						
							|  |  |  | 		*filename = NULL; | 
					
						
							|  |  |  | 	else if (! (*filename = PyString_AsString(v))) | 
					
						
							|  |  |  | 		goto finally; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	Py_DECREF(v); | 
					
						
							|  |  |  | 	if (!(v = PyObject_GetAttrString(err, "lineno"))) | 
					
						
							|  |  |  | 		goto finally; | 
					
						
							|  |  |  | 	hold = PyInt_AsLong(v); | 
					
						
							|  |  |  | 	Py_DECREF(v); | 
					
						
							|  |  |  | 	v = NULL; | 
					
						
							|  |  |  | 	if (hold < 0 && PyErr_Occurred()) | 
					
						
							|  |  |  | 		goto finally; | 
					
						
							|  |  |  | 	*lineno = (int)hold; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (!(v = PyObject_GetAttrString(err, "offset"))) | 
					
						
							|  |  |  | 		goto finally; | 
					
						
							|  |  |  | 	hold = PyInt_AsLong(v); | 
					
						
							|  |  |  | 	Py_DECREF(v); | 
					
						
							|  |  |  | 	v = NULL; | 
					
						
							|  |  |  | 	if (hold < 0 && PyErr_Occurred()) | 
					
						
							|  |  |  | 		goto finally; | 
					
						
							|  |  |  | 	*offset = (int)hold; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (!(v = PyObject_GetAttrString(err, "text"))) | 
					
						
							|  |  |  | 		goto finally; | 
					
						
							|  |  |  | 	if (v == Py_None) | 
					
						
							|  |  |  | 		*text = NULL; | 
					
						
							|  |  |  | 	else if (! (*text = PyString_AsString(v))) | 
					
						
							|  |  |  | 		goto finally; | 
					
						
							|  |  |  | 	Py_DECREF(v); | 
					
						
							|  |  |  | 	return 1; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | finally: | 
					
						
							|  |  |  | 	Py_XDECREF(v); | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1992-08-04 12:41:02 +00:00
										 |  |  | void | 
					
						
							| 
									
										
										
										
											1997-03-05 00:20:32 +00:00
										 |  |  | PyErr_Print() | 
					
						
							| 
									
										
										
										
											1998-02-06 22:27:24 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	PyErr_PrintEx(1); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void | 
					
						
							|  |  |  | PyErr_PrintEx(set_sys_last_vars) | 
					
						
							|  |  |  | 	int set_sys_last_vars; | 
					
						
							| 
									
										
										
										
											1992-08-04 12:41:02 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											1997-05-22 22:35:04 +00:00
										 |  |  | 	int err = 0; | 
					
						
							| 
									
										
										
										
											1997-03-05 00:20:32 +00:00
										 |  |  | 	PyObject *exception, *v, *tb, *f; | 
					
						
							|  |  |  | 	PyErr_Fetch(&exception, &v, &tb); | 
					
						
							| 
									
										
										
										
											1997-08-29 22:07:17 +00:00
										 |  |  | 	PyErr_NormalizeException(&exception, &v, &tb); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1995-01-02 19:04:15 +00:00
										 |  |  | 	if (exception == NULL) | 
					
						
							| 
									
										
										
										
											1997-05-23 00:19:20 +00:00
										 |  |  | 		return; | 
					
						
							| 
									
										
										
										
											1997-08-29 22:07:17 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-08-26 18:09:48 +00:00
										 |  |  | 	if (PyErr_GivenExceptionMatches(exception, PyExc_SystemExit)) { | 
					
						
							| 
									
										
										
										
											1998-02-28 04:31:39 +00:00
										 |  |  | 		if (Py_FlushLine()) | 
					
						
							|  |  |  | 			PyErr_Clear(); | 
					
						
							| 
									
										
										
										
											1997-05-22 22:35:04 +00:00
										 |  |  | 		fflush(stdout); | 
					
						
							| 
									
										
										
										
											1997-03-05 00:20:32 +00:00
										 |  |  | 		if (v == NULL || v == Py_None) | 
					
						
							|  |  |  | 			Py_Exit(0); | 
					
						
							| 
									
										
										
										
											1997-08-29 22:07:17 +00:00
										 |  |  | 		if (PyInstance_Check(v)) { | 
					
						
							|  |  |  | 			/* we expect the error code to be store in the
 | 
					
						
							|  |  |  | 			   `code' attribute | 
					
						
							|  |  |  | 			*/ | 
					
						
							|  |  |  | 			PyObject *code = PyObject_GetAttrString(v, "code"); | 
					
						
							|  |  |  | 			if (code) { | 
					
						
							|  |  |  | 				Py_DECREF(v); | 
					
						
							|  |  |  | 				v = code; | 
					
						
							| 
									
										
										
										
											1997-10-03 13:53:28 +00:00
										 |  |  | 				if (v == Py_None) | 
					
						
							|  |  |  | 					Py_Exit(0); | 
					
						
							| 
									
										
										
										
											1997-08-29 22:07:17 +00:00
										 |  |  | 			} | 
					
						
							|  |  |  | 			/* if we failed to dig out the "code" attribute,
 | 
					
						
							|  |  |  | 			   then just let the else clause below print the | 
					
						
							|  |  |  | 			   error | 
					
						
							|  |  |  | 			*/ | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											1997-03-05 00:20:32 +00:00
										 |  |  | 		if (PyInt_Check(v)) | 
					
						
							|  |  |  | 			Py_Exit((int)PyInt_AsLong(v)); | 
					
						
							| 
									
										
										
										
											1992-08-04 12:41:02 +00:00
										 |  |  | 		else { | 
					
						
							| 
									
										
										
										
											1992-09-25 21:59:05 +00:00
										 |  |  | 			/* OK to use real stderr here */ | 
					
						
							| 
									
										
										
										
											1997-03-05 00:20:32 +00:00
										 |  |  | 			PyObject_Print(v, stderr, Py_PRINT_RAW); | 
					
						
							| 
									
										
										
										
											1992-08-04 12:41:02 +00:00
										 |  |  | 			fprintf(stderr, "\n"); | 
					
						
							| 
									
										
										
										
											1997-03-05 00:20:32 +00:00
										 |  |  | 			Py_Exit(1); | 
					
						
							| 
									
										
										
										
											1992-08-04 12:41:02 +00:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											1998-02-06 22:27:24 +00:00
										 |  |  | 	if (set_sys_last_vars) { | 
					
						
							|  |  |  | 		PySys_SetObject("last_type", exception); | 
					
						
							|  |  |  | 		PySys_SetObject("last_value", v); | 
					
						
							|  |  |  | 		PySys_SetObject("last_traceback", tb); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											1997-03-05 00:20:32 +00:00
										 |  |  | 	f = PySys_GetObject("stderr"); | 
					
						
							| 
									
										
										
										
											1992-09-25 21:59:05 +00:00
										 |  |  | 	if (f == NULL) | 
					
						
							|  |  |  | 		fprintf(stderr, "lost sys.stderr\n"); | 
					
						
							|  |  |  | 	else { | 
					
						
							| 
									
										
										
										
											1998-02-28 04:31:39 +00:00
										 |  |  | 		if (Py_FlushLine()) | 
					
						
							|  |  |  | 			PyErr_Clear(); | 
					
						
							| 
									
										
										
										
											1997-05-22 22:35:04 +00:00
										 |  |  | 		fflush(stdout); | 
					
						
							| 
									
										
										
										
											1998-02-28 04:31:39 +00:00
										 |  |  | 		err = PyTraceBack_Print(tb, f); | 
					
						
							| 
									
										
										
										
											1997-08-26 18:09:48 +00:00
										 |  |  | 		if (err == 0 && | 
					
						
							|  |  |  | 		    PyErr_GivenExceptionMatches(exception, PyExc_SyntaxError)) | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											1997-03-05 00:20:32 +00:00
										 |  |  | 			PyObject *message; | 
					
						
							| 
									
										
										
										
											1994-08-29 12:50:44 +00:00
										 |  |  | 			char *filename, *text; | 
					
						
							|  |  |  | 			int lineno, offset; | 
					
						
							| 
									
										
										
										
											1997-08-29 22:07:17 +00:00
										 |  |  | 			if (!parse_syntax_error(v, &message, &filename, | 
					
						
							|  |  |  | 						&lineno, &offset, &text)) | 
					
						
							| 
									
										
										
										
											1997-03-05 00:20:32 +00:00
										 |  |  | 				PyErr_Clear(); | 
					
						
							| 
									
										
										
										
											1994-08-29 12:50:44 +00:00
										 |  |  | 			else { | 
					
						
							|  |  |  | 				char buf[10]; | 
					
						
							| 
									
										
										
										
											1997-03-05 00:20:32 +00:00
										 |  |  | 				PyFile_WriteString("  File \"", f); | 
					
						
							| 
									
										
										
										
											1994-08-29 12:50:44 +00:00
										 |  |  | 				if (filename == NULL) | 
					
						
							| 
									
										
										
										
											1997-03-05 00:20:32 +00:00
										 |  |  | 					PyFile_WriteString("<string>", f); | 
					
						
							| 
									
										
										
										
											1994-08-29 12:50:44 +00:00
										 |  |  | 				else | 
					
						
							| 
									
										
										
										
											1997-03-05 00:20:32 +00:00
										 |  |  | 					PyFile_WriteString(filename, f); | 
					
						
							|  |  |  | 				PyFile_WriteString("\", line ", f); | 
					
						
							| 
									
										
										
										
											1994-08-29 12:50:44 +00:00
										 |  |  | 				sprintf(buf, "%d", lineno); | 
					
						
							| 
									
										
										
										
											1997-03-05 00:20:32 +00:00
										 |  |  | 				PyFile_WriteString(buf, f); | 
					
						
							|  |  |  | 				PyFile_WriteString("\n", f); | 
					
						
							| 
									
										
										
										
											1994-08-29 12:50:44 +00:00
										 |  |  | 				if (text != NULL) { | 
					
						
							| 
									
										
										
										
											1994-09-19 08:08:50 +00:00
										 |  |  | 					char *nl; | 
					
						
							|  |  |  | 					if (offset > 0 && | 
					
						
							| 
									
										
										
										
											1997-05-06 15:24:59 +00:00
										 |  |  | 					    offset == (int)strlen(text)) | 
					
						
							| 
									
										
										
										
											1994-09-19 08:08:50 +00:00
										 |  |  | 						offset--; | 
					
						
							|  |  |  | 					for (;;) { | 
					
						
							|  |  |  | 						nl = strchr(text, '\n'); | 
					
						
							|  |  |  | 						if (nl == NULL || | 
					
						
							|  |  |  | 						    nl-text >= offset) | 
					
						
							|  |  |  | 							break; | 
					
						
							|  |  |  | 						offset -= (nl+1-text); | 
					
						
							|  |  |  | 						text = nl+1; | 
					
						
							|  |  |  | 					} | 
					
						
							| 
									
										
										
										
											1994-08-29 12:50:44 +00:00
										 |  |  | 					while (*text == ' ' || *text == '\t') { | 
					
						
							|  |  |  | 						text++; | 
					
						
							|  |  |  | 						offset--; | 
					
						
							|  |  |  | 					} | 
					
						
							| 
									
										
										
										
											1997-03-05 00:20:32 +00:00
										 |  |  | 					PyFile_WriteString("    ", f); | 
					
						
							|  |  |  | 					PyFile_WriteString(text, f); | 
					
						
							| 
									
										
										
										
											1994-08-29 12:50:44 +00:00
										 |  |  | 					if (*text == '\0' || | 
					
						
							|  |  |  | 					    text[strlen(text)-1] != '\n') | 
					
						
							| 
									
										
										
										
											1997-03-05 00:20:32 +00:00
										 |  |  | 						PyFile_WriteString("\n", f); | 
					
						
							|  |  |  | 					PyFile_WriteString("    ", f); | 
					
						
							| 
									
										
										
										
											1994-08-29 12:50:44 +00:00
										 |  |  | 					offset--; | 
					
						
							|  |  |  | 					while (offset > 0) { | 
					
						
							| 
									
										
										
										
											1997-03-05 00:20:32 +00:00
										 |  |  | 						PyFile_WriteString(" ", f); | 
					
						
							| 
									
										
										
										
											1994-08-29 12:50:44 +00:00
										 |  |  | 						offset--; | 
					
						
							|  |  |  | 					} | 
					
						
							| 
									
										
										
										
											1997-03-05 00:20:32 +00:00
										 |  |  | 					PyFile_WriteString("^\n", f); | 
					
						
							| 
									
										
										
										
											1994-08-29 12:50:44 +00:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											1997-03-05 00:20:32 +00:00
										 |  |  | 				Py_INCREF(message); | 
					
						
							|  |  |  | 				Py_DECREF(v); | 
					
						
							| 
									
										
										
										
											1994-08-29 12:50:44 +00:00
										 |  |  | 				v = message; | 
					
						
							| 
									
										
										
										
											1997-05-22 22:35:04 +00:00
										 |  |  | 				/* Can't be bothered to check all those
 | 
					
						
							|  |  |  | 				   PyFile_WriteString() calls */ | 
					
						
							|  |  |  | 				if (PyErr_Occurred()) | 
					
						
							|  |  |  | 					err = -1; | 
					
						
							| 
									
										
										
										
											1994-08-29 12:50:44 +00:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											1997-05-22 22:35:04 +00:00
										 |  |  | 		if (err) { | 
					
						
							|  |  |  | 			/* Don't do anything else */ | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		else if (PyClass_Check(exception)) { | 
					
						
							| 
									
										
										
										
											1997-09-16 21:42:03 +00:00
										 |  |  | 			PyClassObject* exc = (PyClassObject*)exception; | 
					
						
							|  |  |  | 			PyObject* className = exc->cl_name; | 
					
						
							|  |  |  | 			PyObject* moduleName = | 
					
						
							|  |  |  | 			      PyDict_GetItemString(exc->cl_dict, "__module__"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			if (moduleName == NULL) | 
					
						
							| 
									
										
										
										
											1997-05-22 22:35:04 +00:00
										 |  |  | 				err = PyFile_WriteString("<unknown>", f); | 
					
						
							| 
									
										
										
										
											1997-09-16 21:42:03 +00:00
										 |  |  | 			else { | 
					
						
							|  |  |  | 				char* modstr = PyString_AsString(moduleName); | 
					
						
							|  |  |  | 				if (modstr && strcmp(modstr, "exceptions"))  | 
					
						
							|  |  |  | 				{ | 
					
						
							|  |  |  | 					err = PyFile_WriteString(modstr, f); | 
					
						
							|  |  |  | 					err += PyFile_WriteString(".", f); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			if (err == 0) { | 
					
						
							|  |  |  | 				if (className == NULL) | 
					
						
							|  |  |  | 				      err = PyFile_WriteString("<unknown>", f); | 
					
						
							|  |  |  | 				else | 
					
						
							|  |  |  | 				      err = PyFile_WriteObject(className, f, | 
					
						
							|  |  |  | 							       Py_PRINT_RAW); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											1995-02-07 15:30:45 +00:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											1997-05-22 22:35:04 +00:00
										 |  |  | 		else | 
					
						
							|  |  |  | 			err = PyFile_WriteObject(exception, f, Py_PRINT_RAW); | 
					
						
							|  |  |  | 		if (err == 0) { | 
					
						
							|  |  |  | 			if (v != NULL && v != Py_None) { | 
					
						
							| 
									
										
										
										
											1997-08-29 22:07:17 +00:00
										 |  |  | 				PyObject *s = PyObject_Str(v); | 
					
						
							|  |  |  | 				/* only print colon if the str() of the
 | 
					
						
							|  |  |  | 				   object is not the empty string | 
					
						
							|  |  |  | 				*/ | 
					
						
							| 
									
										
										
										
											1997-09-05 19:11:53 +00:00
										 |  |  | 				if (s == NULL) | 
					
						
							|  |  |  | 					err = -1; | 
					
						
							|  |  |  | 				else if (!PyString_Check(s) || | 
					
						
							|  |  |  | 					 PyString_GET_SIZE(s) != 0) | 
					
						
							| 
									
										
										
										
											1997-08-29 22:07:17 +00:00
										 |  |  | 					err = PyFile_WriteString(": ", f); | 
					
						
							| 
									
										
										
										
											1997-05-22 22:35:04 +00:00
										 |  |  | 				if (err == 0) | 
					
						
							| 
									
										
										
										
											1997-09-05 19:11:53 +00:00
										 |  |  | 				  err = PyFile_WriteObject(s, f, Py_PRINT_RAW); | 
					
						
							|  |  |  | 				Py_XDECREF(s); | 
					
						
							| 
									
										
										
										
											1997-05-22 22:35:04 +00:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											1992-09-25 21:59:05 +00:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											1997-05-22 22:35:04 +00:00
										 |  |  | 		if (err == 0) | 
					
						
							|  |  |  | 			err = PyFile_WriteString("\n", f); | 
					
						
							| 
									
										
										
										
											1992-08-04 12:41:02 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											1997-03-05 00:20:32 +00:00
										 |  |  | 	Py_XDECREF(exception); | 
					
						
							|  |  |  | 	Py_XDECREF(v); | 
					
						
							|  |  |  | 	Py_XDECREF(tb); | 
					
						
							| 
									
										
										
										
											1997-05-22 22:35:04 +00:00
										 |  |  | 	/* If an error happened here, don't show it.
 | 
					
						
							|  |  |  | 	   XXX This is wrong, but too many callers rely on this behavior. */ | 
					
						
							|  |  |  | 	if (err != 0) | 
					
						
							|  |  |  | 		PyErr_Clear(); | 
					
						
							| 
									
										
										
										
											1992-08-04 12:41:02 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-03-05 00:20:32 +00:00
										 |  |  | PyObject * | 
					
						
							|  |  |  | PyRun_String(str, start, globals, locals) | 
					
						
							| 
									
										
										
										
											1992-08-04 12:41:02 +00:00
										 |  |  | 	char *str; | 
					
						
							|  |  |  | 	int start; | 
					
						
							| 
									
										
										
										
											1997-03-05 00:20:32 +00:00
										 |  |  | 	PyObject *globals, *locals; | 
					
						
							| 
									
										
										
										
											1992-08-04 12:41:02 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											1997-03-05 00:20:32 +00:00
										 |  |  | 	return run_err_node(PyParser_SimpleParseString(str, start), | 
					
						
							| 
									
										
										
										
											1994-08-29 12:50:44 +00:00
										 |  |  | 			    "<string>", globals, locals); | 
					
						
							| 
									
										
										
										
											1992-08-04 12:41:02 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-03-05 00:20:32 +00:00
										 |  |  | PyObject * | 
					
						
							|  |  |  | PyRun_File(fp, filename, start, globals, locals) | 
					
						
							| 
									
										
										
										
											1992-08-04 12:41:02 +00:00
										 |  |  | 	FILE *fp; | 
					
						
							|  |  |  | 	char *filename; | 
					
						
							|  |  |  | 	int start; | 
					
						
							| 
									
										
										
										
											1997-03-05 00:20:32 +00:00
										 |  |  | 	PyObject *globals, *locals; | 
					
						
							| 
									
										
										
										
											1992-08-04 12:41:02 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											1997-03-05 00:20:32 +00:00
										 |  |  | 	return run_err_node(PyParser_SimpleParseFile(fp, filename, start), | 
					
						
							| 
									
										
										
										
											1994-08-29 12:50:44 +00:00
										 |  |  | 			    filename, globals, locals); | 
					
						
							| 
									
										
										
										
											1992-08-04 12:41:02 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-03-05 00:20:32 +00:00
										 |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											1994-08-29 12:50:44 +00:00
										 |  |  | run_err_node(n, filename, globals, locals) | 
					
						
							| 
									
										
										
										
											1992-08-04 12:41:02 +00:00
										 |  |  | 	node *n; | 
					
						
							|  |  |  | 	char *filename; | 
					
						
							| 
									
										
										
										
											1997-03-05 00:20:32 +00:00
										 |  |  | 	PyObject *globals, *locals; | 
					
						
							| 
									
										
										
										
											1992-08-04 12:41:02 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											1994-08-29 12:50:44 +00:00
										 |  |  | 	if (n == NULL) | 
					
						
							|  |  |  | 		return  NULL; | 
					
						
							| 
									
										
										
										
											1992-08-04 12:41:02 +00:00
										 |  |  | 	return run_node(n, filename, globals, locals); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-03-05 00:20:32 +00:00
										 |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											1992-08-04 12:41:02 +00:00
										 |  |  | run_node(n, filename, globals, locals) | 
					
						
							|  |  |  | 	node *n; | 
					
						
							|  |  |  | 	char *filename; | 
					
						
							| 
									
										
										
										
											1997-03-05 00:20:32 +00:00
										 |  |  | 	PyObject *globals, *locals; | 
					
						
							| 
									
										
										
										
											1992-08-04 12:41:02 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											1997-03-05 00:20:32 +00:00
										 |  |  | 	PyCodeObject *co; | 
					
						
							|  |  |  | 	PyObject *v; | 
					
						
							|  |  |  | 	co = PyNode_Compile(n, filename); | 
					
						
							|  |  |  | 	PyNode_Free(n); | 
					
						
							| 
									
										
										
										
											1992-08-04 12:41:02 +00:00
										 |  |  | 	if (co == NULL) | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							| 
									
										
										
										
											1997-03-05 00:20:32 +00:00
										 |  |  | 	v = PyEval_EvalCode(co, globals, locals); | 
					
						
							|  |  |  | 	Py_DECREF(co); | 
					
						
							| 
									
										
										
										
											1992-08-04 12:41:02 +00:00
										 |  |  | 	return v; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-03-05 00:20:32 +00:00
										 |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											1994-09-14 13:31:04 +00:00
										 |  |  | run_pyc_file(fp, filename, globals, locals) | 
					
						
							|  |  |  | 	FILE *fp; | 
					
						
							|  |  |  | 	char *filename; | 
					
						
							| 
									
										
										
										
											1997-03-05 00:20:32 +00:00
										 |  |  | 	PyObject *globals, *locals; | 
					
						
							| 
									
										
										
										
											1994-09-14 13:31:04 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											1997-03-05 00:20:32 +00:00
										 |  |  | 	PyCodeObject *co; | 
					
						
							|  |  |  | 	PyObject *v; | 
					
						
							| 
									
										
										
										
											1994-09-14 13:31:04 +00:00
										 |  |  | 	long magic; | 
					
						
							| 
									
										
										
										
											1997-03-05 00:20:32 +00:00
										 |  |  | 	long PyImport_GetMagicNumber(); | 
					
						
							| 
									
										
										
										
											1994-09-14 13:31:04 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-03-05 00:20:32 +00:00
										 |  |  | 	magic = PyMarshal_ReadLongFromFile(fp); | 
					
						
							|  |  |  | 	if (magic != PyImport_GetMagicNumber()) { | 
					
						
							|  |  |  | 		PyErr_SetString(PyExc_RuntimeError, | 
					
						
							| 
									
										
										
										
											1994-09-14 13:31:04 +00:00
										 |  |  | 			   "Bad magic number in .pyc file"); | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											1997-03-05 00:20:32 +00:00
										 |  |  | 	(void) PyMarshal_ReadLongFromFile(fp); | 
					
						
							|  |  |  | 	v = PyMarshal_ReadObjectFromFile(fp); | 
					
						
							| 
									
										
										
										
											1994-09-14 13:31:04 +00:00
										 |  |  | 	fclose(fp); | 
					
						
							| 
									
										
										
										
											1997-03-05 00:20:32 +00:00
										 |  |  | 	if (v == NULL || !PyCode_Check(v)) { | 
					
						
							|  |  |  | 		Py_XDECREF(v); | 
					
						
							|  |  |  | 		PyErr_SetString(PyExc_RuntimeError, | 
					
						
							| 
									
										
										
										
											1994-09-14 13:31:04 +00:00
										 |  |  | 			   "Bad code object in .pyc file"); | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											1997-03-05 00:20:32 +00:00
										 |  |  | 	co = (PyCodeObject *)v; | 
					
						
							|  |  |  | 	v = PyEval_EvalCode(co, globals, locals); | 
					
						
							|  |  |  | 	Py_DECREF(co); | 
					
						
							| 
									
										
										
										
											1994-09-14 13:31:04 +00:00
										 |  |  | 	return v; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-03-05 00:20:32 +00:00
										 |  |  | PyObject * | 
					
						
							|  |  |  | Py_CompileString(str, filename, start) | 
					
						
							| 
									
										
										
										
											1993-03-30 17:46:03 +00:00
										 |  |  | 	char *str; | 
					
						
							|  |  |  | 	char *filename; | 
					
						
							|  |  |  | 	int start; | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	node *n; | 
					
						
							| 
									
										
										
										
											1997-03-05 00:20:32 +00:00
										 |  |  | 	PyCodeObject *co; | 
					
						
							|  |  |  | 	n = PyParser_SimpleParseString(str, start); | 
					
						
							| 
									
										
										
										
											1994-08-29 12:50:44 +00:00
										 |  |  | 	if (n == NULL) | 
					
						
							| 
									
										
										
										
											1993-03-30 17:46:03 +00:00
										 |  |  | 		return NULL; | 
					
						
							| 
									
										
										
										
											1997-03-05 00:20:32 +00:00
										 |  |  | 	co = PyNode_Compile(n, filename); | 
					
						
							|  |  |  | 	PyNode_Free(n); | 
					
						
							|  |  |  | 	return (PyObject *)co; | 
					
						
							| 
									
										
										
										
											1993-03-30 17:46:03 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1994-08-29 12:50:44 +00:00
										 |  |  | /* Simplified interface to parsefile -- return node or set exception */ | 
					
						
							| 
									
										
										
										
											1992-08-04 12:41:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1994-08-29 12:50:44 +00:00
										 |  |  | node * | 
					
						
							| 
									
										
										
										
											1997-03-05 00:20:32 +00:00
										 |  |  | PyParser_SimpleParseFile(fp, filename, start) | 
					
						
							| 
									
										
										
										
											1992-08-04 12:41:02 +00:00
										 |  |  | 	FILE *fp; | 
					
						
							|  |  |  | 	char *filename; | 
					
						
							|  |  |  | 	int start; | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											1994-08-29 12:50:44 +00:00
										 |  |  | 	node *n; | 
					
						
							|  |  |  | 	perrdetail err; | 
					
						
							| 
									
										
										
										
											1997-03-05 00:20:32 +00:00
										 |  |  | 	n = PyParser_ParseFile(fp, filename, &_PyParser_Grammar, start, | 
					
						
							| 
									
										
										
										
											1994-08-29 12:50:44 +00:00
										 |  |  | 				(char *)0, (char *)0, &err); | 
					
						
							|  |  |  | 	if (n == NULL) | 
					
						
							|  |  |  | 		err_input(&err); | 
					
						
							|  |  |  | 	return n; | 
					
						
							| 
									
										
										
										
											1992-08-04 12:41:02 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1994-08-29 12:50:44 +00:00
										 |  |  | /* Simplified interface to parsestring -- return node or set exception */ | 
					
						
							| 
									
										
										
										
											1992-08-04 12:41:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1994-08-29 12:50:44 +00:00
										 |  |  | node * | 
					
						
							| 
									
										
										
										
											1997-03-05 00:20:32 +00:00
										 |  |  | PyParser_SimpleParseString(str, start) | 
					
						
							| 
									
										
										
										
											1992-08-04 12:41:02 +00:00
										 |  |  | 	char *str; | 
					
						
							|  |  |  | 	int start; | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											1994-08-29 12:50:44 +00:00
										 |  |  | 	node *n; | 
					
						
							|  |  |  | 	perrdetail err; | 
					
						
							| 
									
										
										
										
											1997-03-05 00:20:32 +00:00
										 |  |  | 	n = PyParser_ParseString(str, &_PyParser_Grammar, start, &err); | 
					
						
							| 
									
										
										
										
											1994-08-29 12:50:44 +00:00
										 |  |  | 	if (n == NULL) | 
					
						
							|  |  |  | 		err_input(&err); | 
					
						
							|  |  |  | 	return n; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Set the error appropriate to the given input error code (see errcode.h) */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void | 
					
						
							|  |  |  | err_input(err) | 
					
						
							|  |  |  | 	perrdetail *err; | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											1997-03-05 00:20:32 +00:00
										 |  |  | 	PyObject *v, *w; | 
					
						
							| 
									
										
										
										
											1994-08-29 12:50:44 +00:00
										 |  |  | 	char *msg = NULL; | 
					
						
							| 
									
										
										
										
											1997-03-05 00:20:32 +00:00
										 |  |  | 	v = Py_BuildValue("(ziiz)", err->filename, | 
					
						
							| 
									
										
										
										
											1994-08-29 12:50:44 +00:00
										 |  |  | 			    err->lineno, err->offset, err->text); | 
					
						
							|  |  |  | 	if (err->text != NULL) { | 
					
						
							|  |  |  | 		free(err->text); | 
					
						
							|  |  |  | 		err->text = NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	switch (err->error) { | 
					
						
							|  |  |  | 	case E_SYNTAX: | 
					
						
							|  |  |  | 		msg = "invalid syntax"; | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | 	case E_TOKEN: | 
					
						
							|  |  |  | 		msg = "invalid token"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | 	case E_INTR: | 
					
						
							| 
									
										
										
										
											1997-03-05 00:20:32 +00:00
										 |  |  | 		PyErr_SetNone(PyExc_KeyboardInterrupt); | 
					
						
							| 
									
										
										
										
											1994-08-29 12:50:44 +00:00
										 |  |  | 		return; | 
					
						
							|  |  |  | 	case E_NOMEM: | 
					
						
							| 
									
										
										
										
											1997-03-05 00:20:32 +00:00
										 |  |  | 		PyErr_NoMemory(); | 
					
						
							| 
									
										
										
										
											1994-08-29 12:50:44 +00:00
										 |  |  | 		return; | 
					
						
							|  |  |  | 	case E_EOF: | 
					
						
							|  |  |  | 		msg = "unexpected EOF while parsing"; | 
					
						
							|  |  |  | 		break; | 
					
						
							| 
									
										
										
										
											1998-04-10 19:43:42 +00:00
										 |  |  | 	case E_INDENT: | 
					
						
							|  |  |  | 		msg = "inconsistent use of tabs and spaces in indentation"; | 
					
						
							|  |  |  | 		break; | 
					
						
							| 
									
										
										
										
											1994-08-29 12:50:44 +00:00
										 |  |  | 	default: | 
					
						
							|  |  |  | 		fprintf(stderr, "error=%d\n", err->error); | 
					
						
							|  |  |  | 		msg = "unknown parsing error"; | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											1997-03-05 00:20:32 +00:00
										 |  |  | 	w = Py_BuildValue("(sO)", msg, v); | 
					
						
							|  |  |  | 	Py_XDECREF(v); | 
					
						
							|  |  |  | 	PyErr_SetObject(PyExc_SyntaxError, w); | 
					
						
							|  |  |  | 	Py_XDECREF(w); | 
					
						
							| 
									
										
										
										
											1992-08-04 12:41:02 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Print fatal error message and abort */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void | 
					
						
							| 
									
										
										
										
											1997-03-05 00:20:32 +00:00
										 |  |  | Py_FatalError(msg) | 
					
						
							| 
									
										
										
										
											1992-08-04 12:41:02 +00:00
										 |  |  | 	char *msg; | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											1994-09-29 09:38:33 +00:00
										 |  |  | 	fprintf(stderr, "Fatal Python error: %s\n", msg); | 
					
						
							| 
									
										
										
										
											1995-01-26 00:40:38 +00:00
										 |  |  | #ifdef macintosh
 | 
					
						
							|  |  |  | 	for (;;); | 
					
						
							| 
									
										
										
										
											1995-03-14 15:01:17 +00:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											1996-09-11 23:12:24 +00:00
										 |  |  | #ifdef MS_WIN32
 | 
					
						
							| 
									
										
										
										
											1997-05-22 20:21:30 +00:00
										 |  |  | 	OutputDebugString("Fatal Python error: "); | 
					
						
							| 
									
										
										
										
											1995-03-14 15:01:17 +00:00
										 |  |  | 	OutputDebugString(msg); | 
					
						
							|  |  |  | 	OutputDebugString("\n"); | 
					
						
							| 
									
										
										
										
											1998-08-13 13:33:16 +00:00
										 |  |  | #ifdef _DEBUG
 | 
					
						
							|  |  |  | 	DebugBreak(); | 
					
						
							| 
									
										
										
										
											1995-01-26 00:40:38 +00:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											1998-08-13 13:33:16 +00:00
										 |  |  | #endif /* MS_WIN32 */
 | 
					
						
							| 
									
										
										
										
											1992-08-04 12:41:02 +00:00
										 |  |  | 	abort(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Clean up and exit */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1994-08-29 12:50:44 +00:00
										 |  |  | #ifdef WITH_THREAD
 | 
					
						
							| 
									
										
										
										
											1998-10-01 20:42:43 +00:00
										 |  |  | #include "pythread.h"
 | 
					
						
							| 
									
										
										
										
											1997-03-05 00:20:32 +00:00
										 |  |  | int _PyThread_Started = 0; /* Set by threadmodule.c and maybe others */ | 
					
						
							| 
									
										
										
										
											1992-08-17 08:59:08 +00:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-10-01 16:01:57 +00:00
										 |  |  | #define NEXITFUNCS 32
 | 
					
						
							| 
									
										
										
										
											1994-09-07 14:38:28 +00:00
										 |  |  | static void (*exitfuncs[NEXITFUNCS])(); | 
					
						
							|  |  |  | static int nexitfuncs = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int Py_AtExit(func) | 
					
						
							| 
									
										
										
										
											1997-03-05 00:20:32 +00:00
										 |  |  | 	void (*func) Py_PROTO((void)); | 
					
						
							| 
									
										
										
										
											1994-09-07 14:38:28 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	if (nexitfuncs >= NEXITFUNCS) | 
					
						
							|  |  |  | 		return -1; | 
					
						
							|  |  |  | 	exitfuncs[nexitfuncs++] = func; | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-08-05 02:22:03 +00:00
										 |  |  | static void | 
					
						
							|  |  |  | call_sys_exitfunc() | 
					
						
							| 
									
										
										
										
											1992-08-04 12:41:02 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											1997-03-05 00:20:32 +00:00
										 |  |  | 	PyObject *exitfunc = PySys_GetObject("exitfunc"); | 
					
						
							| 
									
										
										
										
											1992-09-03 20:28:00 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (exitfunc) { | 
					
						
							| 
									
										
										
										
											1998-04-03 21:12:12 +00:00
										 |  |  | 		PyObject *res, *f; | 
					
						
							| 
									
										
										
										
											1997-03-05 00:20:32 +00:00
										 |  |  | 		Py_INCREF(exitfunc); | 
					
						
							|  |  |  | 		PySys_SetObject("exitfunc", (PyObject *)NULL); | 
					
						
							| 
									
										
										
										
											1998-04-03 21:12:12 +00:00
										 |  |  | 		f = PySys_GetObject("stderr"); | 
					
						
							| 
									
										
										
										
											1997-03-05 00:20:32 +00:00
										 |  |  | 		res = PyEval_CallObject(exitfunc, (PyObject *)NULL); | 
					
						
							| 
									
										
										
										
											1992-09-03 20:28:00 +00:00
										 |  |  | 		if (res == NULL) { | 
					
						
							| 
									
										
										
										
											1998-04-03 21:12:12 +00:00
										 |  |  | 			if (f) | 
					
						
							|  |  |  | 			    PyFile_WriteString("Error in sys.exitfunc:\n", f); | 
					
						
							| 
									
										
										
										
											1997-03-05 00:20:32 +00:00
										 |  |  | 			PyErr_Print(); | 
					
						
							| 
									
										
										
										
											1992-09-03 20:28:00 +00:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											1997-03-05 00:20:32 +00:00
										 |  |  | 		Py_DECREF(exitfunc); | 
					
						
							| 
									
										
										
										
											1992-09-03 20:28:00 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-02-28 04:31:39 +00:00
										 |  |  | 	if (Py_FlushLine()) | 
					
						
							|  |  |  | 		PyErr_Clear(); | 
					
						
							| 
									
										
										
										
											1997-08-05 02:22:03 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											1994-09-07 14:38:28 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-08-05 02:22:03 +00:00
										 |  |  | static void | 
					
						
							|  |  |  | call_ll_exitfuncs() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											1994-09-07 14:38:28 +00:00
										 |  |  | 	while (nexitfuncs > 0) | 
					
						
							|  |  |  | 		(*exitfuncs[--nexitfuncs])(); | 
					
						
							| 
									
										
										
										
											1997-08-02 03:10:38 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	fflush(stdout); | 
					
						
							|  |  |  | 	fflush(stderr); | 
					
						
							| 
									
										
										
										
											1992-10-18 18:53:57 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1993-10-11 12:54:31 +00:00
										 |  |  | #ifdef COUNT_ALLOCS
 | 
					
						
							| 
									
										
										
										
											1997-03-05 00:20:32 +00:00
										 |  |  | extern void dump_counts Py_PROTO((void)); | 
					
						
							| 
									
										
										
										
											1993-10-11 12:54:31 +00:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1992-10-18 18:53:57 +00:00
										 |  |  | void | 
					
						
							| 
									
										
										
										
											1997-03-05 00:20:32 +00:00
										 |  |  | Py_Exit(sts) | 
					
						
							| 
									
										
										
										
											1992-10-18 18:53:57 +00:00
										 |  |  | 	int sts; | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											1997-08-05 02:22:03 +00:00
										 |  |  | 	Py_Finalize(); | 
					
						
							| 
									
										
										
										
											1992-08-04 12:41:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1995-10-27 13:22:14 +00:00
										 |  |  | #ifdef macintosh
 | 
					
						
							|  |  |  | 	PyMac_Exit(sts); | 
					
						
							|  |  |  | #else
 | 
					
						
							| 
									
										
										
										
											1992-08-04 12:41:02 +00:00
										 |  |  | 	exit(sts); | 
					
						
							| 
									
										
										
										
											1995-10-27 13:22:14 +00:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											1992-08-04 12:41:02 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1993-07-05 10:31:29 +00:00
										 |  |  | static void | 
					
						
							| 
									
										
										
										
											1992-10-18 18:53:57 +00:00
										 |  |  | initsigs() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											1994-08-29 12:50:44 +00:00
										 |  |  | #ifdef HAVE_SIGNAL_H
 | 
					
						
							|  |  |  | #ifdef SIGPIPE
 | 
					
						
							|  |  |  | 	signal(SIGPIPE, SIG_IGN); | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | #endif /* HAVE_SIGNAL_H */
 | 
					
						
							| 
									
										
										
										
											1997-03-05 00:20:32 +00:00
										 |  |  | 	PyOS_InitInterrupts(); /* May imply initsignal() */ | 
					
						
							| 
									
										
										
										
											1992-10-18 18:53:57 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-05-22 16:35:33 +00:00
										 |  |  | #ifdef Py_TRACE_REFS
 | 
					
						
							| 
									
										
										
										
											1992-08-04 12:41:02 +00:00
										 |  |  | /* Ask a yes/no question */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1992-09-03 20:28:00 +00:00
										 |  |  | int | 
					
						
							| 
									
										
										
										
											1997-03-05 00:20:32 +00:00
										 |  |  | _Py_AskYesNo(prompt) | 
					
						
							| 
									
										
										
										
											1992-08-04 12:41:02 +00:00
										 |  |  | 	char *prompt; | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	char buf[256]; | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	printf("%s [ny] ", prompt); | 
					
						
							|  |  |  | 	if (fgets(buf, sizeof buf, stdin) == NULL) | 
					
						
							|  |  |  | 		return 0; | 
					
						
							|  |  |  | 	return buf[0] == 'y' || buf[0] == 'Y'; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1994-08-29 12:50:44 +00:00
										 |  |  | #ifdef MPW
 | 
					
						
							| 
									
										
										
										
											1992-08-04 12:41:02 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* Check for file descriptor connected to interactive device.
 | 
					
						
							|  |  |  |    Pretend that stdin is always interactive, other files never. */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int | 
					
						
							|  |  |  | isatty(fd) | 
					
						
							|  |  |  | 	int fd; | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	return fd == fileno(stdin); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											1997-02-14 19:45:36 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |  * The file descriptor fd is considered ``interactive'' if either | 
					
						
							|  |  |  |  *   a) isatty(fd) is TRUE, or | 
					
						
							|  |  |  |  *   b) the -i flag was given, and the filename associated with | 
					
						
							|  |  |  |  *      the descriptor is NULL or "<stdin>" or "???". | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | int | 
					
						
							|  |  |  | Py_FdIsInteractive(fp, filename) | 
					
						
							|  |  |  | 	FILE *fp; | 
					
						
							|  |  |  | 	char *filename; | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	if (isatty((int)fileno(fp))) | 
					
						
							|  |  |  | 		return 1; | 
					
						
							|  |  |  | 	if (!Py_InteractiveFlag) | 
					
						
							|  |  |  | 		return 0; | 
					
						
							|  |  |  | 	return (filename == NULL) || | 
					
						
							|  |  |  | 	       (strcmp(filename, "<stdin>") == 0) || | 
					
						
							|  |  |  | 	       (strcmp(filename, "???") == 0); | 
					
						
							|  |  |  | } |