Skip to content

Commit 681f2a5

Browse files
committed
Fix more problems
1 parent 73af4bb commit 681f2a5

File tree

5 files changed

+52
-7
lines changed

5 files changed

+52
-7
lines changed

Lib/tabnanny.py

+10
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,10 @@ def check(file):
107107
errprint("%r: Token Error: %s" % (file, msg))
108108
return
109109

110+
except SyntaxError as msg:
111+
errprint("%r: Token Error: %s" % (file, msg))
112+
return
113+
110114
except IndentationError as msg:
111115
errprint("%r: Indentation Error: %s" % (file, msg))
112116
return
@@ -272,6 +276,12 @@ def format_witnesses(w):
272276
return prefix + " " + ', '.join(firsts)
273277

274278
def process_tokens(tokens):
279+
try:
280+
_process_tokens(tokens)
281+
except TabError as e:
282+
raise NannyNag(e.lineno, e.msg, e.text)
283+
284+
def _process_tokens(tokens):
275285
INDENT = tokenize.INDENT
276286
DEDENT = tokenize.DEDENT
277287
NEWLINE = tokenize.NEWLINE

Lib/test/test_tabnanny.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ def test_when_nannynag_error_verbose(self):
223223
with TemporaryPyFile(SOURCE_CODES["nannynag_errored"]) as file_path:
224224
out = f"{file_path!r}: *** Line 3: trouble in tab city! ***\n"
225225
out += "offending line: '\\tprint(\"world\")\\n'\n"
226-
out += "indent not equal e.g. at tab size 1\n"
226+
out += "inconsistent use of tabs and spaces in indentation\n"
227227

228228
tabnanny.verbose = 1
229229
self.verify_tabnanny_check(file_path, out=out)
@@ -315,7 +315,7 @@ def validate_cmd(self, *args, stdout="", stderr="", partial=False, expect_failur
315315
def test_with_errored_file(self):
316316
"""Should displays error when errored python file is given."""
317317
with TemporaryPyFile(SOURCE_CODES["wrong_indented"]) as file_path:
318-
stderr = f"{file_path!r}: Indentation Error: "
318+
stderr = f"{file_path!r}: Token Error: "
319319
stderr += ('unindent does not match any outer indentation level'
320320
' (<tokenize>, line 3)')
321321
self.validate_cmd(file_path, stderr=stderr, expect_failure=True)

Lib/tokenize.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,10 @@ def error(message, filename=None, location=None):
517517
tokens = list(tokenize(f.readline))
518518
else:
519519
filename = "<stdin>"
520-
tokens = _tokenize(sys.stdin.readline, None)
520+
tokens = _tokenize(
521+
(x.encode('utf-8') for x in iter(sys.stdin.readline, "")
522+
), "utf-8")
523+
521524

522525
# Output the tokenization
523526
for token in tokens:

Lib/trace.py

-1
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,6 @@ def _find_strings(filename, encoding=None):
360360
# Add this special case so that the test in the loop passes.
361361
prev_ttype = token.INDENT
362362
with open(filename, encoding=encoding) as f:
363-
print(filename)
364363
tok = tokenize.generate_tokens(f.readline)
365364
for ttype, tstr, start, end, line in tok:
366365
if ttype == token.STRING:

Python/Python-tokenize.c

+36-3
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,10 @@ _tokenizer_error(struct tok_state *tok)
8989
}
9090
return -1;
9191
case E_DEDENT:
92-
PyErr_SetString(PyExc_IndentationError,
93-
"unindent does not match any outer indentation level");
92+
PyErr_Format(PyExc_IndentationError,
93+
"unindent does not match any outer indentation level "
94+
"(<tokenize>, line %d)",
95+
tok->lineno);
9496
return -1;
9597
case E_INTR:
9698
if (!PyErr_Occurred()) {
@@ -115,7 +117,38 @@ _tokenizer_error(struct tok_state *tok)
115117
default:
116118
msg = "unknown tokenization error";
117119
}
118-
PyErr_SetString(errtype, msg);
120+
121+
// TODO: Clean up this code and factor out common error paths
122+
123+
PyObject* errstr = NULL;
124+
PyObject* error_line = NULL;
125+
126+
Py_ssize_t size = tok->inp - tok->buf;
127+
error_line = PyUnicode_DecodeUTF8(tok->buf, size, "replace");
128+
if (!error_line) {
129+
goto error;
130+
}
131+
PyObject *tmp = Py_BuildValue("(OnnOii)", tok->filename, tok->lineno, 0, error_line, 0, 0);
132+
if (!tmp) {
133+
goto error;
134+
}
135+
Py_CLEAR(error_line);
136+
errstr = PyUnicode_FromString(msg);
137+
if (!errstr) {
138+
goto error;
139+
}
140+
PyObject* value = PyTuple_Pack(2, errstr, tmp);
141+
Py_DECREF(errstr);
142+
Py_DECREF(tmp);
143+
if (!value) {
144+
goto error;
145+
}
146+
PyErr_SetObject(errtype, value);
147+
Py_DECREF(value);
148+
return 0;
149+
error:
150+
Py_XDECREF(errstr);
151+
Py_XDECREF(error_line);
119152
return -1;
120153
}
121154

0 commit comments

Comments
 (0)