comparison Python/pythonrun.c @ 61463:fd25030223b0 legacy-trunk

Untabify C files. Will watch buildbots.
author Antoine Pitrou <solipsis@pitrou.net>
date Sun, 09 May 2010 14:46:46 +0000
parents 435a8355dab6
children bd5f5df7ed78
comparison
equal deleted inserted replaced
61460:d698fb056a45 61463:fd25030223b0
38 #endif 38 #endif
39 39
40 #ifndef Py_REF_DEBUG 40 #ifndef Py_REF_DEBUG
41 #define PRINT_TOTAL_REFS() 41 #define PRINT_TOTAL_REFS()
42 #else /* Py_REF_DEBUG */ 42 #else /* Py_REF_DEBUG */
43 #define PRINT_TOTAL_REFS() fprintf(stderr, \ 43 #define PRINT_TOTAL_REFS() fprintf(stderr, \
44 "[%" PY_FORMAT_SIZE_T "d refs]\n", \ 44 "[%" PY_FORMAT_SIZE_T "d refs]\n", \
45 _Py_GetRefTotal()) 45 _Py_GetRefTotal())
46 #endif 46 #endif
47 47
48 #ifdef __cplusplus 48 #ifdef __cplusplus
49 extern "C" { 49 extern "C" {
50 #endif 50 #endif
55 55
56 /* Forward */ 56 /* Forward */
57 static void initmain(void); 57 static void initmain(void);
58 static void initsite(void); 58 static void initsite(void);
59 static PyObject *run_mod(mod_ty, const char *, PyObject *, PyObject *, 59 static PyObject *run_mod(mod_ty, const char *, PyObject *, PyObject *,
60 PyCompilerFlags *, PyArena *); 60 PyCompilerFlags *, PyArena *);
61 static PyObject *run_pyc_file(FILE *, const char *, PyObject *, PyObject *, 61 static PyObject *run_pyc_file(FILE *, const char *, PyObject *, PyObject *,
62 PyCompilerFlags *); 62 PyCompilerFlags *);
63 static void err_input(perrdetail *); 63 static void err_input(perrdetail *);
64 static void initsigs(void); 64 static void initsigs(void);
65 static void wait_for_thread_shutdown(void); 65 static void wait_for_thread_shutdown(void);
66 static void call_sys_exitfunc(void); 66 static void call_sys_exitfunc(void);
67 static void call_ll_exitfuncs(void); 67 static void call_ll_exitfuncs(void);
93 /* PyModule_GetWarningsModule is no longer necessary as of 2.6 93 /* PyModule_GetWarningsModule is no longer necessary as of 2.6
94 since _warnings is builtin. This API should not be used. */ 94 since _warnings is builtin. This API should not be used. */
95 PyObject * 95 PyObject *
96 PyModule_GetWarningsModule(void) 96 PyModule_GetWarningsModule(void)
97 { 97 {
98 return PyImport_ImportModule("warnings"); 98 return PyImport_ImportModule("warnings");
99 } 99 }
100 100
101 static int initialized = 0; 101 static int initialized = 0;
102 102
103 /* API to access the initialized flag -- useful for esoteric use */ 103 /* API to access the initialized flag -- useful for esoteric use */
104 104
105 int 105 int
106 Py_IsInitialized(void) 106 Py_IsInitialized(void)
107 { 107 {
108 return initialized; 108 return initialized;
109 } 109 }
110 110
111 /* Global initializations. Can be undone by Py_Finalize(). Don't 111 /* Global initializations. Can be undone by Py_Finalize(). Don't
112 call this twice without an intervening Py_Finalize() call. When 112 call this twice without an intervening Py_Finalize() call. When
113 initializations fail, a fatal error is issued and the function does 113 initializations fail, a fatal error is issued and the function does
121 */ 121 */
122 122
123 static int 123 static int
124 add_flag(int flag, const char *envs) 124 add_flag(int flag, const char *envs)
125 { 125 {
126 int env = atoi(envs); 126 int env = atoi(envs);
127 if (flag < env) 127 if (flag < env)
128 flag = env; 128 flag = env;
129 if (flag < 1) 129 if (flag < 1)
130 flag = 1; 130 flag = 1;
131 return flag; 131 return flag;
132 } 132 }
133 133
134 void 134 void
135 Py_InitializeEx(int install_sigs) 135 Py_InitializeEx(int install_sigs)
136 { 136 {
137 PyInterpreterState *interp; 137 PyInterpreterState *interp;
138 PyThreadState *tstate; 138 PyThreadState *tstate;
139 PyObject *bimod, *sysmod; 139 PyObject *bimod, *sysmod;
140 char *p; 140 char *p;
141 char *icodeset = NULL; /* On Windows, input codeset may theoretically 141 char *icodeset = NULL; /* On Windows, input codeset may theoretically
142 differ from output codeset. */ 142 differ from output codeset. */
143 char *codeset = NULL; 143 char *codeset = NULL;
144 char *errors = NULL; 144 char *errors = NULL;
145 int free_codeset = 0; 145 int free_codeset = 0;
146 int overridden = 0; 146 int overridden = 0;
147 PyObject *sys_stream, *sys_isatty; 147 PyObject *sys_stream, *sys_isatty;
148 #if defined(Py_USING_UNICODE) && defined(HAVE_LANGINFO_H) && defined(CODESET) 148 #if defined(Py_USING_UNICODE) && defined(HAVE_LANGINFO_H) && defined(CODESET)
149 char *saved_locale, *loc_codeset; 149 char *saved_locale, *loc_codeset;
150 #endif 150 #endif
151 #ifdef MS_WINDOWS 151 #ifdef MS_WINDOWS
152 char ibuf[128]; 152 char ibuf[128];
153 char buf[128]; 153 char buf[128];
154 #endif 154 #endif
155 extern void _Py_ReadyTypes(void); 155 extern void _Py_ReadyTypes(void);
156 156
157 if (initialized) 157 if (initialized)
158 return; 158 return;
159 initialized = 1; 159 initialized = 1;
160 160
161 if ((p = Py_GETENV("PYTHONDEBUG")) && *p != '\0') 161 if ((p = Py_GETENV("PYTHONDEBUG")) && *p != '\0')
162 Py_DebugFlag = add_flag(Py_DebugFlag, p); 162 Py_DebugFlag = add_flag(Py_DebugFlag, p);
163 if ((p = Py_GETENV("PYTHONVERBOSE")) && *p != '\0') 163 if ((p = Py_GETENV("PYTHONVERBOSE")) && *p != '\0')
164 Py_VerboseFlag = add_flag(Py_VerboseFlag, p); 164 Py_VerboseFlag = add_flag(Py_VerboseFlag, p);
165 if ((p = Py_GETENV("PYTHONOPTIMIZE")) && *p != '\0') 165 if ((p = Py_GETENV("PYTHONOPTIMIZE")) && *p != '\0')
166 Py_OptimizeFlag = add_flag(Py_OptimizeFlag, p); 166 Py_OptimizeFlag = add_flag(Py_OptimizeFlag, p);
167 if ((p = Py_GETENV("PYTHONDONTWRITEBYTECODE")) && *p != '\0') 167 if ((p = Py_GETENV("PYTHONDONTWRITEBYTECODE")) && *p != '\0')
168 Py_DontWriteBytecodeFlag = add_flag(Py_DontWriteBytecodeFlag, p); 168 Py_DontWriteBytecodeFlag = add_flag(Py_DontWriteBytecodeFlag, p);
169 169
170 interp = PyInterpreterState_New(); 170 interp = PyInterpreterState_New();
171 if (interp == NULL) 171 if (interp == NULL)
172 Py_FatalError("Py_Initialize: can't make first interpreter"); 172 Py_FatalError("Py_Initialize: can't make first interpreter");
173 173
174 tstate = PyThreadState_New(interp); 174 tstate = PyThreadState_New(interp);
175 if (tstate == NULL) 175 if (tstate == NULL)
176 Py_FatalError("Py_Initialize: can't make first thread"); 176 Py_FatalError("Py_Initialize: can't make first thread");
177 (void) PyThreadState_Swap(tstate); 177 (void) PyThreadState_Swap(tstate);
178 178
179 _Py_ReadyTypes(); 179 _Py_ReadyTypes();
180 180
181 if (!_PyFrame_Init()) 181 if (!_PyFrame_Init())
182 Py_FatalError("Py_Initialize: can't init frames"); 182 Py_FatalError("Py_Initialize: can't init frames");
183 183
184 if (!_PyInt_Init()) 184 if (!_PyInt_Init())
185 Py_FatalError("Py_Initialize: can't init ints"); 185 Py_FatalError("Py_Initialize: can't init ints");
186 186
187 if (!_PyLong_Init()) 187 if (!_PyLong_Init())
188 Py_FatalError("Py_Initialize: can't init longs"); 188 Py_FatalError("Py_Initialize: can't init longs");
189 189
190 if (!PyByteArray_Init()) 190 if (!PyByteArray_Init())
191 Py_FatalError("Py_Initialize: can't init bytearray"); 191 Py_FatalError("Py_Initialize: can't init bytearray");
192 192
193 _PyFloat_Init(); 193 _PyFloat_Init();
194 194
195 interp->modules = PyDict_New(); 195 interp->modules = PyDict_New();
196 if (interp->modules == NULL) 196 if (interp->modules == NULL)
197 Py_FatalError("Py_Initialize: can't make modules dictionary"); 197 Py_FatalError("Py_Initialize: can't make modules dictionary");
198 interp->modules_reloading = PyDict_New(); 198 interp->modules_reloading = PyDict_New();
199 if (interp->modules_reloading == NULL) 199 if (interp->modules_reloading == NULL)
200 Py_FatalError("Py_Initialize: can't make modules_reloading dictionary"); 200 Py_FatalError("Py_Initialize: can't make modules_reloading dictionary");
201 201
202 #ifdef Py_USING_UNICODE 202 #ifdef Py_USING_UNICODE
203 /* Init Unicode implementation; relies on the codec registry */ 203 /* Init Unicode implementation; relies on the codec registry */
204 _PyUnicode_Init(); 204 _PyUnicode_Init();
205 #endif 205 #endif
206 206
207 bimod = _PyBuiltin_Init(); 207 bimod = _PyBuiltin_Init();
208 if (bimod == NULL) 208 if (bimod == NULL)
209 Py_FatalError("Py_Initialize: can't initialize __builtin__"); 209 Py_FatalError("Py_Initialize: can't initialize __builtin__");
210 interp->builtins = PyModule_GetDict(bimod); 210 interp->builtins = PyModule_GetDict(bimod);
211 if (interp->builtins == NULL) 211 if (interp->builtins == NULL)
212 Py_FatalError("Py_Initialize: can't initialize builtins dict"); 212 Py_FatalError("Py_Initialize: can't initialize builtins dict");
213 Py_INCREF(interp->builtins); 213 Py_INCREF(interp->builtins);
214 214
215 sysmod = _PySys_Init(); 215 sysmod = _PySys_Init();
216 if (sysmod == NULL) 216 if (sysmod == NULL)
217 Py_FatalError("Py_Initialize: can't initialize sys"); 217 Py_FatalError("Py_Initialize: can't initialize sys");
218 interp->sysdict = PyModule_GetDict(sysmod); 218 interp->sysdict = PyModule_GetDict(sysmod);
219 if (interp->sysdict == NULL) 219 if (interp->sysdict == NULL)
220 Py_FatalError("Py_Initialize: can't initialize sys dict"); 220 Py_FatalError("Py_Initialize: can't initialize sys dict");
221 Py_INCREF(interp->sysdict); 221 Py_INCREF(interp->sysdict);
222 _PyImport_FixupExtension("sys", "sys"); 222 _PyImport_FixupExtension("sys", "sys");
223 PySys_SetPath(Py_GetPath()); 223 PySys_SetPath(Py_GetPath());
224 PyDict_SetItemString(interp->sysdict, "modules", 224 PyDict_SetItemString(interp->sysdict, "modules",
225 interp->modules); 225 interp->modules);
226 226
227 _PyImport_Init(); 227 _PyImport_Init();
228 228
229 /* initialize builtin exceptions */ 229 /* initialize builtin exceptions */
230 _PyExc_Init(); 230 _PyExc_Init();
231 _PyImport_FixupExtension("exceptions", "exceptions"); 231 _PyImport_FixupExtension("exceptions", "exceptions");
232 232
233 /* phase 2 of builtins */ 233 /* phase 2 of builtins */
234 _PyImport_FixupExtension("__builtin__", "__builtin__"); 234 _PyImport_FixupExtension("__builtin__", "__builtin__");
235 235
236 _PyImportHooks_Init(); 236 _PyImportHooks_Init();
237 237
238 if (install_sigs) 238 if (install_sigs)
239 initsigs(); /* Signal handling stuff, including initintr() */ 239 initsigs(); /* Signal handling stuff, including initintr() */
240 240
241 /* Initialize warnings. */ 241 /* Initialize warnings. */
242 _PyWarnings_Init(); 242 _PyWarnings_Init();
243 if (PySys_HasWarnOptions()) { 243 if (PySys_HasWarnOptions()) {
244 PyObject *warnings_module = PyImport_ImportModule("warnings"); 244 PyObject *warnings_module = PyImport_ImportModule("warnings");
245 if (!warnings_module) 245 if (!warnings_module)
246 PyErr_Clear(); 246 PyErr_Clear();
247 Py_XDECREF(warnings_module); 247 Py_XDECREF(warnings_module);
248 } 248 }
249 249
250 initmain(); /* Module __main__ */ 250 initmain(); /* Module __main__ */
251 251
252 /* auto-thread-state API, if available */ 252 /* auto-thread-state API, if available */
253 #ifdef WITH_THREAD 253 #ifdef WITH_THREAD
254 _PyGILState_Init(interp, tstate); 254 _PyGILState_Init(interp, tstate);
255 #endif /* WITH_THREAD */ 255 #endif /* WITH_THREAD */
256 256
257 if (!Py_NoSiteFlag) 257 if (!Py_NoSiteFlag)
258 initsite(); /* Module site */ 258 initsite(); /* Module site */
259 259
260 if ((p = Py_GETENV("PYTHONIOENCODING")) && *p != '\0') { 260 if ((p = Py_GETENV("PYTHONIOENCODING")) && *p != '\0') {
261 p = icodeset = codeset = strdup(p); 261 p = icodeset = codeset = strdup(p);
262 free_codeset = 1; 262 free_codeset = 1;
263 errors = strchr(p, ':'); 263 errors = strchr(p, ':');
264 if (errors) { 264 if (errors) {
265 *errors = '\0'; 265 *errors = '\0';
266 errors++; 266 errors++;
267 } 267 }
268 overridden = 1; 268 overridden = 1;
269 } 269 }
270 270
271 #if defined(Py_USING_UNICODE) && defined(HAVE_LANGINFO_H) && defined(CODESET) 271 #if defined(Py_USING_UNICODE) && defined(HAVE_LANGINFO_H) && defined(CODESET)
272 /* On Unix, set the file system encoding according to the 272 /* On Unix, set the file system encoding according to the
273 user's preference, if the CODESET names a well-known 273 user's preference, if the CODESET names a well-known
274 Python codec, and Py_FileSystemDefaultEncoding isn't 274 Python codec, and Py_FileSystemDefaultEncoding isn't
275 initialized by other means. Also set the encoding of 275 initialized by other means. Also set the encoding of
276 stdin and stdout if these are terminals, unless overridden. */ 276 stdin and stdout if these are terminals, unless overridden. */
277 277
278 if (!overridden || !Py_FileSystemDefaultEncoding) { 278 if (!overridden || !Py_FileSystemDefaultEncoding) {
279 saved_locale = strdup(setlocale(LC_CTYPE, NULL)); 279 saved_locale = strdup(setlocale(LC_CTYPE, NULL));
280 setlocale(LC_CTYPE, ""); 280 setlocale(LC_CTYPE, "");
281 loc_codeset = nl_langinfo(CODESET); 281 loc_codeset = nl_langinfo(CODESET);
282 if (loc_codeset && *loc_codeset) { 282 if (loc_codeset && *loc_codeset) {
283 PyObject *enc = PyCodec_Encoder(loc_codeset); 283 PyObject *enc = PyCodec_Encoder(loc_codeset);
284 if (enc) { 284 if (enc) {
285 loc_codeset = strdup(loc_codeset); 285 loc_codeset = strdup(loc_codeset);
286 Py_DECREF(enc); 286 Py_DECREF(enc);
287 } else { 287 } else {
288 if (PyErr_ExceptionMatches(PyExc_LookupError)) { 288 if (PyErr_ExceptionMatches(PyExc_LookupError)) {
289 PyErr_Clear(); 289 PyErr_Clear();
290 loc_codeset = NULL; 290 loc_codeset = NULL;
291 } else { 291 } else {
292 PyErr_Print(); 292 PyErr_Print();
293 exit(1); 293 exit(1);
294 } 294 }
295 } 295 }
296 } else 296 } else
297 loc_codeset = NULL; 297 loc_codeset = NULL;
298 setlocale(LC_CTYPE, saved_locale); 298 setlocale(LC_CTYPE, saved_locale);
299 free(saved_locale); 299 free(saved_locale);
300 300
301 if (!overridden) { 301 if (!overridden) {
302 codeset = icodeset = loc_codeset; 302 codeset = icodeset = loc_codeset;
303 free_codeset = 1; 303 free_codeset = 1;
304 } 304 }
305 305
306 /* Initialize Py_FileSystemDefaultEncoding from 306 /* Initialize Py_FileSystemDefaultEncoding from
307 locale even if PYTHONIOENCODING is set. */ 307 locale even if PYTHONIOENCODING is set. */
308 if (!Py_FileSystemDefaultEncoding) { 308 if (!Py_FileSystemDefaultEncoding) {
309 Py_FileSystemDefaultEncoding = loc_codeset; 309 Py_FileSystemDefaultEncoding = loc_codeset;
310 if (!overridden) 310 if (!overridden)
311 free_codeset = 0; 311 free_codeset = 0;
312 } 312 }
313 } 313 }
314 #endif 314 #endif
315 315
316 #ifdef MS_WINDOWS 316 #ifdef MS_WINDOWS
317 if (!overridden) { 317 if (!overridden) {
318 icodeset = ibuf; 318 icodeset = ibuf;
319 codeset = buf; 319 codeset = buf;
320 sprintf(ibuf, "cp%d", GetConsoleCP()); 320 sprintf(ibuf, "cp%d", GetConsoleCP());
321 sprintf(buf, "cp%d", GetConsoleOutputCP()); 321 sprintf(buf, "cp%d", GetConsoleOutputCP());
322 } 322 }
323 #endif 323 #endif
324 324
325 if (codeset) { 325 if (codeset) {
326 sys_stream = PySys_GetObject("stdin"); 326 sys_stream = PySys_GetObject("stdin");
327 sys_isatty = PyObject_CallMethod(sys_stream, "isatty", ""); 327 sys_isatty = PyObject_CallMethod(sys_stream, "isatty", "");
328 if (!sys_isatty) 328 if (!sys_isatty)
329 PyErr_Clear(); 329 PyErr_Clear();
330 if ((overridden || 330 if ((overridden ||
331 (sys_isatty && PyObject_IsTrue(sys_isatty))) && 331 (sys_isatty && PyObject_IsTrue(sys_isatty))) &&
332 PyFile_Check(sys_stream)) { 332 PyFile_Check(sys_stream)) {
333 if (!PyFile_SetEncodingAndErrors(sys_stream, icodeset, errors)) 333 if (!PyFile_SetEncodingAndErrors(sys_stream, icodeset, errors))
334 Py_FatalError("Cannot set codeset of stdin"); 334 Py_FatalError("Cannot set codeset of stdin");
335 } 335 }
336 Py_XDECREF(sys_isatty); 336 Py_XDECREF(sys_isatty);
337 337
338 sys_stream = PySys_GetObject("stdout"); 338 sys_stream = PySys_GetObject("stdout");
339 sys_isatty = PyObject_CallMethod(sys_stream, "isatty", ""); 339 sys_isatty = PyObject_CallMethod(sys_stream, "isatty", "");
340 if (!sys_isatty) 340 if (!sys_isatty)
341 PyErr_Clear(); 341 PyErr_Clear();
342 if ((overridden || 342 if ((overridden ||
343 (sys_isatty && PyObject_IsTrue(sys_isatty))) && 343 (sys_isatty && PyObject_IsTrue(sys_isatty))) &&
344 PyFile_Check(sys_stream)) { 344 PyFile_Check(sys_stream)) {
345 if (!PyFile_SetEncodingAndErrors(sys_stream, codeset, errors)) 345 if (!PyFile_SetEncodingAndErrors(sys_stream, codeset, errors))
346 Py_FatalError("Cannot set codeset of stdout"); 346 Py_FatalError("Cannot set codeset of stdout");
347 } 347 }
348 Py_XDECREF(sys_isatty); 348 Py_XDECREF(sys_isatty);
349 349
350 sys_stream = PySys_GetObject("stderr"); 350 sys_stream = PySys_GetObject("stderr");
351 sys_isatty = PyObject_CallMethod(sys_stream, "isatty", ""); 351 sys_isatty = PyObject_CallMethod(sys_stream, "isatty", "");
352 if (!sys_isatty) 352 if (!sys_isatty)
353 PyErr_Clear(); 353 PyErr_Clear();
354 if((overridden || 354 if((overridden ||
355 (sys_isatty && PyObject_IsTrue(sys_isatty))) && 355 (sys_isatty && PyObject_IsTrue(sys_isatty))) &&
356 PyFile_Check(sys_stream)) { 356 PyFile_Check(sys_stream)) {
357 if (!PyFile_SetEncodingAndErrors(sys_stream, codeset, errors)) 357 if (!PyFile_SetEncodingAndErrors(sys_stream, codeset, errors))
358 Py_FatalError("Cannot set codeset of stderr"); 358 Py_FatalError("Cannot set codeset of stderr");
359 } 359 }
360 Py_XDECREF(sys_isatty); 360 Py_XDECREF(sys_isatty);
361 361
362 if (free_codeset) 362 if (free_codeset)
363 free(codeset); 363 free(codeset);
364 } 364 }
365 } 365 }
366 366
367 void 367 void
368 Py_Initialize(void) 368 Py_Initialize(void)
369 { 369 {
370 Py_InitializeEx(1); 370 Py_InitializeEx(1);
371 } 371 }
372 372
373 373
374 #ifdef COUNT_ALLOCS 374 #ifdef COUNT_ALLOCS
375 extern void dump_counts(FILE*); 375 extern void dump_counts(FILE*);
390 */ 390 */
391 391
392 void 392 void
393 Py_Finalize(void) 393 Py_Finalize(void)
394 { 394 {
395 PyInterpreterState *interp; 395 PyInterpreterState *interp;
396 PyThreadState *tstate; 396 PyThreadState *tstate;
397 397
398 if (!initialized) 398 if (!initialized)
399 return; 399 return;
400 400
401 wait_for_thread_shutdown(); 401 wait_for_thread_shutdown();
402 402
403 /* The interpreter is still entirely intact at this point, and the 403 /* The interpreter is still entirely intact at this point, and the
404 * exit funcs may be relying on that. In particular, if some thread 404 * exit funcs may be relying on that. In particular, if some thread
405 * or exit func is still waiting to do an import, the import machinery 405 * or exit func is still waiting to do an import, the import machinery
406 * expects Py_IsInitialized() to return true. So don't say the 406 * expects Py_IsInitialized() to return true. So don't say the
407 * interpreter is uninitialized until after the exit funcs have run. 407 * interpreter is uninitialized until after the exit funcs have run.
408 * Note that Threading.py uses an exit func to do a join on all the 408 * Note that Threading.py uses an exit func to do a join on all the
409 * threads created thru it, so this also protects pending imports in 409 * threads created thru it, so this also protects pending imports in
410 * the threads created via Threading. 410 * the threads created via Threading.
411 */ 411 */
412 call_sys_exitfunc(); 412 call_sys_exitfunc();
413 initialized = 0; 413 initialized = 0;
414 414
415 /* Get current thread state and interpreter pointer */ 415 /* Get current thread state and interpreter pointer */
416 tstate = PyThreadState_GET(); 416 tstate = PyThreadState_GET();
417 interp = tstate->interp; 417 interp = tstate->interp;
418 418
419 /* Disable signal handling */ 419 /* Disable signal handling */
420 PyOS_FiniInterrupts(); 420 PyOS_FiniInterrupts();
421 421
422 /* Clear type lookup cache */ 422 /* Clear type lookup cache */
423 PyType_ClearCache(); 423 PyType_ClearCache();
424 424
425 /* Collect garbage. This may call finalizers; it's nice to call these 425 /* Collect garbage. This may call finalizers; it's nice to call these
426 * before all modules are destroyed. 426 * before all modules are destroyed.
427 * XXX If a __del__ or weakref callback is triggered here, and tries to 427 * XXX If a __del__ or weakref callback is triggered here, and tries to
428 * XXX import a module, bad things can happen, because Python no 428 * XXX import a module, bad things can happen, because Python no
429 * XXX longer believes it's initialized. 429 * XXX longer believes it's initialized.
430 * XXX Fatal Python error: Interpreter not initialized (version mismatch?) 430 * XXX Fatal Python error: Interpreter not initialized (version mismatch?)
431 * XXX is easy to provoke that way. I've also seen, e.g., 431 * XXX is easy to provoke that way. I've also seen, e.g.,
432 * XXX Exception exceptions.ImportError: 'No module named sha' 432 * XXX Exception exceptions.ImportError: 'No module named sha'
433 * XXX in <function callback at 0x008F5718> ignored 433 * XXX in <function callback at 0x008F5718> ignored
434 * XXX but I'm unclear on exactly how that one happens. In any case, 434 * XXX but I'm unclear on exactly how that one happens. In any case,
435 * XXX I haven't seen a real-life report of either of these. 435 * XXX I haven't seen a real-life report of either of these.
436 */ 436 */
437 PyGC_Collect(); 437 PyGC_Collect();
438 #ifdef COUNT_ALLOCS 438 #ifdef COUNT_ALLOCS
439 /* With COUNT_ALLOCS, it helps to run GC multiple times: 439 /* With COUNT_ALLOCS, it helps to run GC multiple times:
440 each collection might release some types from the type 440 each collection might release some types from the type
441 list, so they become garbage. */ 441 list, so they become garbage. */
442 while (PyGC_Collect() > 0) 442 while (PyGC_Collect() > 0)
443 /* nothing */; 443 /* nothing */;
444 #endif 444 #endif
445 445
446 /* Destroy all modules */ 446 /* Destroy all modules */
447 PyImport_Cleanup(); 447 PyImport_Cleanup();
448 448
449 /* Collect final garbage. This disposes of cycles created by 449 /* Collect final garbage. This disposes of cycles created by
450 * new-style class definitions, for example. 450 * new-style class definitions, for example.
451 * XXX This is disabled because it caused too many problems. If 451 * XXX This is disabled because it caused too many problems. If
452 * XXX a __del__ or weakref callback triggers here, Python code has 452 * XXX a __del__ or weakref callback triggers here, Python code has
453 * XXX a hard time running, because even the sys module has been 453 * XXX a hard time running, because even the sys module has been
454 * XXX cleared out (sys.stdout is gone, sys.excepthook is gone, etc). 454 * XXX cleared out (sys.stdout is gone, sys.excepthook is gone, etc).
455 * XXX One symptom is a sequence of information-free messages 455 * XXX One symptom is a sequence of information-free messages
456 * XXX coming from threads (if a __del__ or callback is invoked, 456 * XXX coming from threads (if a __del__ or callback is invoked,
457 * XXX other threads can execute too, and any exception they encounter 457 * XXX other threads can execute too, and any exception they encounter
458 * XXX triggers a comedy of errors as subsystem after subsystem 458 * XXX triggers a comedy of errors as subsystem after subsystem
459 * XXX fails to find what it *expects* to find in sys to help report 459 * XXX fails to find what it *expects* to find in sys to help report
460 * XXX the exception and consequent unexpected failures). I've also 460 * XXX the exception and consequent unexpected failures). I've also
461 * XXX seen segfaults then, after adding print statements to the 461 * XXX seen segfaults then, after adding print statements to the
462 * XXX Python code getting called. 462 * XXX Python code getting called.
463 */ 463 */
464 #if 0 464 #if 0
465 PyGC_Collect(); 465 PyGC_Collect();
466 #endif 466 #endif
467 467
468 /* Destroy the database used by _PyImport_{Fixup,Find}Extension */ 468 /* Destroy the database used by _PyImport_{Fixup,Find}Extension */
469 _PyImport_Fini(); 469 _PyImport_Fini();
470 470
471 /* Debugging stuff */ 471 /* Debugging stuff */
472 #ifdef COUNT_ALLOCS 472 #ifdef COUNT_ALLOCS
473 dump_counts(stdout); 473 dump_counts(stdout);
474 #endif 474 #endif
475 475
476 PRINT_TOTAL_REFS(); 476 PRINT_TOTAL_REFS();
477 477
478 #ifdef Py_TRACE_REFS 478 #ifdef Py_TRACE_REFS
479 /* Display all objects still alive -- this can invoke arbitrary 479 /* Display all objects still alive -- this can invoke arbitrary
480 * __repr__ overrides, so requires a mostly-intact interpreter. 480 * __repr__ overrides, so requires a mostly-intact interpreter.
481 * Alas, a lot of stuff may still be alive now that will be cleaned 481 * Alas, a lot of stuff may still be alive now that will be cleaned
482 * up later. 482 * up later.
483 */ 483 */
484 if (Py_GETENV("PYTHONDUMPREFS")) 484 if (Py_GETENV("PYTHONDUMPREFS"))
485 _Py_PrintReferences(stderr); 485 _Py_PrintReferences(stderr);
486 #endif /* Py_TRACE_REFS */ 486 #endif /* Py_TRACE_REFS */
487 487
488 /* Clear interpreter state */ 488 /* Clear interpreter state */
489 PyInterpreterState_Clear(interp); 489 PyInterpreterState_Clear(interp);
490 490
491 /* Now we decref the exception classes. After this point nothing 491 /* Now we decref the exception classes. After this point nothing
492 can raise an exception. That's okay, because each Fini() method 492 can raise an exception. That's okay, because each Fini() method
493 below has been checked to make sure no exceptions are ever 493 below has been checked to make sure no exceptions are ever
494 raised. 494 raised.
495 */ 495 */
496 496
497 _PyExc_Fini(); 497 _PyExc_Fini();
498 498
499 /* Cleanup auto-thread-state */ 499 /* Cleanup auto-thread-state */
500 #ifdef WITH_THREAD 500 #ifdef WITH_THREAD
501 _PyGILState_Fini(); 501 _PyGILState_Fini();
502 #endif /* WITH_THREAD */ 502 #endif /* WITH_THREAD */
503 503
504 /* Delete current thread */ 504 /* Delete current thread */
505 PyThreadState_Swap(NULL); 505 PyThreadState_Swap(NULL);
506 PyInterpreterState_Delete(interp); 506 PyInterpreterState_Delete(interp);
507 507
508 /* Sundry finalizers */ 508 /* Sundry finalizers */
509 PyMethod_Fini(); 509 PyMethod_Fini();
510 PyFrame_Fini(); 510 PyFrame_Fini();
511 PyCFunction_Fini(); 511 PyCFunction_Fini();
512 PyTuple_Fini(); 512 PyTuple_Fini();
513 PyList_Fini(); 513 PyList_Fini();
514 PySet_Fini(); 514 PySet_Fini();
515 PyString_Fini(); 515 PyString_Fini();
516 PyByteArray_Fini(); 516 PyByteArray_Fini();
517 PyInt_Fini(); 517 PyInt_Fini();
518 PyFloat_Fini(); 518 PyFloat_Fini();
519 PyDict_Fini(); 519 PyDict_Fini();
520 520
521 #ifdef Py_USING_UNICODE 521 #ifdef Py_USING_UNICODE
522 /* Cleanup Unicode implementation */ 522 /* Cleanup Unicode implementation */
523 _PyUnicode_Fini(); 523 _PyUnicode_Fini();
524 #endif 524 #endif
525 525
526 /* XXX Still allocated: 526 /* XXX Still allocated:
527 - various static ad-hoc pointers to interned strings 527 - various static ad-hoc pointers to interned strings
528 - int and float free list blocks 528 - int and float free list blocks
529 - whatever various modules and libraries allocate 529 - whatever various modules and libraries allocate
530 */ 530 */
531 531
532 PyGrammar_RemoveAccelerators(&_PyParser_Grammar); 532 PyGrammar_RemoveAccelerators(&_PyParser_Grammar);
533 533
534 #ifdef Py_TRACE_REFS 534 #ifdef Py_TRACE_REFS
535 /* Display addresses (& refcnts) of all objects still alive. 535 /* Display addresses (& refcnts) of all objects still alive.
536 * An address can be used to find the repr of the object, printed 536 * An address can be used to find the repr of the object, printed
537 * above by _Py_PrintReferences. 537 * above by _Py_PrintReferences.
538 */ 538 */
539 if (Py_GETENV("PYTHONDUMPREFS")) 539 if (Py_GETENV("PYTHONDUMPREFS"))
540 _Py_PrintReferenceAddresses(stderr); 540 _Py_PrintReferenceAddresses(stderr);
541 #endif /* Py_TRACE_REFS */ 541 #endif /* Py_TRACE_REFS */
542 #ifdef PYMALLOC_DEBUG 542 #ifdef PYMALLOC_DEBUG
543 if (Py_GETENV("PYTHONMALLOCSTATS")) 543 if (Py_GETENV("PYTHONMALLOCSTATS"))
544 _PyObject_DebugMallocStats(); 544 _PyObject_DebugMallocStats();
545 #endif 545 #endif
546 546
547 call_ll_exitfuncs(); 547 call_ll_exitfuncs();
548 } 548 }
549 549
550 /* Create and initialize a new interpreter and thread, and return the 550 /* Create and initialize a new interpreter and thread, and return the
551 new thread. This requires that Py_Initialize() has been called 551 new thread. This requires that Py_Initialize() has been called
552 first. 552 first.
561 */ 561 */
562 562
563 PyThreadState * 563 PyThreadState *
564 Py_NewInterpreter(void) 564 Py_NewInterpreter(void)
565 { 565 {
566 PyInterpreterState *interp; 566 PyInterpreterState *interp;
567 PyThreadState *tstate, *save_tstate; 567 PyThreadState *tstate, *save_tstate;
568 PyObject *bimod, *sysmod; 568 PyObject *bimod, *sysmod;
569 569
570 if (!initialized) 570 if (!initialized)
571 Py_FatalError("Py_NewInterpreter: call Py_Initialize first"); 571 Py_FatalError("Py_NewInterpreter: call Py_Initialize first");
572 572
573 interp = PyInterpreterState_New(); 573 interp = PyInterpreterState_New();
574 if (interp == NULL) 574 if (interp == NULL)
575 return NULL; 575 return NULL;
576 576
577 tstate = PyThreadState_New(interp); 577 tstate = PyThreadState_New(interp);
578 if (tstate == NULL) { 578 if (tstate == NULL) {
579 PyInterpreterState_Delete(interp); 579 PyInterpreterState_Delete(interp);
580 return NULL; 580 return NULL;
581 } 581 }
582 582
583 save_tstate = PyThreadState_Swap(tstate); 583 save_tstate = PyThreadState_Swap(tstate);
584 584
585 /* XXX The following is lax in error checking */ 585 /* XXX The following is lax in error checking */
586 586
587 interp->modules = PyDict_New(); 587 interp->modules = PyDict_New();
588 interp->modules_reloading = PyDict_New(); 588 interp->modules_reloading = PyDict_New();
589 589
590 bimod = _PyImport_FindExtension("__builtin__", "__builtin__"); 590 bimod = _PyImport_FindExtension("__builtin__", "__builtin__");
591 if (bimod != NULL) { 591 if (bimod != NULL) {
592 interp->builtins = PyModule_GetDict(bimod); 592 interp->builtins = PyModule_GetDict(bimod);
593 if (interp->builtins == NULL) 593 if (interp->builtins == NULL)
594 goto handle_error; 594 goto handle_error;
595 Py_INCREF(interp->builtins); 595 Py_INCREF(interp->builtins);
596 } 596 }
597 sysmod = _PyImport_FindExtension("sys", "sys"); 597 sysmod = _PyImport_FindExtension("sys", "sys");
598 if (bimod != NULL && sysmod != NULL) { 598 if (bimod != NULL && sysmod != NULL) {
599 interp->sysdict = PyModule_GetDict(sysmod); 599 interp->sysdict = PyModule_GetDict(sysmod);
600 if (interp->sysdict == NULL) 600 if (interp->sysdict == NULL)
601 goto handle_error; 601 goto handle_error;
602 Py_INCREF(interp->sysdict); 602 Py_INCREF(interp->sysdict);
603 PySys_SetPath(Py_GetPath()); 603 PySys_SetPath(Py_GetPath());
604 PyDict_SetItemString(interp->sysdict, "modules", 604 PyDict_SetItemString(interp->sysdict, "modules",
605 interp->modules); 605 interp->modules);
606 _PyImportHooks_Init(); 606 _PyImportHooks_Init();
607 initmain(); 607 initmain();
608 if (!Py_NoSiteFlag) 608 if (!Py_NoSiteFlag)
609 initsite(); 609 initsite();
610 } 610 }
611 611
612 if (!PyErr_Occurred()) 612 if (!PyErr_Occurred())
613 return tstate; 613 return tstate;
614 614
615 handle_error: 615 handle_error:
616 /* Oops, it didn't work. Undo it all. */ 616 /* Oops, it didn't work. Undo it all. */
617 617
618 PyErr_Print(); 618 PyErr_Print();
619 PyThreadState_Clear(tstate); 619 PyThreadState_Clear(tstate);
620 PyThreadState_Swap(save_tstate); 620 PyThreadState_Swap(save_tstate);
621 PyThreadState_Delete(tstate); 621 PyThreadState_Delete(tstate);
622 PyInterpreterState_Delete(interp); 622 PyInterpreterState_Delete(interp);
623 623
624 return NULL; 624 return NULL;
625 } 625 }
626 626
627 /* Delete an interpreter and its last thread. This requires that the 627 /* Delete an interpreter and its last thread. This requires that the
628 given thread state is current, that the thread has no remaining 628 given thread state is current, that the thread has no remaining
629 frames, and that it is its interpreter's only remaining thread. 629 frames, and that it is its interpreter's only remaining thread.
637 */ 637 */
638 638
639 void 639 void
640 Py_EndInterpreter(PyThreadState *tstate) 640 Py_EndInterpreter(PyThreadState *tstate)
641 { 641 {
642 PyInterpreterState *interp = tstate->interp; 642 PyInterpreterState *interp = tstate->interp;
643 643
644 if (tstate != PyThreadState_GET()) 644 if (tstate != PyThreadState_GET())
645 Py_FatalError("Py_EndInterpreter: thread is not current"); 645 Py_FatalError("Py_EndInterpreter: thread is not current");
646 if (tstate->frame != NULL) 646 if (tstate->frame != NULL)
647 Py_FatalError("Py_EndInterpreter: thread still has a frame"); 647 Py_FatalError("Py_EndInterpreter: thread still has a frame");
648 if (tstate != interp->tstate_head || tstate->next != NULL) 648 if (tstate != interp->tstate_head || tstate->next != NULL)
649 Py_FatalError("Py_EndInterpreter: not the last thread"); 649 Py_FatalError("Py_EndInterpreter: not the last thread");
650 650
651 PyImport_Cleanup(); 651 PyImport_Cleanup();
652 PyInterpreterState_Clear(interp); 652 PyInterpreterState_Clear(interp);
653 PyThreadState_Swap(NULL); 653 PyThreadState_Swap(NULL);
654 PyInterpreterState_Delete(interp); 654 PyInterpreterState_Delete(interp);
655 } 655 }
656 656
657 static char *progname = "python"; 657 static char *progname = "python";
658 658
659 void 659 void
660 Py_SetProgramName(char *pn) 660 Py_SetProgramName(char *pn)
661 { 661 {
662 if (pn && *pn) 662 if (pn && *pn)
663 progname = pn; 663 progname = pn;
664 } 664 }
665 665
666 char * 666 char *
667 Py_GetProgramName(void) 667 Py_GetProgramName(void)
668 { 668 {
669 return progname; 669 return progname;
670 } 670 }
671 671
672 static char *default_home = NULL; 672 static char *default_home = NULL;
673 673
674 void 674 void
675 Py_SetPythonHome(char *home) 675 Py_SetPythonHome(char *home)
676 { 676 {
677 default_home = home; 677 default_home = home;
678 } 678 }
679 679
680 char * 680 char *
681 Py_GetPythonHome(void) 681 Py_GetPythonHome(void)
682 { 682 {
683 char *home = default_home; 683 char *home = default_home;
684 if (home == NULL && !Py_IgnoreEnvironmentFlag) 684 if (home == NULL && !Py_IgnoreEnvironmentFlag)
685 home = Py_GETENV("PYTHONHOME"); 685 home = Py_GETENV("PYTHONHOME");
686 return home; 686 return home;
687 } 687 }
688 688
689 /* Create __main__ module */ 689 /* Create __main__ module */
690 690
691 static void 691 static void
692 initmain(void) 692 initmain(void)
693 { 693 {
694 PyObject *m, *d; 694 PyObject *m, *d;
695 m = PyImport_AddModule("__main__"); 695 m = PyImport_AddModule("__main__");
696 if (m == NULL) 696 if (m == NULL)
697 Py_FatalError("can't create __main__ module"); 697 Py_FatalError("can't create __main__ module");
698 d = PyModule_GetDict(m); 698 d = PyModule_GetDict(m);
699 if (PyDict_GetItemString(d, "__builtins__") == NULL) { 699 if (PyDict_GetItemString(d, "__builtins__") == NULL) {
700 PyObject *bimod = PyImport_ImportModule("__builtin__"); 700 PyObject *bimod = PyImport_ImportModule("__builtin__");
701 if (bimod == NULL || 701 if (bimod == NULL ||
702 PyDict_SetItemString(d, "__builtins__", bimod) != 0) 702 PyDict_SetItemString(d, "__builtins__", bimod) != 0)
703 Py_FatalError("can't add __builtins__ to __main__"); 703 Py_FatalError("can't add __builtins__ to __main__");
704 Py_XDECREF(bimod); 704 Py_XDECREF(bimod);
705 } 705 }
706 } 706 }
707 707
708 /* Import the site module (not into __main__ though) */ 708 /* Import the site module (not into __main__ though) */
709 709
710 static void 710 static void
711 initsite(void) 711 initsite(void)
712 { 712 {
713 PyObject *m; 713 PyObject *m;
714 m = PyImport_ImportModule("site"); 714 m = PyImport_ImportModule("site");
715 if (m == NULL) { 715 if (m == NULL) {
716 PyErr_Print(); 716 PyErr_Print();
717 Py_Finalize(); 717 Py_Finalize();
718 exit(1); 718 exit(1);
719 } 719 }
720 else { 720 else {
721 Py_DECREF(m); 721 Py_DECREF(m);
722 } 722 }
723 } 723 }
724 724
725 /* Parse input from a file and execute it */ 725 /* Parse input from a file and execute it */
726 726
727 int 727 int
728 PyRun_AnyFileExFlags(FILE *fp, const char *filename, int closeit, 728 PyRun_AnyFileExFlags(FILE *fp, const char *filename, int closeit,
729 PyCompilerFlags *flags) 729 PyCompilerFlags *flags)
730 { 730 {
731 if (filename == NULL) 731 if (filename == NULL)
732 filename = "???"; 732 filename = "???";
733 if (Py_FdIsInteractive(fp, filename)) { 733 if (Py_FdIsInteractive(fp, filename)) {
734 int err = PyRun_InteractiveLoopFlags(fp, filename, flags); 734 int err = PyRun_InteractiveLoopFlags(fp, filename, flags);
735 if (closeit) 735 if (closeit)
736 fclose(fp); 736 fclose(fp);
737 return err; 737 return err;
738 } 738 }
739 else 739 else
740 return PyRun_SimpleFileExFlags(fp, filename, closeit, flags); 740 return PyRun_SimpleFileExFlags(fp, filename, closeit, flags);
741 } 741 }
742 742
743 int 743 int
744 PyRun_InteractiveLoopFlags(FILE *fp, const char *filename, PyCompilerFlags *flags) 744 PyRun_InteractiveLoopFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
745 { 745 {
746 PyObject *v; 746 PyObject *v;
747 int ret; 747 int ret;
748 PyCompilerFlags local_flags; 748 PyCompilerFlags local_flags;
749 749
750 if (flags == NULL) { 750 if (flags == NULL) {
751 flags = &local_flags; 751 flags = &local_flags;
752 local_flags.cf_flags = 0; 752 local_flags.cf_flags = 0;
753 } 753 }
754 v = PySys_GetObject("ps1"); 754 v = PySys_GetObject("ps1");
755 if (v == NULL) { 755 if (v == NULL) {
756 PySys_SetObject("ps1", v = PyString_FromString(">>> ")); 756 PySys_SetObject("ps1", v = PyString_FromString(">>> "));
757 Py_XDECREF(v); 757 Py_XDECREF(v);
758 } 758 }
759 v = PySys_GetObject("ps2"); 759 v = PySys_GetObject("ps2");
760 if (v == NULL) { 760 if (v == NULL) {
761 PySys_SetObject("ps2", v = PyString_FromString("... ")); 761 PySys_SetObject("ps2", v = PyString_FromString("... "));
762 Py_XDECREF(v); 762 Py_XDECREF(v);
763 } 763 }
764 for (;;) { 764 for (;;) {
765 ret = PyRun_InteractiveOneFlags(fp, filename, flags); 765 ret = PyRun_InteractiveOneFlags(fp, filename, flags);
766 PRINT_TOTAL_REFS(); 766 PRINT_TOTAL_REFS();
767 if (ret == E_EOF) 767 if (ret == E_EOF)
768 return 0; 768 return 0;
769 /* 769 /*
770 if (ret == E_NOMEM) 770 if (ret == E_NOMEM)
771 return -1; 771 return -1;
772 */ 772 */
773 } 773 }
774 } 774 }
775 775
776 #if 0 776 #if 0
777 /* compute parser flags based on compiler flags */ 777 /* compute parser flags based on compiler flags */
778 #define PARSER_FLAGS(flags) \ 778 #define PARSER_FLAGS(flags) \
779 ((flags) ? ((((flags)->cf_flags & PyCF_DONT_IMPLY_DEDENT) ? \ 779 ((flags) ? ((((flags)->cf_flags & PyCF_DONT_IMPLY_DEDENT) ? \
780 PyPARSE_DONT_IMPLY_DEDENT : 0)) : 0) 780 PyPARSE_DONT_IMPLY_DEDENT : 0)) : 0)
781 #endif 781 #endif
782 #if 1 782 #if 1
783 /* Keep an example of flags with future keyword support. */ 783 /* Keep an example of flags with future keyword support. */
784 #define PARSER_FLAGS(flags) \ 784 #define PARSER_FLAGS(flags) \
785 ((flags) ? ((((flags)->cf_flags & PyCF_DONT_IMPLY_DEDENT) ? \ 785 ((flags) ? ((((flags)->cf_flags & PyCF_DONT_IMPLY_DEDENT) ? \
786 PyPARSE_DONT_IMPLY_DEDENT : 0) \ 786 PyPARSE_DONT_IMPLY_DEDENT : 0) \
787 | (((flags)->cf_flags & CO_FUTURE_PRINT_FUNCTION) ? \ 787 | (((flags)->cf_flags & CO_FUTURE_PRINT_FUNCTION) ? \
788 PyPARSE_PRINT_IS_FUNCTION : 0) \ 788 PyPARSE_PRINT_IS_FUNCTION : 0) \
789 | (((flags)->cf_flags & CO_FUTURE_UNICODE_LITERALS) ? \ 789 | (((flags)->cf_flags & CO_FUTURE_UNICODE_LITERALS) ? \
790 PyPARSE_UNICODE_LITERALS : 0) \ 790 PyPARSE_UNICODE_LITERALS : 0) \
791 ) : 0) 791 ) : 0)
792 #endif 792 #endif
793 793
794 int 794 int
795 PyRun_InteractiveOneFlags(FILE *fp, const char *filename, PyCompilerFlags *flags) 795 PyRun_InteractiveOneFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
796 { 796 {
797 PyObject *m, *d, *v, *w; 797 PyObject *m, *d, *v, *w;
798 mod_ty mod; 798 mod_ty mod;
799 PyArena *arena; 799 PyArena *arena;
800 char *ps1 = "", *ps2 = ""; 800 char *ps1 = "", *ps2 = "";
801 int errcode = 0; 801 int errcode = 0;
802 802
803 v = PySys_GetObject("ps1"); 803 v = PySys_GetObject("ps1");
804 if (v != NULL) { 804 if (v != NULL) {
805 v = PyObject_Str(v); 805 v = PyObject_Str(v);
806 if (v == NULL) 806 if (v == NULL)
807 PyErr_Clear(); 807 PyErr_Clear();
808 else if (PyString_Check(v)) 808 else if (PyString_Check(v))
809 ps1 = PyString_AsString(v); 809 ps1 = PyString_AsString(v);
810 } 810 }
811 w = PySys_GetObject("ps2"); 811 w = PySys_GetObject("ps2");
812 if (w != NULL) { 812 if (w != NULL) {
813 w = PyObject_Str(w); 813 w = PyObject_Str(w);
814 if (w == NULL) 814 if (w == NULL)
815 PyErr_Clear(); 815 PyErr_Clear();
816 else if (PyString_Check(w)) 816 else if (PyString_Check(w))
817 ps2 = PyString_AsString(w); 817 ps2 = PyString_AsString(w);
818 } 818 }
819 arena = PyArena_New(); 819 arena = PyArena_New();
820 if (arena == NULL) { 820 if (arena == NULL) {
821 Py_XDECREF(v); 821 Py_XDECREF(v);
822 Py_XDECREF(w); 822 Py_XDECREF(w);
823 return -1; 823 return -1;
824 } 824 }
825 mod = PyParser_ASTFromFile(fp, filename, 825 mod = PyParser_ASTFromFile(fp, filename,
826 Py_single_input, ps1, ps2, 826 Py_single_input, ps1, ps2,
827 flags, &errcode, arena); 827 flags, &errcode, arena);
828 Py_XDECREF(v); 828 Py_XDECREF(v);
829 Py_XDECREF(w); 829 Py_XDECREF(w);
830 if (mod == NULL) { 830 if (mod == NULL) {
831 PyArena_Free(arena); 831 PyArena_Free(arena);
832 if (errcode == E_EOF) { 832 if (errcode == E_EOF) {
833 PyErr_Clear(); 833 PyErr_Clear();
834 return E_EOF; 834 return E_EOF;
835 } 835 }
836 PyErr_Print(); 836 PyErr_Print();
837 return -1; 837 return -1;
838 } 838 }
839 m = PyImport_AddModule("__main__"); 839 m = PyImport_AddModule("__main__");
840 if (m == NULL) { 840 if (m == NULL) {
841 PyArena_Free(arena); 841 PyArena_Free(arena);
842 return -1; 842 return -1;
843 } 843 }
844 d = PyModule_GetDict(m); 844 d = PyModule_GetDict(m);
845 v = run_mod(mod, filename, d, d, flags, arena); 845 v = run_mod(mod, filename, d, d, flags, arena);
846 PyArena_Free(arena); 846 PyArena_Free(arena);
847 if (v == NULL) { 847 if (v == NULL) {
848 PyErr_Print(); 848 PyErr_Print();
849 return -1; 849 return -1;
850 } 850 }
851 Py_DECREF(v); 851 Py_DECREF(v);
852 if (Py_FlushLine()) 852 if (Py_FlushLine())
853 PyErr_Clear(); 853 PyErr_Clear();
854 return 0; 854 return 0;
855 } 855 }
856 856
857 /* Check whether a file maybe a pyc file: Look at the extension, 857 /* Check whether a file maybe a pyc file: Look at the extension,
858 the file type, and, if we may close it, at the first few bytes. */ 858 the file type, and, if we may close it, at the first few bytes. */
859 859
860 static int 860 static int
861 maybe_pyc_file(FILE *fp, const char* filename, const char* ext, int closeit) 861 maybe_pyc_file(FILE *fp, const char* filename, const char* ext, int closeit)
862 { 862 {
863 if (strcmp(ext, ".pyc") == 0 || strcmp(ext, ".pyo") == 0) 863 if (strcmp(ext, ".pyc") == 0 || strcmp(ext, ".pyo") == 0)
864 return 1; 864 return 1;
865 865
866 /* Only look into the file if we are allowed to close it, since 866 /* Only look into the file if we are allowed to close it, since
867 it then should also be seekable. */ 867 it then should also be seekable. */
868 if (closeit) { 868 if (closeit) {
869 /* Read only two bytes of the magic. If the file was opened in 869 /* Read only two bytes of the magic. If the file was opened in
870 text mode, the bytes 3 and 4 of the magic (\r\n) might not 870 text mode, the bytes 3 and 4 of the magic (\r\n) might not
871 be read as they are on disk. */ 871 be read as they are on disk. */
872 unsigned int halfmagic = PyImport_GetMagicNumber() & 0xFFFF; 872 unsigned int halfmagic = PyImport_GetMagicNumber() & 0xFFFF;
873 unsigned char buf[2]; 873 unsigned char buf[2];
874 /* Mess: In case of -x, the stream is NOT at its start now, 874 /* Mess: In case of -x, the stream is NOT at its start now,
875 and ungetc() was used to push back the first newline, 875 and ungetc() was used to push back the first newline,
876 which makes the current stream position formally undefined, 876 which makes the current stream position formally undefined,
877 and a x-platform nightmare. 877 and a x-platform nightmare.
878 Unfortunately, we have no direct way to know whether -x 878 Unfortunately, we have no direct way to know whether -x
879 was specified. So we use a terrible hack: if the current 879 was specified. So we use a terrible hack: if the current
880 stream position is not 0, we assume -x was specified, and 880 stream position is not 0, we assume -x was specified, and
881 give up. Bug 132850 on SourceForge spells out the 881 give up. Bug 132850 on SourceForge spells out the
882 hopelessness of trying anything else (fseek and ftell 882 hopelessness of trying anything else (fseek and ftell
883 don't work predictably x-platform for text-mode files). 883 don't work predictably x-platform for text-mode files).
884 */ 884 */
885 int ispyc = 0; 885 int ispyc = 0;
886 if (ftell(fp) == 0) { 886 if (ftell(fp) == 0) {
887 if (fread(buf, 1, 2, fp) == 2 && 887 if (fread(buf, 1, 2, fp) == 2 &&
888 ((unsigned int)buf[1]<<8 | buf[0]) == halfmagic) 888 ((unsigned int)buf[1]<<8 | buf[0]) == halfmagic)
889 ispyc = 1; 889 ispyc = 1;
890 rewind(fp); 890 rewind(fp);
891 } 891 }
892 return ispyc; 892 return ispyc;
893 } 893 }
894 return 0; 894 return 0;
895 } 895 }
896 896
897 int 897 int
898 PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit, 898 PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit,
899 PyCompilerFlags *flags) 899 PyCompilerFlags *flags)
900 { 900 {
901 PyObject *m, *d, *v; 901 PyObject *m, *d, *v;
902 const char *ext; 902 const char *ext;
903 int set_file_name = 0, ret, len; 903 int set_file_name = 0, ret, len;
904 904
905 m = PyImport_AddModule("__main__"); 905 m = PyImport_AddModule("__main__");
906 if (m == NULL) 906 if (m == NULL)
907 return -1; 907 return -1;
908 d = PyModule_GetDict(m); 908 d = PyModule_GetDict(m);
909 if (PyDict_GetItemString(d, "__file__") == NULL) { 909 if (PyDict_GetItemString(d, "__file__") == NULL) {
910 PyObject *f = PyString_FromString(filename); 910 PyObject *f = PyString_FromString(filename);
911 if (f == NULL) 911 if (f == NULL)
912 return -1; 912 return -1;
913 if (PyDict_SetItemString(d, "__file__", f) < 0) { 913 if (PyDict_SetItemString(d, "__file__", f) < 0) {
914 Py_DECREF(f); 914 Py_DECREF(f);
915 return -1; 915 return -1;
916 } 916 }
917 set_file_name = 1; 917 set_file_name = 1;
918 Py_DECREF(f); 918 Py_DECREF(f);
919 } 919 }
920 len = strlen(filename); 920 len = strlen(filename);
921 ext = filename + len - (len > 4 ? 4 : 0); 921 ext = filename + len - (len > 4 ? 4 : 0);
922 if (maybe_pyc_file(fp, filename, ext, closeit)) { 922 if (maybe_pyc_file(fp, filename, ext, closeit)) {
923 /* Try to run a pyc file. First, re-open in binary */ 923 /* Try to run a pyc file. First, re-open in binary */
924 if (closeit) 924 if (closeit)
925 fclose(fp); 925 fclose(fp);
926 if ((fp = fopen(filename, "rb")) == NULL) { 926 if ((fp = fopen(filename, "rb")) == NULL) {
927 fprintf(stderr, "python: Can't reopen .pyc file\n"); 927 fprintf(stderr, "python: Can't reopen .pyc file\n");
928 ret = -1; 928 ret = -1;
929 goto done; 929 goto done;
930 } 930 }
931 /* Turn on optimization if a .pyo file is given */ 931 /* Turn on optimization if a .pyo file is given */
932 if (strcmp(ext, ".pyo") == 0) 932 if (strcmp(ext, ".pyo") == 0)
933 Py_OptimizeFlag = 1; 933 Py_OptimizeFlag = 1;
934 v = run_pyc_file(fp, filename, d, d, flags); 934 v = run_pyc_file(fp, filename, d, d, flags);
935 } else { 935 } else {
936 v = PyRun_FileExFlags(fp, filename, Py_file_input, d, d, 936 v = PyRun_FileExFlags(fp, filename, Py_file_input, d, d,
937 closeit, flags); 937 closeit, flags);
938 } 938 }
939 if (v == NULL) { 939 if (v == NULL) {
940 PyErr_Print(); 940 PyErr_Print();
941 ret = -1; 941 ret = -1;
942 goto done; 942 goto done;
943 } 943 }
944 Py_DECREF(v); 944 Py_DECREF(v);
945 if (Py_FlushLine()) 945 if (Py_FlushLine())
946 PyErr_Clear(); 946 PyErr_Clear();
947 ret = 0; 947 ret = 0;
948 done: 948 done:
949 if (set_file_name && PyDict_DelItemString(d, "__file__")) 949 if (set_file_name && PyDict_DelItemString(d, "__file__"))
950 PyErr_Clear(); 950 PyErr_Clear();
951 return ret; 951 return ret;
952 } 952 }
953 953
954 int 954 int
955 PyRun_SimpleStringFlags(const char *command, PyCompilerFlags *flags) 955 PyRun_SimpleStringFlags(const char *command, PyCompilerFlags *flags)
956 { 956 {
957 PyObject *m, *d, *v; 957 PyObject *m, *d, *v;
958 m = PyImport_AddModule("__main__"); 958 m = PyImport_AddModule("__main__");
959 if (m == NULL) 959 if (m == NULL)
960 return -1; 960 return -1;
961 d = PyModule_GetDict(m); 961 d = PyModule_GetDict(m);
962 v = PyRun_StringFlags(command, Py_file_input, d, d, flags); 962 v = PyRun_StringFlags(command, Py_file_input, d, d, flags);
963 if (v == NULL) { 963 if (v == NULL) {
964 PyErr_Print(); 964 PyErr_Print();
965 return -1; 965 return -1;
966 } 966 }
967 Py_DECREF(v); 967 Py_DECREF(v);
968 if (Py_FlushLine()) 968 if (Py_FlushLine())
969 PyErr_Clear(); 969 PyErr_Clear();
970 return 0; 970 return 0;
971 } 971 }
972 972
973 static int 973 static int
974 parse_syntax_error(PyObject *err, PyObject **message, const char **filename, 974 parse_syntax_error(PyObject *err, PyObject **message, const char **filename,
975 int *lineno, int *offset, const char **text) 975 int *lineno, int *offset, const char **text)
976 { 976 {
977 long hold; 977 long hold;
978 PyObject *v; 978 PyObject *v;
979 979
980 /* old style errors */ 980 /* old style errors */
981 if (PyTuple_Check(err)) 981 if (PyTuple_Check(err))
982 return PyArg_ParseTuple(err, "O(ziiz)", message, filename, 982 return PyArg_ParseTuple(err, "O(ziiz)", message, filename,
983 lineno, offset, text); 983 lineno, offset, text);
984 984
985 /* new style errors. `err' is an instance */ 985 /* new style errors. `err' is an instance */
986 986
987 if (! (v = PyObject_GetAttrString(err, "msg"))) 987 if (! (v = PyObject_GetAttrString(err, "msg")))
988 goto finally; 988 goto finally;
989 *message = v; 989 *message = v;
990 990
991 if (!(v = PyObject_GetAttrString(err, "filename"))) 991 if (!(v = PyObject_GetAttrString(err, "filename")))
992 goto finally; 992 goto finally;
993 if (v == Py_None) 993 if (v == Py_None)
994 *filename = NULL; 994 *filename = NULL;
995 else if (! (*filename = PyString_AsString(v))) 995 else if (! (*filename = PyString_AsString(v)))
996 goto finally; 996 goto finally;
997 997
998 Py_DECREF(v); 998 Py_DECREF(v);
999 if (!(v = PyObject_GetAttrString(err, "lineno"))) 999 if (!(v = PyObject_GetAttrString(err, "lineno")))
1000 goto finally; 1000 goto finally;
1001 hold = PyInt_AsLong(v); 1001 hold = PyInt_AsLong(v);
1002 Py_DECREF(v); 1002 Py_DECREF(v);
1003 v = NULL; 1003 v = NULL;
1004 if (hold < 0 && PyErr_Occurred()) 1004 if (hold < 0 && PyErr_Occurred())
1005 goto finally; 1005 goto finally;
1006 *lineno = (int)hold; 1006 *lineno = (int)hold;
1007 1007
1008 if (!(v = PyObject_GetAttrString(err, "offset"))) 1008 if (!(v = PyObject_GetAttrString(err, "offset")))
1009 goto finally; 1009 goto finally;
1010 if (v == Py_None) { 1010 if (v == Py_None) {
1011 *offset = -1; 1011 *offset = -1;
1012 Py_DECREF(v); 1012 Py_DECREF(v);
1013 v = NULL; 1013 v = NULL;
1014 } else { 1014 } else {
1015 hold = PyInt_AsLong(v); 1015 hold = PyInt_AsLong(v);
1016 Py_DECREF(v); 1016 Py_DECREF(v);
1017 v = NULL; 1017 v = NULL;
1018 if (hold < 0 && PyErr_Occurred()) 1018 if (hold < 0 && PyErr_Occurred())
1019 goto finally; 1019 goto finally;
1020 *offset = (int)hold; 1020 *offset = (int)hold;
1021 } 1021 }
1022 1022
1023 if (!(v = PyObject_GetAttrString(err, "text"))) 1023 if (!(v = PyObject_GetAttrString(err, "text")))
1024 goto finally; 1024 goto finally;
1025 if (v == Py_None) 1025 if (v == Py_None)
1026 *text = NULL; 1026 *text = NULL;
1027 else if (! (*text = PyString_AsString(v))) 1027 else if (! (*text = PyString_AsString(v)))
1028 goto finally; 1028 goto finally;
1029 Py_DECREF(v); 1029 Py_DECREF(v);
1030 return 1; 1030 return 1;
1031 1031
1032 finally: 1032 finally:
1033 Py_XDECREF(v); 1033 Py_XDECREF(v);
1034 return 0; 1034 return 0;
1035 } 1035 }
1036 1036
1037 void 1037 void
1038 PyErr_Print(void) 1038 PyErr_Print(void)
1039 { 1039 {
1040 PyErr_PrintEx(1); 1040 PyErr_PrintEx(1);
1041 } 1041 }
1042 1042
1043 static void 1043 static void
1044 print_error_text(PyObject *f, int offset, const char *text) 1044 print_error_text(PyObject *f, int offset, const char *text)
1045 { 1045 {
1046 char *nl; 1046 char *nl;
1047 if (offset >= 0) { 1047 if (offset >= 0) {
1048 if (offset > 0 && offset == (int)strlen(text)) 1048 if (offset > 0 && offset == (int)strlen(text))
1049 offset--; 1049 offset--;
1050 for (;;) { 1050 for (;;) {
1051 nl = strchr(text, '\n'); 1051 nl = strchr(text, '\n');
1052 if (nl == NULL || nl-text >= offset) 1052 if (nl == NULL || nl-text >= offset)
1053 break; 1053 break;
1054 offset -= (int)(nl+1-text); 1054 offset -= (int)(nl+1-text);
1055 text = nl+1; 1055 text = nl+1;
1056 } 1056 }
1057 while (*text == ' ' || *text == '\t') { 1057 while (*text == ' ' || *text == '\t') {
1058 text++; 1058 text++;
1059 offset--; 1059 offset--;
1060 } 1060 }
1061 } 1061 }
1062 PyFile_WriteString(" ", f); 1062 PyFile_WriteString(" ", f);
1063 PyFile_WriteString(text, f); 1063 PyFile_WriteString(text, f);
1064 if (*text == '\0' || text[strlen(text)-1] != '\n') 1064 if (*text == '\0' || text[strlen(text)-1] != '\n')
1065 PyFile_WriteString("\n", f); 1065 PyFile_WriteString("\n", f);
1066 if (offset == -1) 1066 if (offset == -1)
1067 return; 1067 return;
1068 PyFile_WriteString(" ", f); 1068 PyFile_WriteString(" ", f);
1069 offset--; 1069 offset--;
1070 while (offset > 0) { 1070 while (offset > 0) {
1071 PyFile_WriteString(" ", f); 1071 PyFile_WriteString(" ", f);
1072 offset--; 1072 offset--;
1073 } 1073 }
1074 PyFile_WriteString("^\n", f); 1074 PyFile_WriteString("^\n", f);
1075 } 1075 }
1076 1076
1077 static void 1077 static void
1078 handle_system_exit(void) 1078 handle_system_exit(void)
1079 { 1079 {
1080 PyObject *exception, *value, *tb; 1080 PyObject *exception, *value, *tb;
1081 int exitcode = 0; 1081 int exitcode = 0;
1082 1082
1083 if (Py_InspectFlag) 1083 if (Py_InspectFlag)
1084 /* Don't exit if -i flag was given. This flag is set to 0 1084 /* Don't exit if -i flag was given. This flag is set to 0
1085 * when entering interactive mode for inspecting. */ 1085 * when entering interactive mode for inspecting. */
1086 return; 1086 return;
1087 1087
1088 PyErr_Fetch(&exception, &value, &tb); 1088 PyErr_Fetch(&exception, &value, &tb);
1089 if (Py_FlushLine()) 1089 if (Py_FlushLine())
1090 PyErr_Clear(); 1090 PyErr_Clear();
1091 fflush(stdout); 1091 fflush(stdout);
1092 if (value == NULL || value == Py_None) 1092 if (value == NULL || value == Py_None)
1093 goto done; 1093 goto done;
1094 if (PyExceptionInstance_Check(value)) { 1094 if (PyExceptionInstance_Check(value)) {
1095 /* The error code should be in the `code' attribute. */ 1095 /* The error code should be in the `code' attribute. */
1096 PyObject *code = PyObject_GetAttrString(value, "code"); 1096 PyObject *code = PyObject_GetAttrString(value, "code");
1097 if (code) { 1097 if (code) {
1098 Py_DECREF(value); 1098 Py_DECREF(value);
1099 value = code; 1099 value = code;
1100 if (value == Py_None) 1100 if (value == Py_None)
1101 goto done; 1101 goto done;
1102 } 1102 }
1103 /* If we failed to dig out the 'code' attribute, 1103 /* If we failed to dig out the 'code' attribute,
1104 just let the else clause below print the error. */ 1104 just let the else clause below print the error. */
1105 } 1105 }
1106 if (PyInt_Check(value)) 1106 if (PyInt_Check(value))
1107 exitcode = (int)PyInt_AsLong(value); 1107 exitcode = (int)PyInt_AsLong(value);
1108 else { 1108 else {
1109 PyObject_Print(value, stderr, Py_PRINT_RAW); 1109 PyObject_Print(value, stderr, Py_PRINT_RAW);
1110 PySys_WriteStderr("\n"); 1110 PySys_WriteStderr("\n");
1111 exitcode = 1; 1111 exitcode = 1;
1112 } 1112 }
1113 done: 1113 done:
1114 /* Restore and clear the exception info, in order to properly decref 1114 /* Restore and clear the exception info, in order to properly decref
1115 * the exception, value, and traceback. If we just exit instead, 1115 * the exception, value, and traceback. If we just exit instead,
1116 * these leak, which confuses PYTHONDUMPREFS output, and may prevent 1116 * these leak, which confuses PYTHONDUMPREFS output, and may prevent
1117 * some finalizers from running. 1117 * some finalizers from running.
1118 */ 1118 */
1119 PyErr_Restore(exception, value, tb); 1119 PyErr_Restore(exception, value, tb);
1120 PyErr_Clear(); 1120 PyErr_Clear();
1121 Py_Exit(exitcode); 1121 Py_Exit(exitcode);
1122 /* NOTREACHED */ 1122 /* NOTREACHED */
1123 } 1123 }
1124 1124
1125 void 1125 void
1126 PyErr_PrintEx(int set_sys_last_vars) 1126 PyErr_PrintEx(int set_sys_last_vars)
1127 { 1127 {
1128 PyObject *exception, *v, *tb, *hook; 1128 PyObject *exception, *v, *tb, *hook;
1129 1129
1130 if (PyErr_ExceptionMatches(PyExc_SystemExit)) { 1130 if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
1131 handle_system_exit(); 1131 handle_system_exit();
1132 } 1132 }
1133 PyErr_Fetch(&exception, &v, &tb); 1133 PyErr_Fetch(&exception, &v, &tb);
1134 if (exception == NULL) 1134 if (exception == NULL)
1135 return; 1135 return;
1136 PyErr_NormalizeException(&exception, &v, &tb); 1136 PyErr_NormalizeException(&exception, &v, &tb);
1137 if (exception == NULL) 1137 if (exception == NULL)
1138 return; 1138 return;
1139 /* Now we know v != NULL too */ 1139 /* Now we know v != NULL too */
1140 if (set_sys_last_vars) { 1140 if (set_sys_last_vars) {
1141 PySys_SetObject("last_type", exception); 1141 PySys_SetObject("last_type", exception);
1142 PySys_SetObject("last_value", v); 1142 PySys_SetObject("last_value", v);
1143 PySys_SetObject("last_traceback", tb); 1143 PySys_SetObject("last_traceback", tb);
1144 } 1144 }
1145 hook = PySys_GetObject("excepthook"); 1145 hook = PySys_GetObject("excepthook");
1146 if (hook) { 1146 if (hook) {
1147 PyObject *args = PyTuple_Pack(3, 1147 PyObject *args = PyTuple_Pack(3,
1148 exception, v, tb ? tb : Py_None); 1148 exception, v, tb ? tb : Py_None);
1149 PyObject *result = PyEval_CallObject(hook, args); 1149 PyObject *result = PyEval_CallObject(hook, args);
1150 if (result == NULL) { 1150 if (result == NULL) {
1151 PyObject *exception2, *v2, *tb2; 1151 PyObject *exception2, *v2, *tb2;
1152 if (PyErr_ExceptionMatches(PyExc_SystemExit)) { 1152 if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
1153 handle_system_exit(); 1153 handle_system_exit();
1154 } 1154 }
1155 PyErr_Fetch(&exception2, &v2, &tb2); 1155 PyErr_Fetch(&exception2, &v2, &tb2);
1156 PyErr_NormalizeException(&exception2, &v2, &tb2); 1156 PyErr_NormalizeException(&exception2, &v2, &tb2);
1157 /* It should not be possible for exception2 or v2 1157 /* It should not be possible for exception2 or v2
1158 to be NULL. However PyErr_Display() can't 1158 to be NULL. However PyErr_Display() can't
1159 tolerate NULLs, so just be safe. */ 1159 tolerate NULLs, so just be safe. */
1160 if (exception2 == NULL) { 1160 if (exception2 == NULL) {
1161 exception2 = Py_None; 1161 exception2 = Py_None;
1162 Py_INCREF(exception2); 1162 Py_INCREF(exception2);
1163 } 1163 }
1164 if (v2 == NULL) { 1164 if (v2 == NULL) {
1165 v2 = Py_None; 1165 v2 = Py_None;
1166 Py_INCREF(v2); 1166 Py_INCREF(v2);
1167 } 1167 }
1168 if (Py_FlushLine()) 1168 if (Py_FlushLine())
1169 PyErr_Clear(); 1169 PyErr_Clear();
1170 fflush(stdout); 1170 fflush(stdout);
1171 PySys_WriteStderr("Error in sys.excepthook:\n"); 1171 PySys_WriteStderr("Error in sys.excepthook:\n");
1172 PyErr_Display(exception2, v2, tb2); 1172 PyErr_Display(exception2, v2, tb2);
1173 PySys_WriteStderr("\nOriginal exception was:\n"); 1173 PySys_WriteStderr("\nOriginal exception was:\n");
1174 PyErr_Display(exception, v, tb); 1174 PyErr_Display(exception, v, tb);
1175 Py_DECREF(exception2); 1175 Py_DECREF(exception2);
1176 Py_DECREF(v2); 1176 Py_DECREF(v2);
1177 Py_XDECREF(tb2); 1177 Py_XDECREF(tb2);
1178 } 1178 }
1179 Py_XDECREF(result); 1179 Py_XDECREF(result);
1180 Py_XDECREF(args); 1180 Py_XDECREF(args);
1181 } else { 1181 } else {
1182 PySys_WriteStderr("sys.excepthook is missing\n"); 1182 PySys_WriteStderr("sys.excepthook is missing\n");
1183 PyErr_Display(exception, v, tb); 1183 PyErr_Display(exception, v, tb);
1184 } 1184 }
1185 Py_XDECREF(exception); 1185 Py_XDECREF(exception);
1186 Py_XDECREF(v); 1186 Py_XDECREF(v);
1187 Py_XDECREF(tb); 1187 Py_XDECREF(tb);
1188 } 1188 }
1189 1189
1190 void 1190 void
1191 PyErr_Display(PyObject *exception, PyObject *value, PyObject *tb) 1191 PyErr_Display(PyObject *exception, PyObject *value, PyObject *tb)
1192 { 1192 {
1193 int err = 0; 1193 int err = 0;
1194 PyObject *f = PySys_GetObject("stderr"); 1194 PyObject *f = PySys_GetObject("stderr");
1195 Py_INCREF(value); 1195 Py_INCREF(value);
1196 if (f == NULL) 1196 if (f == NULL)
1197 fprintf(stderr, "lost sys.stderr\n"); 1197 fprintf(stderr, "lost sys.stderr\n");
1198 else { 1198 else {
1199 if (Py_FlushLine()) 1199 if (Py_FlushLine())
1200 PyErr_Clear(); 1200 PyErr_Clear();
1201 fflush(stdout); 1201 fflush(stdout);
1202 if (tb && tb != Py_None) 1202 if (tb && tb != Py_None)
1203 err = PyTraceBack_Print(tb, f); 1203 err = PyTraceBack_Print(tb, f);
1204 if (err == 0 && 1204 if (err == 0 &&
1205 PyObject_HasAttrString(value, "print_file_and_line")) 1205 PyObject_HasAttrString(value, "print_file_and_line"))
1206 { 1206 {
1207 PyObject *message; 1207 PyObject *message;
1208 const char *filename, *text; 1208 const char *filename, *text;
1209 int lineno, offset; 1209 int lineno, offset;
1210 if (!parse_syntax_error(value, &message, &filename, 1210 if (!parse_syntax_error(value, &message, &filename,
1211 &lineno, &offset, &text)) 1211 &lineno, &offset, &text))
1212 PyErr_Clear(); 1212 PyErr_Clear();
1213 else { 1213 else {
1214 char buf[10]; 1214 char buf[10];
1215 PyFile_WriteString(" File \"", f); 1215 PyFile_WriteString(" File \"", f);
1216 if (filename == NULL) 1216 if (filename == NULL)
1217 PyFile_WriteString("<string>", f); 1217 PyFile_WriteString("<string>", f);
1218 else 1218 else
1219 PyFile_WriteString(filename, f); 1219 PyFile_WriteString(filename, f);
1220 PyFile_WriteString("\", line ", f); 1220 PyFile_WriteString("\", line ", f);
1221 PyOS_snprintf(buf, sizeof(buf), "%d", lineno); 1221 PyOS_snprintf(buf, sizeof(buf), "%d", lineno);
1222 PyFile_WriteString(buf, f); 1222 PyFile_WriteString(buf, f);
1223 PyFile_WriteString("\n", f); 1223 PyFile_WriteString("\n", f);
1224 if (text != NULL) 1224 if (text != NULL)
1225 print_error_text(f, offset, text); 1225 print_error_text(f, offset, text);
1226 Py_DECREF(value); 1226 Py_DECREF(value);
1227 value = message; 1227 value = message;
1228 /* Can't be bothered to check all those 1228 /* Can't be bothered to check all those
1229 PyFile_WriteString() calls */ 1229 PyFile_WriteString() calls */
1230 if (PyErr_Occurred()) 1230 if (PyErr_Occurred())
1231 err = -1; 1231 err = -1;
1232 } 1232 }
1233 } 1233 }
1234 if (err) { 1234 if (err) {
1235 /* Don't do anything else */ 1235 /* Don't do anything else */
1236 } 1236 }
1237 else if (PyExceptionClass_Check(exception)) { 1237 else if (PyExceptionClass_Check(exception)) {
1238 PyObject* moduleName; 1238 PyObject* moduleName;
1239 char* className = PyExceptionClass_Name(exception); 1239 char* className = PyExceptionClass_Name(exception);
1240 if (className != NULL) { 1240 if (className != NULL) {
1241 char *dot = strrchr(className, '.'); 1241 char *dot = strrchr(className, '.');
1242 if (dot != NULL) 1242 if (dot != NULL)
1243 className = dot+1; 1243 className = dot+1;
1244 } 1244 }
1245 1245
1246 moduleName = PyObject_GetAttrString(exception, "__module__"); 1246 moduleName = PyObject_GetAttrString(exception, "__module__");
1247 if (moduleName == NULL) 1247 if (moduleName == NULL)
1248 err = PyFile_WriteString("<unknown>", f); 1248 err = PyFile_WriteString("<unknown>", f);
1249 else { 1249 else {
1250 char* modstr = PyString_AsString(moduleName); 1250 char* modstr = PyString_AsString(moduleName);
1251 if (modstr && strcmp(modstr, "exceptions")) 1251 if (modstr && strcmp(modstr, "exceptions"))
1252 { 1252 {
1253 err = PyFile_WriteString(modstr, f); 1253 err = PyFile_WriteString(modstr, f);
1254 err += PyFile_WriteString(".", f); 1254 err += PyFile_WriteString(".", f);
1255 } 1255 }
1256 Py_DECREF(moduleName); 1256 Py_DECREF(moduleName);
1257 } 1257 }
1258 if (err == 0) { 1258 if (err == 0) {
1259 if (className == NULL) 1259 if (className == NULL)
1260 err = PyFile_WriteString("<unknown>", f); 1260 err = PyFile_WriteString("<unknown>", f);
1261 else 1261 else
1262 err = PyFile_WriteString(className, f); 1262 err = PyFile_WriteString(className, f);
1263 } 1263 }
1264 } 1264 }
1265 else 1265 else
1266 err = PyFile_WriteObject(exception, f, Py_PRINT_RAW); 1266 err = PyFile_WriteObject(exception, f, Py_PRINT_RAW);
1267 if (err == 0 && (value != Py_None)) { 1267 if (err == 0 && (value != Py_None)) {
1268 PyObject *s = PyObject_Str(value); 1268 PyObject *s = PyObject_Str(value);
1269 /* only print colon if the str() of the 1269 /* only print colon if the str() of the
1270 object is not the empty string 1270 object is not the empty string
1271 */ 1271 */
1272 if (s == NULL) 1272 if (s == NULL)
1273 err = -1; 1273 err = -1;
1274 else if (!PyString_Check(s) || 1274 else if (!PyString_Check(s) ||
1275 PyString_GET_SIZE(s) != 0) 1275 PyString_GET_SIZE(s) != 0)
1276 err = PyFile_WriteString(": ", f); 1276 err = PyFile_WriteString(": ", f);
1277 if (err == 0) 1277 if (err == 0)
1278 err = PyFile_WriteObject(s, f, Py_PRINT_RAW); 1278 err = PyFile_WriteObject(s, f, Py_PRINT_RAW);
1279 Py_XDECREF(s); 1279 Py_XDECREF(s);
1280 } 1280 }
1281 /* try to write a newline in any case */ 1281 /* try to write a newline in any case */
1282 err += PyFile_WriteString("\n", f); 1282 err += PyFile_WriteString("\n", f);
1283 } 1283 }
1284 Py_DECREF(value); 1284 Py_DECREF(value);
1285 /* If an error happened here, don't show it. 1285 /* If an error happened here, don't show it.
1286 XXX This is wrong, but too many callers rely on this behavior. */ 1286 XXX This is wrong, but too many callers rely on this behavior. */
1287 if (err != 0) 1287 if (err != 0)
1288 PyErr_Clear(); 1288 PyErr_Clear();
1289 } 1289 }
1290 1290
1291 PyObject * 1291 PyObject *
1292 PyRun_StringFlags(const char *str, int start, PyObject *globals, 1292 PyRun_StringFlags(const char *str, int start, PyObject *globals,
1293 PyObject *locals, PyCompilerFlags *flags) 1293 PyObject *locals, PyCompilerFlags *flags)
1294 { 1294 {
1295 PyObject *ret = NULL; 1295 PyObject *ret = NULL;
1296 mod_ty mod; 1296 mod_ty mod;
1297 PyArena *arena = PyArena_New(); 1297 PyArena *arena = PyArena_New();
1298 if (arena == NULL) 1298 if (arena == NULL)
1299 return NULL; 1299 return NULL;
1300 1300
1301 mod = PyParser_ASTFromString(str, "<string>", start, flags, arena); 1301 mod = PyParser_ASTFromString(str, "<string>", start, flags, arena);
1302 if (mod != NULL) 1302 if (mod != NULL)
1303 ret = run_mod(mod, "<string>", globals, locals, flags, arena); 1303 ret = run_mod(mod, "<string>", globals, locals, flags, arena);
1304 PyArena_Free(arena); 1304 PyArena_Free(arena);
1305 return ret; 1305 return ret;
1306 } 1306 }
1307 1307
1308 PyObject * 1308 PyObject *
1309 PyRun_FileExFlags(FILE *fp, const char *filename, int start, PyObject *globals, 1309 PyRun_FileExFlags(FILE *fp, const char *filename, int start, PyObject *globals,
1310 PyObject *locals, int closeit, PyCompilerFlags *flags) 1310 PyObject *locals, int closeit, PyCompilerFlags *flags)
1311 { 1311 {
1312 PyObject *ret; 1312 PyObject *ret;
1313 mod_ty mod; 1313 mod_ty mod;
1314 PyArena *arena = PyArena_New(); 1314 PyArena *arena = PyArena_New();
1315 if (arena == NULL) 1315 if (arena == NULL)
1316 return NULL; 1316 return NULL;
1317 1317
1318 mod = PyParser_ASTFromFile(fp, filename, start, 0, 0, 1318 mod = PyParser_ASTFromFile(fp, filename, start, 0, 0,
1319 flags, NULL, arena); 1319 flags, NULL, arena);
1320 if (closeit) 1320 if (closeit)
1321 fclose(fp); 1321 fclose(fp);
1322 if (mod == NULL) { 1322 if (mod == NULL) {
1323 PyArena_Free(arena); 1323 PyArena_Free(arena);
1324 return NULL; 1324 return NULL;
1325 } 1325 }
1326 ret = run_mod(mod, filename, globals, locals, flags, arena); 1326 ret = run_mod(mod, filename, globals, locals, flags, arena);
1327 PyArena_Free(arena); 1327 PyArena_Free(arena);
1328 return ret; 1328 return ret;
1329 } 1329 }
1330 1330
1331 static PyObject * 1331 static PyObject *
1332 run_mod(mod_ty mod, const char *filename, PyObject *globals, PyObject *locals, 1332 run_mod(mod_ty mod, const char *filename, PyObject *globals, PyObject *locals,
1333 PyCompilerFlags *flags, PyArena *arena) 1333 PyCompilerFlags *flags, PyArena *arena)
1334 { 1334 {
1335 PyCodeObject *co; 1335 PyCodeObject *co;
1336 PyObject *v; 1336 PyObject *v;
1337 co = PyAST_Compile(mod, filename, flags, arena); 1337 co = PyAST_Compile(mod, filename, flags, arena);
1338 if (co == NULL) 1338 if (co == NULL)
1339 return NULL; 1339 return NULL;
1340 v = PyEval_EvalCode(co, globals, locals); 1340 v = PyEval_EvalCode(co, globals, locals);
1341 Py_DECREF(co); 1341 Py_DECREF(co);
1342 return v; 1342 return v;
1343 } 1343 }
1344 1344
1345 static PyObject * 1345 static PyObject *
1346 run_pyc_file(FILE *fp, const char *filename, PyObject *globals, 1346 run_pyc_file(FILE *fp, const char *filename, PyObject *globals,
1347 PyObject *locals, PyCompilerFlags *flags) 1347 PyObject *locals, PyCompilerFlags *flags)
1348 { 1348 {
1349 PyCodeObject *co; 1349 PyCodeObject *co;
1350 PyObject *v; 1350 PyObject *v;
1351 long magic; 1351 long magic;
1352 long PyImport_GetMagicNumber(void); 1352 long PyImport_GetMagicNumber(void);
1353 1353
1354 magic = PyMarshal_ReadLongFromFile(fp); 1354 magic = PyMarshal_ReadLongFromFile(fp);
1355 if (magic != PyImport_GetMagicNumber()) { 1355 if (magic != PyImport_GetMagicNumber()) {
1356 PyErr_SetString(PyExc_RuntimeError, 1356 PyErr_SetString(PyExc_RuntimeError,
1357 "Bad magic number in .pyc file"); 1357 "Bad magic number in .pyc file");
1358 return NULL; 1358 return NULL;
1359 } 1359 }
1360 (void) PyMarshal_ReadLongFromFile(fp); 1360 (void) PyMarshal_ReadLongFromFile(fp);
1361 v = PyMarshal_ReadLastObjectFromFile(fp); 1361 v = PyMarshal_ReadLastObjectFromFile(fp);
1362 fclose(fp); 1362 fclose(fp);
1363 if (v == NULL || !PyCode_Check(v)) { 1363 if (v == NULL || !PyCode_Check(v)) {
1364 Py_XDECREF(v); 1364 Py_XDECREF(v);
1365 PyErr_SetString(PyExc_RuntimeError, 1365 PyErr_SetString(PyExc_RuntimeError,
1366 "Bad code object in .pyc file"); 1366 "Bad code object in .pyc file");
1367 return NULL; 1367 return NULL;
1368 } 1368 }
1369 co = (PyCodeObject *)v; 1369 co = (PyCodeObject *)v;
1370 v = PyEval_EvalCode(co, globals, locals); 1370 v = PyEval_EvalCode(co, globals, locals);
1371 if (v && flags) 1371 if (v && flags)
1372 flags->cf_flags |= (co->co_flags & PyCF_MASK); 1372 flags->cf_flags |= (co->co_flags & PyCF_MASK);
1373 Py_DECREF(co); 1373 Py_DECREF(co);
1374 return v; 1374 return v;
1375 } 1375 }
1376 1376
1377 PyObject * 1377 PyObject *
1378 Py_CompileStringFlags(const char *str, const char *filename, int start, 1378 Py_CompileStringFlags(const char *str, const char *filename, int start,
1379 PyCompilerFlags *flags) 1379 PyCompilerFlags *flags)
1380 { 1380 {
1381 PyCodeObject *co; 1381 PyCodeObject *co;
1382 mod_ty mod; 1382 mod_ty mod;
1383 PyArena *arena = PyArena_New(); 1383 PyArena *arena = PyArena_New();
1384 if (arena == NULL) 1384 if (arena == NULL)
1385 return NULL; 1385 return NULL;
1386 1386
1387 mod = PyParser_ASTFromString(str, filename, start, flags, arena); 1387 mod = PyParser_ASTFromString(str, filename, start, flags, arena);
1388 if (mod == NULL) { 1388 if (mod == NULL) {
1389 PyArena_Free(arena); 1389 PyArena_Free(arena);
1390 return NULL; 1390 return NULL;
1391 } 1391 }
1392 if (flags && (flags->cf_flags & PyCF_ONLY_AST)) { 1392 if (flags && (flags->cf_flags & PyCF_ONLY_AST)) {
1393 PyObject *result = PyAST_mod2obj(mod); 1393 PyObject *result = PyAST_mod2obj(mod);
1394 PyArena_Free(arena); 1394 PyArena_Free(arena);
1395 return result; 1395 return result;
1396 } 1396 }
1397 co = PyAST_Compile(mod, filename, flags, arena); 1397 co = PyAST_Compile(mod, filename, flags, arena);
1398 PyArena_Free(arena); 1398 PyArena_Free(arena);
1399 return (PyObject *)co; 1399 return (PyObject *)co;
1400 } 1400 }
1401 1401
1402 struct symtable * 1402 struct symtable *
1403 Py_SymtableString(const char *str, const char *filename, int start) 1403 Py_SymtableString(const char *str, const char *filename, int start)
1404 { 1404 {
1405 struct symtable *st; 1405 struct symtable *st;
1406 mod_ty mod; 1406 mod_ty mod;
1407 PyCompilerFlags flags; 1407 PyCompilerFlags flags;
1408 PyArena *arena = PyArena_New(); 1408 PyArena *arena = PyArena_New();
1409 if (arena == NULL) 1409 if (arena == NULL)
1410 return NULL; 1410 return NULL;
1411 1411
1412 flags.cf_flags = 0; 1412 flags.cf_flags = 0;
1413 1413
1414 mod = PyParser_ASTFromString(str, filename, start, &flags, arena); 1414 mod = PyParser_ASTFromString(str, filename, start, &flags, arena);
1415 if (mod == NULL) { 1415 if (mod == NULL) {
1416 PyArena_Free(arena); 1416 PyArena_Free(arena);
1417 return NULL; 1417 return NULL;
1418 } 1418 }
1419 st = PySymtable_Build(mod, filename, 0); 1419 st = PySymtable_Build(mod, filename, 0);
1420 PyArena_Free(arena); 1420 PyArena_Free(arena);
1421 return st; 1421 return st;
1422 } 1422 }
1423 1423
1424 /* Preferred access to parser is through AST. */ 1424 /* Preferred access to parser is through AST. */
1425 mod_ty 1425 mod_ty
1426 PyParser_ASTFromString(const char *s, const char *filename, int start, 1426 PyParser_ASTFromString(const char *s, const char *filename, int start,
1427 PyCompilerFlags *flags, PyArena *arena) 1427 PyCompilerFlags *flags, PyArena *arena)
1428 { 1428 {
1429 mod_ty mod; 1429 mod_ty mod;
1430 PyCompilerFlags localflags; 1430 PyCompilerFlags localflags;
1431 perrdetail err; 1431 perrdetail err;
1432 int iflags = PARSER_FLAGS(flags); 1432 int iflags = PARSER_FLAGS(flags);
1433 1433
1434 node *n = PyParser_ParseStringFlagsFilenameEx(s, filename, 1434 node *n = PyParser_ParseStringFlagsFilenameEx(s, filename,
1435 &_PyParser_Grammar, start, &err, 1435 &_PyParser_Grammar, start, &err,
1436 &iflags); 1436 &iflags);
1437 if (flags == NULL) { 1437 if (flags == NULL) {
1438 localflags.cf_flags = 0; 1438 localflags.cf_flags = 0;
1439 flags = &localflags; 1439 flags = &localflags;
1440 } 1440 }
1441 if (n) { 1441 if (n) {
1442 flags->cf_flags |= iflags & PyCF_MASK; 1442 flags->cf_flags |= iflags & PyCF_MASK;
1443 mod = PyAST_FromNode(n, flags, filename, arena); 1443 mod = PyAST_FromNode(n, flags, filename, arena);
1444 PyNode_Free(n); 1444 PyNode_Free(n);
1445 return mod; 1445 return mod;
1446 } 1446 }
1447 else { 1447 else {
1448 err_input(&err); 1448 err_input(&err);
1449 return NULL; 1449 return NULL;
1450 } 1450 }
1451 } 1451 }
1452 1452
1453 mod_ty 1453 mod_ty
1454 PyParser_ASTFromFile(FILE *fp, const char *filename, int start, char *ps1, 1454 PyParser_ASTFromFile(FILE *fp, const char *filename, int start, char *ps1,
1455 char *ps2, PyCompilerFlags *flags, int *errcode, 1455 char *ps2, PyCompilerFlags *flags, int *errcode,
1456 PyArena *arena) 1456 PyArena *arena)
1457 { 1457 {
1458 mod_ty mod; 1458 mod_ty mod;
1459 PyCompilerFlags localflags; 1459 PyCompilerFlags localflags;
1460 perrdetail err; 1460 perrdetail err;
1461 int iflags = PARSER_FLAGS(flags); 1461 int iflags = PARSER_FLAGS(flags);
1462 1462
1463 node *n = PyParser_ParseFileFlagsEx(fp, filename, &_PyParser_Grammar, 1463 node *n = PyParser_ParseFileFlagsEx(fp, filename, &_PyParser_Grammar,
1464 start, ps1, ps2, &err, &iflags); 1464 start, ps1, ps2, &err, &iflags);
1465 if (flags == NULL) { 1465 if (flags == NULL) {
1466 localflags.cf_flags = 0; 1466 localflags.cf_flags = 0;
1467 flags = &localflags; 1467 flags = &localflags;
1468 } 1468 }
1469 if (n) { 1469 if (n) {
1470 flags->cf_flags |= iflags & PyCF_MASK; 1470 flags->cf_flags |= iflags & PyCF_MASK;
1471 mod = PyAST_FromNode(n, flags, filename, arena); 1471 mod = PyAST_FromNode(n, flags, filename, arena);
1472 PyNode_Free(n); 1472 PyNode_Free(n);
1473 return mod; 1473 return mod;
1474 } 1474 }
1475 else { 1475 else {
1476 err_input(&err); 1476 err_input(&err);
1477 if (errcode) 1477 if (errcode)
1478 *errcode = err.error; 1478 *errcode = err.error;
1479 return NULL; 1479 return NULL;
1480 } 1480 }
1481 } 1481 }
1482 1482
1483 /* Simplified interface to parsefile -- return node or set exception */ 1483 /* Simplified interface to parsefile -- return node or set exception */
1484 1484
1485 node * 1485 node *
1486 PyParser_SimpleParseFileFlags(FILE *fp, const char *filename, int start, int flags) 1486 PyParser_SimpleParseFileFlags(FILE *fp, const char *filename, int start, int flags)
1487 { 1487 {
1488 perrdetail err; 1488 perrdetail err;
1489 node *n = PyParser_ParseFileFlags(fp, filename, &_PyParser_Grammar, 1489 node *n = PyParser_ParseFileFlags(fp, filename, &_PyParser_Grammar,
1490 start, NULL, NULL, &err, flags); 1490 start, NULL, NULL, &err, flags);
1491 if (n == NULL) 1491 if (n == NULL)
1492 err_input(&err); 1492 err_input(&err);
1493 1493
1494 return n; 1494 return n;
1495 } 1495 }
1496 1496
1497 /* Simplified interface to parsestring -- return node or set exception */ 1497 /* Simplified interface to parsestring -- return node or set exception */
1498 1498
1499 node * 1499 node *
1500 PyParser_SimpleParseStringFlags(const char *str, int start, int flags) 1500 PyParser_SimpleParseStringFlags(const char *str, int start, int flags)
1501 { 1501 {
1502 perrdetail err; 1502 perrdetail err;
1503 node *n = PyParser_ParseStringFlags(str, &_PyParser_Grammar, 1503 node *n = PyParser_ParseStringFlags(str, &_PyParser_Grammar,
1504 start, &err, flags); 1504 start, &err, flags);
1505 if (n == NULL) 1505 if (n == NULL)
1506 err_input(&err); 1506 err_input(&err);
1507 return n; 1507 return n;
1508 } 1508 }
1509 1509
1510 node * 1510 node *
1511 PyParser_SimpleParseStringFlagsFilename(const char *str, const char *filename, 1511 PyParser_SimpleParseStringFlagsFilename(const char *str, const char *filename,
1512 int start, int flags) 1512 int start, int flags)
1513 { 1513 {
1514 perrdetail err; 1514 perrdetail err;
1515 node *n = PyParser_ParseStringFlagsFilename(str, filename, 1515 node *n = PyParser_ParseStringFlagsFilename(str, filename,
1516 &_PyParser_Grammar, start, &err, flags); 1516 &_PyParser_Grammar, start, &err, flags);
1517 if (n == NULL) 1517 if (n == NULL)
1518 err_input(&err); 1518 err_input(&err);
1519 return n; 1519 return n;
1520 } 1520 }
1521 1521
1522 node * 1522 node *
1523 PyParser_SimpleParseStringFilename(const char *str, const char *filename, int start) 1523 PyParser_SimpleParseStringFilename(const char *str, const char *filename, int start)
1524 { 1524 {
1525 return PyParser_SimpleParseStringFlagsFilename(str, filename, start, 0); 1525 return PyParser_SimpleParseStringFlagsFilename(str, filename, start, 0);
1526 } 1526 }
1527 1527
1528 /* May want to move a more generalized form of this to parsetok.c or 1528 /* May want to move a more generalized form of this to parsetok.c or
1529 even parser modules. */ 1529 even parser modules. */
1530 1530
1531 void 1531 void
1532 PyParser_SetError(perrdetail *err) 1532 PyParser_SetError(perrdetail *err)
1533 { 1533 {
1534 err_input(err); 1534 err_input(err);
1535 } 1535 }
1536 1536
1537 /* Set the error appropriate to the given input error code (see errcode.h) */ 1537 /* Set the error appropriate to the given input error code (see errcode.h) */
1538 1538
1539 static void 1539 static void
1540 err_input(perrdetail *err) 1540 err_input(perrdetail *err)
1541 { 1541 {
1542 PyObject *v, *w, *errtype; 1542 PyObject *v, *w, *errtype;
1543 PyObject* u = NULL; 1543 PyObject* u = NULL;
1544 char *msg = NULL; 1544 char *msg = NULL;
1545 errtype = PyExc_SyntaxError; 1545 errtype = PyExc_SyntaxError;
1546 switch (err->error) { 1546 switch (err->error) {
1547 case E_ERROR: 1547 case E_ERROR:
1548 return; 1548 return;
1549 case E_SYNTAX: 1549 case E_SYNTAX:
1550 errtype = PyExc_IndentationError; 1550 errtype = PyExc_IndentationError;
1551 if (err->expected == INDENT) 1551 if (err->expected == INDENT)
1552 msg = "expected an indented block"; 1552 msg = "expected an indented block";
1553 else if (err->token == INDENT) 1553 else if (err->token == INDENT)
1554 msg = "unexpected indent"; 1554 msg = "unexpected indent";
1555 else if (err->token == DEDENT) 1555 else if (err->token == DEDENT)
1556 msg = "unexpected unindent"; 1556 msg = "unexpected unindent";
1557 else { 1557 else {
1558 errtype = PyExc_SyntaxError; 1558 errtype = PyExc_SyntaxError;
1559 msg = "invalid syntax"; 1559 msg = "invalid syntax";
1560 } 1560 }
1561 break; 1561 break;
1562 case E_TOKEN: 1562 case E_TOKEN:
1563 msg = "invalid token"; 1563 msg = "invalid token";
1564 break; 1564 break;
1565 case E_EOFS: 1565 case E_EOFS:
1566 msg = "EOF while scanning triple-quoted string literal"; 1566 msg = "EOF while scanning triple-quoted string literal";
1567 break; 1567 break;
1568 case E_EOLS: 1568 case E_EOLS:
1569 msg = "EOL while scanning string literal"; 1569 msg = "EOL while scanning string literal";
1570 break; 1570 break;
1571 case E_INTR: 1571 case E_INTR:
1572 if (!PyErr_Occurred()) 1572 if (!PyErr_Occurred())
1573 PyErr_SetNone(PyExc_KeyboardInterrupt); 1573 PyErr_SetNone(PyExc_KeyboardInterrupt);
1574 goto cleanup; 1574 goto cleanup;
1575 case E_NOMEM: 1575 case E_NOMEM:
1576 PyErr_NoMemory(); 1576 PyErr_NoMemory();
1577 goto cleanup; 1577 goto cleanup;
1578 case E_EOF: 1578 case E_EOF:
1579 msg = "unexpected EOF while parsing"; 1579 msg = "unexpected EOF while parsing";
1580 break; 1580 break;
1581 case E_TABSPACE: 1581 case E_TABSPACE:
1582 errtype = PyExc_TabError; 1582 errtype = PyExc_TabError;
1583 msg = "inconsistent use of tabs and spaces in indentation"; 1583 msg = "inconsistent use of tabs and spaces in indentation";
1584 break; 1584 break;
1585 case E_OVERFLOW: 1585 case E_OVERFLOW:
1586 msg = "expression too long"; 1586 msg = "expression too long";
1587 break; 1587 break;
1588 case E_DEDENT: 1588 case E_DEDENT:
1589 errtype = PyExc_IndentationError; 1589 errtype = PyExc_IndentationError;
1590 msg = "unindent does not match any outer indentation level"; 1590 msg = "unindent does not match any outer indentation level";
1591 break; 1591 break;
1592 case E_TOODEEP: 1592 case E_TOODEEP:
1593 errtype = PyExc_IndentationError; 1593 errtype = PyExc_IndentationError;
1594 msg = "too many levels of indentation"; 1594 msg = "too many levels of indentation";
1595 break; 1595 break;
1596 case E_DECODE: { 1596 case E_DECODE: {
1597 PyObject *type, *value, *tb; 1597 PyObject *type, *value, *tb;
1598 PyErr_Fetch(&type, &value, &tb); 1598 PyErr_Fetch(&type, &value, &tb);
1599 if (value != NULL) { 1599 if (value != NULL) {
1600 u = PyObject_Str(value); 1600 u = PyObject_Str(value);
1601 if (u != NULL) { 1601 if (u != NULL) {
1602 msg = PyString_AsString(u); 1602 msg = PyString_AsString(u);
1603 } 1603 }
1604 } 1604 }
1605 if (msg == NULL) 1605 if (msg == NULL)
1606 msg = "unknown decode error"; 1606 msg = "unknown decode error";
1607 Py_XDECREF(type); 1607 Py_XDECREF(type);
1608 Py_XDECREF(value); 1608 Py_XDECREF(value);
1609 Py_XDECREF(tb); 1609 Py_XDECREF(tb);
1610 break; 1610 break;
1611 } 1611 }
1612 case E_LINECONT: 1612 case E_LINECONT:
1613 msg = "unexpected character after line continuation character"; 1613 msg = "unexpected character after line continuation character";
1614 break; 1614 break;
1615 default: 1615 default:
1616 fprintf(stderr, "error=%d\n", err->error); 1616 fprintf(stderr, "error=%d\n", err->error);
1617 msg = "unknown parsing error"; 1617 msg = "unknown parsing error";
1618 break; 1618 break;
1619 } 1619 }
1620 v = Py_BuildValue("(ziiz)", err->filename, 1620 v = Py_BuildValue("(ziiz)", err->filename,
1621 err->lineno, err->offset, err->text); 1621 err->lineno, err->offset, err->text);
1622 w = NULL; 1622 w = NULL;
1623 if (v != NULL) 1623 if (v != NULL)
1624 w = Py_BuildValue("(sO)", msg, v); 1624 w = Py_BuildValue("(sO)", msg, v);
1625 Py_XDECREF(u); 1625 Py_XDECREF(u);
1626 Py_XDECREF(v); 1626 Py_XDECREF(v);
1627 PyErr_SetObject(errtype, w); 1627 PyErr_SetObject(errtype, w);
1628 Py_XDECREF(w); 1628 Py_XDECREF(w);
1629 cleanup: 1629 cleanup:
1630 if (err->text != NULL) { 1630 if (err->text != NULL) {
1631 PyObject_FREE(err->text); 1631 PyObject_FREE(err->text);
1632 err->text = NULL; 1632 err->text = NULL;
1633 } 1633 }
1634 } 1634 }
1635 1635
1636 /* Print fatal error message and abort */ 1636 /* Print fatal error message and abort */
1637 1637
1638 void 1638 void
1639 Py_FatalError(const char *msg) 1639 Py_FatalError(const char *msg)
1640 { 1640 {
1641 fprintf(stderr, "Fatal Python error: %s\n", msg); 1641 fprintf(stderr, "Fatal Python error: %s\n", msg);
1642 fflush(stderr); /* it helps in Windows debug build */ 1642 fflush(stderr); /* it helps in Windows debug build */
1643 1643
1644 #ifdef MS_WINDOWS 1644 #ifdef MS_WINDOWS
1645 { 1645 {
1646 size_t len = strlen(msg); 1646 size_t len = strlen(msg);
1647 WCHAR* buffer; 1647 WCHAR* buffer;
1648 size_t i; 1648 size_t i;
1649 1649
1650 /* Convert the message to wchar_t. This uses a simple one-to-one 1650 /* Convert the message to wchar_t. This uses a simple one-to-one
1651 conversion, assuming that the this error message actually uses ASCII 1651 conversion, assuming that the this error message actually uses ASCII
1652 only. If this ceases to be true, we will have to convert. */ 1652 only. If this ceases to be true, we will have to convert. */
1653 buffer = alloca( (len+1) * (sizeof *buffer)); 1653 buffer = alloca( (len+1) * (sizeof *buffer));
1654 for( i=0; i<=len; ++i) 1654 for( i=0; i<=len; ++i)
1655 buffer[i] = msg[i]; 1655 buffer[i] = msg[i];
1656 OutputDebugStringW(L"Fatal Python error: "); 1656 OutputDebugStringW(L"Fatal Python error: ");
1657 OutputDebugStringW(buffer); 1657 OutputDebugStringW(buffer);
1658 OutputDebugStringW(L"\n"); 1658 OutputDebugStringW(L"\n");
1659 } 1659 }
1660 #ifdef _DEBUG 1660 #ifdef _DEBUG
1661 DebugBreak(); 1661 DebugBreak();
1662 #endif 1662 #endif
1663 #endif /* MS_WINDOWS */ 1663 #endif /* MS_WINDOWS */
1664 abort(); 1664 abort();
1665 } 1665 }
1666 1666
1667 /* Clean up and exit */ 1667 /* Clean up and exit */
1668 1668
1669 #ifdef WITH_THREAD 1669 #ifdef WITH_THREAD
1676 "threading" threads have completed. */ 1676 "threading" threads have completed. */
1677 static void 1677 static void
1678 wait_for_thread_shutdown(void) 1678 wait_for_thread_shutdown(void)
1679 { 1679 {
1680 #ifdef WITH_THREAD 1680 #ifdef WITH_THREAD
1681 PyObject *result; 1681 PyObject *result;
1682 PyThreadState *tstate = PyThreadState_GET(); 1682 PyThreadState *tstate = PyThreadState_GET();
1683 PyObject *threading = PyMapping_GetItemString(tstate->interp->modules, 1683 PyObject *threading = PyMapping_GetItemString(tstate->interp->modules,
1684 "threading"); 1684 "threading");
1685 if (threading == NULL) { 1685 if (threading == NULL) {
1686 /* threading not imported */ 1686 /* threading not imported */
1687 PyErr_Clear(); 1687 PyErr_Clear();
1688 return; 1688 return;
1689 } 1689 }
1690 result = PyObject_CallMethod(threading, "_shutdown", ""); 1690 result = PyObject_CallMethod(threading, "_shutdown", "");
1691 if (result == NULL) 1691 if (result == NULL)
1692 PyErr_WriteUnraisable(threading); 1692 PyErr_WriteUnraisable(threading);
1693 else 1693 else
1694 Py_DECREF(result); 1694 Py_DECREF(result);
1695 Py_DECREF(threading); 1695 Py_DECREF(threading);
1696 #endif 1696 #endif
1697 } 1697 }
1698 1698
1699 #define NEXITFUNCS 32 1699 #define NEXITFUNCS 32
1700 static void (*exitfuncs[NEXITFUNCS])(void); 1700 static void (*exitfuncs[NEXITFUNCS])(void);
1701 static int nexitfuncs = 0; 1701 static int nexitfuncs = 0;
1702 1702
1703 int Py_AtExit(void (*func)(void)) 1703 int Py_AtExit(void (*func)(void))
1704 { 1704 {
1705 if (nexitfuncs >= NEXITFUNCS) 1705 if (nexitfuncs >= NEXITFUNCS)
1706 return -1; 1706 return -1;
1707 exitfuncs[nexitfuncs++] = func; 1707 exitfuncs[nexitfuncs++] = func;
1708 return 0; 1708 return 0;
1709 } 1709 }
1710 1710
1711 static void 1711 static void
1712 call_sys_exitfunc(void) 1712 call_sys_exitfunc(void)
1713 { 1713 {
1714 PyObject *exitfunc = PySys_GetObject("exitfunc"); 1714 PyObject *exitfunc = PySys_GetObject("exitfunc");
1715 1715
1716 if (exitfunc) { 1716 if (exitfunc) {
1717 PyObject *res; 1717 PyObject *res;
1718 Py_INCREF(exitfunc); 1718 Py_INCREF(exitfunc);
1719 PySys_SetObject("exitfunc", (PyObject *)NULL); 1719 PySys_SetObject("exitfunc", (PyObject *)NULL);
1720 res = PyEval_CallObject(exitfunc, (PyObject *)NULL); 1720 res = PyEval_CallObject(exitfunc, (PyObject *)NULL);
1721 if (res == NULL) { 1721 if (res == NULL) {
1722 if (!PyErr_ExceptionMatches(PyExc_SystemExit)) { 1722 if (!PyErr_ExceptionMatches(PyExc_SystemExit)) {
1723 PySys_WriteStderr("Error in sys.exitfunc:\n"); 1723 PySys_WriteStderr("Error in sys.exitfunc:\n");
1724 } 1724 }
1725 PyErr_Print(); 1725 PyErr_Print();
1726 } 1726 }
1727 Py_DECREF(exitfunc); 1727 Py_DECREF(exitfunc);
1728 } 1728 }
1729 1729
1730 if (Py_FlushLine()) 1730 if (Py_FlushLine())
1731 PyErr_Clear(); 1731 PyErr_Clear();
1732 } 1732 }
1733 1733
1734 static void 1734 static void
1735 call_ll_exitfuncs(void) 1735 call_ll_exitfuncs(void)
1736 { 1736 {
1737 while (nexitfuncs > 0) 1737 while (nexitfuncs > 0)
1738 (*exitfuncs[--nexitfuncs])(); 1738 (*exitfuncs[--nexitfuncs])();
1739 1739
1740 fflush(stdout); 1740 fflush(stdout);
1741 fflush(stderr); 1741 fflush(stderr);
1742 } 1742 }
1743 1743
1744 void 1744 void
1745 Py_Exit(int sts) 1745 Py_Exit(int sts)
1746 { 1746 {
1747 Py_Finalize(); 1747 Py_Finalize();
1748 1748
1749 exit(sts); 1749 exit(sts);
1750 } 1750 }
1751 1751
1752 static void 1752 static void
1753 initsigs(void) 1753 initsigs(void)
1754 { 1754 {
1755 #ifdef SIGPIPE 1755 #ifdef SIGPIPE
1756 PyOS_setsig(SIGPIPE, SIG_IGN); 1756 PyOS_setsig(SIGPIPE, SIG_IGN);
1757 #endif 1757 #endif
1758 #ifdef SIGXFZ 1758 #ifdef SIGXFZ
1759 PyOS_setsig(SIGXFZ, SIG_IGN); 1759 PyOS_setsig(SIGXFZ, SIG_IGN);
1760 #endif 1760 #endif
1761 #ifdef SIGXFSZ 1761 #ifdef SIGXFSZ
1762 PyOS_setsig(SIGXFSZ, SIG_IGN); 1762 PyOS_setsig(SIGXFSZ, SIG_IGN);
1763 #endif 1763 #endif
1764 PyOS_InitInterrupts(); /* May imply initsignal() */ 1764 PyOS_InitInterrupts(); /* May imply initsignal() */
1765 } 1765 }
1766 1766
1767 1767
1768 /* 1768 /*
1769 * The file descriptor fd is considered ``interactive'' if either 1769 * The file descriptor fd is considered ``interactive'' if either
1772 * the descriptor is NULL or "<stdin>" or "???". 1772 * the descriptor is NULL or "<stdin>" or "???".
1773 */ 1773 */
1774 int 1774 int
1775 Py_FdIsInteractive(FILE *fp, const char *filename) 1775 Py_FdIsInteractive(FILE *fp, const char *filename)
1776 { 1776 {
1777 if (isatty((int)fileno(fp))) 1777 if (isatty((int)fileno(fp)))
1778 return 1; 1778 return 1;
1779 if (!Py_InteractiveFlag) 1779 if (!Py_InteractiveFlag)
1780 return 0; 1780 return 0;
1781 return (filename == NULL) || 1781 return (filename == NULL) ||
1782 (strcmp(filename, "<stdin>") == 0) || 1782 (strcmp(filename, "<stdin>") == 0) ||
1783 (strcmp(filename, "???") == 0); 1783 (strcmp(filename, "???") == 0);
1784 } 1784 }
1785 1785
1786 1786
1787 #if defined(USE_STACKCHECK) 1787 #if defined(USE_STACKCHECK)
1788 #if defined(WIN32) && defined(_MSC_VER) 1788 #if defined(WIN32) && defined(_MSC_VER)
1796 * Return non-zero when we run out of memory on the stack; zero otherwise. 1796 * Return non-zero when we run out of memory on the stack; zero otherwise.
1797 */ 1797 */
1798 int 1798 int
1799 PyOS_CheckStack(void) 1799 PyOS_CheckStack(void)
1800 { 1800 {
1801 __try { 1801 __try {
1802 /* alloca throws a stack overflow exception if there's 1802 /* alloca throws a stack overflow exception if there's
1803 not enough space left on the stack */ 1803 not enough space left on the stack */
1804 alloca(PYOS_STACK_MARGIN * sizeof(void*)); 1804 alloca(PYOS_STACK_MARGIN * sizeof(void*));
1805 return 0; 1805 return 0;
1806 } __except (GetExceptionCode() == STATUS_STACK_OVERFLOW ? 1806 } __except (GetExceptionCode() == STATUS_STACK_OVERFLOW ?
1807 EXCEPTION_EXECUTE_HANDLER : 1807 EXCEPTION_EXECUTE_HANDLER :
1808 EXCEPTION_CONTINUE_SEARCH) { 1808 EXCEPTION_CONTINUE_SEARCH) {
1809 int errcode = _resetstkoflw(); 1809 int errcode = _resetstkoflw();
1810 if (errcode == 0) 1810 if (errcode == 0)
1811 { 1811 {
1812 Py_FatalError("Could not reset the stack!"); 1812 Py_FatalError("Could not reset the stack!");
1813 } 1813 }
1814 } 1814 }
1815 return 1; 1815 return 1;
1816 } 1816 }
1817 1817
1818 #endif /* WIN32 && _MSC_VER */ 1818 #endif /* WIN32 && _MSC_VER */
1819 1819
1820 /* Alternate implementations can be added here... */ 1820 /* Alternate implementations can be added here... */
1826 1826
1827 PyOS_sighandler_t 1827 PyOS_sighandler_t
1828 PyOS_getsig(int sig) 1828 PyOS_getsig(int sig)
1829 { 1829 {
1830 #ifdef HAVE_SIGACTION 1830 #ifdef HAVE_SIGACTION
1831 struct sigaction context; 1831 struct sigaction context;
1832 if (sigaction(sig, NULL, &context) == -1) 1832 if (sigaction(sig, NULL, &context) == -1)
1833 return SIG_ERR; 1833 return SIG_ERR;
1834 return context.sa_handler; 1834 return context.sa_handler;
1835 #else 1835 #else
1836 PyOS_sighandler_t handler; 1836 PyOS_sighandler_t handler;
1837 /* Special signal handling for the secure CRT in Visual Studio 2005 */ 1837 /* Special signal handling for the secure CRT in Visual Studio 2005 */
1838 #if defined(_MSC_VER) && _MSC_VER >= 1400 1838 #if defined(_MSC_VER) && _MSC_VER >= 1400
1839 switch (sig) { 1839 switch (sig) {
1840 /* Only these signals are valid */ 1840 /* Only these signals are valid */
1841 case SIGINT: 1841 case SIGINT:
1842 case SIGILL: 1842 case SIGILL:
1843 case SIGFPE: 1843 case SIGFPE:
1844 case SIGSEGV: 1844 case SIGSEGV:
1845 case SIGTERM: 1845 case SIGTERM:
1846 case SIGBREAK: 1846 case SIGBREAK:
1847 case SIGABRT: 1847 case SIGABRT:
1848 break; 1848 break;
1849 /* Don't call signal() with other values or it will assert */ 1849 /* Don't call signal() with other values or it will assert */
1850 default: 1850 default:
1851 return SIG_ERR; 1851 return SIG_ERR;
1852 } 1852 }
1853 #endif /* _MSC_VER && _MSC_VER >= 1400 */ 1853 #endif /* _MSC_VER && _MSC_VER >= 1400 */
1854 handler = signal(sig, SIG_IGN); 1854 handler = signal(sig, SIG_IGN);
1855 if (handler != SIG_ERR) 1855 if (handler != SIG_ERR)
1856 signal(sig, handler); 1856 signal(sig, handler);
1857 return handler; 1857 return handler;
1858 #endif 1858 #endif
1859 } 1859 }
1860 1860
1861 PyOS_sighandler_t 1861 PyOS_sighandler_t
1862 PyOS_setsig(int sig, PyOS_sighandler_t handler) 1862 PyOS_setsig(int sig, PyOS_sighandler_t handler)
1863 { 1863 {
1864 #ifdef HAVE_SIGACTION 1864 #ifdef HAVE_SIGACTION
1865 /* Some code in Modules/signalmodule.c depends on sigaction() being 1865 /* Some code in Modules/signalmodule.c depends on sigaction() being
1866 * used here if HAVE_SIGACTION is defined. Fix that if this code 1866 * used here if HAVE_SIGACTION is defined. Fix that if this code
1867 * changes to invalidate that assumption. 1867 * changes to invalidate that assumption.
1868 */ 1868 */
1869 struct sigaction context, ocontext; 1869 struct sigaction context, ocontext;
1870 context.sa_handler = handler; 1870 context.sa_handler = handler;
1871 sigemptyset(&context.sa_mask); 1871 sigemptyset(&context.sa_mask);
1872 context.sa_flags = 0; 1872 context.sa_flags = 0;
1873 if (sigaction(sig, &context, &ocontext) == -1) 1873 if (sigaction(sig, &context, &ocontext) == -1)
1874 return SIG_ERR; 1874 return SIG_ERR;
1875 return ocontext.sa_handler; 1875 return ocontext.sa_handler;
1876 #else 1876 #else
1877 PyOS_sighandler_t oldhandler; 1877 PyOS_sighandler_t oldhandler;
1878 oldhandler = signal(sig, handler); 1878 oldhandler = signal(sig, handler);
1879 #ifdef HAVE_SIGINTERRUPT 1879 #ifdef HAVE_SIGINTERRUPT
1880 siginterrupt(sig, 1); 1880 siginterrupt(sig, 1);
1881 #endif 1881 #endif
1882 return oldhandler; 1882 return oldhandler;
1883 #endif 1883 #endif
1884 } 1884 }
1885 1885
1886 /* Deprecated C API functions still provided for binary compatiblity */ 1886 /* Deprecated C API functions still provided for binary compatiblity */
1887 1887
1888 #undef PyParser_SimpleParseFile 1888 #undef PyParser_SimpleParseFile
1889 PyAPI_FUNC(node *) 1889 PyAPI_FUNC(node *)
1890 PyParser_SimpleParseFile(FILE *fp, const char *filename, int start) 1890 PyParser_SimpleParseFile(FILE *fp, const char *filename, int start)
1891 { 1891 {
1892 return PyParser_SimpleParseFileFlags(fp, filename, start, 0); 1892 return PyParser_SimpleParseFileFlags(fp, filename, start, 0);
1893 } 1893 }
1894 1894
1895 #undef PyParser_SimpleParseString 1895 #undef PyParser_SimpleParseString
1896 PyAPI_FUNC(node *) 1896 PyAPI_FUNC(node *)
1897 PyParser_SimpleParseString(const char *str, int start) 1897 PyParser_SimpleParseString(const char *str, int start)
1898 { 1898 {
1899 return PyParser_SimpleParseStringFlags(str, start, 0); 1899 return PyParser_SimpleParseStringFlags(str, start, 0);
1900 } 1900 }
1901 1901
1902 #undef PyRun_AnyFile 1902 #undef PyRun_AnyFile
1903 PyAPI_FUNC(int) 1903 PyAPI_FUNC(int)
1904 PyRun_AnyFile(FILE *fp, const char *name) 1904 PyRun_AnyFile(FILE *fp, const char *name)
1905 { 1905 {
1906 return PyRun_AnyFileExFlags(fp, name, 0, NULL); 1906 return PyRun_AnyFileExFlags(fp, name, 0, NULL);
1907 } 1907 }
1908 1908
1909 #undef PyRun_AnyFileEx 1909 #undef PyRun_AnyFileEx
1910 PyAPI_FUNC(int) 1910 PyAPI_FUNC(int)
1911 PyRun_AnyFileEx(FILE *fp, const char *name, int closeit) 1911 PyRun_AnyFileEx(FILE *fp, const char *name, int closeit)
1912 { 1912 {
1913 return PyRun_AnyFileExFlags(fp, name, closeit, NULL); 1913 return PyRun_AnyFileExFlags(fp, name, closeit, NULL);
1914 } 1914 }
1915 1915
1916 #undef PyRun_AnyFileFlags 1916 #undef PyRun_AnyFileFlags
1917 PyAPI_FUNC(int) 1917 PyAPI_FUNC(int)
1918 PyRun_AnyFileFlags(FILE *fp, const char *name, PyCompilerFlags *flags) 1918 PyRun_AnyFileFlags(FILE *fp, const char *name, PyCompilerFlags *flags)
1919 { 1919 {
1920 return PyRun_AnyFileExFlags(fp, name, 0, flags); 1920 return PyRun_AnyFileExFlags(fp, name, 0, flags);
1921 } 1921 }
1922 1922
1923 #undef PyRun_File 1923 #undef PyRun_File
1924 PyAPI_FUNC(PyObject *) 1924 PyAPI_FUNC(PyObject *)
1925 PyRun_File(FILE *fp, const char *p, int s, PyObject *g, PyObject *l) 1925 PyRun_File(FILE *fp, const char *p, int s, PyObject *g, PyObject *l)
1926 { 1926 {
1927 return PyRun_FileExFlags(fp, p, s, g, l, 0, NULL); 1927 return PyRun_FileExFlags(fp, p, s, g, l, 0, NULL);
1928 } 1928 }
1929 1929
1930 #undef PyRun_FileEx 1930 #undef PyRun_FileEx
1931 PyAPI_FUNC(PyObject *) 1931 PyAPI_FUNC(PyObject *)
1932 PyRun_FileEx(FILE *fp, const char *p, int s, PyObject *g, PyObject *l, int c) 1932 PyRun_FileEx(FILE *fp, const char *p, int s, PyObject *g, PyObject *l, int c)
1933 { 1933 {
1934 return PyRun_FileExFlags(fp, p, s, g, l, c, NULL); 1934 return PyRun_FileExFlags(fp, p, s, g, l, c, NULL);
1935 } 1935 }
1936 1936
1937 #undef PyRun_FileFlags 1937 #undef PyRun_FileFlags
1938 PyAPI_FUNC(PyObject *) 1938 PyAPI_FUNC(PyObject *)
1939 PyRun_FileFlags(FILE *fp, const char *p, int s, PyObject *g, PyObject *l, 1939 PyRun_FileFlags(FILE *fp, const char *p, int s, PyObject *g, PyObject *l,
1940 PyCompilerFlags *flags) 1940 PyCompilerFlags *flags)
1941 { 1941 {
1942 return PyRun_FileExFlags(fp, p, s, g, l, 0, flags); 1942 return PyRun_FileExFlags(fp, p, s, g, l, 0, flags);
1943 } 1943 }
1944 1944
1945 #undef PyRun_SimpleFile 1945 #undef PyRun_SimpleFile
1946 PyAPI_FUNC(int) 1946 PyAPI_FUNC(int)
1947 PyRun_SimpleFile(FILE *f, const char *p) 1947 PyRun_SimpleFile(FILE *f, const char *p)
1948 { 1948 {
1949 return PyRun_SimpleFileExFlags(f, p, 0, NULL); 1949 return PyRun_SimpleFileExFlags(f, p, 0, NULL);
1950 } 1950 }
1951 1951
1952 #undef PyRun_SimpleFileEx 1952 #undef PyRun_SimpleFileEx
1953 PyAPI_FUNC(int) 1953 PyAPI_FUNC(int)
1954 PyRun_SimpleFileEx(FILE *f, const char *p, int c) 1954 PyRun_SimpleFileEx(FILE *f, const char *p, int c)
1955 { 1955 {
1956 return PyRun_SimpleFileExFlags(f, p, c, NULL); 1956 return PyRun_SimpleFileExFlags(f, p, c, NULL);
1957 } 1957 }
1958 1958
1959 1959
1960 #undef PyRun_String 1960 #undef PyRun_String
1961 PyAPI_FUNC(PyObject *) 1961 PyAPI_FUNC(PyObject *)
1962 PyRun_String(const char *str, int s, PyObject *g, PyObject *l) 1962 PyRun_String(const char *str, int s, PyObject *g, PyObject *l)
1963 { 1963 {
1964 return PyRun_StringFlags(str, s, g, l, NULL); 1964 return PyRun_StringFlags(str, s, g, l, NULL);
1965 } 1965 }
1966 1966
1967 #undef PyRun_SimpleString 1967 #undef PyRun_SimpleString
1968 PyAPI_FUNC(int) 1968 PyAPI_FUNC(int)
1969 PyRun_SimpleString(const char *s) 1969 PyRun_SimpleString(const char *s)
1970 { 1970 {
1971 return PyRun_SimpleStringFlags(s, NULL); 1971 return PyRun_SimpleStringFlags(s, NULL);
1972 } 1972 }
1973 1973
1974 #undef Py_CompileString 1974 #undef Py_CompileString
1975 PyAPI_FUNC(PyObject *) 1975 PyAPI_FUNC(PyObject *)
1976 Py_CompileString(const char *str, const char *p, int s) 1976 Py_CompileString(const char *str, const char *p, int s)
1977 { 1977 {
1978 return Py_CompileStringFlags(str, p, s, NULL); 1978 return Py_CompileStringFlags(str, p, s, NULL);
1979 } 1979 }
1980 1980
1981 #undef PyRun_InteractiveOne 1981 #undef PyRun_InteractiveOne
1982 PyAPI_FUNC(int) 1982 PyAPI_FUNC(int)
1983 PyRun_InteractiveOne(FILE *f, const char *p) 1983 PyRun_InteractiveOne(FILE *f, const char *p)
1984 { 1984 {
1985 return PyRun_InteractiveOneFlags(f, p, NULL); 1985 return PyRun_InteractiveOneFlags(f, p, NULL);
1986 } 1986 }
1987 1987
1988 #undef PyRun_InteractiveLoop 1988 #undef PyRun_InteractiveLoop
1989 PyAPI_FUNC(int) 1989 PyAPI_FUNC(int)
1990 PyRun_InteractiveLoop(FILE *f, const char *p) 1990 PyRun_InteractiveLoop(FILE *f, const char *p)
1991 { 1991 {
1992 return PyRun_InteractiveLoopFlags(f, p, NULL); 1992 return PyRun_InteractiveLoopFlags(f, p, NULL);
1993 } 1993 }
1994 1994
1995 #ifdef __cplusplus 1995 #ifdef __cplusplus
1996 } 1996 }
1997 #endif 1997 #endif