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

Side by Side Diff: Python/errors.c

Issue 11619: On Windows, don't encode filenames in the import machinery
Patch Set: Created 8 years, 5 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:
View unified diff | Download patch
« no previous file with comments | « Python/compile.c ('k') | Python/future.c » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 1
2 /* Error handling */ 2 /* Error handling */
3 3
4 #include "Python.h" 4 #include "Python.h"
5 5
6 #ifndef __STDC__ 6 #ifndef __STDC__
7 #ifndef MS_WINDOWS 7 #ifndef MS_WINDOWS
8 extern char *strerror(int); 8 extern char *strerror(int);
9 #endif 9 #endif
10 #endif 10 #endif
(...skipping 743 matching lines...) Expand 10 before | Expand all | Expand 10 after
754 } 754 }
755 Py_XDECREF(t); 755 Py_XDECREF(t);
756 Py_XDECREF(v); 756 Py_XDECREF(v);
757 Py_XDECREF(tb); 757 Py_XDECREF(tb);
758 } 758 }
759 759
760 extern PyObject *PyModule_GetWarningsModule(void); 760 extern PyObject *PyModule_GetWarningsModule(void);
761 761
762 762
763 void 763 void
764 PyErr_SyntaxLocation(const char *filename, int lineno) { 764 PyErr_SyntaxLocation(const char *filename, int lineno)
765 {
765 PyErr_SyntaxLocationEx(filename, lineno, -1); 766 PyErr_SyntaxLocationEx(filename, lineno, -1);
766 } 767 }
767 768
768 769
769 /* Set file and line information for the current exception. 770 /* Set file and line information for the current exception.
770 If the exception is not a SyntaxError, also sets additional attributes 771 If the exception is not a SyntaxError, also sets additional attributes
771 to make printing of exceptions believe it is a syntax error. */ 772 to make printing of exceptions believe it is a syntax error. */
772 773
773 void 774 void
774 PyErr_SyntaxLocationEx(const char *filename, int lineno, int col_offset) 775 PyErr_SyntaxLocationObject(PyObject *filename, int lineno, int col_offset)
775 { 776 {
776 PyObject *exc, *v, *tb, *tmp; 777 PyObject *exc, *v, *tb, *tmp;
777 778
778 /* add attributes for the line number and filename for the error */ 779 /* add attributes for the line number and filename for the error */
779 PyErr_Fetch(&exc, &v, &tb); 780 PyErr_Fetch(&exc, &v, &tb);
780 PyErr_NormalizeException(&exc, &v, &tb); 781 PyErr_NormalizeException(&exc, &v, &tb);
781 /* XXX check that it is, indeed, a syntax error. It might not 782 /* XXX check that it is, indeed, a syntax error. It might not
782 * be, though. */ 783 * be, though. */
783 tmp = PyLong_FromLong(lineno); 784 tmp = PyLong_FromLong(lineno);
784 if (tmp == NULL) 785 if (tmp == NULL)
785 PyErr_Clear(); 786 PyErr_Clear();
786 else { 787 else {
787 if (PyObject_SetAttrString(v, "lineno", tmp)) 788 if (PyObject_SetAttrString(v, "lineno", tmp))
788 PyErr_Clear(); 789 PyErr_Clear();
789 Py_DECREF(tmp); 790 Py_DECREF(tmp);
790 } 791 }
791 if (col_offset >= 0) { 792 if (col_offset >= 0) {
792 tmp = PyLong_FromLong(col_offset); 793 tmp = PyLong_FromLong(col_offset);
793 if (tmp == NULL) 794 if (tmp == NULL)
794 PyErr_Clear(); 795 PyErr_Clear();
795 else { 796 else {
796 if (PyObject_SetAttrString(v, "offset", tmp)) 797 if (PyObject_SetAttrString(v, "offset", tmp))
797 PyErr_Clear(); 798 PyErr_Clear();
798 Py_DECREF(tmp); 799 Py_DECREF(tmp);
799 } 800 }
800 } 801 }
801 if (filename != NULL) { 802 if (filename != NULL) {
802 tmp = PyUnicode_DecodeFSDefault(filename); 803 if (PyObject_SetAttrString(v, "filename", filename))
803 if (tmp == NULL)
804 PyErr_Clear(); 804 PyErr_Clear();
805 else { 805 tmp = PyErr_ProgramTextObject(filename, lineno);
806 if (PyObject_SetAttrString(v, "filename", tmp))
807 PyErr_Clear();
808 Py_DECREF(tmp);
809 }
810
811 tmp = PyErr_ProgramText(filename, lineno);
812 if (tmp) { 806 if (tmp) {
813 if (PyObject_SetAttrString(v, "text", tmp)) 807 if (PyObject_SetAttrString(v, "text", tmp))
814 PyErr_Clear(); 808 PyErr_Clear();
815 Py_DECREF(tmp); 809 Py_DECREF(tmp);
816 } 810 }
817 } 811 }
818 if (PyObject_SetAttrString(v, "offset", Py_None)) { 812 if (PyObject_SetAttrString(v, "offset", Py_None)) {
819 PyErr_Clear(); 813 PyErr_Clear();
820 } 814 }
821 if (exc != PyExc_SyntaxError) { 815 if (exc != PyExc_SyntaxError) {
822 if (!PyObject_HasAttrString(v, "msg")) { 816 if (!PyObject_HasAttrString(v, "msg")) {
823 tmp = PyObject_Str(v); 817 tmp = PyObject_Str(v);
824 if (tmp) { 818 if (tmp) {
825 if (PyObject_SetAttrString(v, "msg", tmp)) 819 if (PyObject_SetAttrString(v, "msg", tmp))
826 PyErr_Clear(); 820 PyErr_Clear();
827 Py_DECREF(tmp); 821 Py_DECREF(tmp);
828 } else { 822 } else {
829 PyErr_Clear(); 823 PyErr_Clear();
830 } 824 }
831 } 825 }
832 if (!PyObject_HasAttrString(v, "print_file_and_line")) { 826 if (!PyObject_HasAttrString(v, "print_file_and_line")) {
833 if (PyObject_SetAttrString(v, "print_file_and_line", 827 if (PyObject_SetAttrString(v, "print_file_and_line",
834 Py_None)) 828 Py_None))
835 PyErr_Clear(); 829 PyErr_Clear();
836 } 830 }
837 } 831 }
838 PyErr_Restore(exc, v, tb); 832 PyErr_Restore(exc, v, tb);
833 }
834
835 void
836 PyErr_SyntaxLocationEx(const char *filename, int lineno, int col_offset)
837 {
838 PyObject *fileobj;
839 if (filename != NULL) {
840 fileobj = PyUnicode_DecodeFSDefault(filename);
841 if (fileobj == NULL)
842 PyErr_Clear();
843 }
844 else
845 fileobj = NULL;
846 PyErr_SyntaxLocationObject(fileobj, lineno, col_offset);
847 Py_XDECREF(fileobj);
839 } 848 }
840 849
841 /* Attempt to load the line of text that the exception refers to. If it 850 /* Attempt to load the line of text that the exception refers to. If it
842 fails, it will return NULL but will not set an exception. 851 fails, it will return NULL but will not set an exception.
843 852
844 XXX The functionality of this function is quite similar to the 853 XXX The functionality of this function is quite similar to the
845 functionality in tb_displayline() in traceback.c. */ 854 functionality in tb_displayline() in traceback.c. */
846 855
847 PyObject * 856 PyObject *
848 PyErr_ProgramText(const char *filename, int lineno) 857 err_programtext(FILE *fp, int lineno)
849 { 858 {
850 FILE *fp;
851 int i; 859 int i;
852 char linebuf[1000]; 860 char linebuf[1000];
853 861
854 if (filename == NULL || *filename == '\0' || lineno <= 0)
855 return NULL;
856 fp = fopen(filename, "r" PY_STDIOTEXTMODE);
857 if (fp == NULL) 862 if (fp == NULL)
858 return NULL; 863 return NULL;
859 for (i = 0; i < lineno; i++) { 864 for (i = 0; i < lineno; i++) {
860 char *pLastChar = &linebuf[sizeof(linebuf) - 2]; 865 char *pLastChar = &linebuf[sizeof(linebuf) - 2];
861 do { 866 do {
862 *pLastChar = '\0'; 867 *pLastChar = '\0';
863 if (Py_UniversalNewlineFgets(linebuf, sizeof linebuf, 868 if (Py_UniversalNewlineFgets(linebuf, sizeof linebuf,
864 fp, NULL) == NULL) 869 fp, NULL) == NULL)
865 break; 870 break;
866 /* fgets read *something*; if it didn't get as 871 /* fgets read *something*; if it didn't get as
867 far as pLastChar, it must have found a newline 872 far as pLastChar, it must have found a newline
868 or hit the end of the file; if pLastChar is \n, 873 or hit the end of the file; if pLastChar is \n,
869 it obviously found a newline; else we haven't 874 it obviously found a newline; else we haven't
870 yet seen a newline, so must continue */ 875 yet seen a newline, so must continue */
871 } while (*pLastChar != '\0' && *pLastChar != '\n'); 876 } while (*pLastChar != '\0' && *pLastChar != '\n');
872 } 877 }
873 fclose(fp); 878 fclose(fp);
874 if (i == lineno) { 879 if (i == lineno) {
875 char *p = linebuf; 880 char *p = linebuf;
876 PyObject *res; 881 PyObject *res;
877 while (*p == ' ' || *p == '\t' || *p == '\014') 882 while (*p == ' ' || *p == '\t' || *p == '\014')
878 p++; 883 p++;
879 res = PyUnicode_FromString(p); 884 res = PyUnicode_FromString(p);
880 if (res == NULL) 885 if (res == NULL)
881 PyErr_Clear(); 886 PyErr_Clear();
882 return res; 887 return res;
883 } 888 }
884 return NULL; 889 return NULL;
890 }
891
892 PyObject *
893 PyErr_ProgramText(const char *filename, int lineno)
894 {
895 FILE *fp;
896 if (filename == NULL || *filename == '\0' || lineno <= 0)
897 return NULL;
898 fp = fopen(filename, "r" PY_STDIOTEXTMODE);
899 return err_programtext(fp, lineno);
900 }
901
902 PyObject *
903 PyErr_ProgramTextObject(PyObject *filename, int lineno)
904 {
905 FILE *fp;
906 if (filename == NULL || lineno <= 0)
907 return NULL;
908 fp = _Py_fopen(filename, "r" PY_STDIOTEXTMODE);
909 return err_programtext(fp, lineno);
885 } 910 }
886 911
887 #ifdef __cplusplus 912 #ifdef __cplusplus
888 } 913 }
889 #endif 914 #endif
OLDNEW
« no previous file with comments | « Python/compile.c ('k') | Python/future.c » ('j') | no next file with comments »

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