@@ -701,6 +701,7 @@ initstdio(void)
701701 PyObject * std = NULL ;
702702 int status = 0 , fd ;
703703 PyObject * encoding_attr ;
704+ char * encoding , * errors ;
704705
705706 /* Hack to avoid a nasty recursion issue when Python is invoked
706707 in verbose mode: pre-import the Latin-1 and UTF-8 codecs */
@@ -730,6 +731,16 @@ initstdio(void)
730731 goto error ;
731732 }
732733
734+ encoding = Py_GETENV ("PYTHONIOENCODING" );
735+ if (encoding ) {
736+ encoding = strdup (encoding );
737+ errors = strchr (encoding , ':' );
738+ if (errors ) {
739+ * errors = '\0' ;
740+ errors ++ ;
741+ }
742+ }
743+
733744 /* Set sys.stdin */
734745 fd = fileno (stdin );
735746 /* Under some conditions stdin, stdout and stderr may not be connected
@@ -745,8 +756,8 @@ initstdio(void)
745756#endif
746757 }
747758 else {
748- if (!(std = PyFile_FromFd (fd , "<stdin>" , "r" , -1 , NULL , NULL ,
749- "\n" , 0 ))) {
759+ if (!(std = PyFile_FromFd (fd , "<stdin>" , "r" , -1 , encoding ,
760+ errors , "\n" , 0 ))) {
750761 goto error ;
751762 }
752763 } /* if (fd < 0) */
@@ -765,8 +776,8 @@ initstdio(void)
765776#endif
766777 }
767778 else {
768- if (!(std = PyFile_FromFd (fd , "<stdout>" , "w" , -1 , NULL , NULL ,
769- "\n" , 0 ))) {
779+ if (!(std = PyFile_FromFd (fd , "<stdout>" , "w" , -1 , encoding ,
780+ errors , "\n" , 0 ))) {
770781 goto error ;
771782 }
772783 } /* if (fd < 0) */
@@ -786,8 +797,8 @@ initstdio(void)
786797#endif
787798 }
788799 else {
789- if (!(std = PyFile_FromFd (fd , "<stderr>" , "w" , -1 , NULL , NULL ,
790- "\n" , 0 ))) {
800+ if (!(std = PyFile_FromFd (fd , "<stderr>" , "w" , -1 , encoding ,
801+ errors , "\n" , 0 ))) {
791802 goto error ;
792803 }
793804 } /* if (fd < 0) */
0 commit comments