@@ -1071,19 +1071,13 @@ tok_backup(struct tok_state *tok, int c)
1071
1071
}
1072
1072
}
1073
1073
1074
-
1075
1074
static int
1076
- syntaxerror (struct tok_state * tok , const char * format , ...)
1075
+ _syntaxerror_range (struct tok_state * tok , const char * format ,
1076
+ int col_offset , int end_col_offset ,
1077
+ va_list vargs )
1077
1078
{
1078
1079
PyObject * errmsg , * errtext , * args ;
1079
- va_list vargs ;
1080
- #ifdef HAVE_STDARG_PROTOTYPES
1081
- va_start (vargs , format );
1082
- #else
1083
- va_start (vargs );
1084
- #endif
1085
1080
errmsg = PyUnicode_FromFormatV (format , vargs );
1086
- va_end (vargs );
1087
1081
if (!errmsg ) {
1088
1082
goto error ;
1089
1083
}
@@ -1093,7 +1087,14 @@ syntaxerror(struct tok_state *tok, const char *format, ...)
1093
1087
if (!errtext ) {
1094
1088
goto error ;
1095
1089
}
1096
- int offset = (int )PyUnicode_GET_LENGTH (errtext );
1090
+
1091
+ if (col_offset == -1 ) {
1092
+ col_offset = (int )PyUnicode_GET_LENGTH (errtext );
1093
+ }
1094
+ if (end_col_offset == -1 ) {
1095
+ end_col_offset = col_offset ;
1096
+ }
1097
+
1097
1098
Py_ssize_t line_len = strcspn (tok -> line_start , "\n" );
1098
1099
if (line_len != tok -> cur - tok -> line_start ) {
1099
1100
Py_DECREF (errtext );
@@ -1104,8 +1105,8 @@ syntaxerror(struct tok_state *tok, const char *format, ...)
1104
1105
goto error ;
1105
1106
}
1106
1107
1107
- args = Py_BuildValue ("(O(OiiN ))" , errmsg ,
1108
- tok -> filename , tok -> lineno , offset , errtext );
1108
+ args = Py_BuildValue ("(O(OiiNii ))" , errmsg , tok -> filename , tok -> lineno ,
1109
+ col_offset , errtext , tok -> lineno , end_col_offset );
1109
1110
if (args ) {
1110
1111
PyErr_SetObject (PyExc_SyntaxError , args );
1111
1112
Py_DECREF (args );
@@ -1117,6 +1118,38 @@ syntaxerror(struct tok_state *tok, const char *format, ...)
1117
1118
return ERRORTOKEN ;
1118
1119
}
1119
1120
1121
+ static int
1122
+ syntaxerror (struct tok_state * tok , const char * format , ...)
1123
+ {
1124
+ va_list vargs ;
1125
+ #ifdef HAVE_STDARG_PROTOTYPES
1126
+ va_start (vargs , format );
1127
+ #else
1128
+ va_start (vargs );
1129
+ #endif
1130
+ int ret = _syntaxerror_range (tok , format , -1 , -1 , vargs );
1131
+ va_end (vargs );
1132
+ return ret ;
1133
+ }
1134
+
1135
+ static int
1136
+ syntaxerror_known_range (struct tok_state * tok ,
1137
+ int col_offset , int end_col_offset ,
1138
+ const char * format , ...)
1139
+ {
1140
+ va_list vargs ;
1141
+ #ifdef HAVE_STDARG_PROTOTYPES
1142
+ va_start (vargs , format );
1143
+ #else
1144
+ va_start (vargs );
1145
+ #endif
1146
+ int ret = _syntaxerror_range (tok , format , col_offset , end_col_offset , vargs );
1147
+ va_end (vargs );
1148
+ return ret ;
1149
+ }
1150
+
1151
+
1152
+
1120
1153
static int
1121
1154
indenterror (struct tok_state * tok )
1122
1155
{
@@ -1692,12 +1725,12 @@ tok_get(struct tok_state *tok, const char **p_start, const char **p_end)
1692
1725
c = tok_nextc (tok );
1693
1726
}
1694
1727
if (c < '0' || c >= '8' ) {
1695
- tok_backup (tok , c );
1696
1728
if (isdigit (c )) {
1697
1729
return syntaxerror (tok ,
1698
1730
"invalid digit '%c' in octal literal" , c );
1699
1731
}
1700
1732
else {
1733
+ tok_backup (tok , c );
1701
1734
return syntaxerror (tok , "invalid octal literal" );
1702
1735
}
1703
1736
}
@@ -1721,12 +1754,12 @@ tok_get(struct tok_state *tok, const char **p_start, const char **p_end)
1721
1754
c = tok_nextc (tok );
1722
1755
}
1723
1756
if (c != '0' && c != '1' ) {
1724
- tok_backup (tok , c );
1725
1757
if (isdigit (c )) {
1726
1758
return syntaxerror (tok ,
1727
1759
"invalid digit '%c' in binary literal" , c );
1728
1760
}
1729
1761
else {
1762
+ tok_backup (tok , c );
1730
1763
return syntaxerror (tok , "invalid binary literal" );
1731
1764
}
1732
1765
}
@@ -1759,6 +1792,7 @@ tok_get(struct tok_state *tok, const char **p_start, const char **p_end)
1759
1792
}
1760
1793
c = tok_nextc (tok );
1761
1794
}
1795
+ char * zeros_end = tok -> cur ;
1762
1796
if (isdigit (c )) {
1763
1797
nonzero = 1 ;
1764
1798
c = tok_decimal_tail (tok );
@@ -1779,10 +1813,12 @@ tok_get(struct tok_state *tok, const char **p_start, const char **p_end)
1779
1813
else if (nonzero ) {
1780
1814
/* Old-style octal: now disallowed. */
1781
1815
tok_backup (tok , c );
1782
- return syntaxerror (tok ,
1783
- "leading zeros in decimal integer "
1784
- "literals are not permitted; "
1785
- "use an 0o prefix for octal integers" );
1816
+ return syntaxerror_known_range (
1817
+ tok , (int )(tok -> start + 1 - tok -> line_start ),
1818
+ (int )(zeros_end - tok -> line_start ),
1819
+ "leading zeros in decimal integer "
1820
+ "literals are not permitted; "
1821
+ "use an 0o prefix for octal integers" );
1786
1822
}
1787
1823
if (!verify_end_of_number (tok , c , "decimal" )) {
1788
1824
return ERRORTOKEN ;
0 commit comments