comparison Python/pythonrun.c @ 61465:e38335d4df91 2.6

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