comparison Python/pythonrun.c @ 47444:bb2c4bdc3c68

Forward-port PYTHONIOENCODING.
author Martin v. Löwis <martin@v.loewis.de>
date Mon, 02 Jun 2008 11:13:03 +0000
parents 23ab586c427a
children b6b534b828b3
comparison
equal deleted inserted replaced
47443:79373e070d48 47444:bb2c4bdc3c68
699 PyObject *bimod = NULL; 699 PyObject *bimod = NULL;
700 PyObject *m; 700 PyObject *m;
701 PyObject *std = NULL; 701 PyObject *std = NULL;
702 int status = 0, fd; 702 int status = 0, fd;
703 PyObject * encoding_attr; 703 PyObject * encoding_attr;
704 char *encoding, *errors;
704 705
705 /* Hack to avoid a nasty recursion issue when Python is invoked 706 /* Hack to avoid a nasty recursion issue when Python is invoked
706 in verbose mode: pre-import the Latin-1 and UTF-8 codecs */ 707 in verbose mode: pre-import the Latin-1 and UTF-8 codecs */
707 if ((m = PyImport_ImportModule("encodings.utf_8")) == NULL) { 708 if ((m = PyImport_ImportModule("encodings.utf_8")) == NULL) {
708 goto error; 709 goto error;
726 } 727 }
727 728
728 /* Set builtins.open */ 729 /* Set builtins.open */
729 if (PyObject_SetAttrString(bimod, "open", wrapper) == -1) { 730 if (PyObject_SetAttrString(bimod, "open", wrapper) == -1) {
730 goto error; 731 goto error;
732 }
733
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 }
731 } 742 }
732 743
733 /* Set sys.stdin */ 744 /* Set sys.stdin */
734 fd = fileno(stdin); 745 fd = fileno(stdin);
735 /* Under some conditions stdin, stdout and stderr may not be connected 746 /* Under some conditions stdin, stdout and stderr may not be connected
743 #else 754 #else
744 goto error; 755 goto error;
745 #endif 756 #endif
746 } 757 }
747 else { 758 else {
748 if (!(std = PyFile_FromFd(fd, "<stdin>", "r", -1, NULL, NULL, 759 if (!(std = PyFile_FromFd(fd, "<stdin>", "r", -1, encoding,
749 "\n", 0))) { 760 errors, "\n", 0))) {
750 goto error; 761 goto error;
751 } 762 }
752 } /* if (fd < 0) */ 763 } /* if (fd < 0) */
753 PySys_SetObject("__stdin__", std); 764 PySys_SetObject("__stdin__", std);
754 PySys_SetObject("stdin", std); 765 PySys_SetObject("stdin", std);
763 #else 774 #else
764 goto error; 775 goto error;
765 #endif 776 #endif
766 } 777 }
767 else { 778 else {
768 if (!(std = PyFile_FromFd(fd, "<stdout>", "w", -1, NULL, NULL, 779 if (!(std = PyFile_FromFd(fd, "<stdout>", "w", -1, encoding,
769 "\n", 0))) { 780 errors, "\n", 0))) {
770 goto error; 781 goto error;
771 } 782 }
772 } /* if (fd < 0) */ 783 } /* if (fd < 0) */
773 PySys_SetObject("__stdout__", std); 784 PySys_SetObject("__stdout__", std);
774 PySys_SetObject("stdout", std); 785 PySys_SetObject("stdout", std);
784 #else 795 #else
785 goto error; 796 goto error;
786 #endif 797 #endif
787 } 798 }
788 else { 799 else {
789 if (!(std = PyFile_FromFd(fd, "<stderr>", "w", -1, NULL, NULL, 800 if (!(std = PyFile_FromFd(fd, "<stderr>", "w", -1, encoding,
790 "\n", 0))) { 801 errors, "\n", 0))) {
791 goto error; 802 goto error;
792 } 803 }
793 } /* if (fd < 0) */ 804 } /* if (fd < 0) */
794 805
795 /* Same as hack above, pre-import stderr's codec to avoid recursion 806 /* Same as hack above, pre-import stderr's codec to avoid recursion