comparison Python/errors.c @ 85417:df2fdd42b375

Close #11619: The parser and the import machinery do not encode Unicode filenames anymore on Windows.
author Victor Stinner <victor.stinner@gmail.com>
date Mon, 26 Aug 2013 22:28:21 +0200
parents e63f19d0a651
children ef889c3d5dc6
comparison
equal deleted inserted replaced
85416:8fb3a6f9b0a4 85417:df2fdd42b375
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);
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 { 951
952 if (_PyObject_SetAttrId(v, &PyId_filename, tmp)) 952 tmp = PyErr_ProgramTextObject(filename, lineno);
953 PyErr_Clear();
954 Py_DECREF(tmp);
955 }
956
957 tmp = PyErr_ProgramText(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 }
982 } 977 }
983 } 978 }
984 PyErr_Restore(exc, v, tb); 979 PyErr_Restore(exc, v, tb);
985 } 980 }
986 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);
995 }
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 {
1028 return res; 1034 return res;
1029 } 1035 }
1030 return NULL; 1036 return NULL;
1031 } 1037 }
1032 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);
1057 }
1058
1033 #ifdef __cplusplus 1059 #ifdef __cplusplus
1034 } 1060 }
1035 #endif 1061 #endif