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"
|
2007-04-24 21:17:39 -03:00
|
|
|
#undef Yield /* undefine macro conflicting with winbase.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-12-17 16:54:49 -04:00
|
|
|
#include "pyarena.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"
|
2009-10-20 18:29:37 -03:00
|
|
|
#include "abstract.h"
|
1992-08-04 09:41:02 -03:00
|
|
|
|
2006-06-10 09:23:46 -03:00
|
|
|
#ifdef HAVE_SIGNAL_H
|
1992-10-18 15:53:57 -03:00
|
|
|
#include <signal.h>
|
2006-06-10 09:23:46 -03:00
|
|
|
#endif
|
1992-10-18 15:53:57 -03:00
|
|
|
|
2009-01-02 16:47:27 -04:00
|
|
|
#ifdef MS_WINDOWS
|
2009-01-02 16:32:55 -04:00
|
|
|
#include "malloc.h" /* for alloca */
|
2009-01-02 16:47:27 -04:00
|
|
|
#endif
|
2009-01-02 16:32:55 -04:00
|
|
|
|
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
|
|
|
|
|
2006-04-12 23:06:09 -03:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#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_mod(mod_ty, const char *, PyObject *, PyObject *,
|
2010-05-09 11:46:46 -03:00
|
|
|
PyCompilerFlags *, PyArena *);
|
2002-12-11 10:04:59 -04:00
|
|
|
static PyObject *run_pyc_file(FILE *, const char *, PyObject *, PyObject *,
|
2010-05-09 11:46:46 -03:00
|
|
|
PyCompilerFlags *);
|
2000-07-09 00:09:57 -03:00
|
|
|
static void err_input(perrdetail *);
|
|
|
|
static void initsigs(void);
|
2009-10-20 18:29:37 -03:00
|
|
|
static void wait_for_thread_shutdown(void);
|
2000-07-09 00:09:57 -03:00
|
|
|
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 */
|
2011-05-15 03:50:32 -03:00
|
|
|
int Py_InspectFlag; /* Needed to determine whether to exit at SystemExit */
|
1997-08-29 19:32:42 -03:00
|
|
|
int Py_NoSiteFlag; /* Suppress 'import site' */
|
2019-04-09 09:49:49 -03:00
|
|
|
int Py_BytesWarningFlag; /* Warn on comparison between bytearray and unicode */
|
2008-01-07 13:09:35 -04:00
|
|
|
int Py_DontWriteBytecodeFlag; /* Suppress writing bytecode files (*.py[co]) */
|
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;
|
2008-05-06 19:41:46 -03:00
|
|
|
int Py_NoUserSiteDirectory = 0; /* for -s and site.py */
|
2012-02-20 21:42:21 -04:00
|
|
|
int Py_HashRandomizationFlag = 0; /* for -R and PYTHONHASHSEED */
|
1992-08-04 09:41:02 -03:00
|
|
|
|
2013-10-22 05:22:29 -03:00
|
|
|
|
|
|
|
/* Hack to force loading of object files */
|
|
|
|
int (*_PyOS_mystrnicmp_hack)(const char *, const char *, Py_ssize_t) = \
|
|
|
|
PyOS_mystrnicmp; /* Python/pystrcmp.o */
|
|
|
|
|
2008-04-12 20:44:07 -03:00
|
|
|
/* PyModule_GetWarningsModule is no longer necessary as of 2.6
|
|
|
|
since _warnings is builtin. This API should not be used. */
|
|
|
|
PyObject *
|
|
|
|
PyModule_GetWarningsModule(void)
|
2003-07-15 20:03:55 -03:00
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
return PyImport_ImportModule("warnings");
|
2003-07-15 20:03:55 -03:00
|
|
|
}
|
2003-02-18 20:33:33 -04:00
|
|
|
|
2017-10-17 05:35:19 -03:00
|
|
|
static void
|
|
|
|
_PyDebug_PrintTotalRefs(void)
|
|
|
|
{
|
|
|
|
#ifdef Py_REF_DEBUG
|
|
|
|
Py_ssize_t total;
|
|
|
|
|
|
|
|
if (!Py_GETENV("PYTHONSHOWREFCOUNT")) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
total = _Py_GetRefTotal();
|
|
|
|
fprintf(stderr, "[%" PY_FORMAT_SIZE_T "d refs]\n", total);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
return initialized;
|
1997-08-22 01:20:13 -03:00
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
int env = atoi(envs);
|
|
|
|
if (flag < env)
|
|
|
|
flag = env;
|
|
|
|
if (flag < 1)
|
|
|
|
flag = 1;
|
|
|
|
return flag;
|
2001-10-12 19:17:56 -03:00
|
|
|
}
|
|
|
|
|
2015-05-30 11:45:12 -03:00
|
|
|
static int
|
|
|
|
isatty_no_error(PyObject *sys_stream)
|
|
|
|
{
|
|
|
|
PyObject *sys_isatty = PyObject_CallMethod(sys_stream, "isatty", "");
|
|
|
|
if (sys_isatty) {
|
|
|
|
int isatty = PyObject_IsTrue(sys_isatty);
|
|
|
|
Py_DECREF(sys_isatty);
|
|
|
|
if (isatty >= 0)
|
|
|
|
return isatty;
|
|
|
|
}
|
|
|
|
PyErr_Clear();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
PyInterpreterState *interp;
|
|
|
|
PyThreadState *tstate;
|
|
|
|
PyObject *bimod, *sysmod;
|
|
|
|
char *p;
|
|
|
|
char *icodeset = NULL; /* On Windows, input codeset may theoretically
|
|
|
|
differ from output codeset. */
|
|
|
|
char *codeset = NULL;
|
|
|
|
char *errors = NULL;
|
|
|
|
int free_codeset = 0;
|
|
|
|
int overridden = 0;
|
2015-05-30 11:45:12 -03:00
|
|
|
PyObject *sys_stream;
|
2003-08-09 06:47:11 -03:00
|
|
|
#if defined(Py_USING_UNICODE) && defined(HAVE_LANGINFO_H) && defined(CODESET)
|
2010-05-09 11:46:46 -03:00
|
|
|
char *saved_locale, *loc_codeset;
|
2008-06-01 04:20:46 -03:00
|
|
|
#endif
|
|
|
|
#ifdef MS_WINDOWS
|
2010-05-09 11:46:46 -03:00
|
|
|
char ibuf[128];
|
|
|
|
char buf[128];
|
2003-08-09 06:47:11 -03:00
|
|
|
#endif
|
2010-05-09 11:46:46 -03:00
|
|
|
extern void _Py_ReadyTypes(void);
|
1997-08-02 00:10:38 -03:00
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
if (initialized)
|
|
|
|
return;
|
|
|
|
initialized = 1;
|
2003-04-17 12:24:21 -03:00
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
if ((p = Py_GETENV("PYTHONDEBUG")) && *p != '\0')
|
|
|
|
Py_DebugFlag = add_flag(Py_DebugFlag, p);
|
|
|
|
if ((p = Py_GETENV("PYTHONVERBOSE")) && *p != '\0')
|
|
|
|
Py_VerboseFlag = add_flag(Py_VerboseFlag, p);
|
|
|
|
if ((p = Py_GETENV("PYTHONOPTIMIZE")) && *p != '\0')
|
|
|
|
Py_OptimizeFlag = add_flag(Py_OptimizeFlag, p);
|
|
|
|
if ((p = Py_GETENV("PYTHONDONTWRITEBYTECODE")) && *p != '\0')
|
|
|
|
Py_DontWriteBytecodeFlag = add_flag(Py_DontWriteBytecodeFlag, p);
|
2012-02-20 21:42:21 -04:00
|
|
|
/* The variable is only tested for existence here; _PyRandom_Init will
|
|
|
|
check its value further. */
|
|
|
|
if ((p = Py_GETENV("PYTHONHASHSEED")) && *p != '\0')
|
|
|
|
Py_HashRandomizationFlag = add_flag(Py_HashRandomizationFlag, p);
|
|
|
|
|
|
|
|
_PyRandom_Init();
|
1997-08-02 00:10:38 -03:00
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
interp = PyInterpreterState_New();
|
|
|
|
if (interp == NULL)
|
|
|
|
Py_FatalError("Py_Initialize: can't make first interpreter");
|
1997-08-02 00:10:38 -03:00
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
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
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
_Py_ReadyTypes();
|
2001-08-07 14:24:28 -03:00
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
if (!_PyFrame_Init())
|
|
|
|
Py_FatalError("Py_Initialize: can't init frames");
|
2002-12-30 18:29:22 -04:00
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
if (!_PyInt_Init())
|
|
|
|
Py_FatalError("Py_Initialize: can't init ints");
|
2002-12-30 18:29:22 -04:00
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
if (!_PyLong_Init())
|
|
|
|
Py_FatalError("Py_Initialize: can't init longs");
|
2009-03-20 12:51:55 -03:00
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
if (!PyByteArray_Init())
|
|
|
|
Py_FatalError("Py_Initialize: can't init bytearray");
|
Merged revisions 61750,61752,61754,61756,61760,61763,61768,61772,61775,61805,61809,61812,61819,61917,61920,61930,61933-61934 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/branches/trunk-bytearray
........
r61750 | christian.heimes | 2008-03-22 20:47:44 +0100 (Sat, 22 Mar 2008) | 1 line
Copied files from py3k w/o modifications
........
r61752 | christian.heimes | 2008-03-22 20:53:20 +0100 (Sat, 22 Mar 2008) | 7 lines
Take One
* Added initialization code, warnings, flags etc. to the appropriate places
* Added new buffer interface to string type
* Modified tests
* Modified Makefile.pre.in to compile the new files
* Added bytesobject.c to Python.h
........
r61754 | christian.heimes | 2008-03-22 21:22:19 +0100 (Sat, 22 Mar 2008) | 2 lines
Disabled bytearray.extend for now since it causes an infinite recursion
Fixed serveral unit tests
........
r61756 | christian.heimes | 2008-03-22 21:43:38 +0100 (Sat, 22 Mar 2008) | 5 lines
Added PyBytes support to several places:
str + bytearray
ord(bytearray)
bytearray(str, encoding)
........
r61760 | christian.heimes | 2008-03-22 21:56:32 +0100 (Sat, 22 Mar 2008) | 1 line
Fixed more unit tests related to type('') is not unicode
........
r61763 | christian.heimes | 2008-03-22 22:20:28 +0100 (Sat, 22 Mar 2008) | 2 lines
Fixed more unit tests
Fixed bytearray.extend
........
r61768 | christian.heimes | 2008-03-22 22:40:50 +0100 (Sat, 22 Mar 2008) | 1 line
Implemented old buffer interface for bytearray
........
r61772 | christian.heimes | 2008-03-22 23:24:52 +0100 (Sat, 22 Mar 2008) | 1 line
Added backport of the io module
........
r61775 | christian.heimes | 2008-03-23 03:50:49 +0100 (Sun, 23 Mar 2008) | 1 line
Fix str assignement to bytearray. Assignment of a str of size 1 is interpreted as a single byte
........
r61805 | christian.heimes | 2008-03-23 19:33:48 +0100 (Sun, 23 Mar 2008) | 3 lines
Fixed more tests
Fixed bytearray() comparsion with unicode()
Fixed iterator assignment of bytearray
........
r61809 | christian.heimes | 2008-03-23 21:02:21 +0100 (Sun, 23 Mar 2008) | 2 lines
str(bytesarray()) now returns the bytes and not the representation of the bytearray object
Enabled and fixed more unit tests
........
r61812 | christian.heimes | 2008-03-23 21:53:08 +0100 (Sun, 23 Mar 2008) | 3 lines
Clear error PyNumber_AsSsize_t() fails
Use CHARMASK for ob_svall access
disabled a test with memoryview again
........
r61819 | christian.heimes | 2008-03-23 23:05:57 +0100 (Sun, 23 Mar 2008) | 1 line
Untested updates to the PCBuild directory
........
r61917 | christian.heimes | 2008-03-26 00:57:06 +0100 (Wed, 26 Mar 2008) | 1 line
The type system of Python 2.6 has subtle differences to 3.0's. I've removed the Py_TPFLAGS_BASETYPE flags from bytearray for now. bytearray can't be subclasses until the issues with bytearray subclasses are fixed.
........
r61920 | christian.heimes | 2008-03-26 01:44:08 +0100 (Wed, 26 Mar 2008) | 2 lines
Disabled last failing test
I don't understand what the test is testing and how it suppose to work. Ka-Ping, please check it out.
........
r61930 | christian.heimes | 2008-03-26 12:46:18 +0100 (Wed, 26 Mar 2008) | 1 line
Re-enabled bytes warning code
........
r61933 | christian.heimes | 2008-03-26 13:20:46 +0100 (Wed, 26 Mar 2008) | 1 line
Fixed a bug in the new buffer protocol. The buffer slots weren't copied into a subclass.
........
r61934 | christian.heimes | 2008-03-26 13:25:09 +0100 (Wed, 26 Mar 2008) | 1 line
Re-enabled bytearray subclassing - all tests are passing.
........
2008-03-26 09:49:49 -03:00
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
_PyFloat_Init();
|
2005-05-27 12:23:20 -03:00
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
interp->modules = PyDict_New();
|
|
|
|
if (interp->modules == NULL)
|
|
|
|
Py_FatalError("Py_Initialize: can't make modules dictionary");
|
|
|
|
interp->modules_reloading = PyDict_New();
|
|
|
|
if (interp->modules_reloading == NULL)
|
|
|
|
Py_FatalError("Py_Initialize: can't make modules_reloading dictionary");
|
1997-08-02 00:10:38 -03:00
|
|
|
|
2001-08-17 15:39:25 -03:00
|
|
|
#ifdef Py_USING_UNICODE
|
2010-05-09 11:46:46 -03: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
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
bimod = _PyBuiltin_Init();
|
|
|
|
if (bimod == NULL)
|
|
|
|
Py_FatalError("Py_Initialize: can't initialize __builtin__");
|
|
|
|
interp->builtins = PyModule_GetDict(bimod);
|
|
|
|
if (interp->builtins == NULL)
|
|
|
|
Py_FatalError("Py_Initialize: can't initialize builtins dict");
|
|
|
|
Py_INCREF(interp->builtins);
|
|
|
|
|
|
|
|
sysmod = _PySys_Init();
|
|
|
|
if (sysmod == NULL)
|
|
|
|
Py_FatalError("Py_Initialize: can't initialize sys");
|
|
|
|
interp->sysdict = PyModule_GetDict(sysmod);
|
|
|
|
if (interp->sysdict == NULL)
|
|
|
|
Py_FatalError("Py_Initialize: can't initialize sys dict");
|
|
|
|
Py_INCREF(interp->sysdict);
|
|
|
|
_PyImport_FixupExtension("sys", "sys");
|
|
|
|
PySys_SetPath(Py_GetPath());
|
|
|
|
PyDict_SetItemString(interp->sysdict, "modules",
|
|
|
|
interp->modules);
|
|
|
|
|
|
|
|
_PyImport_Init();
|
|
|
|
|
|
|
|
/* initialize builtin exceptions */
|
|
|
|
_PyExc_Init();
|
|
|
|
_PyImport_FixupExtension("exceptions", "exceptions");
|
|
|
|
|
|
|
|
/* phase 2 of builtins */
|
|
|
|
_PyImport_FixupExtension("__builtin__", "__builtin__");
|
|
|
|
|
|
|
|
_PyImportHooks_Init();
|
|
|
|
|
|
|
|
if (install_sigs)
|
|
|
|
initsigs(); /* Signal handling stuff, including initintr() */
|
|
|
|
|
|
|
|
/* Initialize warnings. */
|
|
|
|
_PyWarnings_Init();
|
|
|
|
if (PySys_HasWarnOptions()) {
|
|
|
|
PyObject *warnings_module = PyImport_ImportModule("warnings");
|
|
|
|
if (!warnings_module)
|
|
|
|
PyErr_Clear();
|
|
|
|
Py_XDECREF(warnings_module);
|
|
|
|
}
|
|
|
|
|
|
|
|
initmain(); /* Module __main__ */
|
|
|
|
|
|
|
|
/* auto-thread-state API, if available */
|
2003-04-19 12:41:53 -03:00
|
|
|
#ifdef WITH_THREAD
|
2010-05-09 11:46:46 -03:00
|
|
|
_PyGILState_Init(interp, tstate);
|
2003-04-19 12:41:53 -03:00
|
|
|
#endif /* WITH_THREAD */
|
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
if (!Py_NoSiteFlag)
|
|
|
|
initsite(); /* Module site */
|
2010-03-10 18:30:19 -04:00
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
if ((p = Py_GETENV("PYTHONIOENCODING")) && *p != '\0') {
|
|
|
|
p = icodeset = codeset = strdup(p);
|
|
|
|
free_codeset = 1;
|
|
|
|
errors = strchr(p, ':');
|
|
|
|
if (errors) {
|
|
|
|
*errors = '\0';
|
|
|
|
errors++;
|
|
|
|
}
|
|
|
|
overridden = 1;
|
|
|
|
}
|
2008-06-01 04:20:46 -03:00
|
|
|
|
2003-03-05 11:13:47 -04:00
|
|
|
#if defined(Py_USING_UNICODE) && defined(HAVE_LANGINFO_H) && defined(CODESET)
|
2010-05-09 11:46:46 -03:00
|
|
|
/* 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
|
|
|
|
initialized by other means. Also set the encoding of
|
|
|
|
stdin and stdout if these are terminals, unless overridden. */
|
|
|
|
|
|
|
|
if (!overridden || !Py_FileSystemDefaultEncoding) {
|
|
|
|
saved_locale = strdup(setlocale(LC_CTYPE, NULL));
|
|
|
|
setlocale(LC_CTYPE, "");
|
|
|
|
loc_codeset = nl_langinfo(CODESET);
|
|
|
|
if (loc_codeset && *loc_codeset) {
|
|
|
|
PyObject *enc = PyCodec_Encoder(loc_codeset);
|
|
|
|
if (enc) {
|
|
|
|
loc_codeset = strdup(loc_codeset);
|
|
|
|
Py_DECREF(enc);
|
|
|
|
} else {
|
|
|
|
if (PyErr_ExceptionMatches(PyExc_LookupError)) {
|
|
|
|
PyErr_Clear();
|
|
|
|
loc_codeset = NULL;
|
|
|
|
} else {
|
|
|
|
PyErr_Print();
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
loc_codeset = NULL;
|
|
|
|
setlocale(LC_CTYPE, saved_locale);
|
|
|
|
free(saved_locale);
|
|
|
|
|
|
|
|
if (!overridden) {
|
|
|
|
codeset = icodeset = loc_codeset;
|
|
|
|
free_codeset = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize Py_FileSystemDefaultEncoding from
|
|
|
|
locale even if PYTHONIOENCODING is set. */
|
|
|
|
if (!Py_FileSystemDefaultEncoding) {
|
|
|
|
Py_FileSystemDefaultEncoding = loc_codeset;
|
|
|
|
if (!overridden)
|
|
|
|
free_codeset = 0;
|
|
|
|
}
|
|
|
|
}
|
2008-06-01 04:20:46 -03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef MS_WINDOWS
|
2010-05-09 11:46:46 -03:00
|
|
|
if (!overridden) {
|
|
|
|
icodeset = ibuf;
|
|
|
|
codeset = buf;
|
|
|
|
sprintf(ibuf, "cp%d", GetConsoleCP());
|
|
|
|
sprintf(buf, "cp%d", GetConsoleOutputCP());
|
|
|
|
}
|
2008-06-01 04:20:46 -03:00
|
|
|
#endif
|
2003-08-09 06:47:11 -03:00
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
if (codeset) {
|
|
|
|
sys_stream = PySys_GetObject("stdin");
|
2015-05-30 11:45:12 -03:00
|
|
|
if ((overridden || isatty_no_error(sys_stream)) &&
|
|
|
|
PyFile_Check(sys_stream)) {
|
2010-05-09 11:46:46 -03:00
|
|
|
if (!PyFile_SetEncodingAndErrors(sys_stream, icodeset, errors))
|
|
|
|
Py_FatalError("Cannot set codeset of stdin");
|
|
|
|
}
|
|
|
|
|
|
|
|
sys_stream = PySys_GetObject("stdout");
|
2015-05-30 11:45:12 -03:00
|
|
|
if ((overridden || isatty_no_error(sys_stream)) &&
|
|
|
|
PyFile_Check(sys_stream)) {
|
2010-05-09 11:46:46 -03:00
|
|
|
if (!PyFile_SetEncodingAndErrors(sys_stream, codeset, errors))
|
|
|
|
Py_FatalError("Cannot set codeset of stdout");
|
|
|
|
}
|
|
|
|
|
|
|
|
sys_stream = PySys_GetObject("stderr");
|
2015-05-30 11:45:12 -03:00
|
|
|
if ((overridden || isatty_no_error(sys_stream)) &&
|
|
|
|
PyFile_Check(sys_stream)) {
|
2010-05-09 11:46:46 -03:00
|
|
|
if (!PyFile_SetEncodingAndErrors(sys_stream, codeset, errors))
|
|
|
|
Py_FatalError("Cannot set codeset of stderr");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (free_codeset)
|
|
|
|
free(codeset);
|
|
|
|
}
|
1995-01-09 13:53:26 -04:00
|
|
|
}
|
|
|
|
|
2004-08-19 08:31:58 -03:00
|
|
|
void
|
|
|
|
Py_Initialize(void)
|
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
Py_InitializeEx(1);
|
2004-08-19 08:31:58 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-12-15 12:12:00 -04:00
|
|
|
#ifdef COUNT_ALLOCS
|
2006-04-18 03:24:08 -03:00
|
|
|
extern void dump_counts(FILE*);
|
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
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
PyInterpreterState *interp;
|
|
|
|
PyThreadState *tstate;
|
|
|
|
|
|
|
|
if (!initialized)
|
|
|
|
return;
|
|
|
|
|
|
|
|
wait_for_thread_shutdown();
|
|
|
|
|
|
|
|
/* 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.
|
|
|
|
*/
|
|
|
|
call_sys_exitfunc();
|
2014-11-20 21:04:21 -04:00
|
|
|
initialized = 0;
|
2010-05-09 11:46:46 -03:00
|
|
|
|
|
|
|
/* Get current thread state and interpreter pointer */
|
|
|
|
tstate = PyThreadState_GET();
|
|
|
|
interp = tstate->interp;
|
|
|
|
|
|
|
|
/* Disable signal handling */
|
|
|
|
PyOS_FiniInterrupts();
|
|
|
|
|
|
|
|
/* Clear type lookup cache */
|
|
|
|
PyType_ClearCache();
|
|
|
|
|
|
|
|
/* Collect garbage. This may call finalizers; it's nice to call these
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
PyGC_Collect();
|
2006-04-18 03:24:08 -03:00
|
|
|
#ifdef COUNT_ALLOCS
|
2010-05-09 11:46:46 -03:00
|
|
|
/* With COUNT_ALLOCS, it helps to run GC multiple times:
|
|
|
|
each collection might release some types from the type
|
|
|
|
list, so they become garbage. */
|
|
|
|
while (PyGC_Collect() > 0)
|
|
|
|
/* nothing */;
|
2006-04-18 03:24:08 -03:00
|
|
|
#endif
|
2003-04-17 14:29:22 -03:00
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
/* Destroy all modules */
|
|
|
|
PyImport_Cleanup();
|
|
|
|
|
|
|
|
/* Collect final garbage. This disposes of cycles created by
|
|
|
|
* 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.
|
|
|
|
*/
|
2003-12-01 17:35:27 -04:00
|
|
|
#if 0
|
2010-05-09 11:46:46 -03:00
|
|
|
PyGC_Collect();
|
2003-12-01 17:35:27 -04:00
|
|
|
#endif
|
2003-04-17 14:29:22 -03:00
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
/* Destroy the database used by _PyImport_{Fixup,Find}Extension */
|
|
|
|
_PyImport_Fini();
|
1997-12-08 19:43:45 -04:00
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
/* Debugging stuff */
|
1997-12-08 19:43:45 -04:00
|
|
|
#ifdef COUNT_ALLOCS
|
2017-10-17 06:25:23 -03:00
|
|
|
if (Py_GETENV("PYTHONSHOWALLOCCOUNT")) {
|
|
|
|
dump_counts(stderr);
|
|
|
|
}
|
1997-12-08 19:43:45 -04:00
|
|
|
#endif
|
|
|
|
|
2017-10-17 05:35:19 -03:00
|
|
|
_PyDebug_PrintTotalRefs();
|
1997-12-08 19:43:45 -04:00
|
|
|
|
2003-04-17 12:21:01 -03:00
|
|
|
#ifdef Py_TRACE_REFS
|
2010-05-09 11:46:46 -03:00
|
|
|
/* 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.
|
|
|
|
*/
|
|
|
|
if (Py_GETENV("PYTHONDUMPREFS"))
|
|
|
|
_Py_PrintReferences(stderr);
|
2003-04-17 12:21:01 -03:00
|
|
|
#endif /* Py_TRACE_REFS */
|
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
/* Clear interpreter state */
|
|
|
|
PyInterpreterState_Clear(interp);
|
2003-04-15 11:10:09 -03:00
|
|
|
|
2010-05-09 11:46:46 -03: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.
|
|
|
|
*/
|
2005-03-29 09:36:16 -04:00
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
_PyExc_Fini();
|
2005-03-29 09:36:16 -04:00
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
/* Cleanup auto-thread-state */
|
2007-11-29 19:35:25 -04:00
|
|
|
#ifdef WITH_THREAD
|
2010-05-09 11:46:46 -03:00
|
|
|
_PyGILState_Fini();
|
2007-11-29 19:35:25 -04:00
|
|
|
#endif /* WITH_THREAD */
|
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
/* Delete current thread */
|
|
|
|
PyThreadState_Swap(NULL);
|
|
|
|
PyInterpreterState_Delete(interp);
|
|
|
|
|
|
|
|
/* Sundry finalizers */
|
|
|
|
PyMethod_Fini();
|
|
|
|
PyFrame_Fini();
|
|
|
|
PyCFunction_Fini();
|
|
|
|
PyTuple_Fini();
|
|
|
|
PyList_Fini();
|
|
|
|
PySet_Fini();
|
|
|
|
PyString_Fini();
|
|
|
|
PyByteArray_Fini();
|
|
|
|
PyInt_Fini();
|
|
|
|
PyFloat_Fini();
|
|
|
|
PyDict_Fini();
|
2014-08-28 13:30:00 -03:00
|
|
|
_PyRandom_Fini();
|
1997-08-04 23:22:03 -03:00
|
|
|
|
2002-04-08 05:19:36 -03:00
|
|
|
#ifdef Py_USING_UNICODE
|
2010-05-09 11:46:46 -03:00
|
|
|
/* Cleanup Unicode implementation */
|
|
|
|
_PyUnicode_Fini();
|
2002-04-08 05:19:36 -03:00
|
|
|
#endif
|
|
|
|
|
2010-05-09 11:46:46 -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
|
|
|
|
2010-05-09 11:46:46 -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
|
2010-05-09 11:46:46 -03:00
|
|
|
/* 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);
|
2003-04-17 16:52:29 -03:00
|
|
|
#endif /* Py_TRACE_REFS */
|
2002-04-13 05:29:14 -03:00
|
|
|
#ifdef PYMALLOC_DEBUG
|
2010-05-09 11:46:46 -03:00
|
|
|
if (Py_GETENV("PYTHONMALLOCSTATS"))
|
|
|
|
_PyObject_DebugMallocStats();
|
2002-04-13 05:29:14 -03:00
|
|
|
#endif
|
|
|
|
|
2010-05-09 11:46:46 -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
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
PyInterpreterState *interp;
|
|
|
|
PyThreadState *tstate, *save_tstate;
|
|
|
|
PyObject *bimod, *sysmod;
|
|
|
|
|
|
|
|
if (!initialized)
|
|
|
|
Py_FatalError("Py_NewInterpreter: call Py_Initialize first");
|
|
|
|
|
|
|
|
interp = PyInterpreterState_New();
|
|
|
|
if (interp == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
tstate = PyThreadState_New(interp);
|
|
|
|
if (tstate == NULL) {
|
|
|
|
PyInterpreterState_Delete(interp);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
save_tstate = PyThreadState_Swap(tstate);
|
|
|
|
|
|
|
|
/* XXX The following is lax in error checking */
|
|
|
|
|
|
|
|
interp->modules = PyDict_New();
|
|
|
|
interp->modules_reloading = PyDict_New();
|
|
|
|
|
|
|
|
bimod = _PyImport_FindExtension("__builtin__", "__builtin__");
|
|
|
|
if (bimod != NULL) {
|
|
|
|
interp->builtins = PyModule_GetDict(bimod);
|
|
|
|
if (interp->builtins == NULL)
|
|
|
|
goto handle_error;
|
|
|
|
Py_INCREF(interp->builtins);
|
|
|
|
}
|
|
|
|
sysmod = _PyImport_FindExtension("sys", "sys");
|
|
|
|
if (bimod != NULL && sysmod != NULL) {
|
|
|
|
interp->sysdict = PyModule_GetDict(sysmod);
|
|
|
|
if (interp->sysdict == NULL)
|
|
|
|
goto handle_error;
|
|
|
|
Py_INCREF(interp->sysdict);
|
|
|
|
PySys_SetPath(Py_GetPath());
|
|
|
|
PyDict_SetItemString(interp->sysdict, "modules",
|
|
|
|
interp->modules);
|
|
|
|
_PyImportHooks_Init();
|
|
|
|
initmain();
|
|
|
|
if (!Py_NoSiteFlag)
|
|
|
|
initsite();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!PyErr_Occurred())
|
|
|
|
return tstate;
|
1997-08-02 00:10:38 -03:00
|
|
|
|
2006-08-21 17:16:24 -03:00
|
|
|
handle_error:
|
2010-05-09 11:46:46 -03:00
|
|
|
/* Oops, it didn't work. Undo it all. */
|
1997-08-02 00:10:38 -03:00
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
PyErr_Print();
|
|
|
|
PyThreadState_Clear(tstate);
|
|
|
|
PyThreadState_Swap(save_tstate);
|
|
|
|
PyThreadState_Delete(tstate);
|
|
|
|
PyInterpreterState_Delete(interp);
|
1997-08-02 00:10:38 -03:00
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
return NULL;
|
1997-08-02 00:10:38 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 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
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
PyInterpreterState *interp = tstate->interp;
|
1997-08-02 00:10:38 -03:00
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
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");
|
1997-08-02 00:10:38 -03:00
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
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
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
if (pn && *pn)
|
|
|
|
progname = pn;
|
1997-07-19 16:17:22 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
2000-07-22 15:47:25 -03:00
|
|
|
Py_GetProgramName(void)
|
1997-07-19 16:17:22 -03:00
|
|
|
{
|
2010-05-09 11:46:46 -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
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
default_home = home;
|
1998-02-06 18:27:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
2000-07-22 15:47:25 -03:00
|
|
|
Py_GetPythonHome(void)
|
1998-02-06 18:27:24 -04:00
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
char *home = default_home;
|
|
|
|
if (home == NULL && !Py_IgnoreEnvironmentFlag)
|
|
|
|
home = Py_GETENV("PYTHONHOME");
|
|
|
|
return home;
|
1998-02-06 18:27:24 -04:00
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
PyObject *m, *d;
|
|
|
|
m = PyImport_AddModule("__main__");
|
|
|
|
if (m == NULL)
|
|
|
|
Py_FatalError("can't create __main__ module");
|
|
|
|
d = PyModule_GetDict(m);
|
|
|
|
if (PyDict_GetItemString(d, "__builtins__") == NULL) {
|
|
|
|
PyObject *bimod = PyImport_ImportModule("__builtin__");
|
|
|
|
if (bimod == NULL ||
|
|
|
|
PyDict_SetItemString(d, "__builtins__", bimod) != 0)
|
|
|
|
Py_FatalError("can't add __builtins__ to __main__");
|
|
|
|
Py_XDECREF(bimod);
|
|
|
|
}
|
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
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
PyObject *m;
|
|
|
|
m = PyImport_ImportModule("site");
|
|
|
|
if (m == NULL) {
|
|
|
|
PyErr_Print();
|
|
|
|
Py_Finalize();
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Py_DECREF(m);
|
|
|
|
}
|
1997-08-29 19:32:42 -03:00
|
|
|
}
|
|
|
|
|
1992-08-04 09:41:02 -03:00
|
|
|
/* Parse input from a file and execute it */
|
|
|
|
|
|
|
|
int
|
2006-05-28 07:41:29 -03:00
|
|
|
PyRun_AnyFileExFlags(FILE *fp, const char *filename, int closeit,
|
2010-05-09 11:46:46 -03:00
|
|
|
PyCompilerFlags *flags)
|
1992-08-04 09:41:02 -03:00
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
if (filename == NULL)
|
|
|
|
filename = "???";
|
|
|
|
if (Py_FdIsInteractive(fp, filename)) {
|
|
|
|
int err = PyRun_InteractiveLoopFlags(fp, filename, flags);
|
|
|
|
if (closeit)
|
|
|
|
fclose(fp);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
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
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
PyObject *v;
|
|
|
|
int ret;
|
|
|
|
PyCompilerFlags local_flags;
|
|
|
|
|
|
|
|
if (flags == NULL) {
|
|
|
|
flags = &local_flags;
|
|
|
|
local_flags.cf_flags = 0;
|
|
|
|
}
|
|
|
|
v = PySys_GetObject("ps1");
|
|
|
|
if (v == NULL) {
|
|
|
|
PySys_SetObject("ps1", v = PyString_FromString(">>> "));
|
|
|
|
Py_XDECREF(v);
|
|
|
|
}
|
|
|
|
v = PySys_GetObject("ps2");
|
|
|
|
if (v == NULL) {
|
|
|
|
PySys_SetObject("ps2", v = PyString_FromString("... "));
|
|
|
|
Py_XDECREF(v);
|
|
|
|
}
|
|
|
|
for (;;) {
|
|
|
|
ret = PyRun_InteractiveOneFlags(fp, filename, flags);
|
2017-10-17 05:35:19 -03:00
|
|
|
_PyDebug_PrintTotalRefs();
|
2010-05-09 11:46:46 -03:00
|
|
|
if (ret == E_EOF)
|
|
|
|
return 0;
|
|
|
|
/*
|
|
|
|
if (ret == E_NOMEM)
|
|
|
|
return -1;
|
|
|
|
*/
|
|
|
|
}
|
1992-08-04 09:41:02 -03:00
|
|
|
}
|
|
|
|
|
2008-03-18 20:45:49 -03:00
|
|
|
#if 0
|
2002-03-22 19:53:36 -04:00
|
|
|
/* compute parser flags based on compiler flags */
|
2006-09-06 03:28:06 -03:00
|
|
|
#define PARSER_FLAGS(flags) \
|
2010-05-09 11:46:46 -03:00
|
|
|
((flags) ? ((((flags)->cf_flags & PyCF_DONT_IMPLY_DEDENT) ? \
|
|
|
|
PyPARSE_DONT_IMPLY_DEDENT : 0)) : 0)
|
2008-03-18 20:45:49 -03:00
|
|
|
#endif
|
|
|
|
#if 1
|
2006-09-06 03:28:06 -03:00
|
|
|
/* Keep an example of flags with future keyword support. */
|
2002-03-22 19:53:36 -04:00
|
|
|
#define PARSER_FLAGS(flags) \
|
2010-05-09 11:46:46 -03:00
|
|
|
((flags) ? ((((flags)->cf_flags & PyCF_DONT_IMPLY_DEDENT) ? \
|
|
|
|
PyPARSE_DONT_IMPLY_DEDENT : 0) \
|
|
|
|
| (((flags)->cf_flags & CO_FUTURE_PRINT_FUNCTION) ? \
|
|
|
|
PyPARSE_PRINT_IS_FUNCTION : 0) \
|
|
|
|
| (((flags)->cf_flags & CO_FUTURE_UNICODE_LITERALS) ? \
|
|
|
|
PyPARSE_UNICODE_LITERALS : 0) \
|
|
|
|
) : 0)
|
2006-09-06 03:28:06 -03:00
|
|
|
#endif
|
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
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
PyObject *m, *d, *v, *w;
|
|
|
|
mod_ty mod;
|
|
|
|
PyArena *arena;
|
|
|
|
char *ps1 = "", *ps2 = "";
|
|
|
|
int errcode = 0;
|
|
|
|
|
|
|
|
v = PySys_GetObject("ps1");
|
|
|
|
if (v != NULL) {
|
|
|
|
v = PyObject_Str(v);
|
|
|
|
if (v == NULL)
|
|
|
|
PyErr_Clear();
|
|
|
|
else if (PyString_Check(v))
|
|
|
|
ps1 = PyString_AsString(v);
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
arena = PyArena_New();
|
|
|
|
if (arena == NULL) {
|
|
|
|
Py_XDECREF(v);
|
|
|
|
Py_XDECREF(w);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
mod = PyParser_ASTFromFile(fp, filename,
|
|
|
|
Py_single_input, ps1, ps2,
|
|
|
|
flags, &errcode, arena);
|
|
|
|
Py_XDECREF(v);
|
|
|
|
Py_XDECREF(w);
|
|
|
|
if (mod == NULL) {
|
|
|
|
PyArena_Free(arena);
|
|
|
|
if (errcode == E_EOF) {
|
|
|
|
PyErr_Clear();
|
|
|
|
return E_EOF;
|
|
|
|
}
|
|
|
|
PyErr_Print();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
m = PyImport_AddModule("__main__");
|
|
|
|
if (m == NULL) {
|
|
|
|
PyArena_Free(arena);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
d = PyModule_GetDict(m);
|
|
|
|
v = run_mod(mod, filename, d, d, flags, arena);
|
|
|
|
PyArena_Free(arena);
|
|
|
|
if (v == NULL) {
|
|
|
|
PyErr_Print();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
Py_DECREF(v);
|
|
|
|
if (Py_FlushLine())
|
|
|
|
PyErr_Clear();
|
|
|
|
return 0;
|
1992-08-04 09:41:02 -03:00
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2010-05-09 11:46:46 -03: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];
|
|
|
|
/* Mess: In case of -x, the stream is NOT at its start now,
|
|
|
|
and ungetc() was used to push back the first newline,
|
|
|
|
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).
|
|
|
|
*/
|
|
|
|
int ispyc = 0;
|
|
|
|
if (ftell(fp) == 0) {
|
|
|
|
if (fread(buf, 1, 2, fp) == 2 &&
|
|
|
|
((unsigned int)buf[1]<<8 | buf[0]) == halfmagic)
|
|
|
|
ispyc = 1;
|
|
|
|
rewind(fp);
|
|
|
|
}
|
|
|
|
return ispyc;
|
|
|
|
}
|
|
|
|
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,
|
2010-05-09 11:46:46 -03:00
|
|
|
PyCompilerFlags *flags)
|
|
|
|
{
|
|
|
|
PyObject *m, *d, *v;
|
|
|
|
const char *ext;
|
2012-11-07 04:41:28 -04:00
|
|
|
int set_file_name = 0, len, ret = -1;
|
2010-05-09 11:46:46 -03:00
|
|
|
|
|
|
|
m = PyImport_AddModule("__main__");
|
|
|
|
if (m == NULL)
|
|
|
|
return -1;
|
2012-11-07 04:41:28 -04:00
|
|
|
Py_INCREF(m);
|
2010-05-09 11:46:46 -03:00
|
|
|
d = PyModule_GetDict(m);
|
|
|
|
if (PyDict_GetItemString(d, "__file__") == NULL) {
|
|
|
|
PyObject *f = PyString_FromString(filename);
|
|
|
|
if (f == NULL)
|
2012-11-07 04:41:28 -04:00
|
|
|
goto done;
|
2010-05-09 11:46:46 -03:00
|
|
|
if (PyDict_SetItemString(d, "__file__", f) < 0) {
|
|
|
|
Py_DECREF(f);
|
2012-11-07 04:41:28 -04:00
|
|
|
goto done;
|
2010-05-09 11:46:46 -03:00
|
|
|
}
|
|
|
|
set_file_name = 1;
|
|
|
|
Py_DECREF(f);
|
|
|
|
}
|
|
|
|
len = strlen(filename);
|
|
|
|
ext = filename + len - (len > 4 ? 4 : 0);
|
|
|
|
if (maybe_pyc_file(fp, filename, ext, closeit)) {
|
|
|
|
/* Try to run a pyc file. First, re-open in binary */
|
|
|
|
if (closeit)
|
|
|
|
fclose(fp);
|
|
|
|
if ((fp = fopen(filename, "rb")) == NULL) {
|
|
|
|
fprintf(stderr, "python: Can't reopen .pyc file\n");
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
/* Turn on optimization if a .pyo file is given */
|
|
|
|
if (strcmp(ext, ".pyo") == 0)
|
|
|
|
Py_OptimizeFlag = 1;
|
|
|
|
v = run_pyc_file(fp, filename, d, d, flags);
|
|
|
|
} else {
|
|
|
|
v = PyRun_FileExFlags(fp, filename, Py_file_input, d, d,
|
|
|
|
closeit, flags);
|
|
|
|
}
|
|
|
|
if (v == NULL) {
|
|
|
|
PyErr_Print();
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
Py_DECREF(v);
|
|
|
|
if (Py_FlushLine())
|
|
|
|
PyErr_Clear();
|
|
|
|
ret = 0;
|
2007-03-06 20:40:28 -04:00
|
|
|
done:
|
2010-05-09 11:46:46 -03:00
|
|
|
if (set_file_name && PyDict_DelItemString(d, "__file__"))
|
|
|
|
PyErr_Clear();
|
2012-11-07 04:41:28 -04:00
|
|
|
Py_DECREF(m);
|
2010-05-09 11:46:46 -03:00
|
|
|
return ret;
|
1992-08-04 09:41:02 -03:00
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
PyObject *m, *d, *v;
|
|
|
|
m = PyImport_AddModule("__main__");
|
|
|
|
if (m == NULL)
|
|
|
|
return -1;
|
|
|
|
d = PyModule_GetDict(m);
|
|
|
|
v = PyRun_StringFlags(command, Py_file_input, d, d, flags);
|
|
|
|
if (v == NULL) {
|
|
|
|
PyErr_Print();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
Py_DECREF(v);
|
|
|
|
if (Py_FlushLine())
|
|
|
|
PyErr_Clear();
|
|
|
|
return 0;
|
1992-08-04 09:41:02 -03:00
|
|
|
}
|
|
|
|
|
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,
|
2010-05-09 11:46:46 -03:00
|
|
|
int *lineno, int *offset, const char **text)
|
|
|
|
{
|
|
|
|
long hold;
|
|
|
|
PyObject *v;
|
|
|
|
|
|
|
|
/* old style errors */
|
|
|
|
if (PyTuple_Check(err))
|
|
|
|
return PyArg_ParseTuple(err, "O(ziiz)", message, filename,
|
|
|
|
lineno, offset, text);
|
|
|
|
|
2012-04-03 01:30:38 -03:00
|
|
|
*message = NULL;
|
2010-05-09 11:46:46 -03:00
|
|
|
|
2012-04-03 01:30:38 -03:00
|
|
|
/* new style errors. `err' is an instance */
|
|
|
|
*message = PyObject_GetAttrString(err, "msg");
|
|
|
|
if (!*message)
|
2010-05-09 11:46:46 -03:00
|
|
|
goto finally;
|
|
|
|
|
2012-04-03 01:30:38 -03:00
|
|
|
v = PyObject_GetAttrString(err, "filename");
|
|
|
|
if (!v)
|
2010-05-09 11:46:46 -03:00
|
|
|
goto finally;
|
2012-04-03 01:30:38 -03:00
|
|
|
if (v == Py_None) {
|
|
|
|
Py_DECREF(v);
|
2010-05-09 11:46:46 -03:00
|
|
|
*filename = NULL;
|
2012-04-03 01:30:38 -03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
*filename = PyString_AsString(v);
|
|
|
|
Py_DECREF(v);
|
|
|
|
if (!*filename)
|
|
|
|
goto finally;
|
|
|
|
}
|
2010-05-09 11:46:46 -03:00
|
|
|
|
2012-04-03 01:30:38 -03:00
|
|
|
v = PyObject_GetAttrString(err, "lineno");
|
|
|
|
if (!v)
|
2010-05-09 11:46:46 -03:00
|
|
|
goto finally;
|
|
|
|
hold = PyInt_AsLong(v);
|
|
|
|
Py_DECREF(v);
|
|
|
|
if (hold < 0 && PyErr_Occurred())
|
|
|
|
goto finally;
|
|
|
|
*lineno = (int)hold;
|
|
|
|
|
2012-04-03 01:30:38 -03:00
|
|
|
v = PyObject_GetAttrString(err, "offset");
|
|
|
|
if (!v)
|
2010-05-09 11:46:46 -03:00
|
|
|
goto finally;
|
|
|
|
if (v == Py_None) {
|
|
|
|
*offset = -1;
|
|
|
|
Py_DECREF(v);
|
|
|
|
} else {
|
|
|
|
hold = PyInt_AsLong(v);
|
|
|
|
Py_DECREF(v);
|
|
|
|
if (hold < 0 && PyErr_Occurred())
|
|
|
|
goto finally;
|
|
|
|
*offset = (int)hold;
|
|
|
|
}
|
|
|
|
|
2012-04-03 01:30:38 -03:00
|
|
|
v = PyObject_GetAttrString(err, "text");
|
|
|
|
if (!v)
|
2010-05-09 11:46:46 -03:00
|
|
|
goto finally;
|
2012-04-03 01:30:38 -03:00
|
|
|
if (v == Py_None) {
|
|
|
|
Py_DECREF(v);
|
2010-05-09 11:46:46 -03:00
|
|
|
*text = NULL;
|
2012-04-03 01:30:38 -03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
*text = PyString_AsString(v);
|
|
|
|
Py_DECREF(v);
|
|
|
|
if (!*text)
|
|
|
|
goto finally;
|
|
|
|
}
|
2010-05-09 11:46:46 -03:00
|
|
|
return 1;
|
1997-08-29 19:07:17 -03:00
|
|
|
|
|
|
|
finally:
|
2012-04-03 01:30:38 -03:00
|
|
|
Py_XDECREF(*message);
|
2010-05-09 11:46:46 -03:00
|
|
|
return 0;
|
1997-08-29 19:07:17 -03:00
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
PyErr_PrintEx(1);
|
1998-02-06 18:27:24 -04:00
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
char *nl;
|
|
|
|
if (offset >= 0) {
|
2010-10-29 00:45:34 -03:00
|
|
|
if (offset > 0 && offset == strlen(text) && text[offset - 1] == '\n')
|
|
|
|
offset--;
|
2010-05-09 11:46:46 -03:00
|
|
|
for (;;) {
|
|
|
|
nl = strchr(text, '\n');
|
|
|
|
if (nl == NULL || nl-text >= offset)
|
|
|
|
break;
|
|
|
|
offset -= (int)(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-02-28 03:07:43 -04:00
|
|
|
}
|
|
|
|
|
2001-03-23 13:54:43 -04:00
|
|
|
static void
|
|
|
|
handle_system_exit(void)
|
2001-03-23 11:36:41 -04:00
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
PyObject *exception, *value, *tb;
|
|
|
|
int exitcode = 0;
|
|
|
|
|
|
|
|
if (Py_InspectFlag)
|
|
|
|
/* Don't exit if -i flag was given. This flag is set to 0
|
|
|
|
* when entering interactive mode for inspecting. */
|
|
|
|
return;
|
|
|
|
|
|
|
|
PyErr_Fetch(&exception, &value, &tb);
|
|
|
|
if (Py_FlushLine())
|
|
|
|
PyErr_Clear();
|
|
|
|
fflush(stdout);
|
|
|
|
if (value == NULL || value == Py_None)
|
|
|
|
goto done;
|
|
|
|
if (PyExceptionInstance_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)
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
/* If we failed to dig out the 'code' attribute,
|
|
|
|
just let the else clause below print the error. */
|
|
|
|
}
|
2018-07-31 03:09:36 -03:00
|
|
|
if (_PyAnyInt_Check(value))
|
2010-05-09 11:46:46 -03:00
|
|
|
exitcode = (int)PyInt_AsLong(value);
|
|
|
|
else {
|
2010-05-25 19:30:32 -03:00
|
|
|
PyObject *sys_stderr = PySys_GetObject("stderr");
|
|
|
|
if (sys_stderr != NULL && sys_stderr != Py_None) {
|
|
|
|
PyFile_WriteObject(value, sys_stderr, Py_PRINT_RAW);
|
|
|
|
} else {
|
|
|
|
PyObject_Print(value, stderr, Py_PRINT_RAW);
|
|
|
|
fflush(stderr);
|
|
|
|
}
|
2010-05-09 11:46:46 -03:00
|
|
|
PySys_WriteStderr("\n");
|
|
|
|
exitcode = 1;
|
|
|
|
}
|
2003-04-19 15:47:02 -03:00
|
|
|
done:
|
2010-05-09 11:46:46 -03:00
|
|
|
/* 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
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
PyObject *exception, *v, *tb, *hook;
|
|
|
|
|
|
|
|
if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
|
|
|
|
handle_system_exit();
|
|
|
|
}
|
|
|
|
PyErr_Fetch(&exception, &v, &tb);
|
|
|
|
if (exception == NULL)
|
|
|
|
return;
|
|
|
|
PyErr_NormalizeException(&exception, &v, &tb);
|
|
|
|
if (exception == NULL)
|
|
|
|
return;
|
|
|
|
/* Now we know v != NULL too */
|
|
|
|
if (set_sys_last_vars) {
|
|
|
|
PySys_SetObject("last_type", exception);
|
|
|
|
PySys_SetObject("last_value", v);
|
|
|
|
PySys_SetObject("last_traceback", tb);
|
|
|
|
}
|
|
|
|
hook = PySys_GetObject("excepthook");
|
2010-08-08 18:37:51 -03:00
|
|
|
if (hook && hook != Py_None) {
|
2010-05-09 11:46:46 -03:00
|
|
|
PyObject *args = PyTuple_Pack(3,
|
|
|
|
exception, v, tb ? tb : Py_None);
|
|
|
|
PyObject *result = PyEval_CallObject(hook, args);
|
|
|
|
if (result == NULL) {
|
|
|
|
PyObject *exception2, *v2, *tb2;
|
|
|
|
if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
|
|
|
|
handle_system_exit();
|
|
|
|
}
|
|
|
|
PyErr_Fetch(&exception2, &v2, &tb2);
|
|
|
|
PyErr_NormalizeException(&exception2, &v2, &tb2);
|
|
|
|
/* It should not be possible for exception2 or v2
|
|
|
|
to be NULL. However PyErr_Display() can't
|
|
|
|
tolerate NULLs, so just be safe. */
|
|
|
|
if (exception2 == NULL) {
|
|
|
|
exception2 = Py_None;
|
|
|
|
Py_INCREF(exception2);
|
|
|
|
}
|
|
|
|
if (v2 == NULL) {
|
|
|
|
v2 = Py_None;
|
|
|
|
Py_INCREF(v2);
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
Py_DECREF(exception2);
|
|
|
|
Py_DECREF(v2);
|
|
|
|
Py_XDECREF(tb2);
|
|
|
|
}
|
|
|
|
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);
|
2001-03-22 22:46:52 -04:00
|
|
|
}
|
|
|
|
|
2006-05-27 09:29:24 -03:00
|
|
|
void
|
|
|
|
PyErr_Display(PyObject *exception, PyObject *value, PyObject *tb)
|
2001-03-22 22:46:52 -04:00
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
int err = 0;
|
|
|
|
PyObject *f = PySys_GetObject("stderr");
|
|
|
|
Py_INCREF(value);
|
2010-08-08 18:37:51 -03:00
|
|
|
if (f == NULL || f == Py_None)
|
2010-05-09 11:46:46 -03:00
|
|
|
fprintf(stderr, "lost sys.stderr\n");
|
|
|
|
else {
|
|
|
|
if (Py_FlushLine())
|
|
|
|
PyErr_Clear();
|
|
|
|
fflush(stdout);
|
|
|
|
if (tb && tb != Py_None)
|
|
|
|
err = PyTraceBack_Print(tb, f);
|
|
|
|
if (err == 0 &&
|
|
|
|
PyObject_HasAttrString(value, "print_file_and_line"))
|
|
|
|
{
|
|
|
|
PyObject *message;
|
|
|
|
const char *filename, *text;
|
|
|
|
int lineno, offset;
|
|
|
|
if (!parse_syntax_error(value, &message, &filename,
|
|
|
|
&lineno, &offset, &text))
|
|
|
|
PyErr_Clear();
|
|
|
|
else {
|
|
|
|
char buf[10];
|
|
|
|
PyFile_WriteString(" File \"", f);
|
|
|
|
if (filename == NULL)
|
|
|
|
PyFile_WriteString("<string>", f);
|
|
|
|
else
|
|
|
|
PyFile_WriteString(filename, f);
|
|
|
|
PyFile_WriteString("\", line ", f);
|
|
|
|
PyOS_snprintf(buf, sizeof(buf), "%d", lineno);
|
|
|
|
PyFile_WriteString(buf, f);
|
|
|
|
PyFile_WriteString("\n", f);
|
|
|
|
if (text != NULL)
|
|
|
|
print_error_text(f, offset, text);
|
|
|
|
Py_DECREF(value);
|
|
|
|
value = message;
|
|
|
|
/* Can't be bothered to check all those
|
|
|
|
PyFile_WriteString() calls */
|
|
|
|
if (PyErr_Occurred())
|
|
|
|
err = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (err) {
|
|
|
|
/* Don't do anything else */
|
|
|
|
}
|
|
|
|
else if (PyExceptionClass_Check(exception)) {
|
|
|
|
PyObject* moduleName;
|
|
|
|
char* className = PyExceptionClass_Name(exception);
|
|
|
|
if (className != NULL) {
|
|
|
|
char *dot = strrchr(className, '.');
|
|
|
|
if (dot != NULL)
|
|
|
|
className = dot+1;
|
|
|
|
}
|
|
|
|
|
|
|
|
moduleName = PyObject_GetAttrString(exception, "__module__");
|
|
|
|
if (moduleName == NULL)
|
|
|
|
err = PyFile_WriteString("<unknown>", f);
|
|
|
|
else {
|
|
|
|
char* modstr = PyString_AsString(moduleName);
|
|
|
|
if (modstr && strcmp(modstr, "exceptions"))
|
|
|
|
{
|
|
|
|
err = PyFile_WriteString(modstr, f);
|
|
|
|
err += PyFile_WriteString(".", f);
|
|
|
|
}
|
|
|
|
Py_DECREF(moduleName);
|
|
|
|
}
|
|
|
|
if (err == 0) {
|
|
|
|
if (className == NULL)
|
|
|
|
err = PyFile_WriteString("<unknown>", f);
|
|
|
|
else
|
|
|
|
err = PyFile_WriteString(className, f);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
err = PyFile_WriteObject(exception, f, Py_PRINT_RAW);
|
|
|
|
if (err == 0 && (value != Py_None)) {
|
|
|
|
PyObject *s = PyObject_Str(value);
|
|
|
|
/* only print colon if the str() of the
|
|
|
|
object is not the empty string
|
|
|
|
*/
|
2016-02-27 20:18:43 -04:00
|
|
|
if (s == NULL) {
|
|
|
|
PyErr_Clear();
|
2010-05-09 11:46:46 -03:00
|
|
|
err = -1;
|
2016-02-27 20:18:43 -04:00
|
|
|
PyFile_WriteString(": <exception str() failed>", f);
|
|
|
|
}
|
2010-05-09 11:46:46 -03:00
|
|
|
else if (!PyString_Check(s) ||
|
|
|
|
PyString_GET_SIZE(s) != 0)
|
|
|
|
err = PyFile_WriteString(": ", f);
|
|
|
|
if (err == 0)
|
|
|
|
err = PyFile_WriteObject(s, f, Py_PRINT_RAW);
|
|
|
|
Py_XDECREF(s);
|
|
|
|
}
|
|
|
|
/* try to write a newline in any case */
|
2016-02-27 20:18:43 -04:00
|
|
|
if (err < 0) {
|
|
|
|
PyErr_Clear();
|
|
|
|
}
|
2010-05-09 11:46:46 -03:00
|
|
|
err += PyFile_WriteString("\n", f);
|
|
|
|
}
|
|
|
|
Py_DECREF(value);
|
|
|
|
/* 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 *
|
2006-05-28 07:41:29 -03:00
|
|
|
PyRun_StringFlags(const char *str, int start, PyObject *globals,
|
2010-05-09 11:46:46 -03:00
|
|
|
PyObject *locals, PyCompilerFlags *flags)
|
1992-08-04 09:41:02 -03:00
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
PyObject *ret = NULL;
|
|
|
|
mod_ty mod;
|
|
|
|
PyArena *arena = PyArena_New();
|
|
|
|
if (arena == NULL)
|
|
|
|
return NULL;
|
2010-05-05 17:38:52 -03:00
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
mod = PyParser_ASTFromString(str, "<string>", start, flags, arena);
|
|
|
|
if (mod != NULL)
|
|
|
|
ret = run_mod(mod, "<string>", globals, locals, flags, arena);
|
|
|
|
PyArena_Free(arena);
|
|
|
|
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,
|
2010-05-09 11:46:46 -03:00
|
|
|
PyObject *locals, int closeit, PyCompilerFlags *flags)
|
|
|
|
{
|
|
|
|
PyObject *ret;
|
|
|
|
mod_ty mod;
|
|
|
|
PyArena *arena = PyArena_New();
|
|
|
|
if (arena == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
mod = PyParser_ASTFromFile(fp, filename, start, 0, 0,
|
|
|
|
flags, NULL, arena);
|
|
|
|
if (closeit)
|
|
|
|
fclose(fp);
|
|
|
|
if (mod == NULL) {
|
|
|
|
PyArena_Free(arena);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
ret = run_mod(mod, filename, globals, locals, flags, arena);
|
|
|
|
PyArena_Free(arena);
|
|
|
|
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_mod(mod_ty mod, const char *filename, PyObject *globals, PyObject *locals,
|
2010-05-09 11:46:46 -03:00
|
|
|
PyCompilerFlags *flags, PyArena *arena)
|
1992-08-04 09:41:02 -03:00
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
PyCodeObject *co;
|
|
|
|
PyObject *v;
|
|
|
|
co = PyAST_Compile(mod, filename, flags, arena);
|
|
|
|
if (co == NULL)
|
|
|
|
return NULL;
|
|
|
|
v = PyEval_EvalCode(co, globals, locals);
|
|
|
|
Py_DECREF(co);
|
|
|
|
return v;
|
1992-08-04 09:41:02 -03:00
|
|
|
}
|
|
|
|
|
1997-03-04 20:20:32 -04:00
|
|
|
static PyObject *
|
2006-05-28 07:41:29 -03:00
|
|
|
run_pyc_file(FILE *fp, const char *filename, PyObject *globals,
|
2010-05-09 11:46:46 -03:00
|
|
|
PyObject *locals, PyCompilerFlags *flags)
|
|
|
|
{
|
|
|
|
PyCodeObject *co;
|
|
|
|
PyObject *v;
|
|
|
|
long magic;
|
|
|
|
long PyImport_GetMagicNumber(void);
|
|
|
|
|
|
|
|
magic = PyMarshal_ReadLongFromFile(fp);
|
|
|
|
if (magic != PyImport_GetMagicNumber()) {
|
|
|
|
PyErr_SetString(PyExc_RuntimeError,
|
|
|
|
"Bad magic number in .pyc file");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
(void) PyMarshal_ReadLongFromFile(fp);
|
|
|
|
v = PyMarshal_ReadLastObjectFromFile(fp);
|
|
|
|
fclose(fp);
|
|
|
|
if (v == NULL || !PyCode_Check(v)) {
|
|
|
|
Py_XDECREF(v);
|
|
|
|
PyErr_SetString(PyExc_RuntimeError,
|
|
|
|
"Bad code object in .pyc file");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
co = (PyCodeObject *)v;
|
|
|
|
v = PyEval_EvalCode(co, globals, locals);
|
|
|
|
if (v && flags)
|
|
|
|
flags->cf_flags |= (co->co_flags & PyCF_MASK);
|
|
|
|
Py_DECREF(co);
|
|
|
|
return v;
|
1994-09-14 10:31:04 -03:00
|
|
|
}
|
|
|
|
|
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,
|
2010-05-09 11:46:46 -03:00
|
|
|
PyCompilerFlags *flags)
|
|
|
|
{
|
|
|
|
PyCodeObject *co;
|
|
|
|
mod_ty mod;
|
|
|
|
PyArena *arena = PyArena_New();
|
|
|
|
if (arena == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
mod = PyParser_ASTFromString(str, filename, start, flags, arena);
|
|
|
|
if (mod == NULL) {
|
|
|
|
PyArena_Free(arena);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (flags && (flags->cf_flags & PyCF_ONLY_AST)) {
|
|
|
|
PyObject *result = PyAST_mod2obj(mod);
|
|
|
|
PyArena_Free(arena);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
co = PyAST_Compile(mod, filename, flags, arena);
|
|
|
|
PyArena_Free(arena);
|
|
|
|
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
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
struct symtable *st;
|
|
|
|
mod_ty mod;
|
|
|
|
PyCompilerFlags flags;
|
|
|
|
PyArena *arena = PyArena_New();
|
|
|
|
if (arena == NULL)
|
|
|
|
return NULL;
|
2006-07-22 13:20:49 -03:00
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
flags.cf_flags = 0;
|
2008-03-26 19:51:58 -03:00
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
mod = PyParser_ASTFromString(str, filename, start, &flags, arena);
|
|
|
|
if (mod == NULL) {
|
|
|
|
PyArena_Free(arena);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
st = PySymtable_Build(mod, filename, 0);
|
|
|
|
PyArena_Free(arena);
|
|
|
|
return st;
|
2001-02-02 14:19:15 -04:00
|
|
|
}
|
|
|
|
|
2005-10-20 16:59:25 -03:00
|
|
|
/* Preferred access to parser is through AST. */
|
|
|
|
mod_ty
|
2006-05-28 07:41:29 -03:00
|
|
|
PyParser_ASTFromString(const char *s, const char *filename, int start,
|
2010-05-09 11:46:46 -03:00
|
|
|
PyCompilerFlags *flags, PyArena *arena)
|
|
|
|
{
|
|
|
|
mod_ty mod;
|
|
|
|
PyCompilerFlags localflags;
|
|
|
|
perrdetail err;
|
|
|
|
int iflags = PARSER_FLAGS(flags);
|
|
|
|
|
|
|
|
node *n = PyParser_ParseStringFlagsFilenameEx(s, filename,
|
|
|
|
&_PyParser_Grammar, start, &err,
|
|
|
|
&iflags);
|
|
|
|
if (flags == NULL) {
|
|
|
|
localflags.cf_flags = 0;
|
|
|
|
flags = &localflags;
|
|
|
|
}
|
|
|
|
if (n) {
|
|
|
|
flags->cf_flags |= iflags & PyCF_MASK;
|
|
|
|
mod = PyAST_FromNode(n, flags, filename, arena);
|
|
|
|
PyNode_Free(n);
|
|
|
|
return mod;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
err_input(&err);
|
|
|
|
return NULL;
|
|
|
|
}
|
2005-10-20 16:59:25 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
mod_ty
|
2006-05-28 07:41:29 -03:00
|
|
|
PyParser_ASTFromFile(FILE *fp, const char *filename, int start, char *ps1,
|
2010-05-09 11:46:46 -03:00
|
|
|
char *ps2, PyCompilerFlags *flags, int *errcode,
|
|
|
|
PyArena *arena)
|
|
|
|
{
|
|
|
|
mod_ty mod;
|
|
|
|
PyCompilerFlags localflags;
|
|
|
|
perrdetail err;
|
|
|
|
int iflags = PARSER_FLAGS(flags);
|
|
|
|
|
|
|
|
node *n = PyParser_ParseFileFlagsEx(fp, filename, &_PyParser_Grammar,
|
|
|
|
start, ps1, ps2, &err, &iflags);
|
|
|
|
if (flags == NULL) {
|
|
|
|
localflags.cf_flags = 0;
|
|
|
|
flags = &localflags;
|
|
|
|
}
|
|
|
|
if (n) {
|
|
|
|
flags->cf_flags |= iflags & PyCF_MASK;
|
|
|
|
mod = PyAST_FromNode(n, flags, filename, arena);
|
|
|
|
PyNode_Free(n);
|
|
|
|
return mod;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
err_input(&err);
|
|
|
|
if (errcode)
|
|
|
|
*errcode = err.error;
|
|
|
|
return NULL;
|
|
|
|
}
|
2005-10-20 16:59:25 -03:00
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
perrdetail err;
|
|
|
|
node *n = PyParser_ParseFileFlags(fp, filename, &_PyParser_Grammar,
|
|
|
|
start, NULL, NULL, &err, flags);
|
|
|
|
if (n == NULL)
|
|
|
|
err_input(&err);
|
2006-05-28 07:41:29 -03:00
|
|
|
|
2010-05-09 11:46:46 -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
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
perrdetail err;
|
|
|
|
node *n = PyParser_ParseStringFlags(str, &_PyParser_Grammar,
|
|
|
|
start, &err, flags);
|
|
|
|
if (n == NULL)
|
|
|
|
err_input(&err);
|
|
|
|
return n;
|
1994-08-29 09:50:44 -03:00
|
|
|
}
|
|
|
|
|
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,
|
2010-05-09 11:46:46 -03:00
|
|
|
int start, int flags)
|
2002-07-09 06:23:27 -03:00
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
perrdetail err;
|
|
|
|
node *n = PyParser_ParseStringFlagsFilename(str, filename,
|
|
|
|
&_PyParser_Grammar, start, &err, flags);
|
|
|
|
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_SimpleParseStringFilename(const char *str, const char *filename, int start)
|
2002-07-09 06:23:27 -03:00
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
return PyParser_SimpleParseStringFlagsFilename(str, filename, start, 0);
|
2002-07-09 06:23:27 -03:00
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
err_input(err);
|
2003-04-17 13:02:26 -03:00
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
PyObject *v, *w, *errtype;
|
|
|
|
PyObject* u = NULL;
|
|
|
|
char *msg = NULL;
|
|
|
|
errtype = PyExc_SyntaxError;
|
|
|
|
switch (err->error) {
|
|
|
|
case E_ERROR:
|
2018-07-11 18:53:17 -03:00
|
|
|
goto cleanup;
|
2010-05-09 11:46:46 -03:00
|
|
|
case E_SYNTAX:
|
|
|
|
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";
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case E_TOKEN:
|
|
|
|
msg = "invalid token";
|
|
|
|
break;
|
|
|
|
case E_EOFS:
|
|
|
|
msg = "EOF while scanning triple-quoted string literal";
|
|
|
|
break;
|
|
|
|
case E_EOLS:
|
|
|
|
msg = "EOL while scanning string literal";
|
|
|
|
break;
|
|
|
|
case E_INTR:
|
|
|
|
if (!PyErr_Occurred())
|
|
|
|
PyErr_SetNone(PyExc_KeyboardInterrupt);
|
|
|
|
goto cleanup;
|
|
|
|
case E_NOMEM:
|
|
|
|
PyErr_NoMemory();
|
|
|
|
goto cleanup;
|
|
|
|
case E_EOF:
|
|
|
|
msg = "unexpected EOF while parsing";
|
|
|
|
break;
|
|
|
|
case E_TABSPACE:
|
|
|
|
errtype = PyExc_TabError;
|
|
|
|
msg = "inconsistent use of tabs and spaces in indentation";
|
|
|
|
break;
|
|
|
|
case E_OVERFLOW:
|
|
|
|
msg = "expression too long";
|
|
|
|
break;
|
|
|
|
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;
|
|
|
|
case E_DECODE: {
|
|
|
|
PyObject *type, *value, *tb;
|
|
|
|
PyErr_Fetch(&type, &value, &tb);
|
|
|
|
if (value != NULL) {
|
|
|
|
u = PyObject_Str(value);
|
|
|
|
if (u != NULL) {
|
|
|
|
msg = PyString_AsString(u);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (msg == NULL)
|
|
|
|
msg = "unknown decode error";
|
|
|
|
Py_XDECREF(type);
|
|
|
|
Py_XDECREF(value);
|
|
|
|
Py_XDECREF(tb);
|
|
|
|
break;
|
|
|
|
}
|
2018-09-10 15:46:14 -03:00
|
|
|
case E_IO:
|
|
|
|
msg = "I/O error while reading";
|
|
|
|
break;
|
2010-05-09 11:46:46 -03:00
|
|
|
case E_LINECONT:
|
|
|
|
msg = "unexpected character after line continuation character";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fprintf(stderr, "error=%d\n", err->error);
|
|
|
|
msg = "unknown parsing error";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
v = Py_BuildValue("(ziiz)", err->filename,
|
|
|
|
err->lineno, err->offset, err->text);
|
|
|
|
w = NULL;
|
|
|
|
if (v != NULL)
|
|
|
|
w = Py_BuildValue("(sO)", msg, v);
|
|
|
|
Py_XDECREF(u);
|
|
|
|
Py_XDECREF(v);
|
|
|
|
PyErr_SetObject(errtype, w);
|
|
|
|
Py_XDECREF(w);
|
2008-07-19 07:08:55 -03:00
|
|
|
cleanup:
|
2010-05-09 11:46:46 -03:00
|
|
|
if (err->text != NULL) {
|
|
|
|
PyObject_FREE(err->text);
|
|
|
|
err->text = NULL;
|
|
|
|
}
|
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
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
fprintf(stderr, "Fatal Python error: %s\n", msg);
|
|
|
|
fflush(stderr); /* it helps in Windows debug build */
|
2009-03-31 19:20:35 -03:00
|
|
|
|
2002-06-30 12:26:10 -03:00
|
|
|
#ifdef MS_WINDOWS
|
2010-05-09 11:46:46 -03:00
|
|
|
{
|
|
|
|
size_t len = strlen(msg);
|
|
|
|
WCHAR* buffer;
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
/* Convert the message to wchar_t. This uses a simple one-to-one
|
|
|
|
conversion, assuming that the this error message actually uses ASCII
|
|
|
|
only. If this ceases to be true, we will have to convert. */
|
|
|
|
buffer = alloca( (len+1) * (sizeof *buffer));
|
|
|
|
for( i=0; i<=len; ++i)
|
|
|
|
buffer[i] = msg[i];
|
|
|
|
OutputDebugStringW(L"Fatal Python error: ");
|
|
|
|
OutputDebugStringW(buffer);
|
|
|
|
OutputDebugStringW(L"\n");
|
|
|
|
}
|
1998-08-13 10:33:16 -03:00
|
|
|
#ifdef _DEBUG
|
2010-05-09 11:46:46 -03:00
|
|
|
DebugBreak();
|
1995-01-25 20:40:38 -04:00
|
|
|
#endif
|
2002-06-30 12:26:10 -03:00
|
|
|
#endif /* MS_WINDOWS */
|
2010-05-09 11:46:46 -03:00
|
|
|
abort();
|
1992-08-04 09:41:02 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 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
|
|
|
|
|
2009-10-20 18:29:37 -03:00
|
|
|
/* Wait until threading._shutdown completes, provided
|
|
|
|
the threading module was imported in the first place.
|
|
|
|
The shutdown routine will wait until all non-daemon
|
|
|
|
"threading" threads have completed. */
|
|
|
|
static void
|
|
|
|
wait_for_thread_shutdown(void)
|
|
|
|
{
|
|
|
|
#ifdef WITH_THREAD
|
2010-05-09 11:46:46 -03:00
|
|
|
PyObject *result;
|
|
|
|
PyThreadState *tstate = PyThreadState_GET();
|
|
|
|
PyObject *threading = PyMapping_GetItemString(tstate->interp->modules,
|
|
|
|
"threading");
|
|
|
|
if (threading == NULL) {
|
|
|
|
/* threading not imported */
|
|
|
|
PyErr_Clear();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
result = PyObject_CallMethod(threading, "_shutdown", "");
|
|
|
|
if (result == NULL)
|
|
|
|
PyErr_WriteUnraisable(threading);
|
|
|
|
else
|
|
|
|
Py_DECREF(result);
|
|
|
|
Py_DECREF(threading);
|
2009-10-20 18:29:37 -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
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
if (nexitfuncs >= NEXITFUNCS)
|
|
|
|
return -1;
|
|
|
|
exitfuncs[nexitfuncs++] = func;
|
|
|
|
return 0;
|
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_sys_exitfunc(void)
|
1992-08-04 09:41:02 -03:00
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
PyObject *exitfunc = PySys_GetObject("exitfunc");
|
1992-09-03 17:28:00 -03:00
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
if (exitfunc) {
|
|
|
|
PyObject *res;
|
|
|
|
Py_INCREF(exitfunc);
|
|
|
|
PySys_SetObject("exitfunc", (PyObject *)NULL);
|
|
|
|
res = PyEval_CallObject(exitfunc, (PyObject *)NULL);
|
|
|
|
if (res == NULL) {
|
|
|
|
if (!PyErr_ExceptionMatches(PyExc_SystemExit)) {
|
|
|
|
PySys_WriteStderr("Error in sys.exitfunc:\n");
|
|
|
|
}
|
|
|
|
PyErr_Print();
|
|
|
|
}
|
|
|
|
Py_DECREF(exitfunc);
|
|
|
|
}
|
1992-09-03 17:28:00 -03:00
|
|
|
|
2010-05-09 11:46:46 -03: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
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
while (nexitfuncs > 0)
|
|
|
|
(*exitfuncs[--nexitfuncs])();
|
1997-08-02 00:10:38 -03:00
|
|
|
|
2010-05-09 11:46:46 -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
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
Py_Finalize();
|
1992-08-04 09:41:02 -03:00
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
exit(sts);
|
1992-08-04 09:41:02 -03:00
|
|
|
}
|
|
|
|
|
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
|
2010-05-09 11:46:46 -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
|
2010-05-09 11:46:46 -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
|
2010-05-09 11:46:46 -03:00
|
|
|
PyOS_setsig(SIGXFSZ, SIG_IGN);
|
2002-04-23 17:31:01 -03:00
|
|
|
#endif
|
2010-05-09 11:46:46 -03: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
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
if (isatty((int)fileno(fp)))
|
|
|
|
return 1;
|
|
|
|
if (!Py_InteractiveFlag)
|
|
|
|
return 0;
|
|
|
|
return (filename == NULL) ||
|
|
|
|
(strcmp(filename, "<stdin>") == 0) ||
|
|
|
|
(strcmp(filename, "???") == 0);
|
1997-02-14 15:45:36 -04:00
|
|
|
}
|
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
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
__try {
|
|
|
|
/* alloca throws a stack overflow exception if there's
|
|
|
|
not enough space left on the stack */
|
|
|
|
alloca(PYOS_STACK_MARGIN * sizeof(void*));
|
|
|
|
return 0;
|
|
|
|
} __except (GetExceptionCode() == STATUS_STACK_OVERFLOW ?
|
|
|
|
EXCEPTION_EXECUTE_HANDLER :
|
|
|
|
EXCEPTION_CONTINUE_SEARCH) {
|
|
|
|
int errcode = _resetstkoflw();
|
|
|
|
if (errcode == 0)
|
|
|
|
{
|
|
|
|
Py_FatalError("Could not reset the stack!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
2000-08-27 16:15:31 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
#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
|
2010-05-09 11:46:46 -03:00
|
|
|
struct sigaction context;
|
|
|
|
if (sigaction(sig, NULL, &context) == -1)
|
|
|
|
return SIG_ERR;
|
|
|
|
return context.sa_handler;
|
2000-09-16 13:32:19 -03:00
|
|
|
#else
|
2010-05-09 11:46:46 -03:00
|
|
|
PyOS_sighandler_t handler;
|
2005-11-28 13:34:23 -04:00
|
|
|
/* Special signal handling for the secure CRT in Visual Studio 2005 */
|
|
|
|
#if defined(_MSC_VER) && _MSC_VER >= 1400
|
2010-05-09 11:46:46 -03:00
|
|
|
switch (sig) {
|
|
|
|
/* Only these signals are valid */
|
|
|
|
case SIGINT:
|
|
|
|
case SIGILL:
|
|
|
|
case SIGFPE:
|
|
|
|
case SIGSEGV:
|
|
|
|
case SIGTERM:
|
|
|
|
case SIGBREAK:
|
|
|
|
case SIGABRT:
|
|
|
|
break;
|
|
|
|
/* Don't call signal() with other values or it will assert */
|
|
|
|
default:
|
|
|
|
return SIG_ERR;
|
|
|
|
}
|
2005-11-28 13:34:23 -04:00
|
|
|
#endif /* _MSC_VER && _MSC_VER >= 1400 */
|
2010-05-09 11:46:46 -03:00
|
|
|
handler = signal(sig, SIG_IGN);
|
|
|
|
if (handler != SIG_ERR)
|
|
|
|
signal(sig, handler);
|
|
|
|
return handler;
|
2000-09-16 13:32:19 -03:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
PyOS_sighandler_t
|
|
|
|
PyOS_setsig(int sig, PyOS_sighandler_t handler)
|
|
|
|
{
|
|
|
|
#ifdef HAVE_SIGACTION
|
2010-05-09 11:46:46 -03:00
|
|
|
/* Some code in Modules/signalmodule.c depends on sigaction() being
|
|
|
|
* used here if HAVE_SIGACTION is defined. Fix that if this code
|
|
|
|
* changes to invalidate that assumption.
|
|
|
|
*/
|
|
|
|
struct sigaction context, ocontext;
|
|
|
|
context.sa_handler = handler;
|
|
|
|
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
|
2010-05-09 11:46:46 -03:00
|
|
|
PyOS_sighandler_t oldhandler;
|
|
|
|
oldhandler = signal(sig, handler);
|
2004-10-13 11:48:50 -03:00
|
|
|
#ifdef HAVE_SIGINTERRUPT
|
2010-05-09 11:46:46 -03:00
|
|
|
siginterrupt(sig, 1);
|
2004-10-13 11:48:50 -03:00
|
|
|
#endif
|
2010-05-09 11:46:46 -03:00
|
|
|
return oldhandler;
|
2000-09-16 13:32:19 -03:00
|
|
|
#endif
|
|
|
|
}
|
2005-10-20 16:59:25 -03:00
|
|
|
|
2016-05-26 02:28:50 -03:00
|
|
|
/* Deprecated C API functions still provided for binary compatibility */
|
2005-10-20 16:59:25 -03:00
|
|
|
|
|
|
|
#undef PyParser_SimpleParseFile
|
2006-04-18 15:51:06 -03:00
|
|
|
PyAPI_FUNC(node *)
|
2005-10-20 16:59:25 -03:00
|
|
|
PyParser_SimpleParseFile(FILE *fp, const char *filename, int start)
|
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
return PyParser_SimpleParseFileFlags(fp, filename, start, 0);
|
2005-10-20 16:59:25 -03:00
|
|
|
}
|
|
|
|
|
2006-04-18 15:51:06 -03:00
|
|
|
#undef PyParser_SimpleParseString
|
|
|
|
PyAPI_FUNC(node *)
|
2005-10-20 16:59:25 -03:00
|
|
|
PyParser_SimpleParseString(const char *str, int start)
|
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
return PyParser_SimpleParseStringFlags(str, start, 0);
|
2005-10-20 16:59:25 -03:00
|
|
|
}
|
2006-04-12 23:06:09 -03:00
|
|
|
|
2006-04-18 15:51:06 -03:00
|
|
|
#undef PyRun_AnyFile
|
|
|
|
PyAPI_FUNC(int)
|
|
|
|
PyRun_AnyFile(FILE *fp, const char *name)
|
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
return PyRun_AnyFileExFlags(fp, name, 0, NULL);
|
2006-04-18 15:51:06 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
#undef PyRun_AnyFileEx
|
|
|
|
PyAPI_FUNC(int)
|
|
|
|
PyRun_AnyFileEx(FILE *fp, const char *name, int closeit)
|
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
return PyRun_AnyFileExFlags(fp, name, closeit, NULL);
|
2006-04-18 15:51:06 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
#undef PyRun_AnyFileFlags
|
|
|
|
PyAPI_FUNC(int)
|
|
|
|
PyRun_AnyFileFlags(FILE *fp, const char *name, PyCompilerFlags *flags)
|
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
return PyRun_AnyFileExFlags(fp, name, 0, flags);
|
2006-04-18 15:51:06 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
#undef PyRun_File
|
|
|
|
PyAPI_FUNC(PyObject *)
|
|
|
|
PyRun_File(FILE *fp, const char *p, int s, PyObject *g, PyObject *l)
|
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
return PyRun_FileExFlags(fp, p, s, g, l, 0, NULL);
|
2006-04-18 15:51:06 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
#undef PyRun_FileEx
|
|
|
|
PyAPI_FUNC(PyObject *)
|
|
|
|
PyRun_FileEx(FILE *fp, const char *p, int s, PyObject *g, PyObject *l, int c)
|
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
return PyRun_FileExFlags(fp, p, s, g, l, c, NULL);
|
2006-04-18 15:51:06 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
#undef PyRun_FileFlags
|
|
|
|
PyAPI_FUNC(PyObject *)
|
|
|
|
PyRun_FileFlags(FILE *fp, const char *p, int s, PyObject *g, PyObject *l,
|
2010-05-09 11:46:46 -03:00
|
|
|
PyCompilerFlags *flags)
|
2006-04-18 15:51:06 -03:00
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
return PyRun_FileExFlags(fp, p, s, g, l, 0, flags);
|
2006-04-18 15:51:06 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
#undef PyRun_SimpleFile
|
|
|
|
PyAPI_FUNC(int)
|
|
|
|
PyRun_SimpleFile(FILE *f, const char *p)
|
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
return PyRun_SimpleFileExFlags(f, p, 0, NULL);
|
2006-04-18 15:51:06 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
#undef PyRun_SimpleFileEx
|
|
|
|
PyAPI_FUNC(int)
|
|
|
|
PyRun_SimpleFileEx(FILE *f, const char *p, int c)
|
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
return PyRun_SimpleFileExFlags(f, p, c, NULL);
|
2006-04-18 15:51:06 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#undef PyRun_String
|
|
|
|
PyAPI_FUNC(PyObject *)
|
|
|
|
PyRun_String(const char *str, int s, PyObject *g, PyObject *l)
|
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
return PyRun_StringFlags(str, s, g, l, NULL);
|
2006-04-18 15:51:06 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
#undef PyRun_SimpleString
|
|
|
|
PyAPI_FUNC(int)
|
|
|
|
PyRun_SimpleString(const char *s)
|
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
return PyRun_SimpleStringFlags(s, NULL);
|
2006-04-18 15:51:06 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
#undef Py_CompileString
|
|
|
|
PyAPI_FUNC(PyObject *)
|
|
|
|
Py_CompileString(const char *str, const char *p, int s)
|
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
return Py_CompileStringFlags(str, p, s, NULL);
|
2006-04-18 15:51:06 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
#undef PyRun_InteractiveOne
|
|
|
|
PyAPI_FUNC(int)
|
|
|
|
PyRun_InteractiveOne(FILE *f, const char *p)
|
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
return PyRun_InteractiveOneFlags(f, p, NULL);
|
2006-04-18 15:51:06 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
#undef PyRun_InteractiveLoop
|
|
|
|
PyAPI_FUNC(int)
|
|
|
|
PyRun_InteractiveLoop(FILE *f, const char *p)
|
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
return PyRun_InteractiveLoopFlags(f, p, NULL);
|
2006-04-18 15:51:06 -03:00
|
|
|
}
|
|
|
|
|
2006-04-12 23:06:09 -03:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|