Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(170016)

Delta Between Two Patch Sets: Python/pythonrun.c

Issue 11619: On Windows, don't encode filenames in the import machinery
Left Patch Set: Created 8 years, 7 months ago
Right Patch Set: Created 6 years, 3 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « Python/future.c ('k') | Python/symtable.c » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 1
2 /* Python interpreter top-level routines, including init/exit */ 2 /* Python interpreter top-level routines, including init/exit */
3 3
4 #include "Python.h" 4 #include "Python.h"
5 5
6 #include "Python-ast.h" 6 #include "Python-ast.h"
7 #undef Yield /* undefine macro conflicting with winbase.h */ 7 #undef Yield /* undefine macro conflicting with winbase.h */
8 #include "grammar.h" 8 #include "grammar.h"
9 #include "node.h" 9 #include "node.h"
10 #include "token.h" 10 #include "token.h"
(...skipping 17 matching lines...) Expand all
28 #include <locale.h> 28 #include <locale.h>
29 #include <langinfo.h> 29 #include <langinfo.h>
30 #endif 30 #endif
31 31
32 #ifdef MS_WINDOWS 32 #ifdef MS_WINDOWS
33 #undef BYTE 33 #undef BYTE
34 #include "windows.h" 34 #include "windows.h"
35 #define PATH_MAX MAXPATHLEN 35 #define PATH_MAX MAXPATHLEN
36 #endif 36 #endif
37 37
38 #ifdef Py_REF_DEBUG
39 static
40 void _print_total_refs(void) {
41 PyObject *xoptions, *key, *value;
42 xoptions = PySys_GetXOptions();
43 if (xoptions == NULL)
44 return;
45 key = PyUnicode_FromString("showrefcount");
46 if (key == NULL)
47 return;
48 value = PyDict_GetItem(xoptions, key);
49 Py_DECREF(key);
50 if (value == Py_True)
51 fprintf(stderr,
52 "[%" PY_FORMAT_SIZE_T "d refs, "
53 "%" PY_FORMAT_SIZE_T "d blocks]\n",
54 _Py_GetRefTotal(), _Py_GetAllocatedBlocks());
55 }
56 #endif
57
38 #ifndef Py_REF_DEBUG 58 #ifndef Py_REF_DEBUG
39 #define PRINT_TOTAL_REFS() 59 #define PRINT_TOTAL_REFS()
40 #else /* Py_REF_DEBUG */ 60 #else /* Py_REF_DEBUG */
41 #define PRINT_TOTAL_REFS() fprintf(stderr, \ 61 #define PRINT_TOTAL_REFS() _print_total_refs()
42 "[%" PY_FORMAT_SIZE_T "d refs]\n", \
43 _Py_GetRefTotal())
44 #endif 62 #endif
45 63
46 #ifdef __cplusplus 64 #ifdef __cplusplus
47 extern "C" { 65 extern "C" {
48 #endif 66 #endif
49 67
50 extern wchar_t *Py_GetPath(void); 68 extern wchar_t *Py_GetPath(void);
51 69
52 extern grammar _PyParser_Grammar; /* From graminit.c */ 70 extern grammar _PyParser_Grammar; /* From graminit.c */
53 71
54 /* Forward */ 72 /* Forward */
55 static void initmain(void); 73 static void initmain(PyInterpreterState *interp);
56 static void initfsencoding(void); 74 static int initfsencoding(PyInterpreterState *interp);
57 static void initsite(void); 75 static void initsite(void);
58 static int initstdio(void); 76 static int initstdio(void);
59 static void flush_io(void); 77 static void flush_io(void);
60 static PyObject *run_mod(mod_ty, const char *, PyObject *, PyObject *, 78 static PyObject *run_mod(mod_ty, const char *, PyObject *, PyObject *,
61 PyCompilerFlags *, PyArena *); 79 PyCompilerFlags *, PyArena *);
62 static PyObject *run_pyc_file(FILE *, const char *, PyObject *, PyObject *, 80 static PyObject *run_pyc_file(FILE *, const char *, PyObject *, PyObject *,
63 PyCompilerFlags *); 81 PyCompilerFlags *);
64 static void err_input(perrdetail *); 82 static void err_input(perrdetail *);
65 static void err_free(perrdetail *); 83 static void err_free(perrdetail *);
66 static void initsigs(void); 84 static void initsigs(void);
67 static void call_py_exitfuncs(void); 85 static void call_py_exitfuncs(void);
68 static void wait_for_thread_shutdown(void); 86 static void wait_for_thread_shutdown(void);
69 static void call_ll_exitfuncs(void); 87 static void call_ll_exitfuncs(void);
70 extern void _PyUnicode_Init(void); 88 extern int _PyUnicode_Init(void);
89 extern int _PyStructSequence_Init(void);
71 extern void _PyUnicode_Fini(void); 90 extern void _PyUnicode_Fini(void);
72 extern int _PyLong_Init(void); 91 extern int _PyLong_Init(void);
73 extern void PyLong_Fini(void); 92 extern void PyLong_Fini(void);
74 extern int _PyFaulthandler_Init(void); 93 extern int _PyFaulthandler_Init(void);
75 extern void _PyFaulthandler_Fini(void); 94 extern void _PyFaulthandler_Fini(void);
76 95
77 #ifdef WITH_THREAD 96 #ifdef WITH_THREAD
78 extern void _PyGILState_Init(PyInterpreterState *, PyThreadState *); 97 extern void _PyGILState_Init(PyInterpreterState *, PyThreadState *);
79 extern void _PyGILState_Fini(void); 98 extern void _PyGILState_Fini(void);
80 #endif /* WITH_THREAD */ 99 #endif /* WITH_THREAD */
81 100
82 int Py_DebugFlag; /* Needed by parser.c */ 101 int Py_DebugFlag; /* Needed by parser.c */
83 int Py_VerboseFlag; /* Needed by import.c */ 102 int Py_VerboseFlag; /* Needed by import.c */
84 int Py_QuietFlag; /* Needed by sysmodule.c */ 103 int Py_QuietFlag; /* Needed by sysmodule.c */
85 int Py_InteractiveFlag; /* Needed by Py_FdIsInteractive() below */ 104 int Py_InteractiveFlag; /* Needed by Py_FdIsInteractive() below */
86 int Py_InspectFlag; /* Needed to determine whether to exit at SystemError */ 105 int Py_InspectFlag; /* Needed to determine whether to exit at SystemExit */
87 int Py_NoSiteFlag; /* Suppress 'import site' */ 106 int Py_NoSiteFlag; /* Suppress 'import site' */
88 int Py_BytesWarningFlag; /* Warn on str(bytes) and str(buffer) */ 107 int Py_BytesWarningFlag; /* Warn on str(bytes) and str(buffer) */
89 int Py_DontWriteBytecodeFlag; /* Suppress writing bytecode files (*.py[co]) */ 108 int Py_DontWriteBytecodeFlag; /* Suppress writing bytecode files (*.py[co]) */
90 int Py_UseClassExceptionsFlag = 1; /* Needed by bltinmodule.c: deprecated */ 109 int Py_UseClassExceptionsFlag = 1; /* Needed by bltinmodule.c: deprecated */
91 int Py_FrozenFlag; /* Needed by getpath.c */ 110 int Py_FrozenFlag; /* Needed by getpath.c */
92 int Py_IgnoreEnvironmentFlag; /* e.g. PYTHONPATH, PYTHONHOME */ 111 int Py_IgnoreEnvironmentFlag; /* e.g. PYTHONPATH, PYTHONHOME */
93 int Py_NoUserSiteDirectory = 0; /* for -s and site.py */ 112 int Py_NoUserSiteDirectory = 0; /* for -s and site.py */
94 int Py_UnbufferedStdioFlag = 0; /* Unbuffered binary std{in,out,err} */ 113 int Py_UnbufferedStdioFlag = 0; /* Unbuffered binary std{in,out,err} */
114 int Py_HashRandomizationFlag = 0; /* for -R and PYTHONHASHSEED */
115 int Py_IsolatedFlag = 0; /* for -I, isolate from user's env */
116
117 PyThreadState *_Py_Finalizing = NULL;
95 118
96 /* PyModule_GetWarningsModule is no longer necessary as of 2.6 119 /* PyModule_GetWarningsModule is no longer necessary as of 2.6
97 since _warnings is builtin. This API should not be used. */ 120 since _warnings is builtin. This API should not be used. */
98 PyObject * 121 PyObject *
99 PyModule_GetWarningsModule(void) 122 PyModule_GetWarningsModule(void)
100 { 123 {
101 return PyImport_ImportModule("warnings"); 124 return PyImport_ImportModule("warnings");
102 } 125 }
103 126
104 static int initialized = 0; 127 static int initialized = 0;
(...skipping 27 matching lines...) Expand all
132 if (flag < 1) 155 if (flag < 1)
133 flag = 1; 156 flag = 1;
134 return flag; 157 return flag;
135 } 158 }
136 159
137 static char* 160 static char*
138 get_codec_name(const char *encoding) 161 get_codec_name(const char *encoding)
139 { 162 {
140 char *name_utf8, *name_str; 163 char *name_utf8, *name_str;
141 PyObject *codec, *name = NULL; 164 PyObject *codec, *name = NULL;
165 _Py_IDENTIFIER(name);
142 166
143 codec = _PyCodec_Lookup(encoding); 167 codec = _PyCodec_Lookup(encoding);
144 if (!codec) 168 if (!codec)
145 goto error; 169 goto error;
146 170
147 name = PyObject_GetAttrString(codec, "name"); 171 name = _PyObject_GetAttrId(codec, &PyId_name);
148 Py_CLEAR(codec); 172 Py_CLEAR(codec);
149 if (!name) 173 if (!name)
150 goto error; 174 goto error;
151 175
152 name_utf8 = _PyUnicode_AsString(name); 176 name_utf8 = _PyUnicode_AsString(name);
153 if (name_utf8 == NULL) 177 if (name_utf8 == NULL)
154 goto error; 178 goto error;
155 name_str = strdup(name_utf8); 179 name_str = _PyMem_RawStrdup(name_utf8);
156 Py_DECREF(name); 180 Py_DECREF(name);
157 if (name_str == NULL) { 181 if (name_str == NULL) {
158 PyErr_NoMemory(); 182 PyErr_NoMemory();
159 return NULL; 183 return NULL;
160 } 184 }
161 return name_str; 185 return name_str;
162 186
163 error: 187 error:
164 Py_XDECREF(codec); 188 Py_XDECREF(codec);
165 Py_XDECREF(name); 189 Py_XDECREF(name);
166 return NULL; 190 return NULL;
167 } 191 }
168 192
169 #if defined(HAVE_LANGINFO_H) && defined(CODESET)
170 static char* 193 static char*
171 get_codeset(void) 194 get_locale_encoding(void)
172 { 195 {
196 #ifdef MS_WINDOWS
197 char codepage[100];
198 PyOS_snprintf(codepage, sizeof(codepage), "cp%d", GetACP());
199 return get_codec_name(codepage);
200 #elif defined(HAVE_LANGINFO_H) && defined(CODESET)
173 char* codeset = nl_langinfo(CODESET); 201 char* codeset = nl_langinfo(CODESET);
174 if (!codeset || codeset[0] == '\0') { 202 if (!codeset || codeset[0] == '\0') {
175 PyErr_SetString(PyExc_ValueError, "CODESET is not set or empty"); 203 PyErr_SetString(PyExc_ValueError, "CODESET is not set or empty");
176 return NULL; 204 return NULL;
177 } 205 }
178 return get_codec_name(codeset); 206 return get_codec_name(codeset);
179 } 207 #else
180 #endif 208 PyErr_SetNone(PyExc_NotImplementedError);
209 return NULL;
210 #endif
211 }
212
213 static void
214 import_init(PyInterpreterState *interp, PyObject *sysmod)
215 {
216 PyObject *importlib;
217 PyObject *impmod;
218 PyObject *sys_modules;
219 PyObject *value;
220
221 /* Import _importlib through its frozen version, _frozen_importlib. */
222 if (PyImport_ImportFrozenModule("_frozen_importlib") <= 0) {
223 Py_FatalError("Py_Initialize: can't import _frozen_importlib");
224 }
225 else if (Py_VerboseFlag) {
226 PySys_FormatStderr("import _frozen_importlib # frozen\n");
227 }
228 importlib = PyImport_AddModule("_frozen_importlib");
229 if (importlib == NULL) {
230 Py_FatalError("Py_Initialize: couldn't get _frozen_importlib from "
231 "sys.modules");
232 }
233 interp->importlib = importlib;
234 Py_INCREF(interp->importlib);
235
236 /* Install _importlib as __import__ */
237 impmod = PyInit_imp();
238 if (impmod == NULL) {
239 Py_FatalError("Py_Initialize: can't import imp");
240 }
241 else if (Py_VerboseFlag) {
242 PySys_FormatStderr("import imp # builtin\n");
243 }
244 sys_modules = PyImport_GetModuleDict();
245 if (Py_VerboseFlag) {
246 PySys_FormatStderr("import sys # builtin\n");
247 }
248 if (PyDict_SetItemString(sys_modules, "_imp", impmod) < 0) {
249 Py_FatalError("Py_Initialize: can't save _imp to sys.modules");
250 }
251
252 value = PyObject_CallMethod(importlib, "_install", "OO", sysmod, impmod);
253 if (value == NULL) {
254 PyErr_Print();
255 Py_FatalError("Py_Initialize: importlib install failed");
256 }
257 Py_DECREF(value);
258 Py_DECREF(impmod);
259
260 _PyImportZip_Init();
261 }
262
181 263
182 void 264 void
183 Py_InitializeEx(int install_sigs) 265 _Py_InitializeEx_Private(int install_sigs, int install_importlib)
184 { 266 {
185 PyInterpreterState *interp; 267 PyInterpreterState *interp;
186 PyThreadState *tstate; 268 PyThreadState *tstate;
187 PyObject *bimod, *sysmod, *pstderr; 269 PyObject *bimod, *sysmod, *pstderr;
188 char *p; 270 char *p;
189 extern void _Py_ReadyTypes(void); 271 extern void _Py_ReadyTypes(void);
190 272
191 if (initialized) 273 if (initialized)
192 return; 274 return;
193 initialized = 1; 275 initialized = 1;
276 _Py_Finalizing = NULL;
194 277
195 #if defined(HAVE_LANGINFO_H) && defined(HAVE_SETLOCALE) 278 #if defined(HAVE_LANGINFO_H) && defined(HAVE_SETLOCALE)
196 /* Set up the LC_CTYPE locale, so we can obtain 279 /* Set up the LC_CTYPE locale, so we can obtain
197 the locale's charset without having to switch 280 the locale's charset without having to switch
198 locales. */ 281 locales. */
199 setlocale(LC_CTYPE, ""); 282 setlocale(LC_CTYPE, "");
200 #endif 283 #endif
201 284
202 if ((p = Py_GETENV("PYTHONDEBUG")) && *p != '\0') 285 if ((p = Py_GETENV("PYTHONDEBUG")) && *p != '\0')
203 Py_DebugFlag = add_flag(Py_DebugFlag, p); 286 Py_DebugFlag = add_flag(Py_DebugFlag, p);
204 if ((p = Py_GETENV("PYTHONVERBOSE")) && *p != '\0') 287 if ((p = Py_GETENV("PYTHONVERBOSE")) && *p != '\0')
205 Py_VerboseFlag = add_flag(Py_VerboseFlag, p); 288 Py_VerboseFlag = add_flag(Py_VerboseFlag, p);
206 if ((p = Py_GETENV("PYTHONOPTIMIZE")) && *p != '\0') 289 if ((p = Py_GETENV("PYTHONOPTIMIZE")) && *p != '\0')
207 Py_OptimizeFlag = add_flag(Py_OptimizeFlag, p); 290 Py_OptimizeFlag = add_flag(Py_OptimizeFlag, p);
208 if ((p = Py_GETENV("PYTHONDONTWRITEBYTECODE")) && *p != '\0') 291 if ((p = Py_GETENV("PYTHONDONTWRITEBYTECODE")) && *p != '\0')
209 Py_DontWriteBytecodeFlag = add_flag(Py_DontWriteBytecodeFlag, p); 292 Py_DontWriteBytecodeFlag = add_flag(Py_DontWriteBytecodeFlag, p);
293 /* The variable is only tested for existence here; _PyRandom_Init will
294 check its value further. */
295 if ((p = Py_GETENV("PYTHONHASHSEED")) && *p != '\0')
296 Py_HashRandomizationFlag = add_flag(Py_HashRandomizationFlag, p);
297
298 _PyRandom_Init();
210 299
211 interp = PyInterpreterState_New(); 300 interp = PyInterpreterState_New();
212 if (interp == NULL) 301 if (interp == NULL)
213 Py_FatalError("Py_Initialize: can't make first interpreter"); 302 Py_FatalError("Py_Initialize: can't make first interpreter");
214 303
215 tstate = PyThreadState_New(interp); 304 tstate = PyThreadState_New(interp);
216 if (tstate == NULL) 305 if (tstate == NULL)
217 Py_FatalError("Py_Initialize: can't make first thread"); 306 Py_FatalError("Py_Initialize: can't make first thread");
218 (void) PyThreadState_Swap(tstate); 307 (void) PyThreadState_Swap(tstate);
219 308
(...skipping 13 matching lines...) Expand all
233 322
234 if (!_PyFrame_Init()) 323 if (!_PyFrame_Init())
235 Py_FatalError("Py_Initialize: can't init frames"); 324 Py_FatalError("Py_Initialize: can't init frames");
236 325
237 if (!_PyLong_Init()) 326 if (!_PyLong_Init())
238 Py_FatalError("Py_Initialize: can't init longs"); 327 Py_FatalError("Py_Initialize: can't init longs");
239 328
240 if (!PyByteArray_Init()) 329 if (!PyByteArray_Init())
241 Py_FatalError("Py_Initialize: can't init bytearray"); 330 Py_FatalError("Py_Initialize: can't init bytearray");
242 331
243 _PyFloat_Init(); 332 if (!_PyFloat_Init())
333 Py_FatalError("Py_Initialize: can't init float");
244 334
245 interp->modules = PyDict_New(); 335 interp->modules = PyDict_New();
246 if (interp->modules == NULL) 336 if (interp->modules == NULL)
247 Py_FatalError("Py_Initialize: can't make modules dictionary"); 337 Py_FatalError("Py_Initialize: can't make modules dictionary");
248 interp->modules_reloading = PyDict_New();
249 if (interp->modules_reloading == NULL)
250 Py_FatalError("Py_Initialize: can't make modules_reloading dictionary");
251 338
252 /* Init Unicode implementation; relies on the codec registry */ 339 /* Init Unicode implementation; relies on the codec registry */
253 _PyUnicode_Init(); 340 if (_PyUnicode_Init() < 0)
341 Py_FatalError("Py_Initialize: can't initialize unicode");
342 if (_PyStructSequence_Init() < 0)
343 Py_FatalError("Py_Initialize: can't initialize structseq");
254 344
255 bimod = _PyBuiltin_Init(); 345 bimod = _PyBuiltin_Init();
256 if (bimod == NULL) 346 if (bimod == NULL)
257 Py_FatalError("Py_Initialize: can't initialize builtins modules"); 347 Py_FatalError("Py_Initialize: can't initialize builtins modules");
258 _PyImport_FixupBuiltin(bimod, "builtins"); 348 _PyImport_FixupBuiltin(bimod, "builtins");
259 interp->builtins = PyModule_GetDict(bimod); 349 interp->builtins = PyModule_GetDict(bimod);
260 if (interp->builtins == NULL) 350 if (interp->builtins == NULL)
261 Py_FatalError("Py_Initialize: can't initialize builtins dict"); 351 Py_FatalError("Py_Initialize: can't initialize builtins dict");
262 Py_INCREF(interp->builtins); 352 Py_INCREF(interp->builtins);
263 353
264 /* initialize builtin exceptions */ 354 /* initialize builtin exceptions */
265 _PyExc_Init(); 355 _PyExc_Init(bimod);
266 356
267 sysmod = _PySys_Init(); 357 sysmod = _PySys_Init();
268 if (sysmod == NULL) 358 if (sysmod == NULL)
269 Py_FatalError("Py_Initialize: can't initialize sys"); 359 Py_FatalError("Py_Initialize: can't initialize sys");
270 interp->sysdict = PyModule_GetDict(sysmod); 360 interp->sysdict = PyModule_GetDict(sysmod);
271 if (interp->sysdict == NULL) 361 if (interp->sysdict == NULL)
272 Py_FatalError("Py_Initialize: can't initialize sys dict"); 362 Py_FatalError("Py_Initialize: can't initialize sys dict");
273 Py_INCREF(interp->sysdict); 363 Py_INCREF(interp->sysdict);
274 _PyImport_FixupBuiltin(sysmod, "sys"); 364 _PyImport_FixupBuiltin(sysmod, "sys");
275 PySys_SetPath(Py_GetPath()); 365 PySys_SetPath(Py_GetPath());
276 PyDict_SetItemString(interp->sysdict, "modules", 366 PyDict_SetItemString(interp->sysdict, "modules",
277 interp->modules); 367 interp->modules);
278 368
279 /* Set up a preliminary stderr printer until we have enough 369 /* Set up a preliminary stderr printer until we have enough
280 infrastructure for the io module in place. */ 370 infrastructure for the io module in place. */
281 pstderr = PyFile_NewStdPrinter(fileno(stderr)); 371 pstderr = PyFile_NewStdPrinter(fileno(stderr));
282 if (pstderr == NULL) 372 if (pstderr == NULL)
283 Py_FatalError("Py_Initialize: can't set preliminary stderr"); 373 Py_FatalError("Py_Initialize: can't set preliminary stderr");
284 PySys_SetObject("stderr", pstderr); 374 PySys_SetObject("stderr", pstderr);
285 PySys_SetObject("__stderr__", pstderr); 375 PySys_SetObject("__stderr__", pstderr);
286 Py_DECREF(pstderr); 376 Py_DECREF(pstderr);
287 377
288 _PyImport_Init(); 378 _PyImport_Init();
289 379
290 _PyImportHooks_Init(); 380 _PyImportHooks_Init();
291 381
382 /* Initialize _warnings. */
383 _PyWarnings_Init();
384
385 if (!install_importlib)
386 return;
387
388 import_init(interp, sysmod);
389
292 /* initialize the faulthandler module */ 390 /* initialize the faulthandler module */
293 if (_PyFaulthandler_Init()) 391 if (_PyFaulthandler_Init())
294 Py_FatalError("Py_Initialize: can't initialize faulthandler"); 392 Py_FatalError("Py_Initialize: can't initialize faulthandler");
295 393
296 /* Initialize _warnings. */
297 _PyWarnings_Init();
298
299 _PyTime_Init(); 394 _PyTime_Init();
300 395
301 initfsencoding(); 396 if (initfsencoding(interp) < 0)
397 Py_FatalError("Py_Initialize: unable to load the file system codec");
302 398
303 if (install_sigs) 399 if (install_sigs)
304 initsigs(); /* Signal handling stuff, including initintr() */ 400 initsigs(); /* Signal handling stuff, including initintr() */
305 401
306 initmain(); /* Module __main__ */ 402 initmain(interp); /* Module __main__ */
307 if (initstdio() < 0) 403 if (initstdio() < 0)
308 Py_FatalError( 404 Py_FatalError(
309 "Py_Initialize: can't initialize sys standard streams"); 405 "Py_Initialize: can't initialize sys standard streams");
310 406
311 /* Initialize warnings. */ 407 /* Initialize warnings. */
312 if (PySys_HasWarnOptions()) { 408 if (PySys_HasWarnOptions()) {
313 PyObject *warnings_module = PyImport_ImportModule("warnings"); 409 PyObject *warnings_module = PyImport_ImportModule("warnings");
314 if (warnings_module == NULL) { 410 if (warnings_module == NULL) {
315 fprintf(stderr, "'import warnings' failed; traceback:\n"); 411 fprintf(stderr, "'import warnings' failed; traceback:\n");
316 PyErr_Print(); 412 PyErr_Print();
317 } 413 }
318 Py_XDECREF(warnings_module); 414 Py_XDECREF(warnings_module);
319 } 415 }
320 416
321 if (!Py_NoSiteFlag) 417 if (!Py_NoSiteFlag)
322 initsite(); /* Module site */ 418 initsite(); /* Module site */
323 } 419 }
324 420
325 void 421 void
422 Py_InitializeEx(int install_sigs)
423 {
424 _Py_InitializeEx_Private(install_sigs, 1);
425 }
426
427 void
326 Py_Initialize(void) 428 Py_Initialize(void)
327 { 429 {
328 Py_InitializeEx(1); 430 Py_InitializeEx(1);
329 } 431 }
330 432
331 433
332 #ifdef COUNT_ALLOCS 434 #ifdef COUNT_ALLOCS
333 extern void dump_counts(FILE*); 435 extern void dump_counts(FILE*);
334 #endif 436 #endif
335 437
336 /* Flush stdout and stderr */ 438 /* Flush stdout and stderr */
439
440 static int
441 file_is_closed(PyObject *fobj)
442 {
443 int r;
444 PyObject *tmp = PyObject_GetAttrString(fobj, "closed");
445 if (tmp == NULL) {
446 PyErr_Clear();
447 return 0;
448 }
449 r = PyObject_IsTrue(tmp);
450 Py_DECREF(tmp);
451 if (r < 0)
452 PyErr_Clear();
453 return r > 0;
454 }
337 455
338 static void 456 static void
339 flush_std_files(void) 457 flush_std_files(void)
340 { 458 {
341 PyObject *fout = PySys_GetObject("stdout"); 459 PyObject *fout = PySys_GetObject("stdout");
342 PyObject *ferr = PySys_GetObject("stderr"); 460 PyObject *ferr = PySys_GetObject("stderr");
343 PyObject *tmp; 461 PyObject *tmp;
344 462 _Py_IDENTIFIER(flush);
345 if (fout != NULL && fout != Py_None) { 463
346 tmp = PyObject_CallMethod(fout, "flush", ""); 464 if (fout != NULL && fout != Py_None && !file_is_closed(fout)) {
465 tmp = _PyObject_CallMethodId(fout, &PyId_flush, "");
347 if (tmp == NULL) 466 if (tmp == NULL)
348 PyErr_WriteUnraisable(fout); 467 PyErr_WriteUnraisable(fout);
349 else 468 else
350 Py_DECREF(tmp); 469 Py_DECREF(tmp);
351 } 470 }
352 471
353 if (ferr != NULL && ferr != Py_None) { 472 if (ferr != NULL && ferr != Py_None && !file_is_closed(ferr)) {
354 tmp = PyObject_CallMethod(ferr, "flush", ""); 473 tmp = _PyObject_CallMethodId(ferr, &PyId_flush, "");
355 if (tmp == NULL) 474 if (tmp == NULL)
356 PyErr_Clear(); 475 PyErr_Clear();
357 else 476 else
358 Py_DECREF(tmp); 477 Py_DECREF(tmp);
359 } 478 }
360 } 479 }
361 480
362 /* Undo the effect of Py_Initialize(). 481 /* Undo the effect of Py_Initialize().
363 482
364 Beware: if multiple interpreter and/or thread states exist, these 483 Beware: if multiple interpreter and/or thread states exist, these
(...skipping 22 matching lines...) Expand all
387 /* The interpreter is still entirely intact at this point, and the 506 /* The interpreter is still entirely intact at this point, and the
388 * exit funcs may be relying on that. In particular, if some thread 507 * exit funcs may be relying on that. In particular, if some thread
389 * or exit func is still waiting to do an import, the import machinery 508 * or exit func is still waiting to do an import, the import machinery
390 * expects Py_IsInitialized() to return true. So don't say the 509 * expects Py_IsInitialized() to return true. So don't say the
391 * interpreter is uninitialized until after the exit funcs have run. 510 * interpreter is uninitialized until after the exit funcs have run.
392 * Note that Threading.py uses an exit func to do a join on all the 511 * Note that Threading.py uses an exit func to do a join on all the
393 * threads created thru it, so this also protects pending imports in 512 * threads created thru it, so this also protects pending imports in
394 * the threads created via Threading. 513 * the threads created via Threading.
395 */ 514 */
396 call_py_exitfuncs(); 515 call_py_exitfuncs();
397 initialized = 0;
398
399 /* Flush stdout+stderr */
400 flush_std_files();
401 516
402 /* Get current thread state and interpreter pointer */ 517 /* Get current thread state and interpreter pointer */
403 tstate = PyThreadState_GET(); 518 tstate = PyThreadState_GET();
404 interp = tstate->interp; 519 interp = tstate->interp;
405 520
521 /* Remaining threads (e.g. daemon threads) will automatically exit
522 after taking the GIL (in PyEval_RestoreThread()). */
523 _Py_Finalizing = tstate;
524 initialized = 0;
525
526 /* Flush stdout+stderr */
527 flush_std_files();
528
406 /* Disable signal handling */ 529 /* Disable signal handling */
407 PyOS_FiniInterrupts(); 530 PyOS_FiniInterrupts();
408
409 /* Clear type lookup cache */
410 PyType_ClearCache();
411 531
412 /* Collect garbage. This may call finalizers; it's nice to call these 532 /* Collect garbage. This may call finalizers; it's nice to call these
413 * before all modules are destroyed. 533 * before all modules are destroyed.
414 * XXX If a __del__ or weakref callback is triggered here, and tries to 534 * XXX If a __del__ or weakref callback is triggered here, and tries to
415 * XXX import a module, bad things can happen, because Python no 535 * XXX import a module, bad things can happen, because Python no
416 * XXX longer believes it's initialized. 536 * XXX longer believes it's initialized.
417 * XXX Fatal Python error: Interpreter not initialized (version mismatch ?) 537 * XXX Fatal Python error: Interpreter not initialized (version mismatch ?)
418 * XXX is easy to provoke that way. I've also seen, e.g., 538 * XXX is easy to provoke that way. I've also seen, e.g.,
419 * XXX Exception exceptions.ImportError: 'No module named sha' 539 * XXX Exception exceptions.ImportError: 'No module named sha'
420 * XXX in <function callback at 0x008F5718> ignored 540 * XXX in <function callback at 0x008F5718> ignored
421 * XXX but I'm unclear on exactly how that one happens. In any case, 541 * XXX but I'm unclear on exactly how that one happens. In any case,
422 * XXX I haven't seen a real-life report of either of these. 542 * XXX I haven't seen a real-life report of either of these.
423 */ 543 */
424 PyGC_Collect(); 544 PyGC_Collect();
425 #ifdef COUNT_ALLOCS 545 #ifdef COUNT_ALLOCS
426 /* With COUNT_ALLOCS, it helps to run GC multiple times: 546 /* With COUNT_ALLOCS, it helps to run GC multiple times:
427 each collection might release some types from the type 547 each collection might release some types from the type
428 list, so they become garbage. */ 548 list, so they become garbage. */
429 while (PyGC_Collect() > 0) 549 while (PyGC_Collect() > 0)
430 /* nothing */; 550 /* nothing */;
431 #endif 551 #endif
432 /* We run this while most interpreter state is still alive, so that
433 debug information can be printed out */
434 _PyGC_Fini();
435
436 /* Destroy all modules */ 552 /* Destroy all modules */
437 PyImport_Cleanup(); 553 PyImport_Cleanup();
438 554
439 /* Flush stdout+stderr (again, in case more was printed) */ 555 /* Flush stdout+stderr (again, in case more was printed) */
440 flush_std_files(); 556 flush_std_files();
441 557
442 /* Collect final garbage. This disposes of cycles created by 558 /* Collect final garbage. This disposes of cycles created by
443 * new-style class definitions, for example. 559 * class definitions, for example.
444 * XXX This is disabled because it caused too many problems. If 560 * XXX This is disabled because it caused too many problems. If
445 * XXX a __del__ or weakref callback triggers here, Python code has 561 * XXX a __del__ or weakref callback triggers here, Python code has
446 * XXX a hard time running, because even the sys module has been 562 * XXX a hard time running, because even the sys module has been
447 * XXX cleared out (sys.stdout is gone, sys.excepthook is gone, etc). 563 * XXX cleared out (sys.stdout is gone, sys.excepthook is gone, etc).
448 * XXX One symptom is a sequence of information-free messages 564 * XXX One symptom is a sequence of information-free messages
449 * XXX coming from threads (if a __del__ or callback is invoked, 565 * XXX coming from threads (if a __del__ or callback is invoked,
450 * XXX other threads can execute too, and any exception they encounter 566 * XXX other threads can execute too, and any exception they encounter
451 * XXX triggers a comedy of errors as subsystem after subsystem 567 * XXX triggers a comedy of errors as subsystem after subsystem
452 * XXX fails to find what it *expects* to find in sys to help report 568 * XXX fails to find what it *expects* to find in sys to help report
453 * XXX the exception and consequent unexpected failures). I've also 569 * XXX the exception and consequent unexpected failures). I've also
454 * XXX seen segfaults then, after adding print statements to the 570 * XXX seen segfaults then, after adding print statements to the
455 * XXX Python code getting called. 571 * XXX Python code getting called.
456 */ 572 */
457 #if 0 573 #if 0
458 PyGC_Collect(); 574 PyGC_Collect();
459 #endif 575 #endif
460 576
461 /* Destroy the database used by _PyImport_{Fixup,Find}Extension */ 577 /* Destroy the database used by _PyImport_{Fixup,Find}Extension */
462 _PyImport_Fini(); 578 _PyImport_Fini();
463 579
580 /* Cleanup typeobject.c's internal caches. */
581 _PyType_Fini();
582
464 /* unload faulthandler module */ 583 /* unload faulthandler module */
465 _PyFaulthandler_Fini(); 584 _PyFaulthandler_Fini();
466 585
467 /* Debugging stuff */ 586 /* Debugging stuff */
468 #ifdef COUNT_ALLOCS 587 #ifdef COUNT_ALLOCS
469 dump_counts(stdout); 588 dump_counts(stdout);
470 #endif 589 #endif
471 590
472 PRINT_TOTAL_REFS(); 591 PRINT_TOTAL_REFS();
473 592
474 #ifdef Py_TRACE_REFS 593 #ifdef Py_TRACE_REFS
475 /* Display all objects still alive -- this can invoke arbitrary 594 /* Display all objects still alive -- this can invoke arbitrary
476 * __repr__ overrides, so requires a mostly-intact interpreter. 595 * __repr__ overrides, so requires a mostly-intact interpreter.
477 * Alas, a lot of stuff may still be alive now that will be cleaned 596 * Alas, a lot of stuff may still be alive now that will be cleaned
478 * up later. 597 * up later.
479 */ 598 */
480 if (Py_GETENV("PYTHONDUMPREFS")) 599 if (Py_GETENV("PYTHONDUMPREFS"))
481 _Py_PrintReferences(stderr); 600 _Py_PrintReferences(stderr);
482 #endif /* Py_TRACE_REFS */ 601 #endif /* Py_TRACE_REFS */
483 602
484 /* Clear interpreter state */ 603 /* Clear interpreter state and all thread states. */
485 PyInterpreterState_Clear(interp); 604 PyInterpreterState_Clear(interp);
486 605
487 /* Now we decref the exception classes. After this point nothing 606 /* Now we decref the exception classes. After this point nothing
488 can raise an exception. That's okay, because each Fini() method 607 can raise an exception. That's okay, because each Fini() method
489 below has been checked to make sure no exceptions are ever 608 below has been checked to make sure no exceptions are ever
490 raised. 609 raised.
491 */ 610 */
492 611
493 _PyExc_Fini(); 612 _PyExc_Fini();
494
495 /* Cleanup auto-thread-state */
496 #ifdef WITH_THREAD
497 _PyGILState_Fini();
498 #endif /* WITH_THREAD */
499
500 /* Delete current thread */
501 PyThreadState_Swap(NULL);
502 PyInterpreterState_Delete(interp);
503 613
504 /* Sundry finalizers */ 614 /* Sundry finalizers */
505 PyMethod_Fini(); 615 PyMethod_Fini();
506 PyFrame_Fini(); 616 PyFrame_Fini();
507 PyCFunction_Fini(); 617 PyCFunction_Fini();
508 PyTuple_Fini(); 618 PyTuple_Fini();
509 PyList_Fini(); 619 PyList_Fini();
510 PySet_Fini(); 620 PySet_Fini();
511 PyBytes_Fini(); 621 PyBytes_Fini();
512 PyByteArray_Fini(); 622 PyByteArray_Fini();
513 PyLong_Fini(); 623 PyLong_Fini();
514 PyFloat_Fini(); 624 PyFloat_Fini();
515 PyDict_Fini(); 625 PyDict_Fini();
626 PySlice_Fini();
627 _PyGC_Fini();
516 628
517 /* Cleanup Unicode implementation */ 629 /* Cleanup Unicode implementation */
518 _PyUnicode_Fini(); 630 _PyUnicode_Fini();
519 631
520 /* reset file system default encoding */ 632 /* reset file system default encoding */
521 if (!Py_HasFileSystemDefaultEncoding && Py_FileSystemDefaultEncoding) { 633 if (!Py_HasFileSystemDefaultEncoding && Py_FileSystemDefaultEncoding) {
522 free((char*)Py_FileSystemDefaultEncoding); 634 PyMem_RawFree((char*)Py_FileSystemDefaultEncoding);
523 Py_FileSystemDefaultEncoding = NULL; 635 Py_FileSystemDefaultEncoding = NULL;
524 } 636 }
525 637
526 /* XXX Still allocated: 638 /* XXX Still allocated:
527 - various static ad-hoc pointers to interned strings 639 - various static ad-hoc pointers to interned strings
528 - int and float free list blocks 640 - int and float free list blocks
529 - whatever various modules and libraries allocate 641 - whatever various modules and libraries allocate
530 */ 642 */
531 643
532 PyGrammar_RemoveAccelerators(&_PyParser_Grammar); 644 PyGrammar_RemoveAccelerators(&_PyParser_Grammar);
645
646 /* Cleanup auto-thread-state */
647 #ifdef WITH_THREAD
648 _PyGILState_Fini();
649 #endif /* WITH_THREAD */
650
651 /* Delete current thread. After this, many C API calls become crashy. */
652 PyThreadState_Swap(NULL);
653 PyInterpreterState_Delete(interp);
533 654
534 #ifdef Py_TRACE_REFS 655 #ifdef Py_TRACE_REFS
535 /* Display addresses (& refcnts) of all objects still alive. 656 /* Display addresses (& refcnts) of all objects still alive.
536 * An address can be used to find the repr of the object, printed 657 * An address can be used to find the repr of the object, printed
537 * above by _Py_PrintReferences. 658 * above by _Py_PrintReferences.
538 */ 659 */
539 if (Py_GETENV("PYTHONDUMPREFS")) 660 if (Py_GETENV("PYTHONDUMPREFS"))
540 _Py_PrintReferenceAddresses(stderr); 661 _Py_PrintReferenceAddresses(stderr);
541 #endif /* Py_TRACE_REFS */ 662 #endif /* Py_TRACE_REFS */
542 #ifdef PYMALLOC_DEBUG 663 #ifdef PYMALLOC_DEBUG
543 if (Py_GETENV("PYTHONMALLOCSTATS")) 664 if (Py_GETENV("PYTHONMALLOCSTATS"))
544 _PyObject_DebugMallocStats(); 665 _PyObject_DebugMallocStats(stderr);
545 #endif 666 #endif
546 667
547 call_ll_exitfuncs(); 668 call_ll_exitfuncs();
548 } 669 }
549 670
550 /* Create and initialize a new interpreter and thread, and return the 671 /* Create and initialize a new interpreter and thread, and return the
551 new thread. This requires that Py_Initialize() has been called 672 new thread. This requires that Py_Initialize() has been called
552 first. 673 first.
553 674
554 Unsuccessful initialization yields a NULL pointer. Note that *no* 675 Unsuccessful initialization yields a NULL pointer. Note that *no*
(...skipping 23 matching lines...) Expand all
578 if (tstate == NULL) { 699 if (tstate == NULL) {
579 PyInterpreterState_Delete(interp); 700 PyInterpreterState_Delete(interp);
580 return NULL; 701 return NULL;
581 } 702 }
582 703
583 save_tstate = PyThreadState_Swap(tstate); 704 save_tstate = PyThreadState_Swap(tstate);
584 705
585 /* XXX The following is lax in error checking */ 706 /* XXX The following is lax in error checking */
586 707
587 interp->modules = PyDict_New(); 708 interp->modules = PyDict_New();
588 interp->modules_reloading = PyDict_New();
589 709
590 bimod = _PyImport_FindBuiltin("builtins"); 710 bimod = _PyImport_FindBuiltin("builtins");
591 if (bimod != NULL) { 711 if (bimod != NULL) {
592 interp->builtins = PyModule_GetDict(bimod); 712 interp->builtins = PyModule_GetDict(bimod);
593 if (interp->builtins == NULL) 713 if (interp->builtins == NULL)
594 goto handle_error; 714 goto handle_error;
595 Py_INCREF(interp->builtins); 715 Py_INCREF(interp->builtins);
596 } 716 }
597 717
598 /* initialize builtin exceptions */ 718 /* initialize builtin exceptions */
599 _PyExc_Init(); 719 _PyExc_Init(bimod);
600 720
601 sysmod = _PyImport_FindBuiltin("sys"); 721 sysmod = _PyImport_FindBuiltin("sys");
602 if (bimod != NULL && sysmod != NULL) { 722 if (bimod != NULL && sysmod != NULL) {
603 PyObject *pstderr; 723 PyObject *pstderr;
724
604 interp->sysdict = PyModule_GetDict(sysmod); 725 interp->sysdict = PyModule_GetDict(sysmod);
605 if (interp->sysdict == NULL) 726 if (interp->sysdict == NULL)
606 goto handle_error; 727 goto handle_error;
607 Py_INCREF(interp->sysdict); 728 Py_INCREF(interp->sysdict);
608 PySys_SetPath(Py_GetPath()); 729 PySys_SetPath(Py_GetPath());
609 PyDict_SetItemString(interp->sysdict, "modules", 730 PyDict_SetItemString(interp->sysdict, "modules",
610 interp->modules); 731 interp->modules);
611 /* Set up a preliminary stderr printer until we have enough 732 /* Set up a preliminary stderr printer until we have enough
612 infrastructure for the io module in place. */ 733 infrastructure for the io module in place. */
613 pstderr = PyFile_NewStdPrinter(fileno(stderr)); 734 pstderr = PyFile_NewStdPrinter(fileno(stderr));
614 if (pstderr == NULL) 735 if (pstderr == NULL)
615 Py_FatalError("Py_Initialize: can't set preliminary stderr"); 736 Py_FatalError("Py_Initialize: can't set preliminary stderr");
616 PySys_SetObject("stderr", pstderr); 737 PySys_SetObject("stderr", pstderr);
617 PySys_SetObject("__stderr__", pstderr); 738 PySys_SetObject("__stderr__", pstderr);
618 Py_DECREF(pstderr); 739 Py_DECREF(pstderr);
619 740
620 _PyImportHooks_Init(); 741 _PyImportHooks_Init();
742
743 import_init(interp, sysmod);
744
745 if (initfsencoding(interp) < 0)
746 goto handle_error;
747
621 if (initstdio() < 0) 748 if (initstdio() < 0)
622 Py_FatalError( 749 Py_FatalError(
623 "Py_Initialize: can't initialize sys standard streams"); 750 "Py_Initialize: can't initialize sys standard streams");
624 initmain(); 751 initmain(interp);
625 if (!Py_NoSiteFlag) 752 if (!Py_NoSiteFlag)
626 initsite(); 753 initsite();
627 } 754 }
628 755
629 if (!PyErr_Occurred()) 756 if (!PyErr_Occurred())
630 return tstate; 757 return tstate;
631 758
632 handle_error: 759 handle_error:
633 /* Oops, it didn't work. Undo it all. */ 760 /* Oops, it didn't work. Undo it all. */
634 761
635 PyErr_Print(); 762 PyErr_PrintEx(0);
636 PyThreadState_Clear(tstate); 763 PyThreadState_Clear(tstate);
637 PyThreadState_Swap(save_tstate); 764 PyThreadState_Swap(save_tstate);
638 PyThreadState_Delete(tstate); 765 PyThreadState_Delete(tstate);
639 PyInterpreterState_Delete(interp); 766 PyInterpreterState_Delete(interp);
640 767
641 return NULL; 768 return NULL;
642 } 769 }
643 770
644 /* Delete an interpreter and its last thread. This requires that the 771 /* Delete an interpreter and its last thread. This requires that the
645 given thread state is current, that the thread has no remaining 772 given thread state is current, that the thread has no remaining
646 frames, and that it is its interpreter's only remaining thread. 773 frames, and that it is its interpreter's only remaining thread.
647 It is a fatal error to violate these constraints. 774 It is a fatal error to violate these constraints.
648 775
649 (Py_Finalize() doesn't have these constraints -- it zaps 776 (Py_Finalize() doesn't have these constraints -- it zaps
650 everything, regardless.) 777 everything, regardless.)
651 778
652 Locking: as above. 779 Locking: as above.
653 780
654 */ 781 */
655 782
656 void 783 void
657 Py_EndInterpreter(PyThreadState *tstate) 784 Py_EndInterpreter(PyThreadState *tstate)
658 { 785 {
659 PyInterpreterState *interp = tstate->interp; 786 PyInterpreterState *interp = tstate->interp;
660 787
661 if (tstate != PyThreadState_GET()) 788 if (tstate != PyThreadState_GET())
662 Py_FatalError("Py_EndInterpreter: thread is not current"); 789 Py_FatalError("Py_EndInterpreter: thread is not current");
663 if (tstate->frame != NULL) 790 if (tstate->frame != NULL)
664 Py_FatalError("Py_EndInterpreter: thread still has a frame"); 791 Py_FatalError("Py_EndInterpreter: thread still has a frame");
792
793 wait_for_thread_shutdown();
794
665 if (tstate != interp->tstate_head || tstate->next != NULL) 795 if (tstate != interp->tstate_head || tstate->next != NULL)
666 Py_FatalError("Py_EndInterpreter: not the last thread"); 796 Py_FatalError("Py_EndInterpreter: not the last thread");
667 797
668 PyImport_Cleanup(); 798 PyImport_Cleanup();
669 PyInterpreterState_Clear(interp); 799 PyInterpreterState_Clear(interp);
670 PyThreadState_Swap(NULL); 800 PyThreadState_Swap(NULL);
671 PyInterpreterState_Delete(interp); 801 PyInterpreterState_Delete(interp);
672 } 802 }
673 803
804 #ifdef MS_WINDOWS
674 static wchar_t *progname = L"python"; 805 static wchar_t *progname = L"python";
806 #else
807 static wchar_t *progname = L"python3";
808 #endif
675 809
676 void 810 void
677 Py_SetProgramName(wchar_t *pn) 811 Py_SetProgramName(wchar_t *pn)
678 { 812 {
679 if (pn && *pn) 813 if (pn && *pn)
680 progname = pn; 814 progname = pn;
681 } 815 }
682 816
683 wchar_t * 817 wchar_t *
684 Py_GetProgramName(void) 818 Py_GetProgramName(void)
(...skipping 22 matching lines...) Expand all
707 home = env_home; 841 home = env_home;
708 } 842 }
709 843
710 } 844 }
711 return home; 845 return home;
712 } 846 }
713 847
714 /* Create __main__ module */ 848 /* Create __main__ module */
715 849
716 static void 850 static void
717 initmain(void) 851 initmain(PyInterpreterState *interp)
718 { 852 {
719 PyObject *m, *d; 853 PyObject *m, *d, *loader;
720 m = PyImport_AddModule("__main__"); 854 m = PyImport_AddModule("__main__");
721 if (m == NULL) 855 if (m == NULL)
722 Py_FatalError("can't create __main__ module"); 856 Py_FatalError("can't create __main__ module");
723 d = PyModule_GetDict(m); 857 d = PyModule_GetDict(m);
724 if (PyDict_GetItemString(d, "__builtins__") == NULL) { 858 if (PyDict_GetItemString(d, "__builtins__") == NULL) {
725 PyObject *bimod = PyImport_ImportModule("builtins"); 859 PyObject *bimod = PyImport_ImportModule("builtins");
726 if (bimod == NULL || 860 if (bimod == NULL) {
727 PyDict_SetItemString(d, "__builtins__", bimod) != 0) 861 Py_FatalError("Failed to retrieve builtins module");
728 Py_FatalError("can't add __builtins__ to __main__"); 862 }
863 if (PyDict_SetItemString(d, "__builtins__", bimod) < 0) {
864 Py_FatalError("Failed to initialize __main__.__builtins__");
865 }
729 Py_DECREF(bimod); 866 Py_DECREF(bimod);
730 } 867 }
731 } 868 /* Main is a little special - imp.is_builtin("__main__") will return
732 869 * False, but BuiltinImporter is still the most appropriate initial
733 static void 870 * setting for its __loader__ attribute. A more suitable value will
734 initfsencoding(void) 871 * be set if __main__ gets further initialized later in the startup
872 * process.
873 */
874 loader = PyDict_GetItemString(d, "__loader__");
875 if (loader == NULL || loader == Py_None) {
876 PyObject *loader = PyObject_GetAttrString(interp->importlib,
877 "BuiltinImporter");
878 if (loader == NULL) {
879 Py_FatalError("Failed to retrieve BuiltinImporter");
880 }
881 if (PyDict_SetItemString(d, "__loader__", loader) < 0) {
882 Py_FatalError("Failed to initialize __main__.__loader__");
883 }
884 Py_DECREF(loader);
885 }
886 }
887
888 static int
889 initfsencoding(PyInterpreterState *interp)
735 { 890 {
736 PyObject *codec; 891 PyObject *codec;
737 #if defined(HAVE_LANGINFO_H) && defined(CODESET) 892
738 char *codeset = NULL; 893 if (Py_FileSystemDefaultEncoding == NULL)
739 894 {
740 if (Py_FileSystemDefaultEncoding == NULL) { 895 Py_FileSystemDefaultEncoding = get_locale_encoding();
741 /* On Unix, set the file system encoding according to the 896 if (Py_FileSystemDefaultEncoding == NULL)
742 user's preference, if the CODESET names a well-known
743 Python codec, and Py_FileSystemDefaultEncoding isn't
744 initialized by other means. */
745 codeset = get_codeset();
746 if (codeset == NULL)
747 Py_FatalError("Py_Initialize: Unable to get the locale encoding"); 897 Py_FatalError("Py_Initialize: Unable to get the locale encoding");
748 898
749 Py_FileSystemDefaultEncoding = codeset;
750 Py_HasFileSystemDefaultEncoding = 0; 899 Py_HasFileSystemDefaultEncoding = 0;
751 return; 900 interp->fscodec_initialized = 1;
752 } 901 return 0;
753 #endif 902 }
754 903
755 /* the encoding is mbcs, utf-8 or ascii */ 904 /* the encoding is mbcs, utf-8 or ascii */
756 codec = _PyCodec_Lookup(Py_FileSystemDefaultEncoding); 905 codec = _PyCodec_Lookup(Py_FileSystemDefaultEncoding);
757 if (!codec) { 906 if (!codec) {
758 /* Such error can only occurs in critical situations: no more 907 /* Such error can only occurs in critical situations: no more
759 * memory, import a module of the standard library failed, 908 * memory, import a module of the standard library failed,
760 * etc. */ 909 * etc. */
761 Py_FatalError("Py_Initialize: unable to load the file system codec"); 910 return -1;
762 } else { 911 }
763 Py_DECREF(codec); 912 Py_DECREF(codec);
764 } 913 interp->fscodec_initialized = 1;
914 return 0;
765 } 915 }
766 916
767 /* Import the site module (not into __main__ though) */ 917 /* Import the site module (not into __main__ though) */
768 918
769 static void 919 static void
770 initsite(void) 920 initsite(void)
771 { 921 {
772 PyObject *m; 922 PyObject *m;
773 m = PyImport_ImportModule("site"); 923 m = PyImport_ImportModule("site");
774 if (m == NULL) { 924 if (m == NULL) {
925 fprintf(stderr, "Failed to import the site module\n");
775 PyErr_Print(); 926 PyErr_Print();
776 Py_Finalize(); 927 Py_Finalize();
777 exit(1); 928 exit(1);
778 } 929 }
779 else { 930 else {
780 Py_DECREF(m); 931 Py_DECREF(m);
781 } 932 }
782 } 933 }
783 934
784 static PyObject* 935 static PyObject*
785 create_stdio(PyObject* io, 936 create_stdio(PyObject* io,
786 int fd, int write_mode, char* name, 937 int fd, int write_mode, char* name,
787 char* encoding, char* errors) 938 char* encoding, char* errors)
788 { 939 {
789 PyObject *buf = NULL, *stream = NULL, *text = NULL, *raw = NULL, *res; 940 PyObject *buf = NULL, *stream = NULL, *text = NULL, *raw = NULL, *res;
790 const char* mode; 941 const char* mode;
791 const char* newline; 942 const char* newline;
792 PyObject *line_buffering; 943 PyObject *line_buffering;
793 int buffering, isatty; 944 int buffering, isatty;
945 _Py_IDENTIFIER(open);
946 _Py_IDENTIFIER(isatty);
947 _Py_IDENTIFIER(TextIOWrapper);
948 _Py_IDENTIFIER(name);
949 _Py_IDENTIFIER(mode);
794 950
795 /* stdin is always opened in buffered mode, first because it shouldn't 951 /* stdin is always opened in buffered mode, first because it shouldn't
796 make a difference in common use cases, second because TextIOWrapper 952 make a difference in common use cases, second because TextIOWrapper
797 depends on the presence of a read1() method which only exists on 953 depends on the presence of a read1() method which only exists on
798 buffered streams. 954 buffered streams.
799 */ 955 */
800 if (Py_UnbufferedStdioFlag && write_mode) 956 if (Py_UnbufferedStdioFlag && write_mode)
801 buffering = 0; 957 buffering = 0;
802 else 958 else
803 buffering = -1; 959 buffering = -1;
804 if (write_mode) 960 if (write_mode)
805 mode = "wb"; 961 mode = "wb";
806 else 962 else
807 mode = "rb"; 963 mode = "rb";
808 buf = PyObject_CallMethod(io, "open", "isiOOOi", 964 buf = _PyObject_CallMethodId(io, &PyId_open, "isiOOOi",
809 fd, mode, buffering, 965 fd, mode, buffering,
810 Py_None, Py_None, Py_None, 0); 966 Py_None, Py_None, Py_None, 0);
811 if (buf == NULL) 967 if (buf == NULL)
812 goto error; 968 goto error;
813 969
814 if (buffering) { 970 if (buffering) {
815 raw = PyObject_GetAttrString(buf, "raw"); 971 _Py_IDENTIFIER(raw);
972 raw = _PyObject_GetAttrId(buf, &PyId_raw);
816 if (raw == NULL) 973 if (raw == NULL)
817 goto error; 974 goto error;
818 } 975 }
819 else { 976 else {
820 raw = buf; 977 raw = buf;
821 Py_INCREF(raw); 978 Py_INCREF(raw);
822 } 979 }
823 980
824 text = PyUnicode_FromString(name); 981 text = PyUnicode_FromString(name);
825 if (text == NULL || PyObject_SetAttrString(raw, "name", text) < 0) 982 if (text == NULL || _PyObject_SetAttrId(raw, &PyId_name, text) < 0)
826 goto error; 983 goto error;
827 res = PyObject_CallMethod(raw, "isatty", ""); 984 res = _PyObject_CallMethodId(raw, &PyId_isatty, "");
828 if (res == NULL) 985 if (res == NULL)
829 goto error; 986 goto error;
830 isatty = PyObject_IsTrue(res); 987 isatty = PyObject_IsTrue(res);
831 Py_DECREF(res); 988 Py_DECREF(res);
832 if (isatty == -1) 989 if (isatty == -1)
833 goto error; 990 goto error;
834 if (isatty || Py_UnbufferedStdioFlag) 991 if (isatty || Py_UnbufferedStdioFlag)
835 line_buffering = Py_True; 992 line_buffering = Py_True;
836 else 993 else
837 line_buffering = Py_False; 994 line_buffering = Py_False;
838 995
839 Py_CLEAR(raw); 996 Py_CLEAR(raw);
840 Py_CLEAR(text); 997 Py_CLEAR(text);
841 998
999 #ifdef MS_WINDOWS
1000 /* sys.stdin: enable universal newline mode, translate "\r\n" and "\r"
1001 newlines to "\n".
1002 sys.stdout and sys.stderr: translate "\n" to "\r\n". */
1003 newline = NULL;
1004 #else
1005 /* sys.stdin: split lines at "\n".
1006 sys.stdout and sys.stderr: don't translate newlines (use "\n"). */
842 newline = "\n"; 1007 newline = "\n";
843 #ifdef MS_WINDOWS 1008 #endif
844 if (!write_mode) { 1009
845 /* translate \r\n to \n for sys.stdin on Windows */ 1010 stream = _PyObject_CallMethodId(io, &PyId_TextIOWrapper, "OsssO",
846 newline = NULL; 1011 buf, encoding, errors,
847 } 1012 newline, line_buffering);
848 #endif
849
850 stream = PyObject_CallMethod(io, "TextIOWrapper", "OsssO",
851 buf, encoding, errors,
852 newline, line_buffering);
853 Py_CLEAR(buf); 1013 Py_CLEAR(buf);
854 if (stream == NULL) 1014 if (stream == NULL)
855 goto error; 1015 goto error;
856 1016
857 if (write_mode) 1017 if (write_mode)
858 mode = "w"; 1018 mode = "w";
859 else 1019 else
860 mode = "r"; 1020 mode = "r";
861 text = PyUnicode_FromString(mode); 1021 text = PyUnicode_FromString(mode);
862 if (!text || PyObject_SetAttrString(stream, "mode", text) < 0) 1022 if (!text || _PyObject_SetAttrId(stream, &PyId_mode, text) < 0)
863 goto error; 1023 goto error;
864 Py_CLEAR(text); 1024 Py_CLEAR(text);
865 return stream; 1025 return stream;
866 1026
867 error: 1027 error:
868 Py_XDECREF(buf); 1028 Py_XDECREF(buf);
869 Py_XDECREF(stream); 1029 Py_XDECREF(stream);
870 Py_XDECREF(text); 1030 Py_XDECREF(text);
871 Py_XDECREF(raw); 1031 Py_XDECREF(raw);
872 return NULL; 1032 return NULL;
1033 }
1034
1035 static int
1036 is_valid_fd(int fd)
1037 {
1038 int dummy_fd;
1039 if (fd < 0 || !_PyVerify_fd(fd))
1040 return 0;
1041 dummy_fd = dup(fd);
1042 if (dummy_fd < 0)
1043 return 0;
1044 close(dummy_fd);
1045 return 1;
873 } 1046 }
874 1047
875 /* Initialize sys.stdin, stdout, stderr and builtins.open */ 1048 /* Initialize sys.stdin, stdout, stderr and builtins.open */
876 static int 1049 static int
877 initstdio(void) 1050 initstdio(void)
878 { 1051 {
879 PyObject *iomod = NULL, *wrapper; 1052 PyObject *iomod = NULL, *wrapper;
880 PyObject *bimod = NULL; 1053 PyObject *bimod = NULL;
881 PyObject *m; 1054 PyObject *m;
882 PyObject *std = NULL; 1055 PyObject *std = NULL;
(...skipping 27 matching lines...) Expand all
910 /* Set builtins.open */ 1083 /* Set builtins.open */
911 if (PyObject_SetAttrString(bimod, "open", wrapper) == -1) { 1084 if (PyObject_SetAttrString(bimod, "open", wrapper) == -1) {
912 Py_DECREF(wrapper); 1085 Py_DECREF(wrapper);
913 goto error; 1086 goto error;
914 } 1087 }
915 Py_DECREF(wrapper); 1088 Py_DECREF(wrapper);
916 1089
917 encoding = Py_GETENV("PYTHONIOENCODING"); 1090 encoding = Py_GETENV("PYTHONIOENCODING");
918 errors = NULL; 1091 errors = NULL;
919 if (encoding) { 1092 if (encoding) {
920 encoding = strdup(encoding); 1093 encoding = _PyMem_Strdup(encoding);
1094 if (encoding == NULL) {
1095 PyErr_NoMemory();
1096 goto error;
1097 }
921 errors = strchr(encoding, ':'); 1098 errors = strchr(encoding, ':');
922 if (errors) { 1099 if (errors) {
923 *errors = '\0'; 1100 *errors = '\0';
924 errors++; 1101 errors++;
925 } 1102 }
926 } 1103 }
927 1104
928 /* Set sys.stdin */ 1105 /* Set sys.stdin */
929 fd = fileno(stdin); 1106 fd = fileno(stdin);
930 /* Under some conditions stdin, stdout and stderr may not be connected 1107 /* Under some conditions stdin, stdout and stderr may not be connected
931 * and fileno() may point to an invalid file descriptor. For example 1108 * and fileno() may point to an invalid file descriptor. For example
932 * GUI apps don't have valid standard streams by default. 1109 * GUI apps don't have valid standard streams by default.
933 */ 1110 */
934 if (fd < 0) { 1111 if (!is_valid_fd(fd)) {
935 #ifdef MS_WINDOWS
936 std = Py_None; 1112 std = Py_None;
937 Py_INCREF(std); 1113 Py_INCREF(std);
938 #else
939 goto error;
940 #endif
941 } 1114 }
942 else { 1115 else {
943 std = create_stdio(iomod, fd, 0, "<stdin>", encoding, errors); 1116 std = create_stdio(iomod, fd, 0, "<stdin>", encoding, errors);
944 if (std == NULL) 1117 if (std == NULL)
945 goto error; 1118 goto error;
946 } /* if (fd < 0) */ 1119 } /* if (fd < 0) */
947 PySys_SetObject("__stdin__", std); 1120 PySys_SetObject("__stdin__", std);
948 PySys_SetObject("stdin", std); 1121 PySys_SetObject("stdin", std);
949 Py_DECREF(std); 1122 Py_DECREF(std);
950 1123
951 /* Set sys.stdout */ 1124 /* Set sys.stdout */
952 fd = fileno(stdout); 1125 fd = fileno(stdout);
953 if (fd < 0) { 1126 if (!is_valid_fd(fd)) {
954 #ifdef MS_WINDOWS
955 std = Py_None; 1127 std = Py_None;
956 Py_INCREF(std); 1128 Py_INCREF(std);
957 #else
958 goto error;
959 #endif
960 } 1129 }
961 else { 1130 else {
962 std = create_stdio(iomod, fd, 1, "<stdout>", encoding, errors); 1131 std = create_stdio(iomod, fd, 1, "<stdout>", encoding, errors);
963 if (std == NULL) 1132 if (std == NULL)
964 goto error; 1133 goto error;
965 } /* if (fd < 0) */ 1134 } /* if (fd < 0) */
966 PySys_SetObject("__stdout__", std); 1135 PySys_SetObject("__stdout__", std);
967 PySys_SetObject("stdout", std); 1136 PySys_SetObject("stdout", std);
968 Py_DECREF(std); 1137 Py_DECREF(std);
969 1138
970 #if 1 /* Disable this if you have trouble debugging bootstrap stuff */ 1139 #if 1 /* Disable this if you have trouble debugging bootstrap stuff */
971 /* Set sys.stderr, replaces the preliminary stderr */ 1140 /* Set sys.stderr, replaces the preliminary stderr */
972 fd = fileno(stderr); 1141 fd = fileno(stderr);
973 if (fd < 0) { 1142 if (!is_valid_fd(fd)) {
974 #ifdef MS_WINDOWS
975 std = Py_None; 1143 std = Py_None;
976 Py_INCREF(std); 1144 Py_INCREF(std);
977 #else
978 goto error;
979 #endif
980 } 1145 }
981 else { 1146 else {
982 std = create_stdio(iomod, fd, 1, "<stderr>", encoding, "backslashreplace "); 1147 std = create_stdio(iomod, fd, 1, "<stderr>", encoding, "backslashreplace ");
983 if (std == NULL) 1148 if (std == NULL)
984 goto error; 1149 goto error;
985 } /* if (fd < 0) */ 1150 } /* if (fd < 0) */
986 1151
987 /* Same as hack above, pre-import stderr's codec to avoid recursion 1152 /* Same as hack above, pre-import stderr's codec to avoid recursion
988 when import.c tries to write to stderr in verbose mode. */ 1153 when import.c tries to write to stderr in verbose mode. */
989 encoding_attr = PyObject_GetAttrString(std, "encoding"); 1154 encoding_attr = PyObject_GetAttrString(std, "encoding");
990 if (encoding_attr != NULL) { 1155 if (encoding_attr != NULL) {
991 const char * encoding; 1156 const char * std_encoding;
992 encoding = _PyUnicode_AsString(encoding_attr); 1157 std_encoding = _PyUnicode_AsString(encoding_attr);
993 if (encoding != NULL) { 1158 if (std_encoding != NULL) {
994 _PyCodec_Lookup(encoding); 1159 PyObject *codec_info = _PyCodec_Lookup(std_encoding);
1160 Py_XDECREF(codec_info);
995 } 1161 }
996 Py_DECREF(encoding_attr); 1162 Py_DECREF(encoding_attr);
997 } 1163 }
998 PyErr_Clear(); /* Not a fatal error if codec isn't available */ 1164 PyErr_Clear(); /* Not a fatal error if codec isn't available */
999 1165
1000 PySys_SetObject("__stderr__", std); 1166 if (PySys_SetObject("__stderr__", std) < 0) {
1001 PySys_SetObject("stderr", std); 1167 Py_DECREF(std);
1168 goto error;
1169 }
1170 if (PySys_SetObject("stderr", std) < 0) {
1171 Py_DECREF(std);
1172 goto error;
1173 }
1002 Py_DECREF(std); 1174 Py_DECREF(std);
1003 #endif 1175 #endif
1004 1176
1005 if (0) { 1177 if (0) {
1006 error: 1178 error:
1007 status = -1; 1179 status = -1;
1008 } 1180 }
1009 1181
1010 if (encoding) 1182 PyMem_Free(encoding);
1011 free(encoding);
1012 Py_XDECREF(bimod); 1183 Py_XDECREF(bimod);
1013 Py_XDECREF(iomod); 1184 Py_XDECREF(iomod);
1014 return status; 1185 return status;
1015 } 1186 }
1016 1187
1017 /* Parse input from a file and execute it */ 1188 /* Parse input from a file and execute it */
1018 1189
1019 int 1190 int
1020 PyRun_AnyFileExFlags(FILE *fp, const char *filename, int closeit, 1191 PyRun_AnyFileExFlags(FILE *fp, const char *filename, int closeit,
1021 PyCompilerFlags *flags) 1192 PyCompilerFlags *flags)
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
1090 #endif 1261 #endif
1091 1262
1092 int 1263 int
1093 PyRun_InteractiveOneFlags(FILE *fp, const char *filename, PyCompilerFlags *flags ) 1264 PyRun_InteractiveOneFlags(FILE *fp, const char *filename, PyCompilerFlags *flags )
1094 { 1265 {
1095 PyObject *m, *d, *v, *w, *oenc = NULL; 1266 PyObject *m, *d, *v, *w, *oenc = NULL;
1096 mod_ty mod; 1267 mod_ty mod;
1097 PyArena *arena; 1268 PyArena *arena;
1098 char *ps1 = "", *ps2 = "", *enc = NULL; 1269 char *ps1 = "", *ps2 = "", *enc = NULL;
1099 int errcode = 0; 1270 int errcode = 0;
1271 _Py_IDENTIFIER(encoding);
1100 1272
1101 if (fp == stdin) { 1273 if (fp == stdin) {
1102 /* Fetch encoding from sys.stdin */ 1274 /* Fetch encoding from sys.stdin if possible. */
1103 v = PySys_GetObject("stdin"); 1275 v = PySys_GetObject("stdin");
1104 if (v == NULL || v == Py_None) 1276 if (v && v != Py_None) {
1105 return -1; 1277 oenc = _PyObject_GetAttrId(v, &PyId_encoding);
1106 oenc = PyObject_GetAttrString(v, "encoding"); 1278 if (oenc)
1107 if (!oenc) 1279 enc = _PyUnicode_AsString(oenc);
1108 return -1; 1280 if (!enc)
1109 enc = _PyUnicode_AsString(oenc); 1281 PyErr_Clear();
1110 if (enc == NULL) 1282 }
1111 return -1;
1112 } 1283 }
1113 v = PySys_GetObject("ps1"); 1284 v = PySys_GetObject("ps1");
1114 if (v != NULL) { 1285 if (v != NULL) {
1115 v = PyObject_Str(v); 1286 v = PyObject_Str(v);
1116 if (v == NULL) 1287 if (v == NULL)
1117 PyErr_Clear(); 1288 PyErr_Clear();
1118 else if (PyUnicode_Check(v)) { 1289 else if (PyUnicode_Check(v)) {
1119 ps1 = _PyUnicode_AsString(v); 1290 ps1 = _PyUnicode_AsString(v);
1120 if (ps1 == NULL) { 1291 if (ps1 == NULL) {
1121 PyErr_Clear(); 1292 PyErr_Clear();
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
1208 if (fread(buf, 1, 2, fp) == 2 && 1379 if (fread(buf, 1, 2, fp) == 2 &&
1209 ((unsigned int)buf[1]<<8 | buf[0]) == halfmagic) 1380 ((unsigned int)buf[1]<<8 | buf[0]) == halfmagic)
1210 ispyc = 1; 1381 ispyc = 1;
1211 rewind(fp); 1382 rewind(fp);
1212 } 1383 }
1213 return ispyc; 1384 return ispyc;
1214 } 1385 }
1215 return 0; 1386 return 0;
1216 } 1387 }
1217 1388
1389 static int
1390 set_main_loader(PyObject *d, const char *filename, const char *loader_name)
1391 {
1392 PyInterpreterState *interp;
1393 PyThreadState *tstate;
1394 PyObject *filename_obj, *loader_type, *loader;
1395 int result = 0;
1396
1397 filename_obj = PyUnicode_DecodeFSDefault(filename);
1398 if (filename_obj == NULL)
1399 return -1;
1400 /* Get current thread state and interpreter pointer */
1401 tstate = PyThreadState_GET();
1402 interp = tstate->interp;
1403 loader_type = PyObject_GetAttrString(interp->importlib, loader_name);
1404 if (loader_type == NULL) {
1405 Py_DECREF(filename_obj);
1406 return -1;
1407 }
1408 loader = PyObject_CallFunction(loader_type, "sN", "__main__", filename_obj);
1409 Py_DECREF(loader_type);
1410 if (loader == NULL) {
1411 return -1;
1412 }
1413 if (PyDict_SetItemString(d, "__loader__", loader) < 0) {
1414 result = -1;
1415 }
1416 Py_DECREF(loader);
1417 return result;
1418 }
1419
1218 int 1420 int
1219 PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit, 1421 PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit,
1220 PyCompilerFlags *flags) 1422 PyCompilerFlags *flags)
1221 { 1423 {
1222 PyObject *m, *d, *v; 1424 PyObject *m, *d, *v;
1223 const char *ext; 1425 const char *ext;
1224 int set_file_name = 0, ret; 1426 int set_file_name = 0, ret = -1;
1225 size_t len; 1427 size_t len;
1226 1428
1227 m = PyImport_AddModule("__main__"); 1429 m = PyImport_AddModule("__main__");
1228 if (m == NULL) 1430 if (m == NULL)
1229 return -1; 1431 return -1;
1432 Py_INCREF(m);
1230 d = PyModule_GetDict(m); 1433 d = PyModule_GetDict(m);
1231 if (PyDict_GetItemString(d, "__file__") == NULL) { 1434 if (PyDict_GetItemString(d, "__file__") == NULL) {
1232 PyObject *f; 1435 PyObject *f;
1233 f = PyUnicode_DecodeFSDefault(filename); 1436 f = PyUnicode_DecodeFSDefault(filename);
1234 if (f == NULL) 1437 if (f == NULL)
1235 return -1; 1438 goto done;
1236 if (PyDict_SetItemString(d, "__file__", f) < 0) { 1439 if (PyDict_SetItemString(d, "__file__", f) < 0) {
1237 Py_DECREF(f); 1440 Py_DECREF(f);
1238 return -1; 1441 goto done;
1239 } 1442 }
1240 if (PyDict_SetItemString(d, "__cached__", Py_None) < 0) 1443 if (PyDict_SetItemString(d, "__cached__", Py_None) < 0) {
1241 return -1; 1444 Py_DECREF(f);
1445 goto done;
1446 }
1242 set_file_name = 1; 1447 set_file_name = 1;
1243 Py_DECREF(f); 1448 Py_DECREF(f);
1244 } 1449 }
1245 len = strlen(filename); 1450 len = strlen(filename);
1246 ext = filename + len - (len > 4 ? 4 : 0); 1451 ext = filename + len - (len > 4 ? 4 : 0);
1247 if (maybe_pyc_file(fp, filename, ext, closeit)) { 1452 if (maybe_pyc_file(fp, filename, ext, closeit)) {
1453 FILE *pyc_fp;
1248 /* Try to run a pyc file. First, re-open in binary */ 1454 /* Try to run a pyc file. First, re-open in binary */
1249 if (closeit) 1455 if (closeit)
1250 fclose(fp); 1456 fclose(fp);
1251 if ((fp = fopen(filename, "rb")) == NULL) { 1457 if ((pyc_fp = fopen(filename, "rb")) == NULL) {
1252 fprintf(stderr, "python: Can't reopen .pyc file\n"); 1458 fprintf(stderr, "python: Can't reopen .pyc file\n");
1253 ret = -1;
1254 goto done; 1459 goto done;
1255 } 1460 }
1256 /* Turn on optimization if a .pyo file is given */ 1461 /* Turn on optimization if a .pyo file is given */
1257 if (strcmp(ext, ".pyo") == 0) 1462 if (strcmp(ext, ".pyo") == 0)
1258 Py_OptimizeFlag = 1; 1463 Py_OptimizeFlag = 1;
1259 v = run_pyc_file(fp, filename, d, d, flags); 1464
1465 if (set_main_loader(d, filename, "SourcelessFileLoader") < 0) {
1466 fprintf(stderr, "python: failed to set __main__.__loader__\n");
1467 ret = -1;
1468 fclose(pyc_fp);
1469 goto done;
1470 }
1471 v = run_pyc_file(pyc_fp, filename, d, d, flags);
1472 fclose(pyc_fp);
1260 } else { 1473 } else {
1474 /* When running from stdin, leave __main__.__loader__ alone */
1475 if (strcmp(filename, "<stdin>") != 0 &&
1476 set_main_loader(d, filename, "SourceFileLoader") < 0) {
1477 fprintf(stderr, "python: failed to set __main__.__loader__\n");
1478 ret = -1;
1479 goto done;
1480 }
1261 v = PyRun_FileExFlags(fp, filename, Py_file_input, d, d, 1481 v = PyRun_FileExFlags(fp, filename, Py_file_input, d, d,
1262 closeit, flags); 1482 closeit, flags);
1263 } 1483 }
1264 flush_io(); 1484 flush_io();
1265 if (v == NULL) { 1485 if (v == NULL) {
1266 PyErr_Print(); 1486 PyErr_Print();
1267 ret = -1;
1268 goto done; 1487 goto done;
1269 } 1488 }
1270 Py_DECREF(v); 1489 Py_DECREF(v);
1271 ret = 0; 1490 ret = 0;
1272 done: 1491 done:
1273 if (set_file_name && PyDict_DelItemString(d, "__file__")) 1492 if (set_file_name && PyDict_DelItemString(d, "__file__"))
1274 PyErr_Clear(); 1493 PyErr_Clear();
1494 Py_DECREF(m);
1275 return ret; 1495 return ret;
1276 } 1496 }
1277 1497
1278 int 1498 int
1279 PyRun_SimpleStringFlags(const char *command, PyCompilerFlags *flags) 1499 PyRun_SimpleStringFlags(const char *command, PyCompilerFlags *flags)
1280 { 1500 {
1281 PyObject *m, *d, *v; 1501 PyObject *m, *d, *v;
1282 m = PyImport_AddModule("__main__"); 1502 m = PyImport_AddModule("__main__");
1283 if (m == NULL) 1503 if (m == NULL)
1284 return -1; 1504 return -1;
1285 d = PyModule_GetDict(m); 1505 d = PyModule_GetDict(m);
1286 v = PyRun_StringFlags(command, Py_file_input, d, d, flags); 1506 v = PyRun_StringFlags(command, Py_file_input, d, d, flags);
1287 if (v == NULL) { 1507 if (v == NULL) {
1288 PyErr_Print(); 1508 PyErr_Print();
1289 return -1; 1509 return -1;
1290 } 1510 }
1291 Py_DECREF(v); 1511 Py_DECREF(v);
1292 return 0; 1512 return 0;
1293 } 1513 }
1294 1514
1295 static int 1515 static int
1296 parse_syntax_error(PyObject *err, PyObject **message, const char **filename, 1516 parse_syntax_error(PyObject *err, PyObject **message, const char **filename,
1297 int *lineno, int *offset, const char **text) 1517 int *lineno, int *offset, const char **text)
1298 { 1518 {
1299 long hold; 1519 long hold;
1300 PyObject *v; 1520 PyObject *v;
1301 1521 _Py_IDENTIFIER(msg);
1302 /* old style errors */ 1522 _Py_IDENTIFIER(filename);
1303 if (PyTuple_Check(err)) 1523 _Py_IDENTIFIER(lineno);
1304 return PyArg_ParseTuple(err, "O(ziiz)", message, filename, 1524 _Py_IDENTIFIER(offset);
1305 lineno, offset, text); 1525 _Py_IDENTIFIER(text);
1526
1527 *message = NULL;
1306 1528
1307 /* new style errors. `err' is an instance */ 1529 /* new style errors. `err' is an instance */
1308 1530 *message = _PyObject_GetAttrId(err, &PyId_msg);
1309 if (! (v = PyObject_GetAttrString(err, "msg"))) 1531 if (!*message)
1310 goto finally; 1532 goto finally;
1311 *message = v; 1533
1312 1534 v = _PyObject_GetAttrId(err, &PyId_filename);
1313 if (!(v = PyObject_GetAttrString(err, "filename"))) 1535 if (!v)
1314 goto finally; 1536 goto finally;
1315 if (v == Py_None) 1537 if (v == Py_None) {
1538 Py_DECREF(v);
1316 *filename = NULL; 1539 *filename = NULL;
1317 else if (! (*filename = _PyUnicode_AsString(v))) 1540 }
1318 goto finally; 1541 else {
1319 1542 *filename = _PyUnicode_AsString(v);
1320 Py_DECREF(v); 1543 Py_DECREF(v);
1321 if (!(v = PyObject_GetAttrString(err, "lineno"))) 1544 if (!*filename)
1545 goto finally;
1546 }
1547
1548 v = _PyObject_GetAttrId(err, &PyId_lineno);
1549 if (!v)
1322 goto finally; 1550 goto finally;
1323 hold = PyLong_AsLong(v); 1551 hold = PyLong_AsLong(v);
1324 Py_DECREF(v); 1552 Py_DECREF(v);
1325 v = NULL;
1326 if (hold < 0 && PyErr_Occurred()) 1553 if (hold < 0 && PyErr_Occurred())
1327 goto finally; 1554 goto finally;
1328 *lineno = (int)hold; 1555 *lineno = (int)hold;
1329 1556
1330 if (!(v = PyObject_GetAttrString(err, "offset"))) 1557 v = _PyObject_GetAttrId(err, &PyId_offset);
1558 if (!v)
1331 goto finally; 1559 goto finally;
1332 if (v == Py_None) { 1560 if (v == Py_None) {
1333 *offset = -1; 1561 *offset = -1;
1334 Py_DECREF(v); 1562 Py_DECREF(v);
1335 v = NULL;
1336 } else { 1563 } else {
1337 hold = PyLong_AsLong(v); 1564 hold = PyLong_AsLong(v);
1338 Py_DECREF(v); 1565 Py_DECREF(v);
1339 v = NULL;
1340 if (hold < 0 && PyErr_Occurred()) 1566 if (hold < 0 && PyErr_Occurred())
1341 goto finally; 1567 goto finally;
1342 *offset = (int)hold; 1568 *offset = (int)hold;
1343 } 1569 }
1344 1570
1345 if (!(v = PyObject_GetAttrString(err, "text"))) 1571 v = _PyObject_GetAttrId(err, &PyId_text);
1572 if (!v)
1346 goto finally; 1573 goto finally;
1347 if (v == Py_None) 1574 if (v == Py_None) {
1575 Py_DECREF(v);
1348 *text = NULL; 1576 *text = NULL;
1349 else if (!PyUnicode_Check(v) || 1577 }
1350 !(*text = _PyUnicode_AsString(v))) 1578 else {
1351 goto finally; 1579 *text = _PyUnicode_AsString(v);
1352 Py_DECREF(v); 1580 Py_DECREF(v);
1581 if (!*text)
1582 goto finally;
1583 }
1353 return 1; 1584 return 1;
1354 1585
1355 finally: 1586 finally:
1356 Py_XDECREF(v); 1587 Py_XDECREF(*message);
1357 return 0; 1588 return 0;
1358 } 1589 }
1359 1590
1360 void 1591 void
1361 PyErr_Print(void) 1592 PyErr_Print(void)
1362 { 1593 {
1363 PyErr_PrintEx(1); 1594 PyErr_PrintEx(1);
1364 } 1595 }
1365 1596
1366 static void 1597 static void
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
1404 /* Don't exit if -i flag was given. This flag is set to 0 1635 /* Don't exit if -i flag was given. This flag is set to 0
1405 * when entering interactive mode for inspecting. */ 1636 * when entering interactive mode for inspecting. */
1406 return; 1637 return;
1407 1638
1408 PyErr_Fetch(&exception, &value, &tb); 1639 PyErr_Fetch(&exception, &value, &tb);
1409 fflush(stdout); 1640 fflush(stdout);
1410 if (value == NULL || value == Py_None) 1641 if (value == NULL || value == Py_None)
1411 goto done; 1642 goto done;
1412 if (PyExceptionInstance_Check(value)) { 1643 if (PyExceptionInstance_Check(value)) {
1413 /* The error code should be in the `code' attribute. */ 1644 /* The error code should be in the `code' attribute. */
1414 PyObject *code = PyObject_GetAttrString(value, "code"); 1645 _Py_IDENTIFIER(code);
1646 PyObject *code = _PyObject_GetAttrId(value, &PyId_code);
1415 if (code) { 1647 if (code) {
1416 Py_DECREF(value); 1648 Py_DECREF(value);
1417 value = code; 1649 value = code;
1418 if (value == Py_None) 1650 if (value == Py_None)
1419 goto done; 1651 goto done;
1420 } 1652 }
1421 /* If we failed to dig out the 'code' attribute, 1653 /* If we failed to dig out the 'code' attribute,
1422 just let the else clause below print the error. */ 1654 just let the else clause below print the error. */
1423 } 1655 }
1424 if (PyLong_Check(value)) 1656 if (PyLong_Check(value))
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
1511 Py_XDECREF(exception); 1743 Py_XDECREF(exception);
1512 Py_XDECREF(v); 1744 Py_XDECREF(v);
1513 Py_XDECREF(tb); 1745 Py_XDECREF(tb);
1514 } 1746 }
1515 1747
1516 static void 1748 static void
1517 print_exception(PyObject *f, PyObject *value) 1749 print_exception(PyObject *f, PyObject *value)
1518 { 1750 {
1519 int err = 0; 1751 int err = 0;
1520 PyObject *type, *tb; 1752 PyObject *type, *tb;
1753 _Py_IDENTIFIER(print_file_and_line);
1521 1754
1522 if (!PyExceptionInstance_Check(value)) { 1755 if (!PyExceptionInstance_Check(value)) {
1523 PyFile_WriteString("TypeError: print_exception(): Exception expected for value, ", f); 1756 PyFile_WriteString("TypeError: print_exception(): Exception expected for value, ", f);
1524 PyFile_WriteString(Py_TYPE(value)->tp_name, f); 1757 PyFile_WriteString(Py_TYPE(value)->tp_name, f);
1525 PyFile_WriteString(" found\n", f); 1758 PyFile_WriteString(" found\n", f);
1526 return; 1759 return;
1527 } 1760 }
1528 1761
1529 Py_INCREF(value); 1762 Py_INCREF(value);
1530 fflush(stdout); 1763 fflush(stdout);
1531 type = (PyObject *) Py_TYPE(value); 1764 type = (PyObject *) Py_TYPE(value);
1532 tb = PyException_GetTraceback(value); 1765 tb = PyException_GetTraceback(value);
1533 if (tb && tb != Py_None) 1766 if (tb && tb != Py_None)
1534 err = PyTraceBack_Print(tb, f); 1767 err = PyTraceBack_Print(tb, f);
1535 if (err == 0 && 1768 if (err == 0 &&
1536 PyObject_HasAttrString(value, "print_file_and_line")) 1769 _PyObject_HasAttrId(value, &PyId_print_file_and_line))
1537 { 1770 {
1538 PyObject *message; 1771 PyObject *message;
1539 const char *filename, *text; 1772 const char *filename, *text;
1540 int lineno, offset; 1773 int lineno, offset;
1541 if (!parse_syntax_error(value, &message, &filename, 1774 if (!parse_syntax_error(value, &message, &filename,
1542 &lineno, &offset, &text)) 1775 &lineno, &offset, &text))
1543 PyErr_Clear(); 1776 PyErr_Clear();
1544 else { 1777 else {
1545 char buf[10]; 1778 char buf[10];
1546 PyFile_WriteString(" File \"", f); 1779 PyFile_WriteString(" File \"", f);
(...skipping 14 matching lines...) Expand all
1561 if (PyErr_Occurred()) 1794 if (PyErr_Occurred())
1562 err = -1; 1795 err = -1;
1563 } 1796 }
1564 } 1797 }
1565 if (err) { 1798 if (err) {
1566 /* Don't do anything else */ 1799 /* Don't do anything else */
1567 } 1800 }
1568 else { 1801 else {
1569 PyObject* moduleName; 1802 PyObject* moduleName;
1570 char* className; 1803 char* className;
1804 _Py_IDENTIFIER(__module__);
1571 assert(PyExceptionClass_Check(type)); 1805 assert(PyExceptionClass_Check(type));
1572 className = PyExceptionClass_Name(type); 1806 className = PyExceptionClass_Name(type);
1573 if (className != NULL) { 1807 if (className != NULL) {
1574 char *dot = strrchr(className, '.'); 1808 char *dot = strrchr(className, '.');
1575 if (dot != NULL) 1809 if (dot != NULL)
1576 className = dot+1; 1810 className = dot+1;
1577 } 1811 }
1578 1812
1579 moduleName = PyObject_GetAttrString(type, "__module__"); 1813 moduleName = _PyObject_GetAttrId(type, &PyId___module__);
1580 if (moduleName == NULL || !PyUnicode_Check(moduleName)) 1814 if (moduleName == NULL || !PyUnicode_Check(moduleName))
1581 { 1815 {
1582 Py_DECREF(moduleName); 1816 Py_XDECREF(moduleName);
1583 err = PyFile_WriteString("<unknown>", f); 1817 err = PyFile_WriteString("<unknown>", f);
1584 } 1818 }
1585 else { 1819 else {
1586 char* modstr = _PyUnicode_AsString(moduleName); 1820 char* modstr = _PyUnicode_AsString(moduleName);
1587 if (modstr && strcmp(modstr, "builtins")) 1821 if (modstr && strcmp(modstr, "builtins"))
1588 { 1822 {
1589 err = PyFile_WriteString(modstr, f); 1823 err = PyFile_WriteString(modstr, f);
1590 err += PyFile_WriteString(".", f); 1824 err += PyFile_WriteString(".", f);
1591 } 1825 }
1592 Py_DECREF(moduleName); 1826 Py_DECREF(moduleName);
1593 } 1827 }
1594 if (err == 0) { 1828 if (err == 0) {
1595 if (className == NULL) 1829 if (className == NULL)
1596 err = PyFile_WriteString("<unknown>", f); 1830 err = PyFile_WriteString("<unknown>", f);
1597 else 1831 else
1598 err = PyFile_WriteString(className, f); 1832 err = PyFile_WriteString(className, f);
1599 } 1833 }
1600 } 1834 }
1601 if (err == 0 && (value != Py_None)) { 1835 if (err == 0 && (value != Py_None)) {
1602 PyObject *s = PyObject_Str(value); 1836 PyObject *s = PyObject_Str(value);
1603 /* only print colon if the str() of the 1837 /* only print colon if the str() of the
1604 object is not the empty string 1838 object is not the empty string
1605 */ 1839 */
1606 if (s == NULL) 1840 if (s == NULL)
1607 err = -1; 1841 err = -1;
1608 else if (!PyUnicode_Check(s) || 1842 else if (!PyUnicode_Check(s) ||
1609 PyUnicode_GetSize(s) != 0) 1843 PyUnicode_GetLength(s) != 0)
1610 err = PyFile_WriteString(": ", f); 1844 err = PyFile_WriteString(": ", f);
1611 if (err == 0) 1845 if (err == 0)
1612 err = PyFile_WriteObject(s, f, Py_PRINT_RAW); 1846 err = PyFile_WriteObject(s, f, Py_PRINT_RAW);
1613 Py_XDECREF(s); 1847 Py_XDECREF(s);
1614 } 1848 }
1615 /* try to write a newline in any case */ 1849 /* try to write a newline in any case */
1616 err += PyFile_WriteString("\n", f); 1850 err += PyFile_WriteString("\n", f);
1617 Py_XDECREF(tb); 1851 Py_XDECREF(tb);
1618 Py_DECREF(value); 1852 Py_DECREF(value);
1619 /* If an error happened here, don't show it. 1853 /* If an error happened here, don't show it.
(...skipping 27 matching lines...) Expand all
1647 res = PySet_Contains(seen, cause); 1881 res = PySet_Contains(seen, cause);
1648 if (res == -1) 1882 if (res == -1)
1649 PyErr_Clear(); 1883 PyErr_Clear();
1650 if (res == 0) { 1884 if (res == 0) {
1651 print_exception_recursive( 1885 print_exception_recursive(
1652 f, cause, seen); 1886 f, cause, seen);
1653 err |= PyFile_WriteString( 1887 err |= PyFile_WriteString(
1654 cause_message, f); 1888 cause_message, f);
1655 } 1889 }
1656 } 1890 }
1657 else if (context) { 1891 else if (context &&
1892 !((PyBaseExceptionObject *)value)->suppress_context) {
1658 res = PySet_Contains(seen, context); 1893 res = PySet_Contains(seen, context);
1659 if (res == -1) 1894 if (res == -1)
1660 PyErr_Clear(); 1895 PyErr_Clear();
1661 if (res == 0) { 1896 if (res == 0) {
1662 print_exception_recursive( 1897 print_exception_recursive(
1663 f, context, seen); 1898 f, context, seen);
1664 err |= PyFile_WriteString( 1899 err |= PyFile_WriteString(
1665 context_message, f); 1900 context_message, f);
1666 } 1901 }
1667 } 1902 }
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
1736 ret = run_mod(mod, filename, globals, locals, flags, arena); 1971 ret = run_mod(mod, filename, globals, locals, flags, arena);
1737 PyArena_Free(arena); 1972 PyArena_Free(arena);
1738 return ret; 1973 return ret;
1739 } 1974 }
1740 1975
1741 static void 1976 static void
1742 flush_io(void) 1977 flush_io(void)
1743 { 1978 {
1744 PyObject *f, *r; 1979 PyObject *f, *r;
1745 PyObject *type, *value, *traceback; 1980 PyObject *type, *value, *traceback;
1981 _Py_IDENTIFIER(flush);
1746 1982
1747 /* Save the current exception */ 1983 /* Save the current exception */
1748 PyErr_Fetch(&type, &value, &traceback); 1984 PyErr_Fetch(&type, &value, &traceback);
1749 1985
1750 f = PySys_GetObject("stderr"); 1986 f = PySys_GetObject("stderr");
1751 if (f != NULL) { 1987 if (f != NULL) {
1752 r = PyObject_CallMethod(f, "flush", ""); 1988 r = _PyObject_CallMethodId(f, &PyId_flush, "");
1753 if (r) 1989 if (r)
1754 Py_DECREF(r); 1990 Py_DECREF(r);
1755 else 1991 else
1756 PyErr_Clear(); 1992 PyErr_Clear();
1757 } 1993 }
1758 f = PySys_GetObject("stdout"); 1994 f = PySys_GetObject("stdout");
1759 if (f != NULL) { 1995 if (f != NULL) {
1760 r = PyObject_CallMethod(f, "flush", ""); 1996 r = _PyObject_CallMethodId(f, &PyId_flush, "");
1761 if (r) 1997 if (r)
1762 Py_DECREF(r); 1998 Py_DECREF(r);
1763 else 1999 else
1764 PyErr_Clear(); 2000 PyErr_Clear();
1765 } 2001 }
1766 2002
1767 PyErr_Restore(type, value, traceback); 2003 PyErr_Restore(type, value, traceback);
1768 } 2004 }
1769 2005
1770 static PyObject * 2006 static PyObject *
(...skipping 18 matching lines...) Expand all
1789 PyObject *v; 2025 PyObject *v;
1790 long magic; 2026 long magic;
1791 long PyImport_GetMagicNumber(void); 2027 long PyImport_GetMagicNumber(void);
1792 2028
1793 magic = PyMarshal_ReadLongFromFile(fp); 2029 magic = PyMarshal_ReadLongFromFile(fp);
1794 if (magic != PyImport_GetMagicNumber()) { 2030 if (magic != PyImport_GetMagicNumber()) {
1795 PyErr_SetString(PyExc_RuntimeError, 2031 PyErr_SetString(PyExc_RuntimeError,
1796 "Bad magic number in .pyc file"); 2032 "Bad magic number in .pyc file");
1797 return NULL; 2033 return NULL;
1798 } 2034 }
2035 /* Skip mtime and size */
2036 (void) PyMarshal_ReadLongFromFile(fp);
1799 (void) PyMarshal_ReadLongFromFile(fp); 2037 (void) PyMarshal_ReadLongFromFile(fp);
1800 v = PyMarshal_ReadLastObjectFromFile(fp); 2038 v = PyMarshal_ReadLastObjectFromFile(fp);
1801 fclose(fp);
1802 if (v == NULL || !PyCode_Check(v)) { 2039 if (v == NULL || !PyCode_Check(v)) {
1803 Py_XDECREF(v); 2040 Py_XDECREF(v);
1804 PyErr_SetString(PyExc_RuntimeError, 2041 PyErr_SetString(PyExc_RuntimeError,
1805 "Bad code object in .pyc file"); 2042 "Bad code object in .pyc file");
1806 return NULL; 2043 return NULL;
1807 } 2044 }
1808 co = (PyCodeObject *)v; 2045 co = (PyCodeObject *)v;
1809 v = PyEval_EvalCode((PyObject*)co, globals, locals); 2046 v = PyEval_EvalCode((PyObject*)co, globals, locals);
1810 if (v && flags) 2047 if (v && flags)
1811 flags->cf_flags |= (co->co_flags & PyCF_MASK); 2048 flags->cf_flags |= (co->co_flags & PyCF_MASK);
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
1854 PyCompilerFlags flags; 2091 PyCompilerFlags flags;
1855 PyArena *arena; 2092 PyArena *arena;
1856 2093
1857 arena = PyArena_New(); 2094 arena = PyArena_New();
1858 if (arena == NULL) 2095 if (arena == NULL)
1859 return NULL; 2096 return NULL;
1860 2097
1861 flags.cf_flags = 0; 2098 flags.cf_flags = 0;
1862 mod = PyParser_ASTFromStringObject(str, filename, start, &flags, arena); 2099 mod = PyParser_ASTFromStringObject(str, filename, start, &flags, arena);
1863 if (mod == NULL) { 2100 if (mod == NULL) {
1864 Py_DECREF(filename);
1865 PyArena_Free(arena); 2101 PyArena_Free(arena);
1866 return NULL; 2102 return NULL;
1867 } 2103 }
1868 st = PySymtable_BuildObject(mod, filename, 0); 2104 st = PySymtable_BuildObject(mod, filename, 0);
1869 Py_DECREF(filename);
1870 PyArena_Free(arena); 2105 PyArena_Free(arena);
1871 return st; 2106 return st;
1872 } 2107 }
1873 2108
1874 struct symtable * 2109 struct symtable *
1875 Py_SymtableString(const char *str, const char *filename_str, int start) 2110 Py_SymtableString(const char *str, const char *filename_str, int start)
1876 { 2111 {
1877 PyObject *filename; 2112 PyObject *filename;
1878 struct symtable *st; 2113 struct symtable *st;
1879 2114
(...skipping 240 matching lines...) Expand 10 before | Expand all | Expand 10 after
2120 Py_XDECREF(tb); 2355 Py_XDECREF(tb);
2121 break; 2356 break;
2122 } 2357 }
2123 case E_LINECONT: 2358 case E_LINECONT:
2124 msg = "unexpected character after line continuation character"; 2359 msg = "unexpected character after line continuation character";
2125 break; 2360 break;
2126 2361
2127 case E_IDENTIFIER: 2362 case E_IDENTIFIER:
2128 msg = "invalid character in identifier"; 2363 msg = "invalid character in identifier";
2129 break; 2364 break;
2365 case E_BADSINGLE:
2366 msg = "multiple statements found while compiling a single statement";
2367 break;
2130 default: 2368 default:
2131 fprintf(stderr, "error=%d\n", err->error); 2369 fprintf(stderr, "error=%d\n", err->error);
2132 msg = "unknown parsing error"; 2370 msg = "unknown parsing error";
2133 break; 2371 break;
2134 } 2372 }
2135 /* err->text may not be UTF-8 in case of decoding errors. 2373 /* err->text may not be UTF-8 in case of decoding errors.
2136 Explicitly convert to an object. */ 2374 Explicitly convert to an object. */
2137 if (!err->text) { 2375 if (!err->text) {
2138 errtext = Py_None; 2376 errtext = Py_None;
2139 Py_INCREF(Py_None); 2377 Py_INCREF(Py_None);
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
2172 fprintf(stderr, "Fatal Python error: %s\n", msg); 2410 fprintf(stderr, "Fatal Python error: %s\n", msg);
2173 fflush(stderr); /* it helps in Windows debug build */ 2411 fflush(stderr); /* it helps in Windows debug build */
2174 if (PyErr_Occurred()) { 2412 if (PyErr_Occurred()) {
2175 PyErr_PrintEx(0); 2413 PyErr_PrintEx(0);
2176 } 2414 }
2177 else { 2415 else {
2178 tstate = _Py_atomic_load_relaxed(&_PyThreadState_Current); 2416 tstate = _Py_atomic_load_relaxed(&_PyThreadState_Current);
2179 if (tstate != NULL) { 2417 if (tstate != NULL) {
2180 fputc('\n', stderr); 2418 fputc('\n', stderr);
2181 fflush(stderr); 2419 fflush(stderr);
2182 _Py_DumpTraceback(fd, tstate); 2420 _Py_DumpTracebackThreads(fd, tstate->interp, tstate);
2183 } 2421 }
2184 _PyFaulthandler_Fini(); 2422 _PyFaulthandler_Fini();
2185 } 2423 }
2186 2424
2187 #ifdef MS_WINDOWS 2425 #ifdef MS_WINDOWS
2188 { 2426 {
2189 size_t len = strlen(msg); 2427 size_t len = strlen(msg);
2190 WCHAR* buffer; 2428 WCHAR* buffer;
2191 size_t i; 2429 size_t i;
2192 2430
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
2231 } 2469 }
2232 2470
2233 /* Wait until threading._shutdown completes, provided 2471 /* Wait until threading._shutdown completes, provided
2234 the threading module was imported in the first place. 2472 the threading module was imported in the first place.
2235 The shutdown routine will wait until all non-daemon 2473 The shutdown routine will wait until all non-daemon
2236 "threading" threads have completed. */ 2474 "threading" threads have completed. */
2237 static void 2475 static void
2238 wait_for_thread_shutdown(void) 2476 wait_for_thread_shutdown(void)
2239 { 2477 {
2240 #ifdef WITH_THREAD 2478 #ifdef WITH_THREAD
2479 _Py_IDENTIFIER(_shutdown);
2241 PyObject *result; 2480 PyObject *result;
2242 PyThreadState *tstate = PyThreadState_GET(); 2481 PyThreadState *tstate = PyThreadState_GET();
2243 PyObject *threading = PyMapping_GetItemString(tstate->interp->modules, 2482 PyObject *threading = PyMapping_GetItemString(tstate->interp->modules,
2244 "threading"); 2483 "threading");
2245 if (threading == NULL) { 2484 if (threading == NULL) {
2246 /* threading not imported */ 2485 /* threading not imported */
2247 PyErr_Clear(); 2486 PyErr_Clear();
2248 return; 2487 return;
2249 } 2488 }
2250 result = PyObject_CallMethod(threading, "_shutdown", ""); 2489 result = _PyObject_CallMethodId(threading, &PyId__shutdown, "");
2251 if (result == NULL) { 2490 if (result == NULL) {
2252 PyErr_WriteUnraisable(threading); 2491 PyErr_WriteUnraisable(threading);
2253 } 2492 }
2254 else { 2493 else {
2255 Py_DECREF(result); 2494 Py_DECREF(result);
2256 } 2495 }
2257 Py_DECREF(threading); 2496 Py_DECREF(threading);
2258 #endif 2497 #endif
2259 } 2498 }
2260 2499
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
2294 #ifdef SIGPIPE 2533 #ifdef SIGPIPE
2295 PyOS_setsig(SIGPIPE, SIG_IGN); 2534 PyOS_setsig(SIGPIPE, SIG_IGN);
2296 #endif 2535 #endif
2297 #ifdef SIGXFZ 2536 #ifdef SIGXFZ
2298 PyOS_setsig(SIGXFZ, SIG_IGN); 2537 PyOS_setsig(SIGXFZ, SIG_IGN);
2299 #endif 2538 #endif
2300 #ifdef SIGXFSZ 2539 #ifdef SIGXFSZ
2301 PyOS_setsig(SIGXFSZ, SIG_IGN); 2540 PyOS_setsig(SIGXFSZ, SIG_IGN);
2302 #endif 2541 #endif
2303 PyOS_InitInterrupts(); /* May imply initsignal() */ 2542 PyOS_InitInterrupts(); /* May imply initsignal() */
2543 if (PyErr_Occurred()) {
2544 Py_FatalError("Py_Initialize: can't import signal");
2545 }
2304 } 2546 }
2305 2547
2306 2548
2307 /* Restore signals that the interpreter has called SIG_IGN on to SIG_DFL. 2549 /* Restore signals that the interpreter has called SIG_IGN on to SIG_DFL.
2308 * 2550 *
2309 * All of the code in this function must only use async-signal-safe functions, 2551 * All of the code in this function must only use async-signal-safe functions,
2310 * listed at `man 7 signal` or 2552 * listed at `man 7 signal` or
2311 * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html. 2553 * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html.
2312 */ 2554 */
2313 void 2555 void
(...skipping 247 matching lines...) Expand 10 before | Expand all | Expand 10 after
2561 #undef PyRun_InteractiveLoop 2803 #undef PyRun_InteractiveLoop
2562 PyAPI_FUNC(int) 2804 PyAPI_FUNC(int)
2563 PyRun_InteractiveLoop(FILE *f, const char *p) 2805 PyRun_InteractiveLoop(FILE *f, const char *p)
2564 { 2806 {
2565 return PyRun_InteractiveLoopFlags(f, p, NULL); 2807 return PyRun_InteractiveLoopFlags(f, p, NULL);
2566 } 2808 }
2567 2809
2568 #ifdef __cplusplus 2810 #ifdef __cplusplus
2569 } 2811 }
2570 #endif 2812 #endif
LEFTRIGHT

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+