comparison Python/pythonrun.c @ 69708:2caf82aee7a4 3.2

Issue #10914: Initialize correctly the filesystem codec when creating a new subinterpreter to fix a bootstrap issue with codecs implemented in Python, as the ISO-8859-15 codec. Add fscodec_initialized attribute to the PyInterpreterState structure.
author Victor Stinner <victor.stinner@haypocalc.com>
date Wed, 27 Apr 2011 00:24:21 +0200
parents 5a983773c09a
children 2a19d09b08f8 ce859b848879
comparison
equal deleted inserted replaced
69707:5a983773c09a 69708:2caf82aee7a4
51 51
52 extern grammar _PyParser_Grammar; /* From graminit.c */ 52 extern grammar _PyParser_Grammar; /* From graminit.c */
53 53
54 /* Forward */ 54 /* Forward */
55 static void initmain(void); 55 static void initmain(void);
56 static void initfsencoding(void); 56 static int initfsencoding(PyInterpreterState *interp);
57 static void initsite(void); 57 static void initsite(void);
58 static int initstdio(void); 58 static int initstdio(void);
59 static void flush_io(void); 59 static void flush_io(void);
60 static PyObject *run_mod(mod_ty, const char *, PyObject *, PyObject *, 60 static PyObject *run_mod(mod_ty, const char *, PyObject *, PyObject *,
61 PyCompilerFlags *, PyArena *); 61 PyCompilerFlags *, PyArena *);
289 /* Initialize _warnings. */ 289 /* Initialize _warnings. */
290 _PyWarnings_Init(); 290 _PyWarnings_Init();
291 291
292 _PyTime_Init(); 292 _PyTime_Init();
293 293
294 initfsencoding(); 294 if (initfsencoding(interp) < 0)
295 Py_FatalError("Py_Initialize: unable to load the file system codec");
295 296
296 if (install_sigs) 297 if (install_sigs)
297 initsigs(); /* Signal handling stuff, including initintr() */ 298 initsigs(); /* Signal handling stuff, including initintr() */
298 299
299 initmain(); /* Module __main__ */ 300 initmain(); /* Module __main__ */
606 PySys_SetObject("stderr", pstderr); 607 PySys_SetObject("stderr", pstderr);
607 PySys_SetObject("__stderr__", pstderr); 608 PySys_SetObject("__stderr__", pstderr);
608 Py_DECREF(pstderr); 609 Py_DECREF(pstderr);
609 610
610 _PyImportHooks_Init(); 611 _PyImportHooks_Init();
612
613 if (initfsencoding(interp) < 0)
614 goto handle_error;
615
611 if (initstdio() < 0) 616 if (initstdio() < 0)
612 Py_FatalError( 617 Py_FatalError(
613 "Py_Initialize: can't initialize sys standard streams"); 618 "Py_Initialize: can't initialize sys standard streams");
614 initmain(); 619 initmain();
615 if (!Py_NoSiteFlag) 620 if (!Py_NoSiteFlag)
718 Py_FatalError("can't add __builtins__ to __main__"); 723 Py_FatalError("can't add __builtins__ to __main__");
719 Py_DECREF(bimod); 724 Py_DECREF(bimod);
720 } 725 }
721 } 726 }
722 727
723 static void 728 static int
724 initfsencoding(void) 729 initfsencoding(PyInterpreterState *interp)
725 { 730 {
726 PyObject *codec; 731 PyObject *codec;
727 #if defined(HAVE_LANGINFO_H) && defined(CODESET) 732 #if defined(HAVE_LANGINFO_H) && defined(CODESET)
728 char *codeset = NULL; 733 char *codeset = NULL;
729 734
736 if (codeset == NULL) 741 if (codeset == NULL)
737 Py_FatalError("Py_Initialize: Unable to get the locale encoding"); 742 Py_FatalError("Py_Initialize: Unable to get the locale encoding");
738 743
739 Py_FileSystemDefaultEncoding = codeset; 744 Py_FileSystemDefaultEncoding = codeset;
740 Py_HasFileSystemDefaultEncoding = 0; 745 Py_HasFileSystemDefaultEncoding = 0;
741 return; 746 interp->fscodec_initialized = 1;
747 return 0;
742 } 748 }
743 #endif 749 #endif
744 750
745 /* the encoding is mbcs, utf-8 or ascii */ 751 /* the encoding is mbcs, utf-8 or ascii */
746 codec = _PyCodec_Lookup(Py_FileSystemDefaultEncoding); 752 codec = _PyCodec_Lookup(Py_FileSystemDefaultEncoding);
747 if (!codec) { 753 if (!codec) {
748 /* Such error can only occurs in critical situations: no more 754 /* Such error can only occurs in critical situations: no more
749 * memory, import a module of the standard library failed, 755 * memory, import a module of the standard library failed,
750 * etc. */ 756 * etc. */
751 Py_FatalError("Py_Initialize: unable to load the file system codec"); 757 return -1;
752 } else { 758 }
753 Py_DECREF(codec); 759 Py_DECREF(codec);
754 } 760 interp->fscodec_initialized = 1;
761 return 0;
755 } 762 }
756 763
757 /* Import the site module (not into __main__ though) */ 764 /* Import the site module (not into __main__ though) */
758 765
759 static void 766 static void