comparison Python/pythonrun.c @ 13539:feab677269e0 legacy-trunk

Create two new exceptions: IndentationError and TabError. These are used for indentation related errors. This patch includes Ping's improvements for indentation-related error messages. Closes SourceForge patches #100734 and #100856.
author Fred Drake <fdrake@acm.org>
date Tue, 11 Jul 2000 17:53:00 +0000
parents a89f7f268de4
children dd2b5218e069
comparison
equal deleted inserted replaced
13538:4228775d455e 13539:feab677269e0
12 12
13 #include "Python.h" 13 #include "Python.h"
14 14
15 #include "grammar.h" 15 #include "grammar.h"
16 #include "node.h" 16 #include "node.h"
17 #include "token.h"
17 #include "parsetok.h" 18 #include "parsetok.h"
18 #include "errcode.h" 19 #include "errcode.h"
19 #include "compile.h" 20 #include "compile.h"
20 #include "eval.h" 21 #include "eval.h"
21 #include "marshal.h" 22 #include "marshal.h"
981 982
982 static void 983 static void
983 err_input(err) 984 err_input(err)
984 perrdetail *err; 985 perrdetail *err;
985 { 986 {
986 PyObject *v, *w; 987 PyObject *v, *w, *errtype;
987 char *msg = NULL; 988 char *msg = NULL;
989 errtype = PyExc_SyntaxError;
988 v = Py_BuildValue("(ziiz)", err->filename, 990 v = Py_BuildValue("(ziiz)", err->filename,
989 err->lineno, err->offset, err->text); 991 err->lineno, err->offset, err->text);
990 if (err->text != NULL) { 992 if (err->text != NULL) {
991 PyMem_DEL(err->text); 993 PyMem_DEL(err->text);
992 err->text = NULL; 994 err->text = NULL;
993 } 995 }
994 switch (err->error) { 996 switch (err->error) {
995 case E_SYNTAX: 997 case E_SYNTAX:
996 msg = "invalid syntax"; 998 errtype = PyExc_IndentationError;
999 if (err->expected == INDENT)
1000 msg = "expected an indented block";
1001 else if (err->token == INDENT)
1002 msg = "unexpected indent";
1003 else if (err->token == DEDENT)
1004 msg = "unexpected unindent";
1005 else {
1006 errtype = PyExc_SyntaxError;
1007 msg = "invalid syntax";
1008 }
997 break; 1009 break;
998 case E_TOKEN: 1010 case E_TOKEN:
999 msg = "invalid token"; 1011 msg = "invalid token";
1000 break; 1012 break;
1001 case E_INTR: 1013 case E_INTR:
1007 Py_XDECREF(v); 1019 Py_XDECREF(v);
1008 return; 1020 return;
1009 case E_EOF: 1021 case E_EOF:
1010 msg = "unexpected EOF while parsing"; 1022 msg = "unexpected EOF while parsing";
1011 break; 1023 break;
1012 case E_INDENT: 1024 case E_TABSPACE:
1025 errtype = PyExc_TabError;
1013 msg = "inconsistent use of tabs and spaces in indentation"; 1026 msg = "inconsistent use of tabs and spaces in indentation";
1014 break; 1027 break;
1015 case E_OVERFLOW: 1028 case E_OVERFLOW:
1016 msg = "expression too long"; 1029 msg = "expression too long";
1017 break; 1030 break;
1031 case E_DEDENT:
1032 errtype = PyExc_IndentationError;
1033 msg = "unindent does not match any outer indentation level";
1034 break;
1035 case E_TOODEEP:
1036 errtype = PyExc_IndentationError;
1037 msg = "too many levels of indentation";
1038 break;
1018 default: 1039 default:
1019 fprintf(stderr, "error=%d\n", err->error); 1040 fprintf(stderr, "error=%d\n", err->error);
1020 msg = "unknown parsing error"; 1041 msg = "unknown parsing error";
1021 break; 1042 break;
1022 } 1043 }
1023 w = Py_BuildValue("(sO)", msg, v); 1044 w = Py_BuildValue("(sO)", msg, v);
1024 Py_XDECREF(v); 1045 Py_XDECREF(v);
1025 PyErr_SetObject(PyExc_SyntaxError, w); 1046 PyErr_SetObject(errtype, w);
1026 Py_XDECREF(w); 1047 Py_XDECREF(w);
1027 } 1048 }
1028 1049
1029 /* Print fatal error message and abort */ 1050 /* Print fatal error message and abort */
1030 1051