1992-08-04 09:41:02 -03:00
|
|
|
|
|
|
|
/* Python interpreter top-level routines, including init/exit */
|
|
|
|
|
1997-03-04 20:20:32 -04:00
|
|
|
#include "Python.h"
|
1992-08-04 09:41:02 -03:00
|
|
|
|
2005-10-20 16:59:25 -03:00
|
|
|
#include "Python-ast.h"
|
1992-08-04 09:41:02 -03:00
|
|
|
#include "grammar.h"
|
|
|
|
#include "node.h"
|
2000-07-11 14:53:00 -03:00
|
|
|
#include "token.h"
|
1992-08-04 09:41:02 -03:00
|
|
|
#include "parsetok.h"
|
|
|
|
#include "errcode.h"
|
2005-10-20 16:59:25 -03:00
|
|
|
#include "code.h"
|
1992-08-04 09:41:02 -03:00
|
|
|
#include "compile.h"
|
2001-02-02 14:19:15 -04:00
|
|
|
#include "symtable.h"
|
2005-10-20 16:59:25 -03:00
|
|
|
#include "ast.h"
|
1992-08-05 16:58:53 -03:00
|
|
|
#include "eval.h"
|
1994-09-14 10:31:04 -03:00
|
|
|
#include "marshal.h"
|
1992-08-04 09:41:02 -03:00
|
|
|
|
1992-10-18 15:53:57 -03:00
|
|
|
#include <signal.h>
|
|
|
|
|
2003-03-05 11:13:47 -04:00
|
|
|
#ifdef HAVE_LANGINFO_H
|
|
|
|
#include <locale.h>
|
|
|
|
#include <langinfo.h>
|
|
|
|
#endif
|
|
|
|
|
2002-06-30 12:26:10 -03:00
|
|
|
#ifdef MS_WINDOWS
|
1995-03-14 11:01:17 -04:00
|
|
|
#undef BYTE
|
|
|
|
#include "windows.h"
|
|
|
|
#endif
|
|
|
|
|
2000-07-22 15:47:25 -03:00
|
|
|
extern char *Py_GetPath(void);
|
1992-08-04 09:41:02 -03:00
|
|
|
|
1997-03-04 20:20:32 -04:00
|
|
|
extern grammar _PyParser_Grammar; /* From graminit.c */
|
1992-08-04 09:41:02 -03:00
|
|
|
|
1993-11-01 12:28:59 -04:00
|
|
|
/* Forward */
|
2000-07-09 00:09:57 -03:00
|
|
|
static void initmain(void);
|
|
|
|
static void initsite(void);
|
2005-10-20 16:59:25 -03:00
|
|
|
static PyObject *run_err_mod(mod_ty, const char *, PyObject *, PyObject *,
|
2001-03-01 18:59:14 -04:00
|
|
|
PyCompilerFlags *);
|
2005-10-20 16:59:25 -03:00
|
|
|
static PyObject *run_mod(mod_ty, const char *, PyObject *, PyObject *,
|
2001-03-01 18:59:14 -04:00
|
|
|
PyCompilerFlags *);
|
2002-12-11 10:04:59 -04:00
|
|
|
static PyObject *run_pyc_file(FILE *, const char *, PyObject *, PyObject *,
|
2001-03-21 22:47:58 -04:00
|
|
|
PyCompilerFlags *);
|
2000-07-09 00:09:57 -03:00
|
|
|
static void err_input(perrdetail *);
|
|
|
|
static void initsigs(void);
|
|
|
|
static void call_sys_exitfunc(void);
|
|
|
|
static void call_ll_exitfuncs(void);
|
2000-07-22 15:47:25 -03:00
|
|
|
extern void _PyUnicode_Init(void);
|
|
|
|
extern void _PyUnicode_Fini(void);
|
2000-03-10 19:03:54 -04:00
|
|
|
|
2003-04-19 12:41:53 -03:00
|
|
|
#ifdef WITH_THREAD
|
|
|
|
extern void _PyGILState_Init(PyInterpreterState *, PyThreadState *);
|
|
|
|
extern void _PyGILState_Fini(void);
|
|
|
|
#endif /* WITH_THREAD */
|
|
|
|
|
1997-03-04 20:20:32 -04:00
|
|
|
int Py_DebugFlag; /* Needed by parser.c */
|
|
|
|
int Py_VerboseFlag; /* Needed by import.c */
|
1997-02-14 15:45:36 -04:00
|
|
|
int Py_InteractiveFlag; /* Needed by Py_FdIsInteractive() below */
|
1997-08-29 19:32:42 -03:00
|
|
|
int Py_NoSiteFlag; /* Suppress 'import site' */
|
2000-05-02 16:18:59 -03:00
|
|
|
int Py_UseClassExceptionsFlag = 1; /* Needed by bltinmodule.c: deprecated */
|
1998-02-06 18:27:24 -04:00
|
|
|
int Py_FrozenFlag; /* Needed by getpath.c */
|
2000-05-01 14:55:15 -03:00
|
|
|
int Py_UnicodeFlag = 0; /* Needed by compile.c */
|
2001-07-23 13:30:27 -03:00
|
|
|
int Py_IgnoreEnvironmentFlag; /* e.g. PYTHONPATH, PYTHONHOME */
|
2001-12-06 02:23:26 -04:00
|
|
|
/* _XXX Py_QnewFlag should go away in 2.3. It's true iff -Qnew is passed,
|
|
|
|
on the command line, and is used in 2.2 by ceval.c to make all "/" divisions
|
|
|
|
true divisions (which they will be in 2.3). */
|
|
|
|
int _Py_QnewFlag = 0;
|
1992-08-04 09:41:02 -03:00
|
|
|
|
2003-02-18 20:33:33 -04:00
|
|
|
/* Reference to 'warnings' module, to avoid importing it
|
2003-07-15 20:03:55 -03:00
|
|
|
on the fly when the import lock may be held. See 683658/771097
|
2003-02-18 20:33:33 -04:00
|
|
|
*/
|
2003-07-15 20:03:55 -03:00
|
|
|
static PyObject *warnings_module = NULL;
|
|
|
|
|
|
|
|
/* Returns a borrowed reference to the 'warnings' module, or NULL.
|
|
|
|
If the module is returned, it is guaranteed to have been obtained
|
|
|
|
without acquiring the import lock
|
|
|
|
*/
|
2003-08-09 06:47:11 -03:00
|
|
|
PyObject *PyModule_GetWarningsModule(void)
|
2003-07-15 20:03:55 -03:00
|
|
|
{
|
|
|
|
PyObject *typ, *val, *tb;
|
|
|
|
PyObject *all_modules;
|
|
|
|
/* If we managed to get the module at init time, just use it */
|
|
|
|
if (warnings_module)
|
|
|
|
return warnings_module;
|
|
|
|
/* If it wasn't available at init time, it may be available
|
|
|
|
now in sys.modules (common scenario is frozen apps: import
|
|
|
|
at init time fails, but the frozen init code sets up sys.path
|
|
|
|
correctly, then does an implicit import of warnings for us
|
|
|
|
*/
|
|
|
|
/* Save and restore any exceptions */
|
|
|
|
PyErr_Fetch(&typ, &val, &tb);
|
|
|
|
|
2003-07-15 22:54:38 -03:00
|
|
|
all_modules = PySys_GetObject("modules");
|
2003-07-15 20:03:55 -03:00
|
|
|
if (all_modules) {
|
|
|
|
warnings_module = PyDict_GetItemString(all_modules, "warnings");
|
|
|
|
/* We keep a ref in the global */
|
|
|
|
Py_XINCREF(warnings_module);
|
|
|
|
}
|
|
|
|
PyErr_Restore(typ, val, tb);
|
|
|
|
return warnings_module;
|
|
|
|
}
|
2003-02-18 20:33:33 -04:00
|
|
|
|
1997-08-02 00:10:38 -03:00
|
|
|
static int initialized = 0;
|
|
|
|
|
2000-07-16 09:04:32 -03:00
|
|
|
/* API to access the initialized flag -- useful for esoteric use */
|
1997-08-22 01:20:13 -03:00
|
|
|
|
|
|
|
int
|
2000-07-22 15:47:25 -03:00
|
|
|
Py_IsInitialized(void)
|
1997-08-22 01:20:13 -03:00
|
|
|
{
|
|
|
|
return initialized;
|
|
|
|
}
|
|
|
|
|
1997-08-02 00:10:38 -03: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 09:41:02 -03:00
|
|
|
|
2001-10-12 19:17:56 -03:00
|
|
|
static int
|
|
|
|
add_flag(int flag, const char *envs)
|
|
|
|
{
|
|
|
|
int env = atoi(envs);
|
|
|
|
if (flag < env)
|
|
|
|
flag = env;
|
|
|
|
if (flag < 1)
|
|
|
|
flag = 1;
|
|
|
|
return flag;
|
|
|
|
}
|
|
|
|
|
1992-08-04 09:41:02 -03:00
|
|
|
void
|
2004-08-19 08:31:58 -03:00
|
|
|
Py_InitializeEx(int install_sigs)
|
1992-08-04 09:41:02 -03:00
|
|
|
{
|
1997-08-02 00:10:38 -03:00
|
|
|
PyInterpreterState *interp;
|
|
|
|
PyThreadState *tstate;
|
|
|
|
PyObject *bimod, *sysmod;
|
|
|
|
char *p;
|
2003-08-09 06:47:11 -03:00
|
|
|
#if defined(Py_USING_UNICODE) && defined(HAVE_LANGINFO_H) && defined(CODESET)
|
|
|
|
char *codeset;
|
|
|
|
char *saved_locale;
|
|
|
|
PyObject *sys_stream, *sys_isatty;
|
|
|
|
#endif
|
2001-08-16 05:21:42 -03:00
|
|
|
extern void _Py_ReadyTypes(void);
|
1997-08-02 00:10:38 -03:00
|
|
|
|
1997-08-29 19:32:42 -03:00
|
|
|
if (initialized)
|
1997-08-20 19:40:18 -03:00
|
|
|
return;
|
1997-08-29 19:32:42 -03:00
|
|
|
initialized = 1;
|
2003-04-17 12:24:21 -03:00
|
|
|
|
2001-07-23 13:30:27 -03:00
|
|
|
if ((p = Py_GETENV("PYTHONDEBUG")) && *p != '\0')
|
2001-10-12 19:17:56 -03:00
|
|
|
Py_DebugFlag = add_flag(Py_DebugFlag, p);
|
2001-07-23 13:30:27 -03:00
|
|
|
if ((p = Py_GETENV("PYTHONVERBOSE")) && *p != '\0')
|
2001-10-12 19:17:56 -03:00
|
|
|
Py_VerboseFlag = add_flag(Py_VerboseFlag, p);
|
2001-07-23 13:30:27 -03:00
|
|
|
if ((p = Py_GETENV("PYTHONOPTIMIZE")) && *p != '\0')
|
2001-10-12 19:17:56 -03:00
|
|
|
Py_OptimizeFlag = add_flag(Py_OptimizeFlag, p);
|
1997-08-02 00:10:38 -03: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 09:41:02 -03:00
|
|
|
|
2001-08-16 05:21:42 -03:00
|
|
|
_Py_ReadyTypes();
|
2001-08-07 14:24:28 -03:00
|
|
|
|
2002-12-30 23:42:13 -04:00
|
|
|
if (!_PyFrame_Init())
|
2002-12-30 18:29:22 -04:00
|
|
|
Py_FatalError("Py_Initialize: can't init frames");
|
|
|
|
|
2002-12-30 23:42:13 -04:00
|
|
|
if (!_PyInt_Init())
|
2002-12-30 18:29:22 -04:00
|
|
|
Py_FatalError("Py_Initialize: can't init ints");
|
|
|
|
|
2005-05-27 12:23:20 -03:00
|
|
|
_PyFloat_Init();
|
|
|
|
|
1997-08-02 00:10:38 -03:00
|
|
|
interp->modules = PyDict_New();
|
|
|
|
if (interp->modules == NULL)
|
|
|
|
Py_FatalError("Py_Initialize: can't make modules dictionary");
|
|
|
|
|
2001-08-17 15:39:25 -03:00
|
|
|
#ifdef Py_USING_UNICODE
|
2000-03-10 19:03:54 -04:00
|
|
|
/* Init Unicode implementation; relies on the codec registry */
|
|
|
|
_PyUnicode_Init();
|
2001-08-17 15:39:25 -03:00
|
|
|
#endif
|
2000-03-10 19:03:54 -04:00
|
|
|
|
2000-05-25 20:09:49 -03:00
|
|
|
bimod = _PyBuiltin_Init();
|
1997-08-02 00:10:38 -03:00
|
|
|
if (bimod == NULL)
|
|
|
|
Py_FatalError("Py_Initialize: can't initialize __builtin__");
|
1997-11-04 15:36:18 -04:00
|
|
|
interp->builtins = PyModule_GetDict(bimod);
|
|
|
|
Py_INCREF(interp->builtins);
|
1997-08-02 00:10:38 -03: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-04 20:20:32 -04:00
|
|
|
PySys_SetPath(Py_GetPath());
|
1997-08-02 00:10:38 -03:00
|
|
|
PyDict_SetItemString(interp->sysdict, "modules",
|
|
|
|
interp->modules);
|
|
|
|
|
1999-07-08 14:26:56 -03:00
|
|
|
_PyImport_Init();
|
|
|
|
|
2000-05-25 20:09:49 -03:00
|
|
|
/* initialize builtin exceptions */
|
2001-08-02 01:15:00 -03:00
|
|
|
_PyExc_Init();
|
2001-08-13 20:04:56 -03:00
|
|
|
_PyImport_FixupExtension("exceptions", "exceptions");
|
2000-05-25 20:09:49 -03:00
|
|
|
|
1997-08-29 19:07:17 -03:00
|
|
|
/* phase 2 of builtins */
|
1997-09-18 13:42:02 -03:00
|
|
|
_PyImport_FixupExtension("__builtin__", "__builtin__");
|
1997-08-29 19:07:17 -03:00
|
|
|
|
2002-12-30 18:08:05 -04:00
|
|
|
_PyImportHooks_Init();
|
|
|
|
|
2004-08-19 08:31:58 -03:00
|
|
|
if (install_sigs)
|
|
|
|
initsigs(); /* Signal handling stuff, including initintr() */
|
1995-01-09 13:53:26 -04:00
|
|
|
|
1997-08-02 00:10:38 -03:00
|
|
|
initmain(); /* Module __main__ */
|
1997-08-29 19:32:42 -03:00
|
|
|
if (!Py_NoSiteFlag)
|
|
|
|
initsite(); /* Module site */
|
2003-02-25 16:25:12 -04:00
|
|
|
|
2003-04-19 12:41:53 -03:00
|
|
|
/* auto-thread-state API, if available */
|
|
|
|
#ifdef WITH_THREAD
|
|
|
|
_PyGILState_Init(interp, tstate);
|
|
|
|
#endif /* WITH_THREAD */
|
|
|
|
|
2003-07-15 20:03:55 -03:00
|
|
|
warnings_module = PyImport_ImportModule("warnings");
|
|
|
|
if (!warnings_module)
|
|
|
|
PyErr_Clear();
|
2003-03-05 11:13:47 -04:00
|
|
|
|
|
|
|
#if defined(Py_USING_UNICODE) && defined(HAVE_LANGINFO_H) && defined(CODESET)
|
|
|
|
/* On Unix, set the file system encoding according to the
|
|
|
|
user's preference, if the CODESET names a well-known
|
|
|
|
Python codec, and Py_FileSystemDefaultEncoding isn't
|
2003-08-09 06:47:11 -03:00
|
|
|
initialized by other means. Also set the encoding of
|
|
|
|
stdin and stdout if these are terminals. */
|
|
|
|
|
2003-11-13 03:43:21 -04:00
|
|
|
saved_locale = strdup(setlocale(LC_CTYPE, NULL));
|
2003-08-09 06:47:11 -03:00
|
|
|
setlocale(LC_CTYPE, "");
|
|
|
|
codeset = nl_langinfo(CODESET);
|
|
|
|
if (codeset && *codeset) {
|
|
|
|
PyObject *enc = PyCodec_Encoder(codeset);
|
|
|
|
if (enc) {
|
|
|
|
codeset = strdup(codeset);
|
|
|
|
Py_DECREF(enc);
|
|
|
|
} else {
|
|
|
|
codeset = NULL;
|
|
|
|
PyErr_Clear();
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
codeset = NULL;
|
|
|
|
setlocale(LC_CTYPE, saved_locale);
|
2003-11-13 03:43:21 -04:00
|
|
|
free(saved_locale);
|
2003-08-09 06:47:11 -03:00
|
|
|
|
|
|
|
if (codeset) {
|
2003-08-11 09:20:24 -03:00
|
|
|
sys_stream = PySys_GetObject("stdin");
|
2003-08-09 06:47:11 -03:00
|
|
|
sys_isatty = PyObject_CallMethod(sys_stream, "isatty", "");
|
|
|
|
if (!sys_isatty)
|
|
|
|
PyErr_Clear();
|
|
|
|
if(sys_isatty && PyObject_IsTrue(sys_isatty)) {
|
|
|
|
if (!PyFile_SetEncoding(sys_stream, codeset))
|
|
|
|
Py_FatalError("Cannot set codeset of stdin");
|
2003-03-05 11:13:47 -04:00
|
|
|
}
|
2003-08-09 06:47:11 -03:00
|
|
|
Py_XDECREF(sys_isatty);
|
|
|
|
|
|
|
|
sys_stream = PySys_GetObject("stdout");
|
|
|
|
sys_isatty = PyObject_CallMethod(sys_stream, "isatty", "");
|
|
|
|
if (!sys_isatty)
|
|
|
|
PyErr_Clear();
|
|
|
|
if(sys_isatty && PyObject_IsTrue(sys_isatty)) {
|
|
|
|
if (!PyFile_SetEncoding(sys_stream, codeset))
|
|
|
|
Py_FatalError("Cannot set codeset of stdout");
|
|
|
|
}
|
|
|
|
Py_XDECREF(sys_isatty);
|
|
|
|
|
|
|
|
if (!Py_FileSystemDefaultEncoding)
|
|
|
|
Py_FileSystemDefaultEncoding = codeset;
|
|
|
|
else
|
|
|
|
free(codeset);
|
2003-03-05 11:13:47 -04:00
|
|
|
}
|
|
|
|
#endif
|
1995-01-09 13:53:26 -04:00
|
|
|
}
|
|
|
|
|
2004-08-19 08:31:58 -03:00
|
|
|
void
|
|
|
|
Py_Initialize(void)
|
|
|
|
{
|
|
|
|
Py_InitializeEx(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-12-15 12:12:00 -04:00
|
|
|
#ifdef COUNT_ALLOCS
|
2000-07-09 00:09:57 -03:00
|
|
|
extern void dump_counts(void);
|
1998-12-15 12:12:00 -04:00
|
|
|
#endif
|
|
|
|
|
1997-08-02 00:10:38 -03: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 17:56:21 -03:00
|
|
|
|
|
|
|
void
|
2000-07-22 15:47:25 -03:00
|
|
|
Py_Finalize(void)
|
1997-05-05 17:56:21 -03:00
|
|
|
{
|
1997-08-02 00:10:38 -03:00
|
|
|
PyInterpreterState *interp;
|
1997-05-05 17:56:21 -03:00
|
|
|
PyThreadState *tstate;
|
1997-08-02 00:10:38 -03:00
|
|
|
|
1997-08-29 19:32:42 -03:00
|
|
|
if (!initialized)
|
1997-08-20 19:40:18 -03:00
|
|
|
return;
|
1997-08-02 00:10:38 -03:00
|
|
|
|
2001-01-20 23:40:37 -04:00
|
|
|
/* The interpreter is still entirely intact at this point, and the
|
|
|
|
* exit funcs may be relying on that. In particular, if some thread
|
|
|
|
* or exit func is still waiting to do an import, the import machinery
|
|
|
|
* expects Py_IsInitialized() to return true. So don't say the
|
|
|
|
* interpreter is uninitialized until after the exit funcs have run.
|
|
|
|
* Note that Threading.py uses an exit func to do a join on all the
|
|
|
|
* threads created thru it, so this also protects pending imports in
|
|
|
|
* the threads created via Threading.
|
|
|
|
*/
|
1998-01-19 18:00:38 -04:00
|
|
|
call_sys_exitfunc();
|
2001-01-20 23:40:37 -04:00
|
|
|
initialized = 0;
|
1998-01-19 18:00:38 -04:00
|
|
|
|
1997-11-03 17:58:47 -04:00
|
|
|
/* Get current thread state and interpreter pointer */
|
2004-03-24 18:22:12 -04:00
|
|
|
tstate = PyThreadState_GET();
|
1997-08-02 00:10:38 -03:00
|
|
|
interp = tstate->interp;
|
|
|
|
|
1997-11-03 17:58:47 -04:00
|
|
|
/* Disable signal handling */
|
|
|
|
PyOS_FiniInterrupts();
|
|
|
|
|
2003-02-18 20:33:33 -04:00
|
|
|
/* drop module references we saved */
|
2003-07-15 20:03:55 -03:00
|
|
|
Py_XDECREF(warnings_module);
|
|
|
|
warnings_module = NULL;
|
2003-02-18 20:33:33 -04:00
|
|
|
|
2003-04-17 14:29:22 -03:00
|
|
|
/* Collect garbage. This may call finalizers; it's nice to call these
|
2003-12-01 17:35:27 -04:00
|
|
|
* before all modules are destroyed.
|
|
|
|
* XXX If a __del__ or weakref callback is triggered here, and tries to
|
|
|
|
* XXX import a module, bad things can happen, because Python no
|
|
|
|
* XXX longer believes it's initialized.
|
|
|
|
* XXX Fatal Python error: Interpreter not initialized (version mismatch?)
|
|
|
|
* XXX is easy to provoke that way. I've also seen, e.g.,
|
|
|
|
* XXX Exception exceptions.ImportError: 'No module named sha'
|
|
|
|
* XXX in <function callback at 0x008F5718> ignored
|
|
|
|
* XXX but I'm unclear on exactly how that one happens. In any case,
|
|
|
|
* XXX I haven't seen a real-life report of either of these.
|
|
|
|
*/
|
2003-04-17 14:29:22 -03:00
|
|
|
PyGC_Collect();
|
|
|
|
|
1997-11-03 17:58:47 -04:00
|
|
|
/* Destroy all modules */
|
1997-08-02 00:10:38 -03:00
|
|
|
PyImport_Cleanup();
|
1997-11-03 17:58:47 -04:00
|
|
|
|
2003-04-17 14:29:22 -03:00
|
|
|
/* Collect final garbage. This disposes of cycles created by
|
2003-12-01 17:35:27 -04:00
|
|
|
* new-style class definitions, for example.
|
|
|
|
* XXX This is disabled because it caused too many problems. If
|
|
|
|
* XXX a __del__ or weakref callback triggers here, Python code has
|
|
|
|
* XXX a hard time running, because even the sys module has been
|
|
|
|
* XXX cleared out (sys.stdout is gone, sys.excepthook is gone, etc).
|
|
|
|
* XXX One symptom is a sequence of information-free messages
|
|
|
|
* XXX coming from threads (if a __del__ or callback is invoked,
|
|
|
|
* XXX other threads can execute too, and any exception they encounter
|
|
|
|
* XXX triggers a comedy of errors as subsystem after subsystem
|
|
|
|
* XXX fails to find what it *expects* to find in sys to help report
|
|
|
|
* XXX the exception and consequent unexpected failures). I've also
|
|
|
|
* XXX seen segfaults then, after adding print statements to the
|
|
|
|
* XXX Python code getting called.
|
|
|
|
*/
|
|
|
|
#if 0
|
2003-04-17 14:29:22 -03:00
|
|
|
PyGC_Collect();
|
2003-12-01 17:35:27 -04:00
|
|
|
#endif
|
2003-04-17 14:29:22 -03:00
|
|
|
|
1997-12-08 19:43:45 -04: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
|
|
|
|
|
2003-04-17 12:21:01 -03:00
|
|
|
#ifdef Py_TRACE_REFS
|
|
|
|
/* Display all objects still alive -- this can invoke arbitrary
|
|
|
|
* __repr__ overrides, so requires a mostly-intact interpreter.
|
|
|
|
* Alas, a lot of stuff may still be alive now that will be cleaned
|
|
|
|
* up later.
|
|
|
|
*/
|
2003-04-17 16:52:29 -03:00
|
|
|
if (Py_GETENV("PYTHONDUMPREFS"))
|
2003-04-17 12:21:01 -03:00
|
|
|
_Py_PrintReferences(stderr);
|
|
|
|
#endif /* Py_TRACE_REFS */
|
|
|
|
|
2003-04-22 08:18:00 -03:00
|
|
|
/* Cleanup auto-thread-state */
|
|
|
|
#ifdef WITH_THREAD
|
|
|
|
_PyGILState_Fini();
|
|
|
|
#endif /* WITH_THREAD */
|
|
|
|
|
2003-04-15 11:10:09 -03:00
|
|
|
/* Clear interpreter state */
|
2000-05-25 20:09:49 -03:00
|
|
|
PyInterpreterState_Clear(interp);
|
2003-04-15 11:10:09 -03:00
|
|
|
|
2005-03-29 09:36:16 -04: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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
_PyExc_Fini();
|
|
|
|
|
2003-04-15 11:10:09 -03:00
|
|
|
/* Delete current thread */
|
2000-05-25 20:09:49 -03:00
|
|
|
PyThreadState_Swap(NULL);
|
|
|
|
PyInterpreterState_Delete(interp);
|
|
|
|
|
2003-04-15 11:10:09 -03:00
|
|
|
/* Sundry finalizers */
|
1997-08-04 23:22:03 -03:00
|
|
|
PyMethod_Fini();
|
|
|
|
PyFrame_Fini();
|
|
|
|
PyCFunction_Fini();
|
|
|
|
PyTuple_Fini();
|
2004-10-07 00:58:07 -03:00
|
|
|
PyList_Fini();
|
2005-08-01 18:39:29 -03:00
|
|
|
PySet_Fini();
|
1997-08-02 00:10:38 -03:00
|
|
|
PyString_Fini();
|
1997-08-04 23:22:03 -03:00
|
|
|
PyInt_Fini();
|
|
|
|
PyFloat_Fini();
|
|
|
|
|
2002-04-08 05:19:36 -03:00
|
|
|
#ifdef Py_USING_UNICODE
|
|
|
|
/* Cleanup Unicode implementation */
|
|
|
|
_PyUnicode_Fini();
|
|
|
|
#endif
|
|
|
|
|
1997-08-04 23:22:03 -03:00
|
|
|
/* 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 00:10:38 -03:00
|
|
|
|
|
|
|
PyGrammar_RemoveAccelerators(&_PyParser_Grammar);
|
1997-08-04 23:22:03 -03:00
|
|
|
|
2003-04-17 16:52:29 -03:00
|
|
|
#ifdef Py_TRACE_REFS
|
|
|
|
/* Display addresses (& refcnts) of all objects still alive.
|
|
|
|
* An address can be used to find the repr of the object, printed
|
|
|
|
* above by _Py_PrintReferences.
|
|
|
|
*/
|
|
|
|
if (Py_GETENV("PYTHONDUMPREFS"))
|
|
|
|
_Py_PrintReferenceAddresses(stderr);
|
|
|
|
#endif /* Py_TRACE_REFS */
|
2002-04-13 05:29:14 -03:00
|
|
|
#ifdef PYMALLOC_DEBUG
|
|
|
|
if (Py_GETENV("PYTHONMALLOCSTATS"))
|
|
|
|
_PyObject_DebugMallocStats();
|
|
|
|
#endif
|
|
|
|
|
1997-08-04 23:22:03 -03:00
|
|
|
call_ll_exitfuncs();
|
1997-08-02 00:10:38 -03: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 *
|
2000-07-22 15:47:25 -03:00
|
|
|
Py_NewInterpreter(void)
|
1997-08-02 00:10:38 -03:00
|
|
|
{
|
1997-05-05 17:56:21 -03:00
|
|
|
PyInterpreterState *interp;
|
1997-08-02 00:10:38 -03:00
|
|
|
PyThreadState *tstate, *save_tstate;
|
|
|
|
PyObject *bimod, *sysmod;
|
1997-07-19 16:17:22 -03:00
|
|
|
|
1997-08-02 00:10:38 -03:00
|
|
|
if (!initialized)
|
|
|
|
Py_FatalError("Py_NewInterpreter: call Py_Initialize first");
|
1997-07-19 16:17:22 -03:00
|
|
|
|
1997-05-05 17:56:21 -03:00
|
|
|
interp = PyInterpreterState_New();
|
|
|
|
if (interp == NULL)
|
1997-08-02 00:10:38 -03:00
|
|
|
return NULL;
|
1997-07-19 16:17:22 -03:00
|
|
|
|
1997-05-05 17:56:21 -03:00
|
|
|
tstate = PyThreadState_New(interp);
|
1997-08-02 00:10:38 -03:00
|
|
|
if (tstate == NULL) {
|
|
|
|
PyInterpreterState_Delete(interp);
|
|
|
|
return NULL;
|
|
|
|
}
|
1997-05-05 17:56:21 -03:00
|
|
|
|
1997-08-02 00:10:38 -03:00
|
|
|
save_tstate = PyThreadState_Swap(tstate);
|
1997-05-05 17:56:21 -03:00
|
|
|
|
1997-08-02 00:10:38 -03:00
|
|
|
/* XXX The following is lax in error checking */
|
|
|
|
|
|
|
|
interp->modules = PyDict_New();
|
|
|
|
|
|
|
|
bimod = _PyImport_FindExtension("__builtin__", "__builtin__");
|
|
|
|
if (bimod != NULL) {
|
1997-11-04 15:36:18 -04:00
|
|
|
interp->builtins = PyModule_GetDict(bimod);
|
|
|
|
Py_INCREF(interp->builtins);
|
1997-08-02 00:10:38 -03: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);
|
2003-01-22 05:00:38 -04:00
|
|
|
_PyImportHooks_Init();
|
1997-08-02 00:10:38 -03:00
|
|
|
initmain();
|
1997-08-29 19:32:42 -03:00
|
|
|
if (!Py_NoSiteFlag)
|
|
|
|
initsite();
|
1997-08-02 00:10:38 -03: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
|
2000-07-22 15:47:25 -03:00
|
|
|
Py_EndInterpreter(PyThreadState *tstate)
|
1997-08-02 00:10:38 -03:00
|
|
|
{
|
|
|
|
PyInterpreterState *interp = tstate->interp;
|
|
|
|
|
2004-03-24 18:22:12 -04:00
|
|
|
if (tstate != PyThreadState_GET())
|
1997-08-02 00:10:38 -03:00
|
|
|
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 16:17:22 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static char *progname = "python";
|
|
|
|
|
|
|
|
void
|
2000-07-22 15:47:25 -03:00
|
|
|
Py_SetProgramName(char *pn)
|
1997-07-19 16:17:22 -03:00
|
|
|
{
|
|
|
|
if (pn && *pn)
|
|
|
|
progname = pn;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
2000-07-22 15:47:25 -03:00
|
|
|
Py_GetProgramName(void)
|
1997-07-19 16:17:22 -03:00
|
|
|
{
|
|
|
|
return progname;
|
1997-05-05 17:56:21 -03:00
|
|
|
}
|
|
|
|
|
1998-02-06 18:27:24 -04:00
|
|
|
static char *default_home = NULL;
|
|
|
|
|
|
|
|
void
|
2000-07-22 15:47:25 -03:00
|
|
|
Py_SetPythonHome(char *home)
|
1998-02-06 18:27:24 -04:00
|
|
|
{
|
|
|
|
default_home = home;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
2000-07-22 15:47:25 -03:00
|
|
|
Py_GetPythonHome(void)
|
1998-02-06 18:27:24 -04:00
|
|
|
{
|
|
|
|
char *home = default_home;
|
2001-07-23 13:30:27 -03:00
|
|
|
if (home == NULL && !Py_IgnoreEnvironmentFlag)
|
|
|
|
home = Py_GETENV("PYTHONHOME");
|
1998-02-06 18:27:24 -04:00
|
|
|
return home;
|
|
|
|
}
|
|
|
|
|
1995-01-09 13:53:26 -04:00
|
|
|
/* Create __main__ module */
|
|
|
|
|
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
initmain(void)
|
1995-01-09 13:53:26 -04:00
|
|
|
{
|
1997-03-04 20:20:32 -04:00
|
|
|
PyObject *m, *d;
|
|
|
|
m = PyImport_AddModule("__main__");
|
1995-01-09 13:53:26 -04:00
|
|
|
if (m == NULL)
|
1997-03-04 20:20:32 -04:00
|
|
|
Py_FatalError("can't create __main__ module");
|
|
|
|
d = PyModule_GetDict(m);
|
|
|
|
if (PyDict_GetItemString(d, "__builtins__") == NULL) {
|
1997-11-19 12:15:37 -04:00
|
|
|
PyObject *bimod = PyImport_ImportModule("__builtin__");
|
|
|
|
if (bimod == NULL ||
|
|
|
|
PyDict_SetItemString(d, "__builtins__", bimod) != 0)
|
1997-03-04 20:20:32 -04:00
|
|
|
Py_FatalError("can't add __builtins__ to __main__");
|
1999-01-29 17:30:22 -04:00
|
|
|
Py_DECREF(bimod);
|
1995-01-09 13:53:26 -04:00
|
|
|
}
|
1992-08-04 09:41:02 -03:00
|
|
|
}
|
|
|
|
|
1997-08-29 19:32:42 -03:00
|
|
|
/* Import the site module (not into __main__ though) */
|
|
|
|
|
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
initsite(void)
|
1997-08-29 19:32:42 -03:00
|
|
|
{
|
|
|
|
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 09:41:02 -03:00
|
|
|
/* Parse input from a file and execute it */
|
|
|
|
|
|
|
|
int
|
2005-10-20 16:59:25 -03:00
|
|
|
PyRun_AnyFileExFlags(FILE *fp, char *filename, int closeit,
|
2001-03-21 22:47:58 -04:00
|
|
|
PyCompilerFlags *flags)
|
1992-08-04 09:41:02 -03:00
|
|
|
{
|
|
|
|
if (filename == NULL)
|
|
|
|
filename = "???";
|
2000-08-27 16:21:52 -03:00
|
|
|
if (Py_FdIsInteractive(fp, filename)) {
|
2001-03-21 22:47:58 -04:00
|
|
|
int err = PyRun_InteractiveLoopFlags(fp, filename, flags);
|
2000-08-27 16:21:52 -03:00
|
|
|
if (closeit)
|
|
|
|
fclose(fp);
|
|
|
|
return err;
|
|
|
|
}
|
1992-08-04 09:41:02 -03:00
|
|
|
else
|
2001-03-21 22:47:58 -04:00
|
|
|
return PyRun_SimpleFileExFlags(fp, filename, closeit, flags);
|
1992-08-04 09:41:02 -03:00
|
|
|
}
|
|
|
|
|
2001-03-21 22:47:58 -04:00
|
|
|
int
|
2002-12-11 10:04:59 -04:00
|
|
|
PyRun_InteractiveLoopFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
|
1992-08-04 09:41:02 -03:00
|
|
|
{
|
1997-03-04 20:20:32 -04:00
|
|
|
PyObject *v;
|
1992-08-04 09:41:02 -03:00
|
|
|
int ret;
|
2001-03-21 22:47:58 -04:00
|
|
|
PyCompilerFlags local_flags;
|
2001-03-01 18:59:14 -04:00
|
|
|
|
2001-03-21 22:47:58 -04:00
|
|
|
if (flags == NULL) {
|
|
|
|
flags = &local_flags;
|
2001-07-15 23:29:45 -03:00
|
|
|
local_flags.cf_flags = 0;
|
2001-03-21 22:47:58 -04:00
|
|
|
}
|
1997-03-04 20:20:32 -04:00
|
|
|
v = PySys_GetObject("ps1");
|
1992-08-04 09:41:02 -03:00
|
|
|
if (v == NULL) {
|
1997-03-04 20:20:32 -04:00
|
|
|
PySys_SetObject("ps1", v = PyString_FromString(">>> "));
|
|
|
|
Py_XDECREF(v);
|
1992-08-04 09:41:02 -03:00
|
|
|
}
|
1997-03-04 20:20:32 -04:00
|
|
|
v = PySys_GetObject("ps2");
|
1992-08-04 09:41:02 -03:00
|
|
|
if (v == NULL) {
|
1997-03-04 20:20:32 -04:00
|
|
|
PySys_SetObject("ps2", v = PyString_FromString("... "));
|
|
|
|
Py_XDECREF(v);
|
1992-08-04 09:41:02 -03:00
|
|
|
}
|
|
|
|
for (;;) {
|
2001-03-21 22:47:58 -04:00
|
|
|
ret = PyRun_InteractiveOneFlags(fp, filename, flags);
|
1996-05-22 13:35:33 -03:00
|
|
|
#ifdef Py_REF_DEBUG
|
1995-03-29 12:57:48 -04:00
|
|
|
fprintf(stderr, "[%ld refs]\n", _Py_RefTotal);
|
1992-08-04 09:41:02 -03:00
|
|
|
#endif
|
|
|
|
if (ret == E_EOF)
|
|
|
|
return 0;
|
|
|
|
/*
|
|
|
|
if (ret == E_NOMEM)
|
|
|
|
return -1;
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-03-22 19:53:36 -04:00
|
|
|
/* compute parser flags based on compiler flags */
|
|
|
|
#define PARSER_FLAGS(flags) \
|
2003-02-13 18:07:59 -04:00
|
|
|
(((flags) && (flags)->cf_flags & PyCF_DONT_IMPLY_DEDENT) ? \
|
|
|
|
PyPARSE_DONT_IMPLY_DEDENT : 0)
|
2002-03-22 19:53:36 -04:00
|
|
|
|
2001-03-01 18:59:14 -04:00
|
|
|
int
|
2002-12-11 10:04:59 -04:00
|
|
|
PyRun_InteractiveOneFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
|
1992-08-04 09:41:02 -03:00
|
|
|
{
|
1997-03-04 20:20:32 -04:00
|
|
|
PyObject *m, *d, *v, *w;
|
2005-10-20 16:59:25 -03:00
|
|
|
mod_ty mod;
|
1997-11-25 16:58:13 -04:00
|
|
|
char *ps1 = "", *ps2 = "";
|
2005-10-20 16:59:25 -03:00
|
|
|
int errcode = 0;
|
2001-07-16 02:37:24 -03:00
|
|
|
|
1997-03-04 20:20:32 -04:00
|
|
|
v = PySys_GetObject("ps1");
|
1997-11-25 16:58:13 -04: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 09:41:02 -03:00
|
|
|
}
|
1997-11-25 16:58:13 -04: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 09:41:02 -03:00
|
|
|
}
|
2005-10-20 16:59:25 -03:00
|
|
|
mod = PyParser_ASTFromFile(fp, filename,
|
|
|
|
Py_single_input, ps1, ps2,
|
|
|
|
flags, &errcode);
|
1997-03-04 20:20:32 -04:00
|
|
|
Py_XDECREF(v);
|
|
|
|
Py_XDECREF(w);
|
2005-10-20 16:59:25 -03:00
|
|
|
if (mod == NULL) {
|
|
|
|
if (errcode == E_EOF) {
|
|
|
|
PyErr_Clear();
|
1994-08-29 09:50:44 -03:00
|
|
|
return E_EOF;
|
|
|
|
}
|
1997-03-04 20:20:32 -04:00
|
|
|
PyErr_Print();
|
2005-10-20 16:59:25 -03:00
|
|
|
return -1;
|
1992-08-04 09:41:02 -03:00
|
|
|
}
|
1997-03-04 20:20:32 -04:00
|
|
|
m = PyImport_AddModule("__main__");
|
1992-08-04 09:41:02 -03:00
|
|
|
if (m == NULL)
|
|
|
|
return -1;
|
1997-03-04 20:20:32 -04:00
|
|
|
d = PyModule_GetDict(m);
|
2005-10-20 16:59:25 -03:00
|
|
|
v = run_mod(mod, filename, d, d, flags);
|
|
|
|
free_mod(mod);
|
1992-08-04 09:41:02 -03:00
|
|
|
if (v == NULL) {
|
1997-03-04 20:20:32 -04:00
|
|
|
PyErr_Print();
|
1992-08-04 09:41:02 -03:00
|
|
|
return -1;
|
|
|
|
}
|
1997-03-04 20:20:32 -04:00
|
|
|
Py_DECREF(v);
|
1997-05-22 19:35:04 -03:00
|
|
|
if (Py_FlushLine())
|
|
|
|
PyErr_Clear();
|
1992-08-04 09:41:02 -03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-01-04 16:30:56 -04:00
|
|
|
/* Check whether a file maybe a pyc file: Look at the extension,
|
|
|
|
the file type, and, if we may close it, at the first few bytes. */
|
|
|
|
|
|
|
|
static int
|
2002-12-11 10:04:59 -04:00
|
|
|
maybe_pyc_file(FILE *fp, const char* filename, const char* ext, int closeit)
|
2001-01-04 16:30:56 -04:00
|
|
|
{
|
|
|
|
if (strcmp(ext, ".pyc") == 0 || strcmp(ext, ".pyo") == 0)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
/* Only look into the file if we are allowed to close it, since
|
|
|
|
it then should also be seekable. */
|
|
|
|
if (closeit) {
|
|
|
|
/* Read only two bytes of the magic. If the file was opened in
|
|
|
|
text mode, the bytes 3 and 4 of the magic (\r\n) might not
|
|
|
|
be read as they are on disk. */
|
|
|
|
unsigned int halfmagic = PyImport_GetMagicNumber() & 0xFFFF;
|
|
|
|
unsigned char buf[2];
|
2001-02-11 00:35:39 -04:00
|
|
|
/* Mess: In case of -x, the stream is NOT at its start now,
|
|
|
|
and ungetc() was used to push back the first newline,
|
2001-02-17 18:02:34 -04:00
|
|
|
which makes the current stream position formally undefined,
|
|
|
|
and a x-platform nightmare.
|
|
|
|
Unfortunately, we have no direct way to know whether -x
|
|
|
|
was specified. So we use a terrible hack: if the current
|
|
|
|
stream position is not 0, we assume -x was specified, and
|
|
|
|
give up. Bug 132850 on SourceForge spells out the
|
|
|
|
hopelessness of trying anything else (fseek and ftell
|
|
|
|
don't work predictably x-platform for text-mode files).
|
2001-02-11 00:35:39 -04:00
|
|
|
*/
|
|
|
|
int ispyc = 0;
|
2001-02-17 18:02:34 -04:00
|
|
|
if (ftell(fp) == 0) {
|
|
|
|
if (fread(buf, 1, 2, fp) == 2 &&
|
2003-04-17 12:24:21 -03:00
|
|
|
((unsigned int)buf[1]<<8 | buf[0]) == halfmagic)
|
2001-02-17 18:02:34 -04:00
|
|
|
ispyc = 1;
|
|
|
|
rewind(fp);
|
|
|
|
}
|
2001-02-11 00:35:39 -04:00
|
|
|
return ispyc;
|
2001-01-04 16:30:56 -04:00
|
|
|
}
|
|
|
|
return 0;
|
2003-04-17 12:24:21 -03:00
|
|
|
}
|
2001-01-04 16:30:56 -04:00
|
|
|
|
2001-03-21 22:47:58 -04:00
|
|
|
int
|
2002-12-11 10:04:59 -04:00
|
|
|
PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit,
|
2001-03-21 22:47:58 -04:00
|
|
|
PyCompilerFlags *flags)
|
1992-08-04 09:41:02 -03:00
|
|
|
{
|
1997-03-04 20:20:32 -04:00
|
|
|
PyObject *m, *d, *v;
|
2002-12-11 10:04:59 -04:00
|
|
|
const char *ext;
|
1994-09-14 10:31:04 -03:00
|
|
|
|
1997-03-04 20:20:32 -04:00
|
|
|
m = PyImport_AddModule("__main__");
|
1992-08-04 09:41:02 -03:00
|
|
|
if (m == NULL)
|
|
|
|
return -1;
|
1997-03-04 20:20:32 -04:00
|
|
|
d = PyModule_GetDict(m);
|
2002-10-17 18:24:58 -03:00
|
|
|
if (PyDict_GetItemString(d, "__file__") == NULL) {
|
|
|
|
PyObject *f = PyString_FromString(filename);
|
|
|
|
if (f == NULL)
|
|
|
|
return -1;
|
|
|
|
if (PyDict_SetItemString(d, "__file__", f) < 0) {
|
|
|
|
Py_DECREF(f);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
Py_DECREF(f);
|
|
|
|
}
|
1994-09-14 10:31:04 -03:00
|
|
|
ext = filename + strlen(filename) - 4;
|
2001-01-04 16:30:56 -04:00
|
|
|
if (maybe_pyc_file(fp, filename, ext, closeit)) {
|
1994-09-14 10:31:04 -03:00
|
|
|
/* Try to run a pyc file. First, re-open in binary */
|
2000-08-27 16:21:52 -03:00
|
|
|
if (closeit)
|
|
|
|
fclose(fp);
|
2002-10-17 18:24:58 -03:00
|
|
|
if ((fp = fopen(filename, "rb")) == NULL) {
|
1994-09-14 10:31:04 -03:00
|
|
|
fprintf(stderr, "python: Can't reopen .pyc file\n");
|
|
|
|
return -1;
|
|
|
|
}
|
1997-04-02 01:28:38 -04:00
|
|
|
/* Turn on optimization if a .pyo file is given */
|
|
|
|
if (strcmp(ext, ".pyo") == 0)
|
|
|
|
Py_OptimizeFlag = 1;
|
2001-03-21 22:47:58 -04:00
|
|
|
v = run_pyc_file(fp, filename, d, d, flags);
|
1994-09-14 10:31:04 -03:00
|
|
|
} else {
|
2003-04-17 12:24:21 -03:00
|
|
|
v = PyRun_FileExFlags(fp, filename, Py_file_input, d, d,
|
2001-03-21 22:47:58 -04:00
|
|
|
closeit, flags);
|
1994-09-14 10:31:04 -03:00
|
|
|
}
|
1992-08-04 09:41:02 -03:00
|
|
|
if (v == NULL) {
|
1997-03-04 20:20:32 -04:00
|
|
|
PyErr_Print();
|
1992-08-04 09:41:02 -03:00
|
|
|
return -1;
|
|
|
|
}
|
1997-03-04 20:20:32 -04:00
|
|
|
Py_DECREF(v);
|
1997-05-22 19:35:04 -03:00
|
|
|
if (Py_FlushLine())
|
|
|
|
PyErr_Clear();
|
1992-08-04 09:41:02 -03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
Add warning mode for classic division, almost exactly as specified in
PEP 238. Changes:
- add a new flag variable Py_DivisionWarningFlag, declared in
pydebug.h, defined in object.c, set in main.c, and used in
{int,long,float,complex}object.c. When this flag is set, the
classic division operator issues a DeprecationWarning message.
- add a new API PyRun_SimpleStringFlags() to match
PyRun_SimpleString(). The main() function calls this so that
commands run with -c can also benefit from -Dnew.
- While I was at it, I changed the usage message in main() somewhat:
alphabetized the options, split it in *four* parts to fit in under
512 bytes (not that I still believe this is necessary -- doc strings
elsewhere are much longer), and perhaps most visibly, don't display
the full list of options on each command line error. Instead, the
full list is only displayed when -h is used, and otherwise a brief
reminder of -h is displayed. When -h is used, write to stdout so
that you can do `python -h | more'.
Notes:
- I don't want to use the -W option to control whether the classic
division warning is issued or not, because the machinery to decide
whether to display the warning or not is very expensive (it involves
calling into the warnings.py module). You can use -Werror to turn
the warnings into exceptions though.
- The -Dnew option doesn't select future division for all of the
program -- only for the __main__ module. I don't know if I'll ever
change this -- it would require changes to the .pyc file magic
number to do it right, and a more global notion of compiler flags.
- You can usefully combine -Dwarn and -Dnew: this gives the __main__
module new division, and warns about classic division everywhere
else.
2001-08-31 14:40:15 -03:00
|
|
|
int
|
2002-12-11 10:04:59 -04:00
|
|
|
PyRun_SimpleStringFlags(const char *command, PyCompilerFlags *flags)
|
1992-08-04 09:41:02 -03:00
|
|
|
{
|
1997-03-04 20:20:32 -04:00
|
|
|
PyObject *m, *d, *v;
|
|
|
|
m = PyImport_AddModule("__main__");
|
1992-08-04 09:41:02 -03:00
|
|
|
if (m == NULL)
|
|
|
|
return -1;
|
1997-03-04 20:20:32 -04:00
|
|
|
d = PyModule_GetDict(m);
|
Add warning mode for classic division, almost exactly as specified in
PEP 238. Changes:
- add a new flag variable Py_DivisionWarningFlag, declared in
pydebug.h, defined in object.c, set in main.c, and used in
{int,long,float,complex}object.c. When this flag is set, the
classic division operator issues a DeprecationWarning message.
- add a new API PyRun_SimpleStringFlags() to match
PyRun_SimpleString(). The main() function calls this so that
commands run with -c can also benefit from -Dnew.
- While I was at it, I changed the usage message in main() somewhat:
alphabetized the options, split it in *four* parts to fit in under
512 bytes (not that I still believe this is necessary -- doc strings
elsewhere are much longer), and perhaps most visibly, don't display
the full list of options on each command line error. Instead, the
full list is only displayed when -h is used, and otherwise a brief
reminder of -h is displayed. When -h is used, write to stdout so
that you can do `python -h | more'.
Notes:
- I don't want to use the -W option to control whether the classic
division warning is issued or not, because the machinery to decide
whether to display the warning or not is very expensive (it involves
calling into the warnings.py module). You can use -Werror to turn
the warnings into exceptions though.
- The -Dnew option doesn't select future division for all of the
program -- only for the __main__ module. I don't know if I'll ever
change this -- it would require changes to the .pyc file magic
number to do it right, and a more global notion of compiler flags.
- You can usefully combine -Dwarn and -Dnew: this gives the __main__
module new division, and warns about classic division everywhere
else.
2001-08-31 14:40:15 -03:00
|
|
|
v = PyRun_StringFlags(command, Py_file_input, d, d, flags);
|
1992-08-04 09:41:02 -03:00
|
|
|
if (v == NULL) {
|
1997-03-04 20:20:32 -04:00
|
|
|
PyErr_Print();
|
1992-08-04 09:41:02 -03:00
|
|
|
return -1;
|
|
|
|
}
|
1997-03-04 20:20:32 -04:00
|
|
|
Py_DECREF(v);
|
1997-05-22 19:35:04 -03:00
|
|
|
if (Py_FlushLine())
|
|
|
|
PyErr_Clear();
|
1992-08-04 09:41:02 -03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1997-08-29 19:07:17 -03:00
|
|
|
static int
|
2002-12-11 10:04:59 -04:00
|
|
|
parse_syntax_error(PyObject *err, PyObject **message, const char **filename,
|
|
|
|
int *lineno, int *offset, const char **text)
|
1997-08-29 19:07:17 -03:00
|
|
|
{
|
|
|
|
long hold;
|
|
|
|
PyObject *v;
|
|
|
|
|
|
|
|
/* old style errors */
|
|
|
|
if (PyTuple_Check(err))
|
2002-03-31 21:41:20 -04:00
|
|
|
return PyArg_ParseTuple(err, "O(ziiz)", message, filename,
|
|
|
|
lineno, offset, text);
|
1997-08-29 19:07:17 -03:00
|
|
|
|
|
|
|
/* 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;
|
2001-02-28 03:07:43 -04:00
|
|
|
if (v == Py_None) {
|
|
|
|
*offset = -1;
|
|
|
|
Py_DECREF(v);
|
|
|
|
v = NULL;
|
|
|
|
} else {
|
|
|
|
hold = PyInt_AsLong(v);
|
|
|
|
Py_DECREF(v);
|
|
|
|
v = NULL;
|
|
|
|
if (hold < 0 && PyErr_Occurred())
|
|
|
|
goto finally;
|
|
|
|
*offset = (int)hold;
|
|
|
|
}
|
1997-08-29 19:07:17 -03:00
|
|
|
|
|
|
|
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 09:41:02 -03:00
|
|
|
void
|
2000-07-22 15:47:25 -03:00
|
|
|
PyErr_Print(void)
|
1998-02-06 18:27:24 -04:00
|
|
|
{
|
|
|
|
PyErr_PrintEx(1);
|
|
|
|
}
|
|
|
|
|
2001-02-28 03:07:43 -04:00
|
|
|
static void
|
2002-12-11 10:04:59 -04:00
|
|
|
print_error_text(PyObject *f, int offset, const char *text)
|
2001-02-28 03:07:43 -04:00
|
|
|
{
|
|
|
|
char *nl;
|
|
|
|
if (offset >= 0) {
|
|
|
|
if (offset > 0 && offset == (int)strlen(text))
|
|
|
|
offset--;
|
|
|
|
for (;;) {
|
|
|
|
nl = strchr(text, '\n');
|
|
|
|
if (nl == NULL || nl-text >= offset)
|
|
|
|
break;
|
|
|
|
offset -= (nl+1-text);
|
|
|
|
text = nl+1;
|
|
|
|
}
|
|
|
|
while (*text == ' ' || *text == '\t') {
|
|
|
|
text++;
|
|
|
|
offset--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
PyFile_WriteString(" ", f);
|
|
|
|
PyFile_WriteString(text, f);
|
|
|
|
if (*text == '\0' || text[strlen(text)-1] != '\n')
|
|
|
|
PyFile_WriteString("\n", f);
|
|
|
|
if (offset == -1)
|
|
|
|
return;
|
|
|
|
PyFile_WriteString(" ", f);
|
|
|
|
offset--;
|
|
|
|
while (offset > 0) {
|
|
|
|
PyFile_WriteString(" ", f);
|
|
|
|
offset--;
|
|
|
|
}
|
|
|
|
PyFile_WriteString("^\n", f);
|
|
|
|
}
|
|
|
|
|
2001-03-23 13:54:43 -04:00
|
|
|
static void
|
|
|
|
handle_system_exit(void)
|
2001-03-23 11:36:41 -04:00
|
|
|
{
|
2001-03-23 13:54:43 -04:00
|
|
|
PyObject *exception, *value, *tb;
|
2003-04-19 15:47:02 -03:00
|
|
|
int exitcode = 0;
|
|
|
|
|
2001-03-23 13:54:43 -04:00
|
|
|
PyErr_Fetch(&exception, &value, &tb);
|
2001-03-23 11:36:41 -04:00
|
|
|
if (Py_FlushLine())
|
|
|
|
PyErr_Clear();
|
|
|
|
fflush(stdout);
|
|
|
|
if (value == NULL || value == Py_None)
|
2003-04-19 15:47:02 -03:00
|
|
|
goto done;
|
2001-03-23 11:36:41 -04:00
|
|
|
if (PyInstance_Check(value)) {
|
|
|
|
/* The error code should be in the `code' attribute. */
|
|
|
|
PyObject *code = PyObject_GetAttrString(value, "code");
|
|
|
|
if (code) {
|
|
|
|
Py_DECREF(value);
|
|
|
|
value = code;
|
|
|
|
if (value == Py_None)
|
2003-04-19 15:47:02 -03:00
|
|
|
goto done;
|
2001-03-23 11:36:41 -04:00
|
|
|
}
|
|
|
|
/* If we failed to dig out the 'code' attribute,
|
|
|
|
just let the else clause below print the error. */
|
|
|
|
}
|
|
|
|
if (PyInt_Check(value))
|
2003-04-19 15:47:02 -03:00
|
|
|
exitcode = (int)PyInt_AsLong(value);
|
2001-03-23 11:36:41 -04:00
|
|
|
else {
|
|
|
|
PyObject_Print(value, stderr, Py_PRINT_RAW);
|
|
|
|
PySys_WriteStderr("\n");
|
2003-04-19 15:47:02 -03:00
|
|
|
exitcode = 1;
|
2001-03-23 11:36:41 -04:00
|
|
|
}
|
2003-04-19 15:47:02 -03:00
|
|
|
done:
|
|
|
|
/* Restore and clear the exception info, in order to properly decref
|
|
|
|
* the exception, value, and traceback. If we just exit instead,
|
|
|
|
* these leak, which confuses PYTHONDUMPREFS output, and may prevent
|
|
|
|
* some finalizers from running.
|
|
|
|
*/
|
|
|
|
PyErr_Restore(exception, value, tb);
|
|
|
|
PyErr_Clear();
|
|
|
|
Py_Exit(exitcode);
|
|
|
|
/* NOTREACHED */
|
2001-03-23 11:36:41 -04:00
|
|
|
}
|
|
|
|
|
1998-02-06 18:27:24 -04:00
|
|
|
void
|
2000-07-22 15:47:25 -03:00
|
|
|
PyErr_PrintEx(int set_sys_last_vars)
|
1992-08-04 09:41:02 -03:00
|
|
|
{
|
2001-03-22 22:46:52 -04:00
|
|
|
PyObject *exception, *v, *tb, *hook;
|
2001-03-23 13:54:43 -04:00
|
|
|
|
|
|
|
if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
|
|
|
|
handle_system_exit();
|
|
|
|
}
|
1997-03-04 20:20:32 -04:00
|
|
|
PyErr_Fetch(&exception, &v, &tb);
|
2005-10-20 16:59:25 -03:00
|
|
|
if (exception == NULL)
|
|
|
|
return;
|
1997-08-29 19:07:17 -03:00
|
|
|
PyErr_NormalizeException(&exception, &v, &tb);
|
1995-01-02 15:04:15 -04:00
|
|
|
if (exception == NULL)
|
1997-05-22 21:19:20 -03:00
|
|
|
return;
|
1998-02-06 18:27:24 -04:00
|
|
|
if (set_sys_last_vars) {
|
|
|
|
PySys_SetObject("last_type", exception);
|
|
|
|
PySys_SetObject("last_value", v);
|
|
|
|
PySys_SetObject("last_traceback", tb);
|
|
|
|
}
|
2001-03-22 22:46:52 -04:00
|
|
|
hook = PySys_GetObject("excepthook");
|
|
|
|
if (hook) {
|
2003-10-12 16:09:37 -03:00
|
|
|
PyObject *args = PyTuple_Pack(3,
|
2001-03-22 22:46:52 -04:00
|
|
|
exception, v ? v : Py_None, tb ? tb : Py_None);
|
|
|
|
PyObject *result = PyEval_CallObject(hook, args);
|
|
|
|
if (result == NULL) {
|
|
|
|
PyObject *exception2, *v2, *tb2;
|
2001-03-23 13:54:43 -04:00
|
|
|
if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
|
|
|
|
handle_system_exit();
|
|
|
|
}
|
2001-03-22 22:46:52 -04:00
|
|
|
PyErr_Fetch(&exception2, &v2, &tb2);
|
|
|
|
PyErr_NormalizeException(&exception2, &v2, &tb2);
|
|
|
|
if (Py_FlushLine())
|
|
|
|
PyErr_Clear();
|
|
|
|
fflush(stdout);
|
|
|
|
PySys_WriteStderr("Error in sys.excepthook:\n");
|
|
|
|
PyErr_Display(exception2, v2, tb2);
|
|
|
|
PySys_WriteStderr("\nOriginal exception was:\n");
|
|
|
|
PyErr_Display(exception, v, tb);
|
2001-12-07 11:35:35 -04:00
|
|
|
Py_XDECREF(exception2);
|
|
|
|
Py_XDECREF(v2);
|
|
|
|
Py_XDECREF(tb2);
|
2001-03-22 22:46:52 -04:00
|
|
|
}
|
|
|
|
Py_XDECREF(result);
|
|
|
|
Py_XDECREF(args);
|
|
|
|
} else {
|
|
|
|
PySys_WriteStderr("sys.excepthook is missing\n");
|
|
|
|
PyErr_Display(exception, v, tb);
|
|
|
|
}
|
|
|
|
Py_XDECREF(exception);
|
|
|
|
Py_XDECREF(v);
|
|
|
|
Py_XDECREF(tb);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PyErr_Display(PyObject *exception, PyObject *value, PyObject *tb)
|
|
|
|
{
|
|
|
|
int err = 0;
|
|
|
|
PyObject *f = PySys_GetObject("stderr");
|
2004-03-22 16:16:58 -04:00
|
|
|
Py_INCREF(value);
|
1992-09-25 18:59:05 -03:00
|
|
|
if (f == NULL)
|
|
|
|
fprintf(stderr, "lost sys.stderr\n");
|
|
|
|
else {
|
1998-02-28 00:31:39 -04:00
|
|
|
if (Py_FlushLine())
|
|
|
|
PyErr_Clear();
|
1997-05-22 19:35:04 -03:00
|
|
|
fflush(stdout);
|
2001-03-22 22:46:52 -04:00
|
|
|
if (tb && tb != Py_None)
|
|
|
|
err = PyTraceBack_Print(tb, f);
|
1997-08-26 15:09:48 -03:00
|
|
|
if (err == 0 &&
|
2004-03-22 16:16:58 -04:00
|
|
|
PyObject_HasAttrString(value, "print_file_and_line"))
|
1997-08-26 15:09:48 -03:00
|
|
|
{
|
1997-03-04 20:20:32 -04:00
|
|
|
PyObject *message;
|
2002-12-11 10:04:59 -04:00
|
|
|
const char *filename, *text;
|
1994-08-29 09:50:44 -03:00
|
|
|
int lineno, offset;
|
2004-03-22 16:16:58 -04:00
|
|
|
if (!parse_syntax_error(value, &message, &filename,
|
1997-08-29 19:07:17 -03:00
|
|
|
&lineno, &offset, &text))
|
1997-03-04 20:20:32 -04:00
|
|
|
PyErr_Clear();
|
1994-08-29 09:50:44 -03:00
|
|
|
else {
|
|
|
|
char buf[10];
|
1997-03-04 20:20:32 -04:00
|
|
|
PyFile_WriteString(" File \"", f);
|
1994-08-29 09:50:44 -03:00
|
|
|
if (filename == NULL)
|
1997-03-04 20:20:32 -04:00
|
|
|
PyFile_WriteString("<string>", f);
|
1994-08-29 09:50:44 -03:00
|
|
|
else
|
1997-03-04 20:20:32 -04:00
|
|
|
PyFile_WriteString(filename, f);
|
|
|
|
PyFile_WriteString("\", line ", f);
|
2001-11-28 16:42:20 -04:00
|
|
|
PyOS_snprintf(buf, sizeof(buf), "%d", lineno);
|
1997-03-04 20:20:32 -04:00
|
|
|
PyFile_WriteString(buf, f);
|
|
|
|
PyFile_WriteString("\n", f);
|
2001-02-28 03:07:43 -04:00
|
|
|
if (text != NULL)
|
|
|
|
print_error_text(f, offset, text);
|
2004-03-22 16:16:58 -04:00
|
|
|
Py_DECREF(value);
|
|
|
|
value = message;
|
1997-05-22 19:35:04 -03:00
|
|
|
/* Can't be bothered to check all those
|
|
|
|
PyFile_WriteString() calls */
|
|
|
|
if (PyErr_Occurred())
|
|
|
|
err = -1;
|
1994-08-29 09:50:44 -03:00
|
|
|
}
|
|
|
|
}
|
1997-05-22 19:35:04 -03:00
|
|
|
if (err) {
|
|
|
|
/* Don't do anything else */
|
|
|
|
}
|
|
|
|
else if (PyClass_Check(exception)) {
|
1997-09-16 18:42:03 -03:00
|
|
|
PyClassObject* exc = (PyClassObject*)exception;
|
|
|
|
PyObject* className = exc->cl_name;
|
|
|
|
PyObject* moduleName =
|
|
|
|
PyDict_GetItemString(exc->cl_dict, "__module__");
|
|
|
|
|
|
|
|
if (moduleName == NULL)
|
1997-05-22 19:35:04 -03:00
|
|
|
err = PyFile_WriteString("<unknown>", f);
|
1997-09-16 18:42:03 -03:00
|
|
|
else {
|
|
|
|
char* modstr = PyString_AsString(moduleName);
|
2003-04-17 12:24:21 -03:00
|
|
|
if (modstr && strcmp(modstr, "exceptions"))
|
1997-09-16 18:42:03 -03:00
|
|
|
{
|
|
|
|
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 11:30:45 -04:00
|
|
|
}
|
1997-05-22 19:35:04 -03:00
|
|
|
else
|
|
|
|
err = PyFile_WriteObject(exception, f, Py_PRINT_RAW);
|
|
|
|
if (err == 0) {
|
2004-03-22 16:16:58 -04:00
|
|
|
if (value != Py_None) {
|
|
|
|
PyObject *s = PyObject_Str(value);
|
1997-08-29 19:07:17 -03:00
|
|
|
/* only print colon if the str() of the
|
|
|
|
object is not the empty string
|
|
|
|
*/
|
1997-09-05 16:11:53 -03:00
|
|
|
if (s == NULL)
|
|
|
|
err = -1;
|
|
|
|
else if (!PyString_Check(s) ||
|
|
|
|
PyString_GET_SIZE(s) != 0)
|
1997-08-29 19:07:17 -03:00
|
|
|
err = PyFile_WriteString(": ", f);
|
1997-05-22 19:35:04 -03:00
|
|
|
if (err == 0)
|
1997-09-05 16:11:53 -03:00
|
|
|
err = PyFile_WriteObject(s, f, Py_PRINT_RAW);
|
|
|
|
Py_XDECREF(s);
|
1997-05-22 19:35:04 -03:00
|
|
|
}
|
1992-09-25 18:59:05 -03:00
|
|
|
}
|
1997-05-22 19:35:04 -03:00
|
|
|
if (err == 0)
|
|
|
|
err = PyFile_WriteString("\n", f);
|
1992-08-04 09:41:02 -03:00
|
|
|
}
|
2004-03-22 16:16:58 -04:00
|
|
|
Py_DECREF(value);
|
1997-05-22 19:35:04 -03: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 09:41:02 -03:00
|
|
|
}
|
|
|
|
|
1997-03-04 20:20:32 -04:00
|
|
|
PyObject *
|
2005-10-20 16:59:25 -03:00
|
|
|
PyRun_StringFlags(const char *str, int start, PyObject *globals,
|
|
|
|
PyObject *locals, PyCompilerFlags *flags)
|
1992-08-04 09:41:02 -03:00
|
|
|
{
|
2005-10-20 16:59:25 -03:00
|
|
|
PyObject *ret;
|
|
|
|
mod_ty mod = PyParser_ASTFromString(str, "<string>", start, flags);
|
|
|
|
ret = run_err_mod(mod, "<string>", globals, locals, flags);
|
|
|
|
free_mod(mod);
|
|
|
|
return ret;
|
2001-03-21 22:47:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
PyObject *
|
2002-12-11 10:04:59 -04:00
|
|
|
PyRun_FileExFlags(FILE *fp, const char *filename, int start, PyObject *globals,
|
2001-03-21 22:47:58 -04:00
|
|
|
PyObject *locals, int closeit, PyCompilerFlags *flags)
|
|
|
|
{
|
2005-10-20 16:59:25 -03:00
|
|
|
PyObject *ret;
|
|
|
|
mod_ty mod = PyParser_ASTFromFile(fp, filename, start, 0, 0,
|
|
|
|
flags, NULL);
|
|
|
|
if (mod == NULL)
|
|
|
|
return NULL;
|
2001-03-21 22:47:58 -04:00
|
|
|
if (closeit)
|
|
|
|
fclose(fp);
|
2005-10-20 16:59:25 -03:00
|
|
|
ret = run_err_mod(mod, filename, globals, locals, flags);
|
|
|
|
free_mod(mod);
|
|
|
|
return ret;
|
2001-03-21 22:47:58 -04:00
|
|
|
}
|
|
|
|
|
1997-03-04 20:20:32 -04:00
|
|
|
static PyObject *
|
2005-10-20 16:59:25 -03:00
|
|
|
run_err_mod(mod_ty mod, const char *filename, PyObject *globals,
|
|
|
|
PyObject *locals, PyCompilerFlags *flags)
|
1992-08-04 09:41:02 -03:00
|
|
|
{
|
2005-10-20 16:59:25 -03:00
|
|
|
if (mod == NULL)
|
1994-08-29 09:50:44 -03:00
|
|
|
return NULL;
|
2005-10-20 16:59:25 -03:00
|
|
|
return run_mod(mod, filename, globals, locals, flags);
|
1992-08-04 09:41:02 -03:00
|
|
|
}
|
|
|
|
|
1997-03-04 20:20:32 -04:00
|
|
|
static PyObject *
|
2005-10-20 16:59:25 -03:00
|
|
|
run_mod(mod_ty mod, const char *filename, PyObject *globals, PyObject *locals,
|
2001-03-01 18:59:14 -04:00
|
|
|
PyCompilerFlags *flags)
|
1992-08-04 09:41:02 -03:00
|
|
|
{
|
1997-03-04 20:20:32 -04:00
|
|
|
PyCodeObject *co;
|
|
|
|
PyObject *v;
|
2005-10-20 16:59:25 -03:00
|
|
|
co = PyAST_Compile(mod, filename, flags);
|
1992-08-04 09:41:02 -03:00
|
|
|
if (co == NULL)
|
|
|
|
return NULL;
|
1997-03-04 20:20:32 -04:00
|
|
|
v = PyEval_EvalCode(co, globals, locals);
|
|
|
|
Py_DECREF(co);
|
1992-08-04 09:41:02 -03:00
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
1997-03-04 20:20:32 -04:00
|
|
|
static PyObject *
|
2005-10-20 16:59:25 -03:00
|
|
|
run_pyc_file(FILE *fp, const char *filename, PyObject *globals,
|
|
|
|
PyObject *locals, PyCompilerFlags *flags)
|
1994-09-14 10:31:04 -03:00
|
|
|
{
|
1997-03-04 20:20:32 -04:00
|
|
|
PyCodeObject *co;
|
|
|
|
PyObject *v;
|
1994-09-14 10:31:04 -03:00
|
|
|
long magic;
|
2000-08-31 02:38:39 -03:00
|
|
|
long PyImport_GetMagicNumber(void);
|
1994-09-14 10:31:04 -03:00
|
|
|
|
1997-03-04 20:20:32 -04:00
|
|
|
magic = PyMarshal_ReadLongFromFile(fp);
|
|
|
|
if (magic != PyImport_GetMagicNumber()) {
|
|
|
|
PyErr_SetString(PyExc_RuntimeError,
|
1994-09-14 10:31:04 -03:00
|
|
|
"Bad magic number in .pyc file");
|
|
|
|
return NULL;
|
|
|
|
}
|
1997-03-04 20:20:32 -04:00
|
|
|
(void) PyMarshal_ReadLongFromFile(fp);
|
2001-01-27 20:27:39 -04:00
|
|
|
v = PyMarshal_ReadLastObjectFromFile(fp);
|
1994-09-14 10:31:04 -03:00
|
|
|
fclose(fp);
|
1997-03-04 20:20:32 -04:00
|
|
|
if (v == NULL || !PyCode_Check(v)) {
|
|
|
|
Py_XDECREF(v);
|
|
|
|
PyErr_SetString(PyExc_RuntimeError,
|
1994-09-14 10:31:04 -03:00
|
|
|
"Bad code object in .pyc file");
|
|
|
|
return NULL;
|
|
|
|
}
|
1997-03-04 20:20:32 -04:00
|
|
|
co = (PyCodeObject *)v;
|
|
|
|
v = PyEval_EvalCode(co, globals, locals);
|
2001-08-10 18:41:33 -03:00
|
|
|
if (v && flags)
|
|
|
|
flags->cf_flags |= (co->co_flags & PyCF_MASK);
|
1997-03-04 20:20:32 -04:00
|
|
|
Py_DECREF(co);
|
1994-09-14 10:31:04 -03:00
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2001-03-21 22:47:58 -04:00
|
|
|
PyObject *
|
2003-04-17 12:24:21 -03:00
|
|
|
Py_CompileStringFlags(const char *str, const char *filename, int start,
|
2001-03-21 22:47:58 -04:00
|
|
|
PyCompilerFlags *flags)
|
1993-03-30 13:46:03 -04:00
|
|
|
{
|
2005-10-20 16:59:25 -03:00
|
|
|
mod_ty mod;
|
1997-03-04 20:20:32 -04:00
|
|
|
PyCodeObject *co;
|
2005-10-20 16:59:25 -03:00
|
|
|
mod = PyParser_ASTFromString(str, filename, start, flags);
|
|
|
|
if (mod == NULL)
|
1993-03-30 13:46:03 -04:00
|
|
|
return NULL;
|
2005-10-20 16:59:25 -03:00
|
|
|
co = PyAST_Compile(mod, filename, flags);
|
|
|
|
free_mod(mod);
|
1997-03-04 20:20:32 -04:00
|
|
|
return (PyObject *)co;
|
1993-03-30 13:46:03 -04:00
|
|
|
}
|
|
|
|
|
2001-02-02 14:19:15 -04:00
|
|
|
struct symtable *
|
2002-12-11 10:04:59 -04:00
|
|
|
Py_SymtableString(const char *str, const char *filename, int start)
|
2001-02-02 14:19:15 -04:00
|
|
|
{
|
2005-10-20 16:59:25 -03:00
|
|
|
mod_ty mod;
|
2001-02-02 14:19:15 -04:00
|
|
|
struct symtable *st;
|
2005-10-20 16:59:25 -03:00
|
|
|
|
|
|
|
mod = PyParser_ASTFromString(str, filename, start, NULL);
|
|
|
|
if (mod == NULL)
|
2001-02-02 14:19:15 -04:00
|
|
|
return NULL;
|
2005-10-20 16:59:25 -03:00
|
|
|
st = PySymtable_Build(mod, filename, 0);
|
|
|
|
free_mod(mod);
|
2001-02-02 14:19:15 -04:00
|
|
|
return st;
|
|
|
|
}
|
|
|
|
|
2005-10-20 16:59:25 -03:00
|
|
|
/* Preferred access to parser is through AST. */
|
|
|
|
mod_ty
|
|
|
|
PyParser_ASTFromString(const char *s, const char *filename, int start,
|
|
|
|
PyCompilerFlags *flags)
|
|
|
|
{
|
|
|
|
node *n;
|
|
|
|
mod_ty mod;
|
|
|
|
perrdetail err;
|
|
|
|
n = PyParser_ParseStringFlagsFilename(s, filename, &_PyParser_Grammar,
|
|
|
|
start, &err,
|
|
|
|
PARSER_FLAGS(flags));
|
|
|
|
if (n) {
|
|
|
|
mod = PyAST_FromNode(n, flags, filename);
|
|
|
|
PyNode_Free(n);
|
|
|
|
return mod;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
err_input(&err);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mod_ty
|
|
|
|
PyParser_ASTFromFile(FILE *fp, const char *filename, int start, char *ps1,
|
|
|
|
char *ps2, PyCompilerFlags *flags, int *errcode)
|
|
|
|
{
|
|
|
|
node *n;
|
|
|
|
mod_ty mod;
|
|
|
|
perrdetail err;
|
|
|
|
n = PyParser_ParseFileFlags(fp, filename, &_PyParser_Grammar, start,
|
|
|
|
ps1, ps2, &err, PARSER_FLAGS(flags));
|
|
|
|
if (n) {
|
|
|
|
mod = PyAST_FromNode(n, flags, filename);
|
|
|
|
PyNode_Free(n);
|
|
|
|
return mod;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
err_input(&err);
|
|
|
|
if (errcode)
|
|
|
|
*errcode = err.error;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1994-08-29 09:50:44 -03:00
|
|
|
/* Simplified interface to parsefile -- return node or set exception */
|
1992-08-04 09:41:02 -03:00
|
|
|
|
1994-08-29 09:50:44 -03:00
|
|
|
node *
|
2002-12-11 10:04:59 -04:00
|
|
|
PyParser_SimpleParseFileFlags(FILE *fp, const char *filename, int start, int flags)
|
1992-08-04 09:41:02 -03:00
|
|
|
{
|
1994-08-29 09:50:44 -03:00
|
|
|
node *n;
|
|
|
|
perrdetail err;
|
2001-07-16 02:37:24 -03:00
|
|
|
n = PyParser_ParseFileFlags(fp, filename, &_PyParser_Grammar, start,
|
|
|
|
(char *)0, (char *)0, &err, flags);
|
1994-08-29 09:50:44 -03:00
|
|
|
if (n == NULL)
|
|
|
|
err_input(&err);
|
2005-10-20 16:59:25 -03:00
|
|
|
|
1994-08-29 09:50:44 -03:00
|
|
|
return n;
|
1992-08-04 09:41:02 -03:00
|
|
|
}
|
|
|
|
|
1994-08-29 09:50:44 -03:00
|
|
|
/* Simplified interface to parsestring -- return node or set exception */
|
1992-08-04 09:41:02 -03:00
|
|
|
|
1994-08-29 09:50:44 -03:00
|
|
|
node *
|
2002-12-11 10:04:59 -04:00
|
|
|
PyParser_SimpleParseStringFlags(const char *str, int start, int flags)
|
1992-08-04 09:41:02 -03:00
|
|
|
{
|
1994-08-29 09:50:44 -03:00
|
|
|
node *n;
|
|
|
|
perrdetail err;
|
2001-07-16 02:37:24 -03:00
|
|
|
n = PyParser_ParseStringFlags(str, &_PyParser_Grammar, start, &err,
|
|
|
|
flags);
|
1994-08-29 09:50:44 -03:00
|
|
|
if (n == NULL)
|
|
|
|
err_input(&err);
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2002-07-09 06:23:27 -03:00
|
|
|
node *
|
2002-12-11 10:04:59 -04:00
|
|
|
PyParser_SimpleParseStringFlagsFilename(const char *str, const char *filename,
|
2002-07-09 06:23:27 -03:00
|
|
|
int start, int flags)
|
|
|
|
{
|
|
|
|
node *n;
|
|
|
|
perrdetail err;
|
|
|
|
|
2003-04-17 12:24:21 -03:00
|
|
|
n = PyParser_ParseStringFlagsFilename(str, filename,
|
2002-07-09 06:23:27 -03:00
|
|
|
&_PyParser_Grammar,
|
|
|
|
start, &err, flags);
|
|
|
|
if (n == NULL)
|
|
|
|
err_input(&err);
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
node *
|
2002-12-11 10:04:59 -04:00
|
|
|
PyParser_SimpleParseStringFilename(const char *str, const char *filename, int start)
|
2002-07-09 06:23:27 -03:00
|
|
|
{
|
|
|
|
return PyParser_SimpleParseStringFlagsFilename(str, filename,
|
|
|
|
start, 0);
|
|
|
|
}
|
|
|
|
|
2003-04-17 13:02:26 -03:00
|
|
|
/* May want to move a more generalized form of this to parsetok.c or
|
|
|
|
even parser modules. */
|
|
|
|
|
|
|
|
void
|
|
|
|
PyParser_SetError(perrdetail *err)
|
|
|
|
{
|
|
|
|
err_input(err);
|
|
|
|
}
|
|
|
|
|
1994-08-29 09:50:44 -03:00
|
|
|
/* Set the error appropriate to the given input error code (see errcode.h) */
|
|
|
|
|
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
err_input(perrdetail *err)
|
1994-08-29 09:50:44 -03:00
|
|
|
{
|
2000-07-11 14:53:00 -03:00
|
|
|
PyObject *v, *w, *errtype;
|
2002-08-04 14:29:52 -03:00
|
|
|
PyObject* u = NULL;
|
1994-08-29 09:50:44 -03:00
|
|
|
char *msg = NULL;
|
2000-07-11 14:53:00 -03:00
|
|
|
errtype = PyExc_SyntaxError;
|
1994-08-29 09:50:44 -03:00
|
|
|
switch (err->error) {
|
|
|
|
case E_SYNTAX:
|
2000-07-11 14:53:00 -03:00
|
|
|
errtype = PyExc_IndentationError;
|
|
|
|
if (err->expected == INDENT)
|
|
|
|
msg = "expected an indented block";
|
|
|
|
else if (err->token == INDENT)
|
|
|
|
msg = "unexpected indent";
|
|
|
|
else if (err->token == DEDENT)
|
|
|
|
msg = "unexpected unindent";
|
|
|
|
else {
|
|
|
|
errtype = PyExc_SyntaxError;
|
|
|
|
msg = "invalid syntax";
|
|
|
|
}
|
1994-08-29 09:50:44 -03:00
|
|
|
break;
|
|
|
|
case E_TOKEN:
|
|
|
|
msg = "invalid token";
|
|
|
|
break;
|
2002-08-14 22:20:16 -03:00
|
|
|
case E_EOFS:
|
|
|
|
msg = "EOF while scanning triple-quoted string";
|
|
|
|
break;
|
|
|
|
case E_EOLS:
|
|
|
|
msg = "EOL while scanning single-quoted string";
|
|
|
|
break;
|
1994-08-29 09:50:44 -03:00
|
|
|
case E_INTR:
|
2004-07-07 14:44:12 -03:00
|
|
|
if (!PyErr_Occurred())
|
|
|
|
PyErr_SetNone(PyExc_KeyboardInterrupt);
|
1994-08-29 09:50:44 -03:00
|
|
|
return;
|
|
|
|
case E_NOMEM:
|
1997-03-04 20:20:32 -04:00
|
|
|
PyErr_NoMemory();
|
1994-08-29 09:50:44 -03:00
|
|
|
return;
|
|
|
|
case E_EOF:
|
|
|
|
msg = "unexpected EOF while parsing";
|
|
|
|
break;
|
2000-07-11 14:53:00 -03:00
|
|
|
case E_TABSPACE:
|
|
|
|
errtype = PyExc_TabError;
|
1998-04-10 16:43:42 -03:00
|
|
|
msg = "inconsistent use of tabs and spaces in indentation";
|
|
|
|
break;
|
2000-06-20 16:10:44 -03:00
|
|
|
case E_OVERFLOW:
|
|
|
|
msg = "expression too long";
|
|
|
|
break;
|
2000-07-11 14:53:00 -03:00
|
|
|
case E_DEDENT:
|
|
|
|
errtype = PyExc_IndentationError;
|
|
|
|
msg = "unindent does not match any outer indentation level";
|
|
|
|
break;
|
|
|
|
case E_TOODEEP:
|
|
|
|
errtype = PyExc_IndentationError;
|
|
|
|
msg = "too many levels of indentation";
|
|
|
|
break;
|
2005-08-24 05:39:24 -03:00
|
|
|
case E_DECODE: {
|
|
|
|
PyObject *type, *value, *tb;
|
|
|
|
PyErr_Fetch(&type, &value, &tb);
|
2002-08-04 14:29:52 -03:00
|
|
|
if (value != NULL) {
|
2005-08-24 05:39:24 -03:00
|
|
|
u = PyObject_Str(value);
|
2002-08-04 14:29:52 -03:00
|
|
|
if (u != NULL) {
|
|
|
|
msg = PyString_AsString(u);
|
|
|
|
}
|
|
|
|
}
|
2004-07-21 02:35:02 -03:00
|
|
|
if (msg == NULL)
|
|
|
|
msg = "unknown decode error";
|
2005-08-24 05:39:24 -03:00
|
|
|
Py_DECREF(type);
|
|
|
|
Py_DECREF(value);
|
2005-10-01 22:48:49 -03:00
|
|
|
Py_XDECREF(tb);
|
2004-07-21 02:35:02 -03:00
|
|
|
break;
|
2002-08-04 14:29:52 -03:00
|
|
|
}
|
2005-03-03 07:45:45 -04:00
|
|
|
case E_LINECONT:
|
|
|
|
msg = "unexpected character after line continuation character";
|
|
|
|
break;
|
1994-08-29 09:50:44 -03:00
|
|
|
default:
|
|
|
|
fprintf(stderr, "error=%d\n", err->error);
|
|
|
|
msg = "unknown parsing error";
|
|
|
|
break;
|
|
|
|
}
|
2005-10-20 16:59:25 -03:00
|
|
|
v = Py_BuildValue("(ziiz)", err->filename,
|
|
|
|
err->lineno, err->offset, err->text);
|
|
|
|
if (err->text != NULL) {
|
|
|
|
PyMem_DEL(err->text);
|
|
|
|
err->text = NULL;
|
|
|
|
}
|
|
|
|
w = NULL;
|
|
|
|
if (v != NULL)
|
|
|
|
w = Py_BuildValue("(sO)", msg, v);
|
2002-08-04 14:29:52 -03:00
|
|
|
Py_XDECREF(u);
|
2001-03-23 00:01:07 -04:00
|
|
|
Py_XDECREF(v);
|
2000-07-11 14:53:00 -03:00
|
|
|
PyErr_SetObject(errtype, w);
|
1997-03-04 20:20:32 -04:00
|
|
|
Py_XDECREF(w);
|
1992-08-04 09:41:02 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Print fatal error message and abort */
|
|
|
|
|
|
|
|
void
|
2002-07-08 23:57:01 -03:00
|
|
|
Py_FatalError(const char *msg)
|
1992-08-04 09:41:02 -03:00
|
|
|
{
|
1994-09-29 06:38:33 -03:00
|
|
|
fprintf(stderr, "Fatal Python error: %s\n", msg);
|
2002-06-30 12:26:10 -03:00
|
|
|
#ifdef MS_WINDOWS
|
1997-05-22 17:21:30 -03:00
|
|
|
OutputDebugString("Fatal Python error: ");
|
1995-03-14 11:01:17 -04:00
|
|
|
OutputDebugString(msg);
|
|
|
|
OutputDebugString("\n");
|
1998-08-13 10:33:16 -03:00
|
|
|
#ifdef _DEBUG
|
|
|
|
DebugBreak();
|
1995-01-25 20:40:38 -04:00
|
|
|
#endif
|
2002-06-30 12:26:10 -03:00
|
|
|
#endif /* MS_WINDOWS */
|
1992-08-04 09:41:02 -03:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Clean up and exit */
|
|
|
|
|
1994-08-29 09:50:44 -03:00
|
|
|
#ifdef WITH_THREAD
|
1998-10-01 17:42:43 -03:00
|
|
|
#include "pythread.h"
|
1992-08-17 05:59:08 -03:00
|
|
|
#endif
|
|
|
|
|
1998-10-01 13:01:57 -03:00
|
|
|
#define NEXITFUNCS 32
|
2000-07-22 15:47:25 -03:00
|
|
|
static void (*exitfuncs[NEXITFUNCS])(void);
|
1994-09-07 11:38:28 -03:00
|
|
|
static int nexitfuncs = 0;
|
|
|
|
|
2000-07-22 15:47:25 -03:00
|
|
|
int Py_AtExit(void (*func)(void))
|
1994-09-07 11:38:28 -03:00
|
|
|
{
|
|
|
|
if (nexitfuncs >= NEXITFUNCS)
|
|
|
|
return -1;
|
|
|
|
exitfuncs[nexitfuncs++] = func;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1997-08-04 23:22:03 -03:00
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
call_sys_exitfunc(void)
|
1992-08-04 09:41:02 -03:00
|
|
|
{
|
1997-03-04 20:20:32 -04:00
|
|
|
PyObject *exitfunc = PySys_GetObject("exitfunc");
|
1992-09-03 17:28:00 -03:00
|
|
|
|
|
|
|
if (exitfunc) {
|
2001-03-23 13:34:02 -04:00
|
|
|
PyObject *res;
|
1997-03-04 20:20:32 -04:00
|
|
|
Py_INCREF(exitfunc);
|
|
|
|
PySys_SetObject("exitfunc", (PyObject *)NULL);
|
|
|
|
res = PyEval_CallObject(exitfunc, (PyObject *)NULL);
|
1992-09-03 17:28:00 -03:00
|
|
|
if (res == NULL) {
|
2001-03-23 11:36:41 -04:00
|
|
|
if (!PyErr_ExceptionMatches(PyExc_SystemExit)) {
|
|
|
|
PySys_WriteStderr("Error in sys.exitfunc:\n");
|
|
|
|
}
|
1997-03-04 20:20:32 -04:00
|
|
|
PyErr_Print();
|
1992-09-03 17:28:00 -03:00
|
|
|
}
|
1997-03-04 20:20:32 -04:00
|
|
|
Py_DECREF(exitfunc);
|
1992-09-03 17:28:00 -03:00
|
|
|
}
|
|
|
|
|
1998-02-28 00:31:39 -04:00
|
|
|
if (Py_FlushLine())
|
|
|
|
PyErr_Clear();
|
1997-08-04 23:22:03 -03:00
|
|
|
}
|
1994-09-07 11:38:28 -03:00
|
|
|
|
1997-08-04 23:22:03 -03:00
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
call_ll_exitfuncs(void)
|
1997-08-04 23:22:03 -03:00
|
|
|
{
|
1994-09-07 11:38:28 -03:00
|
|
|
while (nexitfuncs > 0)
|
|
|
|
(*exitfuncs[--nexitfuncs])();
|
1997-08-02 00:10:38 -03:00
|
|
|
|
|
|
|
fflush(stdout);
|
|
|
|
fflush(stderr);
|
1992-10-18 15:53:57 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2000-07-22 15:47:25 -03:00
|
|
|
Py_Exit(int sts)
|
1992-10-18 15:53:57 -03:00
|
|
|
{
|
1997-08-04 23:22:03 -03:00
|
|
|
Py_Finalize();
|
1992-08-04 09:41:02 -03:00
|
|
|
|
|
|
|
exit(sts);
|
|
|
|
}
|
|
|
|
|
1993-07-05 07:31:29 -03:00
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
initsigs(void)
|
1992-10-18 15:53:57 -03:00
|
|
|
{
|
1994-08-29 09:50:44 -03:00
|
|
|
#ifdef SIGPIPE
|
2004-10-13 11:48:50 -03:00
|
|
|
PyOS_setsig(SIGPIPE, SIG_IGN);
|
1994-08-29 09:50:44 -03:00
|
|
|
#endif
|
2001-08-16 05:21:42 -03:00
|
|
|
#ifdef SIGXFZ
|
2004-10-13 11:48:50 -03:00
|
|
|
PyOS_setsig(SIGXFZ, SIG_IGN);
|
2001-08-16 05:21:42 -03:00
|
|
|
#endif
|
2002-04-23 17:31:01 -03:00
|
|
|
#ifdef SIGXFSZ
|
2004-10-13 11:48:50 -03:00
|
|
|
PyOS_setsig(SIGXFSZ, SIG_IGN);
|
2002-04-23 17:31:01 -03:00
|
|
|
#endif
|
1997-03-04 20:20:32 -04:00
|
|
|
PyOS_InitInterrupts(); /* May imply initsignal() */
|
1992-10-18 15:53:57 -03:00
|
|
|
}
|
|
|
|
|
1997-02-14 15:45:36 -04: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
|
2002-12-11 10:04:59 -04:00
|
|
|
Py_FdIsInteractive(FILE *fp, const char *filename)
|
1997-02-14 15:45:36 -04:00
|
|
|
{
|
|
|
|
if (isatty((int)fileno(fp)))
|
|
|
|
return 1;
|
|
|
|
if (!Py_InteractiveFlag)
|
|
|
|
return 0;
|
|
|
|
return (filename == NULL) ||
|
|
|
|
(strcmp(filename, "<stdin>") == 0) ||
|
|
|
|
(strcmp(filename, "???") == 0);
|
|
|
|
}
|
2000-08-27 16:15:31 -03:00
|
|
|
|
|
|
|
|
2003-04-17 12:24:21 -03:00
|
|
|
#if defined(USE_STACKCHECK)
|
2000-08-27 16:15:31 -03:00
|
|
|
#if defined(WIN32) && defined(_MSC_VER)
|
|
|
|
|
|
|
|
/* Stack checking for Microsoft C */
|
|
|
|
|
|
|
|
#include <malloc.h>
|
|
|
|
#include <excpt.h>
|
|
|
|
|
2000-08-31 02:38:39 -03:00
|
|
|
/*
|
|
|
|
* Return non-zero when we run out of memory on the stack; zero otherwise.
|
|
|
|
*/
|
2000-08-27 16:15:31 -03:00
|
|
|
int
|
2000-08-31 02:52:44 -03:00
|
|
|
PyOS_CheckStack(void)
|
2000-08-27 16:15:31 -03:00
|
|
|
{
|
|
|
|
__try {
|
2002-10-04 22:47:34 -03:00
|
|
|
/* alloca throws a stack overflow exception if there's
|
2000-08-27 16:15:31 -03:00
|
|
|
not enough space left on the stack */
|
2002-10-04 22:47:34 -03:00
|
|
|
alloca(PYOS_STACK_MARGIN * sizeof(void*));
|
2000-08-27 16:15:31 -03:00
|
|
|
return 0;
|
|
|
|
} __except (EXCEPTION_EXECUTE_HANDLER) {
|
|
|
|
/* just ignore all errors */
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* WIN32 && _MSC_VER */
|
|
|
|
|
|
|
|
/* Alternate implementations can be added here... */
|
|
|
|
|
|
|
|
#endif /* USE_STACKCHECK */
|
2000-09-16 13:32:19 -03:00
|
|
|
|
|
|
|
|
|
|
|
/* Wrappers around sigaction() or signal(). */
|
|
|
|
|
|
|
|
PyOS_sighandler_t
|
|
|
|
PyOS_getsig(int sig)
|
|
|
|
{
|
|
|
|
#ifdef HAVE_SIGACTION
|
|
|
|
struct sigaction context;
|
2004-10-13 11:48:50 -03:00
|
|
|
if (sigaction(sig, NULL, &context) == -1)
|
|
|
|
return SIG_ERR;
|
2000-09-16 13:32:19 -03:00
|
|
|
return context.sa_handler;
|
|
|
|
#else
|
|
|
|
PyOS_sighandler_t handler;
|
|
|
|
handler = signal(sig, SIG_IGN);
|
2004-10-13 11:48:50 -03:00
|
|
|
if (handler != SIG_ERR)
|
|
|
|
signal(sig, handler);
|
2000-09-16 13:32:19 -03:00
|
|
|
return handler;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
PyOS_sighandler_t
|
|
|
|
PyOS_setsig(int sig, PyOS_sighandler_t handler)
|
|
|
|
{
|
|
|
|
#ifdef HAVE_SIGACTION
|
2004-10-13 11:48:50 -03:00
|
|
|
struct sigaction context, ocontext;
|
2000-09-16 13:32:19 -03:00
|
|
|
context.sa_handler = handler;
|
2004-10-13 11:48:50 -03:00
|
|
|
sigemptyset(&context.sa_mask);
|
|
|
|
context.sa_flags = 0;
|
|
|
|
if (sigaction(sig, &context, &ocontext) == -1)
|
|
|
|
return SIG_ERR;
|
|
|
|
return ocontext.sa_handler;
|
2000-09-16 13:32:19 -03:00
|
|
|
#else
|
2004-10-13 11:48:50 -03:00
|
|
|
PyOS_sighandler_t oldhandler;
|
|
|
|
oldhandler = signal(sig, handler);
|
|
|
|
#ifdef HAVE_SIGINTERRUPT
|
|
|
|
siginterrupt(sig, 1);
|
|
|
|
#endif
|
|
|
|
return oldhandler;
|
2000-09-16 13:32:19 -03:00
|
|
|
#endif
|
|
|
|
}
|
2005-10-20 16:59:25 -03:00
|
|
|
|
|
|
|
/* Deprecated C API functions still provided for binary compatiblity */
|
|
|
|
|
|
|
|
#undef PyParser_SimpleParseFile
|
|
|
|
#undef PyParser_SimpleParseString
|
|
|
|
|
|
|
|
node *
|
|
|
|
PyParser_SimpleParseFile(FILE *fp, const char *filename, int start)
|
|
|
|
{
|
|
|
|
return PyParser_SimpleParseFileFlags(fp, filename, start, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
node *
|
|
|
|
PyParser_SimpleParseString(const char *str, int start)
|
|
|
|
{
|
|
|
|
return PyParser_SimpleParseStringFlags(str, start, 0);
|
|
|
|
}
|