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

Side by Side Diff: Python/errors.c

Issue 11619: On Windows, don't encode filenames in the import machinery
Patch Set: Created 6 years 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 883 matching lines...) Expand 10 before | Expand all | Expand 10 after
894 Py_XDECREF(t); 894 Py_XDECREF(t);
895 Py_XDECREF(v); 895 Py_XDECREF(v);
896 Py_XDECREF(tb); 896 Py_XDECREF(tb);
897 PyErr_Clear(); /* Just in case */ 897 PyErr_Clear(); /* Just in case */
898 } 898 }
899 899
900 extern PyObject *PyModule_GetWarningsModule(void); 900 extern PyObject *PyModule_GetWarningsModule(void);
901 901
902 902
903 void 903 void
904 PyErr_SyntaxLocation(const char *filename, int lineno) { 904 PyErr_SyntaxLocation(const char *filename, int lineno)
905 {
905 PyErr_SyntaxLocationEx(filename, lineno, -1); 906 PyErr_SyntaxLocationEx(filename, lineno, -1);
906 } 907 }
907 908
908 909
909 /* Set file and line information for the current exception. 910 /* Set file and line information for the current exception.
910 If the exception is not a SyntaxError, also sets additional attributes 911 If the exception is not a SyntaxError, also sets additional attributes
911 to make printing of exceptions believe it is a syntax error. */ 912 to make printing of exceptions believe it is a syntax error. */
912 913
913 void 914 void
914 PyErr_SyntaxLocationEx(const char *filename, int lineno, int col_offset) 915 PyErr_SyntaxLocationObject(PyObject *filename, int lineno, int col_offset)
915 { 916 {
916 PyObject *exc, *v, *tb, *tmp; 917 PyObject *exc, *v, *tb, *tmp;
917 _Py_IDENTIFIER(filename); 918 _Py_IDENTIFIER(filename);
918 _Py_IDENTIFIER(lineno); 919 _Py_IDENTIFIER(lineno);
919 _Py_IDENTIFIER(msg); 920 _Py_IDENTIFIER(msg);
920 _Py_IDENTIFIER(offset); 921 _Py_IDENTIFIER(offset);
921 _Py_IDENTIFIER(print_file_and_line); 922 _Py_IDENTIFIER(print_file_and_line);
922 _Py_IDENTIFIER(text); 923 _Py_IDENTIFIER(text);
923 924
924 /* add attributes for the line number and filename for the error */ 925 /* add attributes for the line number and filename for the error */
(...skipping 13 matching lines...) Expand all
938 tmp = PyLong_FromLong(col_offset); 939 tmp = PyLong_FromLong(col_offset);
939 if (tmp == NULL) 940 if (tmp == NULL)
940 PyErr_Clear(); 941 PyErr_Clear();
941 else { 942 else {
942 if (_PyObject_SetAttrId(v, &PyId_offset, tmp)) 943 if (_PyObject_SetAttrId(v, &PyId_offset, tmp))
943 PyErr_Clear(); 944 PyErr_Clear();
944 Py_DECREF(tmp); 945 Py_DECREF(tmp);
945 } 946 }
946 } 947 }
947 if (filename != NULL) { 948 if (filename != NULL) {
948 tmp = PyUnicode_DecodeFSDefault(filename); 949 if (_PyObject_SetAttrId(v, &PyId_filename, filename))
949 if (tmp == NULL)
950 PyErr_Clear(); 950 PyErr_Clear();
951 else {
952 if (_PyObject_SetAttrId(v, &PyId_filename, tmp))
953 PyErr_Clear();
954 Py_DECREF(tmp);
955 }
956 951
957 tmp = PyErr_ProgramText(filename, lineno); 952 tmp = PyErr_ProgramTextObject(filename, lineno);
958 if (tmp) { 953 if (tmp) {
959 if (_PyObject_SetAttrId(v, &PyId_text, tmp)) 954 if (_PyObject_SetAttrId(v, &PyId_text, tmp))
960 PyErr_Clear(); 955 PyErr_Clear();
961 Py_DECREF(tmp); 956 Py_DECREF(tmp);
962 } 957 }
963 } 958 }
964 if (_PyObject_SetAttrId(v, &PyId_offset, Py_None)) { 959 if (_PyObject_SetAttrId(v, &PyId_offset, Py_None)) {
965 PyErr_Clear(); 960 PyErr_Clear();
966 } 961 }
967 if (exc != PyExc_SyntaxError) { 962 if (exc != PyExc_SyntaxError) {
968 if (!_PyObject_HasAttrId(v, &PyId_msg)) { 963 if (!_PyObject_HasAttrId(v, &PyId_msg)) {
969 tmp = PyObject_Str(v); 964 tmp = PyObject_Str(v);
970 if (tmp) { 965 if (tmp) {
971 if (_PyObject_SetAttrId(v, &PyId_msg, tmp)) 966 if (_PyObject_SetAttrId(v, &PyId_msg, tmp))
972 PyErr_Clear(); 967 PyErr_Clear();
973 Py_DECREF(tmp); 968 Py_DECREF(tmp);
974 } else { 969 } else {
975 PyErr_Clear(); 970 PyErr_Clear();
976 } 971 }
977 } 972 }
978 if (!_PyObject_HasAttrId(v, &PyId_print_file_and_line)) { 973 if (!_PyObject_HasAttrId(v, &PyId_print_file_and_line)) {
979 if (_PyObject_SetAttrId(v, &PyId_print_file_and_line, 974 if (_PyObject_SetAttrId(v, &PyId_print_file_and_line,
980 Py_None)) 975 Py_None))
981 PyErr_Clear(); 976 PyErr_Clear();
982 } 977 }
983 } 978 }
984 PyErr_Restore(exc, v, tb); 979 PyErr_Restore(exc, v, tb);
980 }
981
982 void
983 PyErr_SyntaxLocationEx(const char *filename, int lineno, int col_offset)
984 {
985 PyObject *fileobj;
986 if (filename != NULL) {
987 fileobj = PyUnicode_DecodeFSDefault(filename);
988 if (fileobj == NULL)
989 PyErr_Clear();
990 }
991 else
992 fileobj = NULL;
993 PyErr_SyntaxLocationObject(fileobj, lineno, col_offset);
994 Py_XDECREF(fileobj);
985 } 995 }
986 996
987 /* Attempt to load the line of text that the exception refers to. If it 997 /* Attempt to load the line of text that the exception refers to. If it
988 fails, it will return NULL but will not set an exception. 998 fails, it will return NULL but will not set an exception.
989 999
990 XXX The functionality of this function is quite similar to the 1000 XXX The functionality of this function is quite similar to the
991 functionality in tb_displayline() in traceback.c. */ 1001 functionality in tb_displayline() in traceback.c. */
992 1002
993 PyObject * 1003 PyObject *
994 PyErr_ProgramText(const char *filename, int lineno) 1004 err_programtext(FILE *fp, int lineno)
995 { 1005 {
996 FILE *fp;
997 int i; 1006 int i;
998 char linebuf[1000]; 1007 char linebuf[1000];
999 1008
1000 if (filename == NULL || *filename == '\0' || lineno <= 0)
1001 return NULL;
1002 fp = fopen(filename, "r" PY_STDIOTEXTMODE);
1003 if (fp == NULL) 1009 if (fp == NULL)
1004 return NULL; 1010 return NULL;
1005 for (i = 0; i < lineno; i++) { 1011 for (i = 0; i < lineno; i++) {
1006 char *pLastChar = &linebuf[sizeof(linebuf) - 2]; 1012 char *pLastChar = &linebuf[sizeof(linebuf) - 2];
1007 do { 1013 do {
1008 *pLastChar = '\0'; 1014 *pLastChar = '\0';
1009 if (Py_UniversalNewlineFgets(linebuf, sizeof linebuf, 1015 if (Py_UniversalNewlineFgets(linebuf, sizeof linebuf,
1010 fp, NULL) == NULL) 1016 fp, NULL) == NULL)
1011 break; 1017 break;
1012 /* fgets read *something*; if it didn't get as 1018 /* fgets read *something*; if it didn't get as
1013 far as pLastChar, it must have found a newline 1019 far as pLastChar, it must have found a newline
1014 or hit the end of the file; if pLastChar is \n, 1020 or hit the end of the file; if pLastChar is \n,
1015 it obviously found a newline; else we haven't 1021 it obviously found a newline; else we haven't
1016 yet seen a newline, so must continue */ 1022 yet seen a newline, so must continue */
1017 } while (*pLastChar != '\0' && *pLastChar != '\n'); 1023 } while (*pLastChar != '\0' && *pLastChar != '\n');
1018 } 1024 }
1019 fclose(fp); 1025 fclose(fp);
1020 if (i == lineno) { 1026 if (i == lineno) {
1021 char *p = linebuf; 1027 char *p = linebuf;
1022 PyObject *res; 1028 PyObject *res;
1023 while (*p == ' ' || *p == '\t' || *p == '\014') 1029 while (*p == ' ' || *p == '\t' || *p == '\014')
1024 p++; 1030 p++;
1025 res = PyUnicode_FromString(p); 1031 res = PyUnicode_FromString(p);
1026 if (res == NULL) 1032 if (res == NULL)
1027 PyErr_Clear(); 1033 PyErr_Clear();
1028 return res; 1034 return res;
1029 } 1035 }
1030 return NULL; 1036 return NULL;
1037 }
1038
1039 PyObject *
1040 PyErr_ProgramText(const char *filename, int lineno)
1041 {
1042 FILE *fp;
1043 if (filename == NULL || *filename == '\0' || lineno <= 0)
1044 return NULL;
1045 fp = fopen(filename, "r" PY_STDIOTEXTMODE);
1046 return err_programtext(fp, lineno);
1047 }
1048
1049 PyObject *
1050 PyErr_ProgramTextObject(PyObject *filename, int lineno)
1051 {
1052 FILE *fp;
1053 if (filename == NULL || lineno <= 0)
1054 return NULL;
1055 fp = _Py_fopen(filename, "r" PY_STDIOTEXTMODE);
1056 return err_programtext(fp, lineno);
1031 } 1057 }
1032 1058
1033 #ifdef __cplusplus 1059 #ifdef __cplusplus
1034 } 1060 }
1035 #endif 1061 #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+