annotate cos/python/Python/pythonrun.c @ 113:1f40be088ee8

Added first start stlink
author Windel Bouwman
date Sat, 05 Jan 2013 00:06:27 +0100
parents 7f74363f4c82
children
rev   line source
27
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2 /* Python interpreter top-level routines, including init/exit */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4 #include "Python.h"
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
5
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
6 #include "Python-ast.h"
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
7 #undef Yield /* undefine macro conflicting with winbase.h */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
8 #include "grammar.h"
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
9 #include "node.h"
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
10 #include "token.h"
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
11 #include "parsetok.h"
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
12 #include "errcode.h"
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
13 #include "code.h"
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
14 #include "symtable.h"
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
15 #include "ast.h"
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
16 #include "marshal.h"
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
17 #include "osdefs.h"
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
18
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
19 #define PRINT_TOTAL_REFS()
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
20
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
21 extern wchar_t *Py_GetPath(void);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
22
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
23 extern grammar _PyParser_Grammar; /* From graminit.c */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
24
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
25 /* Forward */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
26 static void initmain(void);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
27 static int initfsencoding(PyInterpreterState *interp);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
28 static void initsite(void);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
29 static int initstdio(void);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
30 static void flush_io(void);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
31 static PyObject *run_mod(mod_ty, const char *, PyObject *, PyObject *,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
32 PyCompilerFlags *, PyArena *);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
33 static PyObject *run_pyc_file(FILE *, const char *, PyObject *, PyObject *,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
34 PyCompilerFlags *);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
35 static void err_input(perrdetail *);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
36 static void err_free(perrdetail *);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
37 static void initsigs(void);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
38 static void call_py_exitfuncs(void);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
39 static void wait_for_thread_shutdown(void);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
40 static void call_ll_exitfuncs(void);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
41 extern int _PyUnicode_Init(void);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
42 extern void _PyUnicode_Fini(void);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
43 extern int _PyLong_Init(void);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
44 extern void PyLong_Fini(void);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
45 extern int _PyFaulthandler_Init(void);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
46 extern void _PyFaulthandler_Fini(void);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
47
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
48 #ifdef WITH_THREAD
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
49 extern void _PyGILState_Init(PyInterpreterState *, PyThreadState *);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
50 extern void _PyGILState_Fini(void);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
51 #endif /* WITH_THREAD */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
52
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
53 int Py_DebugFlag; /* Needed by parser.c */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
54 int Py_VerboseFlag; /* Needed by import.c */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
55 int Py_QuietFlag; /* Needed by sysmodule.c */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
56 int Py_InteractiveFlag; /* Needed by Py_FdIsInteractive() below */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
57 int Py_InspectFlag; /* Needed to determine whether to exit at SystemExit */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
58 int Py_NoSiteFlag; /* Suppress 'import site' */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
59 int Py_BytesWarningFlag; /* Warn on str(bytes) and str(buffer) */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
60 int Py_DontWriteBytecodeFlag; /* Suppress writing bytecode files (*.py[co]) */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
61 int Py_UseClassExceptionsFlag = 1; /* Needed by bltinmodule.c: deprecated */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
62 int Py_FrozenFlag; /* Needed by getpath.c */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
63 int Py_IgnoreEnvironmentFlag; /* e.g. PYTHONPATH, PYTHONHOME */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
64 int Py_NoUserSiteDirectory = 0; /* for -s and site.py */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
65 int Py_UnbufferedStdioFlag = 0; /* Unbuffered binary std{in,out,err} */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
66
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
67 PyThreadState *_Py_Finalizing = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
68
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
69 /* PyModule_GetWarningsModule is no longer necessary as of 2.6
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
70 since _warnings is builtin. This API should not be used. */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
71 PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
72 PyModule_GetWarningsModule(void)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
73 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
74 return PyImport_ImportModule("warnings");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
75 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
76
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
77 static int initialized = 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
78
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
79 /* API to access the initialized flag -- useful for esoteric use */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
80
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
81 int
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
82 Py_IsInitialized(void)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
83 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
84 return initialized;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
85 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
86
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
87 /* Global initializations. Can be undone by Py_Finalize(). Don't
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
88 call this twice without an intervening Py_Finalize() call. When
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
89 initializations fail, a fatal error is issued and the function does
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
90 not return. On return, the first thread and interpreter state have
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
91 been created.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
92
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
93 Locking: you must hold the interpreter lock while calling this.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
94 (If the lock has not yet been initialized, that's equivalent to
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
95 having the lock, but you cannot use multiple threads.)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
96
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
97 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
98
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
99 static int
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
100 add_flag(int flag, const char *envs)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
101 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
102 int env = atoi(envs);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
103 if (flag < env)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
104 flag = env;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
105 if (flag < 1)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
106 flag = 1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
107 return flag;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
108 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
109
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
110 static char*
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
111 get_codec_name(const char *encoding)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
112 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
113 char *name_utf8, *name_str;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
114 PyObject *codec, *name = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
115 _Py_IDENTIFIER(name);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
116
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
117 codec = _PyCodec_Lookup(encoding);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
118 if (!codec)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
119 goto error;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
120
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
121 name = _PyObject_GetAttrId(codec, &PyId_name);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
122 Py_CLEAR(codec);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
123 if (!name)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
124 goto error;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
125
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
126 name_utf8 = _PyUnicode_AsString(name);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
127 if (name_utf8 == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
128 goto error;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
129 name_str = strdup(name_utf8);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
130 Py_DECREF(name);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
131 if (name_str == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
132 PyErr_NoMemory();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
133 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
134 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
135 return name_str;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
136
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
137 error:
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
138 Py_XDECREF(codec);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
139 Py_XDECREF(name);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
140 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
141 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
142
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
143 static char*
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
144 get_locale_encoding(void)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
145 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
146 PyErr_SetNone(PyExc_NotImplementedError);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
147 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
148 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
149
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
150 void
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
151 Py_InitializeEx(int install_sigs)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
152 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
153 PyInterpreterState *interp;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
154 PyThreadState *tstate;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
155 PyObject *bimod, *sysmod, *pstderr;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
156 char *p;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
157 extern void _Py_ReadyTypes(void);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
158
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
159 if (initialized)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
160 return;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
161 initialized = 1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
162 _Py_Finalizing = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
163
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
164 if ((p = Py_GETENV("PYTHONDEBUG")) && *p != '\0')
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
165 Py_DebugFlag = add_flag(Py_DebugFlag, p);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
166 if ((p = Py_GETENV("PYTHONVERBOSE")) && *p != '\0')
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
167 Py_VerboseFlag = add_flag(Py_VerboseFlag, p);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
168 if ((p = Py_GETENV("PYTHONOPTIMIZE")) && *p != '\0')
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
169 Py_OptimizeFlag = add_flag(Py_OptimizeFlag, p);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
170 if ((p = Py_GETENV("PYTHONDONTWRITEBYTECODE")) && *p != '\0')
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
171 Py_DontWriteBytecodeFlag = add_flag(Py_DontWriteBytecodeFlag, p);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
172
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
173 interp = PyInterpreterState_New();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
174 if (interp == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
175 Py_FatalError("Py_Initialize: can't make first interpreter");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
176
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
177 tstate = PyThreadState_New(interp);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
178 if (tstate == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
179 Py_FatalError("Py_Initialize: can't make first thread");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
180 (void) PyThreadState_Swap(tstate);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
181
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
182 #ifdef WITH_THREAD
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
183 /* We can't call _PyEval_FiniThreads() in Py_Finalize because
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
184 destroying the GIL might fail when it is being referenced from
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
185 another running thread (see issue #9901).
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
186 Instead we destroy the previously created GIL here, which ensures
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
187 that we can call Py_Initialize / Py_Finalize multiple times. */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
188 _PyEval_FiniThreads();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
189
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
190 /* Auto-thread-state API */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
191 _PyGILState_Init(interp, tstate);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
192 #endif /* WITH_THREAD */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
193
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
194 _Py_ReadyTypes();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
195
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
196 if (!_PyFrame_Init())
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
197 Py_FatalError("Py_Initialize: can't init frames");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
198
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
199 if (!_PyLong_Init())
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
200 Py_FatalError("Py_Initialize: can't init longs");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
201
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
202 if (!PyByteArray_Init())
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
203 Py_FatalError("Py_Initialize: can't init bytearray");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
204
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
205 _PyFloat_Init();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
206
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
207 interp->modules = PyDict_New();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
208 if (interp->modules == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
209 Py_FatalError("Py_Initialize: can't make modules dictionary");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
210 interp->modules_reloading = PyDict_New();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
211 if (interp->modules_reloading == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
212 Py_FatalError("Py_Initialize: can't make modules_reloading dictionary");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
213
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
214 /* Init Unicode implementation; relies on the codec registry */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
215 if (_PyUnicode_Init() < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
216 Py_FatalError("Py_Initialize: can't initialize unicode");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
217
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
218 bimod = _PyBuiltin_Init();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
219 if (bimod == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
220 Py_FatalError("Py_Initialize: can't initialize builtins modules");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
221 _PyImport_FixupBuiltin(bimod, "builtins");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
222 interp->builtins = PyModule_GetDict(bimod);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
223 if (interp->builtins == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
224 Py_FatalError("Py_Initialize: can't initialize builtins dict");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
225 Py_INCREF(interp->builtins);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
226
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
227 /* initialize builtin exceptions */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
228 _PyExc_Init();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
229
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
230 sysmod = _PySys_Init();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
231 if (sysmod == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
232 Py_FatalError("Py_Initialize: can't initialize sys");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
233 interp->sysdict = PyModule_GetDict(sysmod);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
234 if (interp->sysdict == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
235 Py_FatalError("Py_Initialize: can't initialize sys dict");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
236 Py_INCREF(interp->sysdict);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
237 _PyImport_FixupBuiltin(sysmod, "sys");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
238 PySys_SetPath(Py_GetPath());
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
239 PyDict_SetItemString(interp->sysdict, "modules",
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
240 interp->modules);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
241
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
242 /* Set up a preliminary stderr printer until we have enough
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
243 infrastructure for the io module in place. */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
244 pstderr = PyFile_NewStdPrinter(fileno(stderr));
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
245 if (pstderr == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
246 Py_FatalError("Py_Initialize: can't set preliminary stderr");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
247 PySys_SetObject("stderr", pstderr);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
248 PySys_SetObject("__stderr__", pstderr);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
249 Py_DECREF(pstderr);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
250
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
251 _PyImport_Init();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
252
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
253 _PyImportHooks_Init();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
254
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
255 /* initialize the faulthandler module */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
256 if (_PyFaulthandler_Init())
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
257 Py_FatalError("Py_Initialize: can't initialize faulthandler");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
258
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
259 /* Initialize _warnings. */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
260 _PyWarnings_Init();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
261
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
262 _PyTime_Init();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
263
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
264 if (initfsencoding(interp) < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
265 Py_FatalError("Py_Initialize: unable to load the file system codec");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
266
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
267 if (install_sigs)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
268 initsigs(); /* Signal handling stuff, including initintr() */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
269
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
270 initmain(); /* Module __main__ */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
271 if (initstdio() < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
272 Py_FatalError(
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
273 "Py_Initialize: can't initialize sys standard streams");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
274
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
275 /* Initialize warnings. */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
276 if (PySys_HasWarnOptions()) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
277 PyObject *warnings_module = PyImport_ImportModule("warnings");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
278 if (warnings_module == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
279 fprintf(stderr, "'import warnings' failed; traceback:\n");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
280 PyErr_Print();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
281 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
282 Py_XDECREF(warnings_module);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
283 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
284
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
285 if (!Py_NoSiteFlag)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
286 initsite(); /* Module site */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
287 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
288
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
289 void
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
290 Py_Initialize(void)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
291 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
292 Py_InitializeEx(1);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
293 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
294
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
295 /* Flush stdout and stderr */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
296
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
297 static int
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
298 file_is_closed(PyObject *fobj)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
299 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
300 int r;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
301 PyObject *tmp = PyObject_GetAttrString(fobj, "closed");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
302 if (tmp == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
303 PyErr_Clear();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
304 return 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
305 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
306 r = PyObject_IsTrue(tmp);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
307 Py_DECREF(tmp);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
308 if (r < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
309 PyErr_Clear();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
310 return r > 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
311 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
312
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
313 static void
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
314 flush_std_files(void)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
315 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
316 PyObject *fout = PySys_GetObject("stdout");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
317 PyObject *ferr = PySys_GetObject("stderr");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
318 PyObject *tmp;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
319 _Py_IDENTIFIER(flush);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
320
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
321 if (fout != NULL && fout != Py_None && !file_is_closed(fout)) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
322 tmp = _PyObject_CallMethodId(fout, &PyId_flush, "");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
323 if (tmp == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
324 PyErr_WriteUnraisable(fout);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
325 else
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
326 Py_DECREF(tmp);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
327 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
328
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
329 if (ferr != NULL && ferr != Py_None && !file_is_closed(ferr)) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
330 tmp = _PyObject_CallMethodId(ferr, &PyId_flush, "");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
331 if (tmp == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
332 PyErr_Clear();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
333 else
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
334 Py_DECREF(tmp);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
335 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
336 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
337
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
338 /* Undo the effect of Py_Initialize().
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
339
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
340 Beware: if multiple interpreter and/or thread states exist, these
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
341 are not wiped out; only the current thread and interpreter state
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
342 are deleted. But since everything else is deleted, those other
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
343 interpreter and thread states should no longer be used.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
344
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
345 (XXX We should do better, e.g. wipe out all interpreters and
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
346 threads.)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
347
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
348 Locking: as above.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
349
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
350 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
351
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
352 void
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
353 Py_Finalize(void)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
354 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
355 PyInterpreterState *interp;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
356 PyThreadState *tstate;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
357
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
358 if (!initialized)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
359 return;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
360
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
361 wait_for_thread_shutdown();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
362
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
363 /* The interpreter is still entirely intact at this point, and the
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
364 * exit funcs may be relying on that. In particular, if some thread
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
365 * or exit func is still waiting to do an import, the import machinery
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
366 * expects Py_IsInitialized() to return true. So don't say the
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
367 * interpreter is uninitialized until after the exit funcs have run.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
368 * Note that Threading.py uses an exit func to do a join on all the
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
369 * threads created thru it, so this also protects pending imports in
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
370 * the threads created via Threading.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
371 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
372 call_py_exitfuncs();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
373
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
374 /* Get current thread state and interpreter pointer */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
375 tstate = PyThreadState_GET();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
376 interp = tstate->interp;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
377
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
378 /* Remaining threads (e.g. daemon threads) will automatically exit
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
379 after taking the GIL (in PyEval_RestoreThread()). */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
380 _Py_Finalizing = tstate;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
381 initialized = 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
382
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
383 /* Flush stdout+stderr */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
384 flush_std_files();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
385
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
386 /* Disable signal handling */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
387 PyOS_FiniInterrupts();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
388
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
389 /* Clear type lookup cache */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
390 PyType_ClearCache();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
391
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
392 /* Collect garbage. This may call finalizers; it's nice to call these
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
393 * before all modules are destroyed.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
394 * XXX If a __del__ or weakref callback is triggered here, and tries to
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
395 * XXX import a module, bad things can happen, because Python no
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
396 * XXX longer believes it's initialized.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
397 * XXX Fatal Python error: Interpreter not initialized (version mismatch?)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
398 * XXX is easy to provoke that way. I've also seen, e.g.,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
399 * XXX Exception exceptions.ImportError: 'No module named sha'
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
400 * XXX in <function callback at 0x008F5718> ignored
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
401 * XXX but I'm unclear on exactly how that one happens. In any case,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
402 * XXX I haven't seen a real-life report of either of these.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
403 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
404 PyGC_Collect();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
405 /* We run this while most interpreter state is still alive, so that
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
406 debug information can be printed out */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
407 _PyGC_Fini();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
408
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
409 /* Destroy all modules */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
410 PyImport_Cleanup();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
411
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
412 /* Flush stdout+stderr (again, in case more was printed) */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
413 flush_std_files();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
414
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
415 /* Collect final garbage. This disposes of cycles created by
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
416 * new-style class definitions, for example.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
417 * XXX This is disabled because it caused too many problems. If
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
418 * XXX a __del__ or weakref callback triggers here, Python code has
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
419 * XXX a hard time running, because even the sys module has been
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
420 * XXX cleared out (sys.stdout is gone, sys.excepthook is gone, etc).
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
421 * XXX One symptom is a sequence of information-free messages
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
422 * XXX coming from threads (if a __del__ or callback is invoked,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
423 * XXX other threads can execute too, and any exception they encounter
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
424 * XXX triggers a comedy of errors as subsystem after subsystem
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
425 * XXX fails to find what it *expects* to find in sys to help report
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
426 * XXX the exception and consequent unexpected failures). I've also
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
427 * XXX seen segfaults then, after adding print statements to the
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
428 * XXX Python code getting called.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
429 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
430
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
431 /* Destroy the database used by _PyImport_{Fixup,Find}Extension */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
432 _PyImport_Fini();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
433
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
434 /* unload faulthandler module */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
435 _PyFaulthandler_Fini();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
436
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
437 /* Clear interpreter state */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
438 PyInterpreterState_Clear(interp);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
439
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
440 /* Now we decref the exception classes. After this point nothing
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
441 can raise an exception. That's okay, because each Fini() method
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
442 below has been checked to make sure no exceptions are ever
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
443 raised.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
444 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
445
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
446 _PyExc_Fini();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
447
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
448 /* Cleanup auto-thread-state */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
449 #ifdef WITH_THREAD
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
450 _PyGILState_Fini();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
451 #endif /* WITH_THREAD */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
452
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
453 /* Delete current thread */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
454 PyThreadState_Swap(NULL);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
455 PyInterpreterState_Delete(interp);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
456
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
457 /* Sundry finalizers */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
458 PyMethod_Fini();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
459 PyFrame_Fini();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
460 PyCFunction_Fini();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
461 PyTuple_Fini();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
462 PyList_Fini();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
463 PySet_Fini();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
464 PyBytes_Fini();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
465 PyByteArray_Fini();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
466 PyLong_Fini();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
467 PyFloat_Fini();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
468 PyDict_Fini();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
469 PySlice_Fini();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
470
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
471 /* Cleanup Unicode implementation */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
472 _PyUnicode_Fini();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
473
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
474 /* reset file system default encoding */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
475 if (!Py_HasFileSystemDefaultEncoding && Py_FileSystemDefaultEncoding) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
476 free((char*)Py_FileSystemDefaultEncoding);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
477 Py_FileSystemDefaultEncoding = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
478 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
479
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
480 /* XXX Still allocated:
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
481 - various static ad-hoc pointers to interned strings
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
482 - int and float free list blocks
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
483 - whatever various modules and libraries allocate
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
484 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
485
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
486 PyGrammar_RemoveAccelerators(&_PyParser_Grammar);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
487
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
488 call_ll_exitfuncs();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
489 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
490
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
491 /* Create and initialize a new interpreter and thread, and return the
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
492 new thread. This requires that Py_Initialize() has been called
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
493 first.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
494
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
495 Unsuccessful initialization yields a NULL pointer. Note that *no*
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
496 exception information is available even in this case -- the
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
497 exception information is held in the thread, and there is no
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
498 thread.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
499
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
500 Locking: as above.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
501
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
502 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
503
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
504 PyThreadState *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
505 Py_NewInterpreter(void)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
506 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
507 PyInterpreterState *interp;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
508 PyThreadState *tstate, *save_tstate;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
509 PyObject *bimod, *sysmod;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
510
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
511 if (!initialized)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
512 Py_FatalError("Py_NewInterpreter: call Py_Initialize first");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
513
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
514 interp = PyInterpreterState_New();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
515 if (interp == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
516 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
517
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
518 tstate = PyThreadState_New(interp);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
519 if (tstate == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
520 PyInterpreterState_Delete(interp);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
521 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
522 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
523
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
524 save_tstate = PyThreadState_Swap(tstate);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
525
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
526 /* XXX The following is lax in error checking */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
527
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
528 interp->modules = PyDict_New();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
529 interp->modules_reloading = PyDict_New();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
530
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
531 bimod = _PyImport_FindBuiltin("builtins");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
532 if (bimod != NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
533 interp->builtins = PyModule_GetDict(bimod);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
534 if (interp->builtins == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
535 goto handle_error;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
536 Py_INCREF(interp->builtins);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
537 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
538
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
539 /* initialize builtin exceptions */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
540 _PyExc_Init();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
541
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
542 sysmod = _PyImport_FindBuiltin("sys");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
543 if (bimod != NULL && sysmod != NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
544 PyObject *pstderr;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
545 interp->sysdict = PyModule_GetDict(sysmod);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
546 if (interp->sysdict == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
547 goto handle_error;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
548 Py_INCREF(interp->sysdict);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
549 PySys_SetPath(Py_GetPath());
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
550 PyDict_SetItemString(interp->sysdict, "modules",
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
551 interp->modules);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
552 /* Set up a preliminary stderr printer until we have enough
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
553 infrastructure for the io module in place. */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
554 pstderr = PyFile_NewStdPrinter(fileno(stderr));
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
555 if (pstderr == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
556 Py_FatalError("Py_Initialize: can't set preliminary stderr");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
557 PySys_SetObject("stderr", pstderr);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
558 PySys_SetObject("__stderr__", pstderr);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
559 Py_DECREF(pstderr);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
560
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
561 _PyImportHooks_Init();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
562
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
563 if (initfsencoding(interp) < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
564 goto handle_error;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
565
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
566 if (initstdio() < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
567 Py_FatalError(
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
568 "Py_Initialize: can't initialize sys standard streams");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
569 initmain();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
570 if (!Py_NoSiteFlag)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
571 initsite();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
572 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
573
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
574 if (!PyErr_Occurred())
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
575 return tstate;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
576
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
577 handle_error:
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
578 /* Oops, it didn't work. Undo it all. */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
579
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
580 PyErr_PrintEx(0);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
581 PyThreadState_Clear(tstate);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
582 PyThreadState_Swap(save_tstate);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
583 PyThreadState_Delete(tstate);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
584 PyInterpreterState_Delete(interp);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
585
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
586 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
587 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
588
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
589 /* Delete an interpreter and its last thread. This requires that the
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
590 given thread state is current, that the thread has no remaining
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
591 frames, and that it is its interpreter's only remaining thread.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
592 It is a fatal error to violate these constraints.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
593
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
594 (Py_Finalize() doesn't have these constraints -- it zaps
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
595 everything, regardless.)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
596
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
597 Locking: as above.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
598
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
599 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
600
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
601 void
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
602 Py_EndInterpreter(PyThreadState *tstate)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
603 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
604 PyInterpreterState *interp = tstate->interp;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
605
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
606 if (tstate != PyThreadState_GET())
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
607 Py_FatalError("Py_EndInterpreter: thread is not current");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
608 if (tstate->frame != NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
609 Py_FatalError("Py_EndInterpreter: thread still has a frame");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
610 if (tstate != interp->tstate_head || tstate->next != NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
611 Py_FatalError("Py_EndInterpreter: not the last thread");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
612
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
613 PyImport_Cleanup();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
614 PyInterpreterState_Clear(interp);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
615 PyThreadState_Swap(NULL);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
616 PyInterpreterState_Delete(interp);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
617 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
618
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
619 static wchar_t *progname = L"python";
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
620
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
621 void
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
622 Py_SetProgramName(wchar_t *pn)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
623 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
624 if (pn && *pn)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
625 progname = pn;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
626 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
627
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
628 wchar_t *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
629 Py_GetProgramName(void)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
630 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
631 return progname;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
632 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
633
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
634 static wchar_t *default_home = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
635 static wchar_t env_home[PATH_MAX+1];
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
636
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
637 void
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
638 Py_SetPythonHome(wchar_t *home)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
639 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
640 default_home = home;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
641 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
642
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
643 wchar_t *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
644 Py_GetPythonHome(void)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
645 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
646 wchar_t *home = default_home;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
647 if (home == NULL && !Py_IgnoreEnvironmentFlag) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
648 char* chome = Py_GETENV("PYTHONHOME");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
649 if (chome) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
650 size_t r = mbstowcs(env_home, chome, PATH_MAX+1);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
651 if (r != (size_t)-1 && r <= PATH_MAX)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
652 home = env_home;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
653 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
654
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
655 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
656 return home;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
657 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
658
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
659 /* Create __main__ module */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
660
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
661 static void
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
662 initmain(void)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
663 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
664 PyObject *m, *d;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
665 m = PyImport_AddModule("__main__");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
666 if (m == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
667 Py_FatalError("can't create __main__ module");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
668 d = PyModule_GetDict(m);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
669 if (PyDict_GetItemString(d, "__builtins__") == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
670 PyObject *bimod = PyImport_ImportModule("builtins");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
671 if (bimod == NULL ||
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
672 PyDict_SetItemString(d, "__builtins__", bimod) != 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
673 Py_FatalError("can't add __builtins__ to __main__");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
674 Py_DECREF(bimod);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
675 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
676 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
677
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
678 static int
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
679 initfsencoding(PyInterpreterState *interp)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
680 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
681 PyObject *codec;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
682
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
683 if (Py_FileSystemDefaultEncoding == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
684 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
685 Py_FileSystemDefaultEncoding = get_locale_encoding();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
686 if (Py_FileSystemDefaultEncoding == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
687 Py_FatalError("Py_Initialize: Unable to get the locale encoding");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
688
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
689 Py_HasFileSystemDefaultEncoding = 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
690 interp->fscodec_initialized = 1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
691 return 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
692 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
693
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
694 /* the encoding is mbcs, utf-8 or ascii */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
695 codec = _PyCodec_Lookup(Py_FileSystemDefaultEncoding);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
696 if (!codec) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
697 /* Such error can only occurs in critical situations: no more
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
698 * memory, import a module of the standard library failed,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
699 * etc. */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
700 return -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
701 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
702 Py_DECREF(codec);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
703 interp->fscodec_initialized = 1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
704 return 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
705 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
706
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
707 /* Import the site module (not into __main__ though) */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
708
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
709 static void
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
710 initsite(void)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
711 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
712 PyObject *m;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
713 m = PyImport_ImportModule("site");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
714 if (m == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
715 PyErr_Print();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
716 Py_Finalize();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
717 exit(1);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
718 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
719 else {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
720 Py_DECREF(m);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
721 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
722 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
723
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
724 static PyObject*
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
725 create_stdio(PyObject* io,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
726 int fd, int write_mode, char* name,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
727 char* encoding, char* errors)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
728 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
729 PyObject *buf = NULL, *stream = NULL, *text = NULL, *raw = NULL, *res;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
730 const char* mode;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
731 const char* newline;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
732 PyObject *line_buffering;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
733 int buffering, isatty;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
734 _Py_IDENTIFIER(open);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
735 _Py_IDENTIFIER(isatty);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
736 _Py_IDENTIFIER(TextIOWrapper);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
737 _Py_IDENTIFIER(name);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
738 _Py_IDENTIFIER(mode);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
739
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
740 /* stdin is always opened in buffered mode, first because it shouldn't
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
741 make a difference in common use cases, second because TextIOWrapper
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
742 depends on the presence of a read1() method which only exists on
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
743 buffered streams.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
744 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
745 if (Py_UnbufferedStdioFlag && write_mode)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
746 buffering = 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
747 else
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
748 buffering = -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
749 if (write_mode)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
750 mode = "wb";
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
751 else
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
752 mode = "rb";
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
753 buf = _PyObject_CallMethodId(io, &PyId_open, "isiOOOi",
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
754 fd, mode, buffering,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
755 Py_None, Py_None, Py_None, 0);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
756 if (buf == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
757 goto error;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
758
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
759 if (buffering) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
760 _Py_IDENTIFIER(raw);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
761 raw = _PyObject_GetAttrId(buf, &PyId_raw);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
762 if (raw == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
763 goto error;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
764 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
765 else {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
766 raw = buf;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
767 Py_INCREF(raw);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
768 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
769
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
770 text = PyUnicode_FromString(name);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
771 if (text == NULL || _PyObject_SetAttrId(raw, &PyId_name, text) < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
772 goto error;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
773 res = _PyObject_CallMethodId(raw, &PyId_isatty, "");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
774 if (res == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
775 goto error;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
776 isatty = PyObject_IsTrue(res);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
777 Py_DECREF(res);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
778 if (isatty == -1)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
779 goto error;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
780 if (isatty || Py_UnbufferedStdioFlag)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
781 line_buffering = Py_True;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
782 else
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
783 line_buffering = Py_False;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
784
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
785 Py_CLEAR(raw);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
786 Py_CLEAR(text);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
787
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
788 newline = "\n";
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
789
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
790 stream = _PyObject_CallMethodId(io, &PyId_TextIOWrapper, "OsssO",
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
791 buf, encoding, errors,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
792 newline, line_buffering);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
793 Py_CLEAR(buf);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
794 if (stream == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
795 goto error;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
796
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
797 if (write_mode)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
798 mode = "w";
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
799 else
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
800 mode = "r";
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
801 text = PyUnicode_FromString(mode);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
802 if (!text || _PyObject_SetAttrId(stream, &PyId_mode, text) < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
803 goto error;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
804 Py_CLEAR(text);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
805 return stream;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
806
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
807 error:
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
808 Py_XDECREF(buf);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
809 Py_XDECREF(stream);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
810 Py_XDECREF(text);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
811 Py_XDECREF(raw);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
812 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
813 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
814
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
815 static int
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
816 is_valid_fd(int fd)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
817 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
818 int dummy_fd;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
819 if (fd < 0 || !_PyVerify_fd(fd))
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
820 return 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
821 dummy_fd = dup(fd);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
822 if (dummy_fd < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
823 return 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
824 close(dummy_fd);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
825 return 1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
826 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
827
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
828 /* Initialize sys.stdin, stdout, stderr and builtins.open */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
829 static int
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
830 initstdio(void)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
831 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
832 PyObject *iomod = NULL, *wrapper;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
833 PyObject *bimod = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
834 PyObject *m;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
835 PyObject *std = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
836 int status = 0, fd;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
837 PyObject * encoding_attr;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
838 char *encoding = NULL, *errors;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
839
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
840 /* Hack to avoid a nasty recursion issue when Python is invoked
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
841 in verbose mode: pre-import the Latin-1 and UTF-8 codecs */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
842 if ((m = PyImport_ImportModule("encodings.utf_8")) == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
843 goto error;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
844 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
845 Py_DECREF(m);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
846
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
847 if (!(m = PyImport_ImportModule("encodings.latin_1"))) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
848 goto error;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
849 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
850 Py_DECREF(m);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
851
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
852 if (!(bimod = PyImport_ImportModule("builtins"))) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
853 goto error;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
854 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
855
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
856 if (!(iomod = PyImport_ImportModule("io"))) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
857 goto error;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
858 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
859 if (!(wrapper = PyObject_GetAttrString(iomod, "OpenWrapper"))) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
860 goto error;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
861 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
862
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
863 /* Set builtins.open */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
864 if (PyObject_SetAttrString(bimod, "open", wrapper) == -1) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
865 Py_DECREF(wrapper);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
866 goto error;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
867 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
868 Py_DECREF(wrapper);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
869
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
870 encoding = Py_GETENV("PYTHONIOENCODING");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
871 errors = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
872 if (encoding) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
873 encoding = strdup(encoding);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
874 errors = strchr(encoding, ':');
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
875 if (errors) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
876 *errors = '\0';
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
877 errors++;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
878 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
879 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
880
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
881 /* Set sys.stdin */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
882 fd = fileno(stdin);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
883 /* Under some conditions stdin, stdout and stderr may not be connected
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
884 * and fileno() may point to an invalid file descriptor. For example
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
885 * GUI apps don't have valid standard streams by default.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
886 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
887 if (!is_valid_fd(fd)) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
888 std = Py_None;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
889 Py_INCREF(std);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
890 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
891 else {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
892 std = create_stdio(iomod, fd, 0, "<stdin>", encoding, errors);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
893 if (std == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
894 goto error;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
895 } /* if (fd < 0) */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
896 PySys_SetObject("__stdin__", std);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
897 PySys_SetObject("stdin", std);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
898 Py_DECREF(std);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
899
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
900 /* Set sys.stdout */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
901 fd = fileno(stdout);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
902 if (!is_valid_fd(fd)) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
903 std = Py_None;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
904 Py_INCREF(std);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
905 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
906 else {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
907 std = create_stdio(iomod, fd, 1, "<stdout>", encoding, errors);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
908 if (std == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
909 goto error;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
910 } /* if (fd < 0) */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
911 PySys_SetObject("__stdout__", std);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
912 PySys_SetObject("stdout", std);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
913 Py_DECREF(std);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
914
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
915 #if 1 /* Disable this if you have trouble debugging bootstrap stuff */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
916 /* Set sys.stderr, replaces the preliminary stderr */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
917 fd = fileno(stderr);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
918 if (!is_valid_fd(fd)) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
919 std = Py_None;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
920 Py_INCREF(std);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
921 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
922 else {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
923 std = create_stdio(iomod, fd, 1, "<stderr>", encoding, "backslashreplace");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
924 if (std == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
925 goto error;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
926 } /* if (fd < 0) */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
927
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
928 /* Same as hack above, pre-import stderr's codec to avoid recursion
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
929 when import.c tries to write to stderr in verbose mode. */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
930 encoding_attr = PyObject_GetAttrString(std, "encoding");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
931 if (encoding_attr != NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
932 const char * encoding;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
933 encoding = _PyUnicode_AsString(encoding_attr);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
934 if (encoding != NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
935 _PyCodec_Lookup(encoding);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
936 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
937 Py_DECREF(encoding_attr);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
938 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
939 PyErr_Clear(); /* Not a fatal error if codec isn't available */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
940
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
941 PySys_SetObject("__stderr__", std);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
942 PySys_SetObject("stderr", std);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
943 Py_DECREF(std);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
944 #endif
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
945
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
946 if (0) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
947 error:
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
948 status = -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
949 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
950
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
951 if (encoding)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
952 free(encoding);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
953 Py_XDECREF(bimod);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
954 Py_XDECREF(iomod);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
955 return status;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
956 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
957
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
958 /* Parse input from a file and execute it */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
959
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
960 int
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
961 PyRun_AnyFileExFlags(FILE *fp, const char *filename, int closeit,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
962 PyCompilerFlags *flags)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
963 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
964 if (filename == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
965 filename = "???";
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
966 if (Py_FdIsInteractive(fp, filename)) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
967 int err = PyRun_InteractiveLoopFlags(fp, filename, flags);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
968 if (closeit)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
969 fclose(fp);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
970 return err;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
971 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
972 else
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
973 return PyRun_SimpleFileExFlags(fp, filename, closeit, flags);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
974 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
975
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
976 int
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
977 PyRun_InteractiveLoopFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
978 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
979 PyObject *v;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
980 int ret;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
981 PyCompilerFlags local_flags;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
982
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
983 if (flags == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
984 flags = &local_flags;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
985 local_flags.cf_flags = 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
986 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
987 v = PySys_GetObject("ps1");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
988 if (v == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
989 PySys_SetObject("ps1", v = PyUnicode_FromString(">>> "));
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
990 Py_XDECREF(v);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
991 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
992 v = PySys_GetObject("ps2");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
993 if (v == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
994 PySys_SetObject("ps2", v = PyUnicode_FromString("... "));
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
995 Py_XDECREF(v);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
996 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
997 for (;;) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
998 ret = PyRun_InteractiveOneFlags(fp, filename, flags);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
999 PRINT_TOTAL_REFS();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1000 if (ret == E_EOF)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1001 return 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1002 /*
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1003 if (ret == E_NOMEM)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1004 return -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1005 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1006 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1007 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1008
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1009 /* compute parser flags based on compiler flags */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1010 static int PARSER_FLAGS(PyCompilerFlags *flags)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1011 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1012 int parser_flags = 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1013 if (!flags)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1014 return 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1015 if (flags->cf_flags & PyCF_DONT_IMPLY_DEDENT)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1016 parser_flags |= PyPARSE_DONT_IMPLY_DEDENT;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1017 if (flags->cf_flags & PyCF_IGNORE_COOKIE)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1018 parser_flags |= PyPARSE_IGNORE_COOKIE;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1019 if (flags->cf_flags & CO_FUTURE_BARRY_AS_BDFL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1020 parser_flags |= PyPARSE_BARRY_AS_BDFL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1021 return parser_flags;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1022 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1023
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1024 int
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1025 PyRun_InteractiveOneFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1026 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1027 PyObject *m, *d, *v, *w, *oenc = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1028 mod_ty mod;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1029 PyArena *arena;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1030 char *ps1 = "", *ps2 = "", *enc = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1031 int errcode = 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1032 _Py_IDENTIFIER(encoding);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1033
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1034 if (fp == stdin) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1035 /* Fetch encoding from sys.stdin */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1036 v = PySys_GetObject("stdin");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1037 if (v == NULL || v == Py_None)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1038 return -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1039 oenc = _PyObject_GetAttrId(v, &PyId_encoding);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1040 if (!oenc)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1041 return -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1042 enc = _PyUnicode_AsString(oenc);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1043 if (enc == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1044 return -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1045 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1046 v = PySys_GetObject("ps1");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1047 if (v != NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1048 v = PyObject_Str(v);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1049 if (v == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1050 PyErr_Clear();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1051 else if (PyUnicode_Check(v)) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1052 ps1 = _PyUnicode_AsString(v);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1053 if (ps1 == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1054 PyErr_Clear();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1055 ps1 = "";
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1056 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1057 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1058 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1059 w = PySys_GetObject("ps2");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1060 if (w != NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1061 w = PyObject_Str(w);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1062 if (w == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1063 PyErr_Clear();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1064 else if (PyUnicode_Check(w)) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1065 ps2 = _PyUnicode_AsString(w);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1066 if (ps2 == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1067 PyErr_Clear();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1068 ps2 = "";
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1069 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1070 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1071 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1072 arena = PyArena_New();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1073 if (arena == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1074 Py_XDECREF(v);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1075 Py_XDECREF(w);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1076 Py_XDECREF(oenc);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1077 return -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1078 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1079 mod = PyParser_ASTFromFile(fp, filename, enc,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1080 Py_single_input, ps1, ps2,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1081 flags, &errcode, arena);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1082 Py_XDECREF(v);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1083 Py_XDECREF(w);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1084 Py_XDECREF(oenc);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1085 if (mod == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1086 PyArena_Free(arena);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1087 if (errcode == E_EOF) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1088 PyErr_Clear();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1089 return E_EOF;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1090 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1091 PyErr_Print();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1092 return -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1093 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1094 m = PyImport_AddModule("__main__");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1095 if (m == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1096 PyArena_Free(arena);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1097 return -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1098 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1099 d = PyModule_GetDict(m);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1100 v = run_mod(mod, filename, d, d, flags, arena);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1101 PyArena_Free(arena);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1102 flush_io();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1103 if (v == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1104 PyErr_Print();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1105 return -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1106 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1107 Py_DECREF(v);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1108 return 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1109 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1110
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1111 /* Check whether a file maybe a pyc file: Look at the extension,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1112 the file type, and, if we may close it, at the first few bytes. */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1113
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1114 static int
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1115 maybe_pyc_file(FILE *fp, const char* filename, const char* ext, int closeit)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1116 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1117 if (strcmp(ext, ".pyc") == 0 || strcmp(ext, ".pyo") == 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1118 return 1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1119
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1120 /* Only look into the file if we are allowed to close it, since
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1121 it then should also be seekable. */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1122 if (closeit) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1123 /* Read only two bytes of the magic. If the file was opened in
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1124 text mode, the bytes 3 and 4 of the magic (\r\n) might not
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1125 be read as they are on disk. */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1126 unsigned int halfmagic = PyImport_GetMagicNumber() & 0xFFFF;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1127 unsigned char buf[2];
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1128 /* Mess: In case of -x, the stream is NOT at its start now,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1129 and ungetc() was used to push back the first newline,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1130 which makes the current stream position formally undefined,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1131 and a x-platform nightmare.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1132 Unfortunately, we have no direct way to know whether -x
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1133 was specified. So we use a terrible hack: if the current
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1134 stream position is not 0, we assume -x was specified, and
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1135 give up. Bug 132850 on SourceForge spells out the
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1136 hopelessness of trying anything else (fseek and ftell
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1137 don't work predictably x-platform for text-mode files).
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1138 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1139 int ispyc = 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1140 if (ftell(fp) == 0) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1141 if (fread(buf, 1, 2, fp) == 2 &&
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1142 ((unsigned int)buf[1]<<8 | buf[0]) == halfmagic)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1143 ispyc = 1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1144 rewind(fp);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1145 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1146 return ispyc;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1147 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1148 return 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1149 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1150
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1151 int
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1152 PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1153 PyCompilerFlags *flags)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1154 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1155 PyObject *m, *d, *v;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1156 const char *ext;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1157 int set_file_name = 0, ret;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1158 size_t len;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1159
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1160 m = PyImport_AddModule("__main__");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1161 if (m == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1162 return -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1163 d = PyModule_GetDict(m);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1164 if (PyDict_GetItemString(d, "__file__") == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1165 PyObject *f;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1166 f = PyUnicode_DecodeFSDefault(filename);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1167 if (f == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1168 return -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1169 if (PyDict_SetItemString(d, "__file__", f) < 0) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1170 Py_DECREF(f);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1171 return -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1172 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1173 if (PyDict_SetItemString(d, "__cached__", Py_None) < 0) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1174 Py_DECREF(f);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1175 return -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1176 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1177 set_file_name = 1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1178 Py_DECREF(f);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1179 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1180 len = strlen(filename);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1181 ext = filename + len - (len > 4 ? 4 : 0);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1182 if (maybe_pyc_file(fp, filename, ext, closeit)) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1183 /* Try to run a pyc file. First, re-open in binary */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1184 if (closeit)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1185 fclose(fp);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1186 if ((fp = fopen(filename, "rb")) == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1187 fprintf(stderr, "python: Can't reopen .pyc file\n");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1188 ret = -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1189 goto done;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1190 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1191 /* Turn on optimization if a .pyo file is given */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1192 if (strcmp(ext, ".pyo") == 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1193 Py_OptimizeFlag = 1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1194 v = run_pyc_file(fp, filename, d, d, flags);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1195 } else {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1196 v = PyRun_FileExFlags(fp, filename, Py_file_input, d, d,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1197 closeit, flags);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1198 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1199 flush_io();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1200 if (v == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1201 PyErr_Print();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1202 ret = -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1203 goto done;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1204 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1205 Py_DECREF(v);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1206 ret = 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1207 done:
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1208 if (set_file_name && PyDict_DelItemString(d, "__file__"))
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1209 PyErr_Clear();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1210 return ret;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1211 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1212
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1213 int
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1214 PyRun_SimpleStringFlags(const char *command, PyCompilerFlags *flags)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1215 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1216 PyObject *m, *d, *v;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1217 m = PyImport_AddModule("__main__");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1218 if (m == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1219 return -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1220 d = PyModule_GetDict(m);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1221 v = PyRun_StringFlags(command, Py_file_input, d, d, flags);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1222 if (v == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1223 PyErr_Print();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1224 return -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1225 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1226 Py_DECREF(v);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1227 return 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1228 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1229
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1230 static int
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1231 parse_syntax_error(PyObject *err, PyObject **message, const char **filename,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1232 int *lineno, int *offset, const char **text)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1233 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1234 long hold;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1235 PyObject *v;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1236 _Py_IDENTIFIER(msg);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1237 _Py_IDENTIFIER(filename);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1238 _Py_IDENTIFIER(lineno);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1239 _Py_IDENTIFIER(offset);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1240 _Py_IDENTIFIER(text);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1241
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1242 /* old style errors */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1243 if (PyTuple_Check(err))
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1244 return PyArg_ParseTuple(err, "O(ziiz)", message, filename,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1245 lineno, offset, text);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1246
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1247 /* new style errors. `err' is an instance */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1248
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1249 if (! (v = _PyObject_GetAttrId(err, &PyId_msg)))
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1250 goto finally;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1251 *message = v;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1252
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1253 if (!(v = _PyObject_GetAttrId(err, &PyId_filename)))
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1254 goto finally;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1255 if (v == Py_None)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1256 *filename = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1257 else if (! (*filename = _PyUnicode_AsString(v)))
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1258 goto finally;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1259
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1260 Py_DECREF(v);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1261 if (!(v = _PyObject_GetAttrId(err, &PyId_lineno)))
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1262 goto finally;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1263 hold = PyLong_AsLong(v);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1264 Py_DECREF(v);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1265 v = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1266 if (hold < 0 && PyErr_Occurred())
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1267 goto finally;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1268 *lineno = (int)hold;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1269
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1270 if (!(v = _PyObject_GetAttrId(err, &PyId_offset)))
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1271 goto finally;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1272 if (v == Py_None) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1273 *offset = -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1274 Py_DECREF(v);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1275 v = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1276 } else {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1277 hold = PyLong_AsLong(v);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1278 Py_DECREF(v);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1279 v = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1280 if (hold < 0 && PyErr_Occurred())
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1281 goto finally;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1282 *offset = (int)hold;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1283 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1284
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1285 if (!(v = _PyObject_GetAttrId(err, &PyId_text)))
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1286 goto finally;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1287 if (v == Py_None)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1288 *text = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1289 else if (!PyUnicode_Check(v) ||
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1290 !(*text = _PyUnicode_AsString(v)))
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1291 goto finally;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1292 Py_DECREF(v);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1293 return 1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1294
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1295 finally:
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1296 Py_XDECREF(v);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1297 return 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1298 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1299
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1300 void
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1301 PyErr_Print(void)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1302 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1303 PyErr_PrintEx(1);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1304 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1305
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1306 static void
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1307 print_error_text(PyObject *f, int offset, const char *text)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1308 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1309 char *nl;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1310 if (offset >= 0) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1311 if (offset > 0 && offset == strlen(text) && text[offset - 1] == '\n')
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1312 offset--;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1313 for (;;) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1314 nl = strchr(text, '\n');
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1315 if (nl == NULL || nl-text >= offset)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1316 break;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1317 offset -= (int)(nl+1-text);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1318 text = nl+1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1319 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1320 while (*text == ' ' || *text == '\t') {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1321 text++;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1322 offset--;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1323 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1324 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1325 PyFile_WriteString(" ", f);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1326 PyFile_WriteString(text, f);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1327 if (*text == '\0' || text[strlen(text)-1] != '\n')
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1328 PyFile_WriteString("\n", f);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1329 if (offset == -1)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1330 return;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1331 PyFile_WriteString(" ", f);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1332 while (--offset > 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1333 PyFile_WriteString(" ", f);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1334 PyFile_WriteString("^\n", f);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1335 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1336
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1337 static void
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1338 handle_system_exit(void)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1339 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1340 PyObject *exception, *value, *tb;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1341 int exitcode = 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1342
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1343 if (Py_InspectFlag)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1344 /* Don't exit if -i flag was given. This flag is set to 0
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1345 * when entering interactive mode for inspecting. */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1346 return;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1347
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1348 PyErr_Fetch(&exception, &value, &tb);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1349 fflush(stdout);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1350 if (value == NULL || value == Py_None)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1351 goto done;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1352 if (PyExceptionInstance_Check(value)) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1353 /* The error code should be in the `code' attribute. */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1354 _Py_IDENTIFIER(code);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1355 PyObject *code = _PyObject_GetAttrId(value, &PyId_code);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1356 if (code) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1357 Py_DECREF(value);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1358 value = code;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1359 if (value == Py_None)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1360 goto done;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1361 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1362 /* If we failed to dig out the 'code' attribute,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1363 just let the else clause below print the error. */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1364 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1365 if (PyLong_Check(value))
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1366 exitcode = (int)PyLong_AsLong(value);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1367 else {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1368 PyObject *sys_stderr = PySys_GetObject("stderr");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1369 if (sys_stderr != NULL && sys_stderr != Py_None) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1370 PyFile_WriteObject(value, sys_stderr, Py_PRINT_RAW);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1371 } else {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1372 PyObject_Print(value, stderr, Py_PRINT_RAW);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1373 fflush(stderr);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1374 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1375 PySys_WriteStderr("\n");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1376 exitcode = 1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1377 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1378 done:
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1379 /* Restore and clear the exception info, in order to properly decref
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1380 * the exception, value, and traceback. If we just exit instead,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1381 * these leak, which confuses PYTHONDUMPREFS output, and may prevent
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1382 * some finalizers from running.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1383 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1384 PyErr_Restore(exception, value, tb);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1385 PyErr_Clear();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1386 Py_Exit(exitcode);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1387 /* NOTREACHED */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1388 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1389
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1390 void
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1391 PyErr_PrintEx(int set_sys_last_vars)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1392 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1393 PyObject *exception, *v, *tb, *hook;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1394
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1395 if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1396 handle_system_exit();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1397 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1398 PyErr_Fetch(&exception, &v, &tb);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1399 if (exception == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1400 return;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1401 PyErr_NormalizeException(&exception, &v, &tb);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1402 if (tb == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1403 tb = Py_None;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1404 Py_INCREF(tb);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1405 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1406 PyException_SetTraceback(v, tb);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1407 if (exception == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1408 return;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1409 /* Now we know v != NULL too */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1410 if (set_sys_last_vars) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1411 PySys_SetObject("last_type", exception);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1412 PySys_SetObject("last_value", v);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1413 PySys_SetObject("last_traceback", tb);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1414 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1415 hook = PySys_GetObject("excepthook");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1416 if (hook) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1417 PyObject *args = PyTuple_Pack(3, exception, v, tb);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1418 PyObject *result = PyEval_CallObject(hook, args);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1419 if (result == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1420 PyObject *exception2, *v2, *tb2;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1421 if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1422 handle_system_exit();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1423 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1424 PyErr_Fetch(&exception2, &v2, &tb2);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1425 PyErr_NormalizeException(&exception2, &v2, &tb2);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1426 /* It should not be possible for exception2 or v2
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1427 to be NULL. However PyErr_Display() can't
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1428 tolerate NULLs, so just be safe. */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1429 if (exception2 == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1430 exception2 = Py_None;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1431 Py_INCREF(exception2);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1432 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1433 if (v2 == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1434 v2 = Py_None;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1435 Py_INCREF(v2);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1436 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1437 fflush(stdout);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1438 PySys_WriteStderr("Error in sys.excepthook:\n");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1439 PyErr_Display(exception2, v2, tb2);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1440 PySys_WriteStderr("\nOriginal exception was:\n");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1441 PyErr_Display(exception, v, tb);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1442 Py_DECREF(exception2);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1443 Py_DECREF(v2);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1444 Py_XDECREF(tb2);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1445 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1446 Py_XDECREF(result);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1447 Py_XDECREF(args);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1448 } else {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1449 PySys_WriteStderr("sys.excepthook is missing\n");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1450 PyErr_Display(exception, v, tb);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1451 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1452 Py_XDECREF(exception);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1453 Py_XDECREF(v);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1454 Py_XDECREF(tb);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1455 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1456
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1457 static void
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1458 print_exception(PyObject *f, PyObject *value)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1459 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1460 int err = 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1461 PyObject *type, *tb;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1462 _Py_IDENTIFIER(print_file_and_line);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1463
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1464 if (!PyExceptionInstance_Check(value)) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1465 PyFile_WriteString("TypeError: print_exception(): Exception expected for value, ", f);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1466 PyFile_WriteString(Py_TYPE(value)->tp_name, f);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1467 PyFile_WriteString(" found\n", f);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1468 return;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1469 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1470
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1471 Py_INCREF(value);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1472 fflush(stdout);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1473 type = (PyObject *) Py_TYPE(value);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1474 tb = PyException_GetTraceback(value);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1475 if (tb && tb != Py_None)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1476 err = PyTraceBack_Print(tb, f);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1477 if (err == 0 &&
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1478 _PyObject_HasAttrId(value, &PyId_print_file_and_line))
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1479 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1480 PyObject *message;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1481 const char *filename, *text;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1482 int lineno, offset;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1483 if (!parse_syntax_error(value, &message, &filename,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1484 &lineno, &offset, &text))
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1485 PyErr_Clear();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1486 else {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1487 char buf[10];
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1488 PyFile_WriteString(" File \"", f);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1489 if (filename == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1490 PyFile_WriteString("<string>", f);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1491 else
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1492 PyFile_WriteString(filename, f);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1493 PyFile_WriteString("\", line ", f);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1494 PyOS_snprintf(buf, sizeof(buf), "%d", lineno);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1495 PyFile_WriteString(buf, f);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1496 PyFile_WriteString("\n", f);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1497 if (text != NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1498 print_error_text(f, offset, text);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1499 Py_DECREF(value);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1500 value = message;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1501 /* Can't be bothered to check all those
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1502 PyFile_WriteString() calls */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1503 if (PyErr_Occurred())
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1504 err = -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1505 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1506 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1507 if (err) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1508 /* Don't do anything else */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1509 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1510 else {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1511 PyObject* moduleName;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1512 char* className;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1513 _Py_IDENTIFIER(__module__);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1514 assert(PyExceptionClass_Check(type));
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1515 className = PyExceptionClass_Name(type);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1516 if (className != NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1517 char *dot = strrchr(className, '.');
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1518 if (dot != NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1519 className = dot+1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1520 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1521
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1522 moduleName = _PyObject_GetAttrId(type, &PyId___module__);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1523 if (moduleName == NULL || !PyUnicode_Check(moduleName))
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1524 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1525 Py_XDECREF(moduleName);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1526 err = PyFile_WriteString("<unknown>", f);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1527 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1528 else {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1529 char* modstr = _PyUnicode_AsString(moduleName);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1530 if (modstr && strcmp(modstr, "builtins"))
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1531 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1532 err = PyFile_WriteString(modstr, f);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1533 err += PyFile_WriteString(".", f);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1534 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1535 Py_DECREF(moduleName);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1536 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1537 if (err == 0) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1538 if (className == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1539 err = PyFile_WriteString("<unknown>", f);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1540 else
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1541 err = PyFile_WriteString(className, f);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1542 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1543 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1544 if (err == 0 && (value != Py_None)) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1545 PyObject *s = PyObject_Str(value);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1546 /* only print colon if the str() of the
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1547 object is not the empty string
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1548 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1549 if (s == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1550 err = -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1551 else if (!PyUnicode_Check(s) ||
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1552 PyUnicode_GetLength(s) != 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1553 err = PyFile_WriteString(": ", f);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1554 if (err == 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1555 err = PyFile_WriteObject(s, f, Py_PRINT_RAW);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1556 Py_XDECREF(s);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1557 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1558 /* try to write a newline in any case */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1559 err += PyFile_WriteString("\n", f);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1560 Py_XDECREF(tb);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1561 Py_DECREF(value);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1562 /* If an error happened here, don't show it.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1563 XXX This is wrong, but too many callers rely on this behavior. */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1564 if (err != 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1565 PyErr_Clear();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1566 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1567
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1568 static const char *cause_message =
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1569 "\nThe above exception was the direct cause "
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1570 "of the following exception:\n\n";
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1571
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1572 static const char *context_message =
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1573 "\nDuring handling of the above exception, "
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1574 "another exception occurred:\n\n";
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1575
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1576 static void
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1577 print_exception_recursive(PyObject *f, PyObject *value, PyObject *seen)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1578 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1579 int err = 0, res;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1580 PyObject *cause, *context;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1581
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1582 if (seen != NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1583 /* Exception chaining */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1584 if (PySet_Add(seen, value) == -1)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1585 PyErr_Clear();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1586 else if (PyExceptionInstance_Check(value)) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1587 cause = PyException_GetCause(value);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1588 context = PyException_GetContext(value);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1589 if (cause) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1590 res = PySet_Contains(seen, cause);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1591 if (res == -1)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1592 PyErr_Clear();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1593 if (res == 0) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1594 print_exception_recursive(
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1595 f, cause, seen);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1596 err |= PyFile_WriteString(
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1597 cause_message, f);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1598 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1599 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1600 else if (context) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1601 res = PySet_Contains(seen, context);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1602 if (res == -1)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1603 PyErr_Clear();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1604 if (res == 0) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1605 print_exception_recursive(
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1606 f, context, seen);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1607 err |= PyFile_WriteString(
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1608 context_message, f);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1609 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1610 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1611 Py_XDECREF(context);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1612 Py_XDECREF(cause);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1613 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1614 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1615 print_exception(f, value);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1616 if (err != 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1617 PyErr_Clear();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1618 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1619
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1620 void
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1621 PyErr_Display(PyObject *exception, PyObject *value, PyObject *tb)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1622 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1623 PyObject *seen;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1624 PyObject *f = PySys_GetObject("stderr");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1625 if (f == Py_None) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1626 /* pass */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1627 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1628 else if (f == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1629 _PyObject_Dump(value);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1630 fprintf(stderr, "lost sys.stderr\n");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1631 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1632 else {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1633 /* We choose to ignore seen being possibly NULL, and report
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1634 at least the main exception (it could be a MemoryError).
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1635 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1636 seen = PySet_New(NULL);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1637 if (seen == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1638 PyErr_Clear();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1639 print_exception_recursive(f, value, seen);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1640 Py_XDECREF(seen);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1641 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1642 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1643
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1644 PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1645 PyRun_StringFlags(const char *str, int start, PyObject *globals,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1646 PyObject *locals, PyCompilerFlags *flags)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1647 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1648 PyObject *ret = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1649 mod_ty mod;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1650 PyArena *arena = PyArena_New();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1651 if (arena == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1652 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1653
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1654 mod = PyParser_ASTFromString(str, "<string>", start, flags, arena);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1655 if (mod != NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1656 ret = run_mod(mod, "<string>", globals, locals, flags, arena);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1657 PyArena_Free(arena);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1658 return ret;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1659 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1660
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1661 PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1662 PyRun_FileExFlags(FILE *fp, const char *filename, int start, PyObject *globals,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1663 PyObject *locals, int closeit, PyCompilerFlags *flags)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1664 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1665 PyObject *ret;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1666 mod_ty mod;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1667 PyArena *arena = PyArena_New();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1668 if (arena == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1669 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1670
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1671 mod = PyParser_ASTFromFile(fp, filename, NULL, start, 0, 0,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1672 flags, NULL, arena);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1673 if (closeit)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1674 fclose(fp);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1675 if (mod == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1676 PyArena_Free(arena);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1677 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1678 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1679 ret = run_mod(mod, filename, globals, locals, flags, arena);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1680 PyArena_Free(arena);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1681 return ret;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1682 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1683
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1684 static void
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1685 flush_io(void)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1686 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1687 PyObject *f, *r;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1688 PyObject *type, *value, *traceback;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1689 _Py_IDENTIFIER(flush);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1690
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1691 /* Save the current exception */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1692 PyErr_Fetch(&type, &value, &traceback);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1693
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1694 f = PySys_GetObject("stderr");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1695 if (f != NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1696 r = _PyObject_CallMethodId(f, &PyId_flush, "");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1697 if (r)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1698 Py_DECREF(r);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1699 else
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1700 PyErr_Clear();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1701 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1702 f = PySys_GetObject("stdout");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1703 if (f != NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1704 r = _PyObject_CallMethodId(f, &PyId_flush, "");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1705 if (r)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1706 Py_DECREF(r);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1707 else
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1708 PyErr_Clear();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1709 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1710
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1711 PyErr_Restore(type, value, traceback);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1712 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1713
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1714 static PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1715 run_mod(mod_ty mod, const char *filename, PyObject *globals, PyObject *locals,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1716 PyCompilerFlags *flags, PyArena *arena)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1717 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1718 PyCodeObject *co;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1719 PyObject *v;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1720 co = PyAST_Compile(mod, filename, flags, arena);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1721 if (co == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1722 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1723 v = PyEval_EvalCode((PyObject*)co, globals, locals);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1724 Py_DECREF(co);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1725 return v;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1726 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1727
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1728 static PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1729 run_pyc_file(FILE *fp, const char *filename, PyObject *globals,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1730 PyObject *locals, PyCompilerFlags *flags)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1731 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1732 PyCodeObject *co;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1733 PyObject *v;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1734 long magic;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1735 long PyImport_GetMagicNumber(void);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1736
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1737 magic = PyMarshal_ReadLongFromFile(fp);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1738 if (magic != PyImport_GetMagicNumber()) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1739 PyErr_SetString(PyExc_RuntimeError,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1740 "Bad magic number in .pyc file");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1741 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1742 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1743 (void) PyMarshal_ReadLongFromFile(fp);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1744 v = PyMarshal_ReadLastObjectFromFile(fp);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1745 fclose(fp);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1746 if (v == NULL || !PyCode_Check(v)) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1747 Py_XDECREF(v);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1748 PyErr_SetString(PyExc_RuntimeError,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1749 "Bad code object in .pyc file");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1750 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1751 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1752 co = (PyCodeObject *)v;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1753 v = PyEval_EvalCode((PyObject*)co, globals, locals);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1754 if (v && flags)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1755 flags->cf_flags |= (co->co_flags & PyCF_MASK);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1756 Py_DECREF(co);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1757 return v;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1758 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1759
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1760 PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1761 Py_CompileStringExFlags(const char *str, const char *filename, int start,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1762 PyCompilerFlags *flags, int optimize)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1763 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1764 PyCodeObject *co;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1765 mod_ty mod;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1766 PyArena *arena = PyArena_New();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1767 if (arena == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1768 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1769
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1770 mod = PyParser_ASTFromString(str, filename, start, flags, arena);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1771 if (mod == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1772 PyArena_Free(arena);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1773 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1774 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1775 if (flags && (flags->cf_flags & PyCF_ONLY_AST)) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1776 PyObject *result = PyAST_mod2obj(mod);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1777 PyArena_Free(arena);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1778 return result;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1779 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1780 co = PyAST_CompileEx(mod, filename, flags, optimize, arena);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1781 PyArena_Free(arena);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1782 return (PyObject *)co;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1783 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1784
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1785 /* For use in Py_LIMITED_API */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1786 #undef Py_CompileString
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1787 PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1788 PyCompileString(const char *str, const char *filename, int start)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1789 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1790 return Py_CompileStringFlags(str, filename, start, NULL);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1791 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1792
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1793 struct symtable *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1794 Py_SymtableString(const char *str, const char *filename, int start)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1795 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1796 struct symtable *st;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1797 mod_ty mod;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1798 PyCompilerFlags flags;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1799 PyArena *arena = PyArena_New();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1800 if (arena == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1801 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1802
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1803 flags.cf_flags = 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1804 mod = PyParser_ASTFromString(str, filename, start, &flags, arena);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1805 if (mod == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1806 PyArena_Free(arena);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1807 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1808 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1809 st = PySymtable_Build(mod, filename, 0);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1810 PyArena_Free(arena);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1811 return st;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1812 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1813
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1814 /* Preferred access to parser is through AST. */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1815 mod_ty
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1816 PyParser_ASTFromString(const char *s, const char *filename, int start,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1817 PyCompilerFlags *flags, PyArena *arena)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1818 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1819 mod_ty mod;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1820 PyCompilerFlags localflags;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1821 perrdetail err;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1822 int iflags = PARSER_FLAGS(flags);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1823
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1824 node *n = PyParser_ParseStringFlagsFilenameEx(s, filename,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1825 &_PyParser_Grammar, start, &err,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1826 &iflags);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1827 if (flags == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1828 localflags.cf_flags = 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1829 flags = &localflags;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1830 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1831 if (n) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1832 flags->cf_flags |= iflags & PyCF_MASK;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1833 mod = PyAST_FromNode(n, flags, filename, arena);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1834 PyNode_Free(n);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1835 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1836 else {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1837 err_input(&err);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1838 mod = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1839 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1840 err_free(&err);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1841 return mod;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1842 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1843
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1844 mod_ty
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1845 PyParser_ASTFromFile(FILE *fp, const char *filename, const char* enc,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1846 int start, char *ps1,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1847 char *ps2, PyCompilerFlags *flags, int *errcode,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1848 PyArena *arena)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1849 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1850 mod_ty mod;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1851 PyCompilerFlags localflags;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1852 perrdetail err;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1853 int iflags = PARSER_FLAGS(flags);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1854
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1855 node *n = PyParser_ParseFileFlagsEx(fp, filename, enc,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1856 &_PyParser_Grammar,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1857 start, ps1, ps2, &err, &iflags);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1858 if (flags == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1859 localflags.cf_flags = 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1860 flags = &localflags;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1861 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1862 if (n) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1863 flags->cf_flags |= iflags & PyCF_MASK;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1864 mod = PyAST_FromNode(n, flags, filename, arena);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1865 PyNode_Free(n);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1866 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1867 else {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1868 err_input(&err);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1869 if (errcode)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1870 *errcode = err.error;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1871 mod = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1872 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1873 err_free(&err);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1874 return mod;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1875 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1876
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1877 /* Simplified interface to parsefile -- return node or set exception */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1878
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1879 node *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1880 PyParser_SimpleParseFileFlags(FILE *fp, const char *filename, int start, int flags)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1881 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1882 perrdetail err;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1883 node *n = PyParser_ParseFileFlags(fp, filename, NULL,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1884 &_PyParser_Grammar,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1885 start, NULL, NULL, &err, flags);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1886 if (n == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1887 err_input(&err);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1888 err_free(&err);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1889
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1890 return n;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1891 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1892
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1893 /* Simplified interface to parsestring -- return node or set exception */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1894
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1895 node *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1896 PyParser_SimpleParseStringFlags(const char *str, int start, int flags)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1897 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1898 perrdetail err;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1899 node *n = PyParser_ParseStringFlags(str, &_PyParser_Grammar,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1900 start, &err, flags);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1901 if (n == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1902 err_input(&err);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1903 err_free(&err);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1904 return n;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1905 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1906
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1907 node *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1908 PyParser_SimpleParseStringFlagsFilename(const char *str, const char *filename,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1909 int start, int flags)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1910 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1911 perrdetail err;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1912 node *n = PyParser_ParseStringFlagsFilename(str, filename,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1913 &_PyParser_Grammar, start, &err, flags);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1914 if (n == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1915 err_input(&err);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1916 err_free(&err);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1917 return n;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1918 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1919
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1920 node *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1921 PyParser_SimpleParseStringFilename(const char *str, const char *filename, int start)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1922 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1923 return PyParser_SimpleParseStringFlagsFilename(str, filename, start, 0);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1924 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1925
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1926 /* May want to move a more generalized form of this to parsetok.c or
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1927 even parser modules. */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1928
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1929 void
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1930 PyParser_ClearError(perrdetail *err)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1931 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1932 err_free(err);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1933 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1934
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1935 void
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1936 PyParser_SetError(perrdetail *err)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1937 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1938 err_input(err);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1939 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1940
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1941 static void
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1942 err_free(perrdetail *err)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1943 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1944 Py_CLEAR(err->filename);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1945 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1946
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1947 /* Set the error appropriate to the given input error code (see errcode.h) */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1948
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1949 static void
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1950 err_input(perrdetail *err)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1951 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1952 PyObject *v, *w, *errtype, *errtext;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1953 PyObject *msg_obj = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1954 char *msg = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1955
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1956 errtype = PyExc_SyntaxError;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1957 switch (err->error) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1958 case E_ERROR:
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1959 return;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1960 case E_SYNTAX:
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1961 errtype = PyExc_IndentationError;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1962 if (err->expected == INDENT)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1963 msg = "expected an indented block";
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1964 else if (err->token == INDENT)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1965 msg = "unexpected indent";
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1966 else if (err->token == DEDENT)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1967 msg = "unexpected unindent";
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1968 else {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1969 errtype = PyExc_SyntaxError;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1970 msg = "invalid syntax";
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1971 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1972 break;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1973 case E_TOKEN:
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1974 msg = "invalid token";
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1975 break;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1976 case E_EOFS:
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1977 msg = "EOF while scanning triple-quoted string literal";
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1978 break;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1979 case E_EOLS:
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1980 msg = "EOL while scanning string literal";
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1981 break;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1982 case E_INTR:
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1983 if (!PyErr_Occurred())
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1984 PyErr_SetNone(PyExc_KeyboardInterrupt);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1985 goto cleanup;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1986 case E_NOMEM:
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1987 PyErr_NoMemory();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1988 goto cleanup;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1989 case E_EOF:
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1990 msg = "unexpected EOF while parsing";
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1991 break;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1992 case E_TABSPACE:
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1993 errtype = PyExc_TabError;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1994 msg = "inconsistent use of tabs and spaces in indentation";
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1995 break;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1996 case E_OVERFLOW:
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1997 msg = "expression too long";
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1998 break;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1999 case E_DEDENT:
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2000 errtype = PyExc_IndentationError;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2001 msg = "unindent does not match any outer indentation level";
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2002 break;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2003 case E_TOODEEP:
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2004 errtype = PyExc_IndentationError;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2005 msg = "too many levels of indentation";
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2006 break;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2007 case E_DECODE: {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2008 PyObject *type, *value, *tb;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2009 PyErr_Fetch(&type, &value, &tb);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2010 msg = "unknown decode error";
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2011 if (value != NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2012 msg_obj = PyObject_Str(value);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2013 Py_XDECREF(type);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2014 Py_XDECREF(value);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2015 Py_XDECREF(tb);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2016 break;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2017 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2018 case E_LINECONT:
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2019 msg = "unexpected character after line continuation character";
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2020 break;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2021
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2022 case E_IDENTIFIER:
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2023 msg = "invalid character in identifier";
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2024 break;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2025 default:
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2026 fprintf(stderr, "error=%d\n", err->error);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2027 msg = "unknown parsing error";
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2028 break;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2029 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2030 /* err->text may not be UTF-8 in case of decoding errors.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2031 Explicitly convert to an object. */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2032 if (!err->text) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2033 errtext = Py_None;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2034 Py_INCREF(Py_None);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2035 } else {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2036 errtext = PyUnicode_DecodeUTF8(err->text, strlen(err->text),
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2037 "replace");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2038 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2039 v = Py_BuildValue("(OiiN)", err->filename,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2040 err->lineno, err->offset, errtext);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2041 if (v != NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2042 if (msg_obj)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2043 w = Py_BuildValue("(OO)", msg_obj, v);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2044 else
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2045 w = Py_BuildValue("(sO)", msg, v);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2046 } else
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2047 w = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2048 Py_XDECREF(v);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2049 PyErr_SetObject(errtype, w);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2050 Py_XDECREF(w);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2051 cleanup:
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2052 Py_XDECREF(msg_obj);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2053 if (err->text != NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2054 PyObject_FREE(err->text);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2055 err->text = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2056 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2057 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2058
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2059 /* Print fatal error message and abort */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2060
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2061 void
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2062 Py_FatalError(const char *msg)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2063 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2064 const int fd = fileno(stderr);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2065 PyThreadState *tstate;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2066
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2067 fprintf(stderr, "Fatal Python error: %s\n", msg);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2068 fflush(stderr); /* it helps in Windows debug build */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2069 if (PyErr_Occurred()) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2070 PyErr_PrintEx(0);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2071 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2072 else {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2073 tstate = _Py_atomic_load_relaxed(&_PyThreadState_Current);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2074 if (tstate != NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2075 fputc('\n', stderr);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2076 fflush(stderr);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2077 _Py_DumpTracebackThreads(fd, tstate->interp, tstate);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2078 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2079 _PyFaulthandler_Fini();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2080 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2081
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2082 abort();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2083 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2084
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2085 /* Clean up and exit */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2086
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2087 #ifdef WITH_THREAD
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2088 #include "pythread.h"
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2089 #endif
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2090
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2091 static void (*pyexitfunc)(void) = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2092 /* For the atexit module. */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2093 void _Py_PyAtExit(void (*func)(void))
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2094 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2095 pyexitfunc = func;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2096 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2097
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2098 static void
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2099 call_py_exitfuncs(void)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2100 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2101 if (pyexitfunc == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2102 return;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2103
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2104 (*pyexitfunc)();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2105 PyErr_Clear();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2106 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2107
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2108 /* Wait until threading._shutdown completes, provided
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2109 the threading module was imported in the first place.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2110 The shutdown routine will wait until all non-daemon
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2111 "threading" threads have completed. */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2112 static void
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2113 wait_for_thread_shutdown(void)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2114 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2115 #ifdef WITH_THREAD
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2116 _Py_IDENTIFIER(_shutdown);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2117 PyObject *result;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2118 PyThreadState *tstate = PyThreadState_GET();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2119 PyObject *threading = PyMapping_GetItemString(tstate->interp->modules,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2120 "threading");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2121 if (threading == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2122 /* threading not imported */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2123 PyErr_Clear();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2124 return;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2125 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2126 result = _PyObject_CallMethodId(threading, &PyId__shutdown, "");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2127 if (result == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2128 PyErr_WriteUnraisable(threading);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2129 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2130 else {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2131 Py_DECREF(result);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2132 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2133 Py_DECREF(threading);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2134 #endif
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2135 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2136
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2137 #define NEXITFUNCS 32
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2138 static void (*exitfuncs[NEXITFUNCS])(void);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2139 static int nexitfuncs = 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2140
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2141 int Py_AtExit(void (*func)(void))
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2142 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2143 if (nexitfuncs >= NEXITFUNCS)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2144 return -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2145 exitfuncs[nexitfuncs++] = func;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2146 return 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2147 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2148
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2149 static void
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2150 call_ll_exitfuncs(void)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2151 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2152 while (nexitfuncs > 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2153 (*exitfuncs[--nexitfuncs])();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2154
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2155 fflush(stdout);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2156 fflush(stderr);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2157 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2158
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2159 void
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2160 Py_Exit(int sts)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2161 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2162 Py_Finalize();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2163
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2164 exit(sts);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2165 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2166
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2167 static void
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2168 initsigs(void)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2169 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2170 #ifdef SIGPIPE
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2171 PyOS_setsig(SIGPIPE, SIG_IGN);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2172 #endif
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2173 #ifdef SIGXFZ
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2174 PyOS_setsig(SIGXFZ, SIG_IGN);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2175 #endif
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2176 #ifdef SIGXFSZ
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2177 PyOS_setsig(SIGXFSZ, SIG_IGN);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2178 #endif
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2179 PyOS_InitInterrupts(); /* May imply initsignal() */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2180 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2181
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2182
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2183 /* Restore signals that the interpreter has called SIG_IGN on to SIG_DFL.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2184 *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2185 * All of the code in this function must only use async-signal-safe functions,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2186 * listed at `man 7 signal` or
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2187 * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2188 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2189 void
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2190 _Py_RestoreSignals(void)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2191 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2192 #ifdef SIGPIPE
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2193 PyOS_setsig(SIGPIPE, SIG_DFL);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2194 #endif
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2195 #ifdef SIGXFZ
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2196 PyOS_setsig(SIGXFZ, SIG_DFL);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2197 #endif
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2198 #ifdef SIGXFSZ
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2199 PyOS_setsig(SIGXFSZ, SIG_DFL);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2200 #endif
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2201 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2202
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2203
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2204 /*
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2205 * The file descriptor fd is considered ``interactive'' if either
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2206 * a) isatty(fd) is TRUE, or
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2207 * b) the -i flag was given, and the filename associated with
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2208 * the descriptor is NULL or "<stdin>" or "???".
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2209 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2210 int
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2211 Py_FdIsInteractive(FILE *fp, const char *filename)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2212 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2213 if (isatty((int)fileno(fp)))
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2214 return 1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2215 if (!Py_InteractiveFlag)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2216 return 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2217 return (filename == NULL) ||
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2218 (strcmp(filename, "<stdin>") == 0) ||
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2219 (strcmp(filename, "???") == 0);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2220 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2221
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2222 /* Wrappers around sigaction() or signal(). */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2223
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2224 PyOS_sighandler_t
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2225 PyOS_getsig(int sig)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2226 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2227 #ifdef HAVE_SIGACTION
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2228 struct sigaction context;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2229 if (sigaction(sig, NULL, &context) == -1)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2230 return SIG_ERR;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2231 return context.sa_handler;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2232 #else
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2233 PyOS_sighandler_t handler;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2234 handler = signal(sig, SIG_IGN);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2235 if (handler != SIG_ERR)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2236 signal(sig, handler);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2237 return handler;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2238 #endif
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2239 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2240
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2241 /*
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2242 * All of the code in this function must only use async-signal-safe functions,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2243 * listed at `man 7 signal` or
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2244 * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2245 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2246 PyOS_sighandler_t
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2247 PyOS_setsig(int sig, PyOS_sighandler_t handler)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2248 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2249 #ifdef HAVE_SIGACTION
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2250 /* Some code in Modules/signalmodule.c depends on sigaction() being
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2251 * used here if HAVE_SIGACTION is defined. Fix that if this code
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2252 * changes to invalidate that assumption.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2253 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2254 struct sigaction context, ocontext;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2255 context.sa_handler = handler;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2256 sigemptyset(&context.sa_mask);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2257 context.sa_flags = 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2258 if (sigaction(sig, &context, &ocontext) == -1)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2259 return SIG_ERR;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2260 return ocontext.sa_handler;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2261 #else
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2262 PyOS_sighandler_t oldhandler;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2263 oldhandler = signal(sig, handler);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2264 #ifdef HAVE_SIGINTERRUPT
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2265 siginterrupt(sig, 1);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2266 #endif
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2267 return oldhandler;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2268 #endif
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2269 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2270