/src/cpython/Parser/tokenizer/helpers.c
Line | Count | Source (jump to first uncovered line) |
1 | | #include "Python.h" |
2 | | #include "errcode.h" |
3 | | #include "pycore_token.h" |
4 | | |
5 | | #include "../lexer/state.h" |
6 | | |
7 | | |
8 | | /* ############## ERRORS ############## */ |
9 | | |
10 | | static int |
11 | | _syntaxerror_range(struct tok_state *tok, const char *format, |
12 | | int col_offset, int end_col_offset, |
13 | | va_list vargs) |
14 | 2.68k | { |
15 | | // In release builds, we don't want to overwrite a previous error, but in debug builds we |
16 | | // want to fail if we are not doing it so we can fix it. |
17 | 2.68k | assert(tok->done != E_ERROR); |
18 | 2.68k | if (tok->done == E_ERROR) { |
19 | 0 | return ERRORTOKEN; |
20 | 0 | } |
21 | 2.68k | PyObject *errmsg, *errtext, *args; |
22 | 2.68k | errmsg = PyUnicode_FromFormatV(format, vargs); |
23 | 2.68k | if (!errmsg) { |
24 | 0 | goto error; |
25 | 0 | } |
26 | | |
27 | 2.68k | errtext = PyUnicode_DecodeUTF8(tok->line_start, tok->cur - tok->line_start, |
28 | 2.68k | "replace"); |
29 | 2.68k | if (!errtext) { |
30 | 0 | goto error; |
31 | 0 | } |
32 | | |
33 | 2.68k | if (col_offset == -1) { |
34 | 2.65k | col_offset = (int)PyUnicode_GET_LENGTH(errtext); |
35 | 2.65k | } |
36 | 2.68k | if (end_col_offset == -1) { |
37 | 2.65k | end_col_offset = col_offset; |
38 | 2.65k | } |
39 | | |
40 | 2.68k | Py_ssize_t line_len = strcspn(tok->line_start, "\n"); |
41 | 2.68k | if (line_len != tok->cur - tok->line_start) { |
42 | 1.94k | Py_DECREF(errtext); |
43 | 1.94k | errtext = PyUnicode_DecodeUTF8(tok->line_start, line_len, |
44 | 1.94k | "replace"); |
45 | 1.94k | } |
46 | 2.68k | if (!errtext) { |
47 | 0 | goto error; |
48 | 0 | } |
49 | | |
50 | 2.68k | args = Py_BuildValue("(O(OiiNii))", errmsg, tok->filename, tok->lineno, |
51 | 2.68k | col_offset, errtext, tok->lineno, end_col_offset); |
52 | 2.68k | if (args) { |
53 | 2.68k | PyErr_SetObject(PyExc_SyntaxError, args); |
54 | 2.68k | Py_DECREF(args); |
55 | 2.68k | } |
56 | | |
57 | 2.68k | error: |
58 | 2.68k | Py_XDECREF(errmsg); |
59 | 2.68k | tok->done = E_ERROR; |
60 | 2.68k | return ERRORTOKEN; |
61 | 2.68k | } |
62 | | |
63 | | int |
64 | | _PyTokenizer_syntaxerror(struct tok_state *tok, const char *format, ...) |
65 | 2.65k | { |
66 | | // This errors are cleaned on startup. Todo: Fix it. |
67 | 2.65k | va_list vargs; |
68 | 2.65k | va_start(vargs, format); |
69 | 2.65k | int ret = _syntaxerror_range(tok, format, -1, -1, vargs); |
70 | 2.65k | va_end(vargs); |
71 | 2.65k | return ret; |
72 | 2.65k | } |
73 | | |
74 | | int |
75 | | _PyTokenizer_syntaxerror_known_range(struct tok_state *tok, |
76 | | int col_offset, int end_col_offset, |
77 | | const char *format, ...) |
78 | 32 | { |
79 | 32 | va_list vargs; |
80 | 32 | va_start(vargs, format); |
81 | 32 | int ret = _syntaxerror_range(tok, format, col_offset, end_col_offset, vargs); |
82 | 32 | va_end(vargs); |
83 | 32 | return ret; |
84 | 32 | } |
85 | | |
86 | | int |
87 | | _PyTokenizer_indenterror(struct tok_state *tok) |
88 | 5 | { |
89 | 5 | tok->done = E_TABSPACE; |
90 | 5 | tok->cur = tok->inp; |
91 | 5 | return ERRORTOKEN; |
92 | 5 | } |
93 | | |
94 | | char * |
95 | | _PyTokenizer_error_ret(struct tok_state *tok) /* XXX */ |
96 | 1.84k | { |
97 | 1.84k | tok->decoding_erred = 1; |
98 | 1.84k | if ((tok->fp != NULL || tok->readline != NULL) && tok->buf != NULL) {/* see _PyTokenizer_Free */ |
99 | 0 | PyMem_Free(tok->buf); |
100 | 0 | } |
101 | 1.84k | tok->buf = tok->cur = tok->inp = NULL; |
102 | 1.84k | tok->start = NULL; |
103 | 1.84k | tok->end = NULL; |
104 | 1.84k | tok->done = E_DECODE; |
105 | 1.84k | return NULL; /* as if it were EOF */ |
106 | 1.84k | } |
107 | | |
108 | | int |
109 | | _PyTokenizer_warn_invalid_escape_sequence(struct tok_state *tok, int first_invalid_escape_char) |
110 | 1.15k | { |
111 | 1.15k | if (!tok->report_warnings) { |
112 | 0 | return 0; |
113 | 0 | } |
114 | | |
115 | 1.15k | PyObject *msg = PyUnicode_FromFormat( |
116 | 1.15k | "\"\\%c\" is an invalid escape sequence. " |
117 | 1.15k | "Such sequences will not work in the future. " |
118 | 1.15k | "Did you mean \"\\\\%c\"? A raw string is also an option.", |
119 | 1.15k | (char) first_invalid_escape_char, |
120 | 1.15k | (char) first_invalid_escape_char |
121 | 1.15k | ); |
122 | | |
123 | 1.15k | if (msg == NULL) { |
124 | 0 | return -1; |
125 | 0 | } |
126 | | |
127 | 1.15k | if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, msg, tok->filename, |
128 | 1.15k | tok->lineno, NULL, NULL) < 0) { |
129 | 0 | Py_DECREF(msg); |
130 | |
|
131 | 0 | if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) { |
132 | | /* Replace the SyntaxWarning exception with a SyntaxError |
133 | | to get a more accurate error report */ |
134 | 0 | PyErr_Clear(); |
135 | |
|
136 | 0 | return _PyTokenizer_syntaxerror(tok, |
137 | 0 | "\"\\%c\" is an invalid escape sequence. " |
138 | 0 | "Did you mean \"\\\\%c\"? A raw string is also an option.", |
139 | 0 | (char) first_invalid_escape_char, |
140 | 0 | (char) first_invalid_escape_char); |
141 | 0 | } |
142 | | |
143 | 0 | return -1; |
144 | 0 | } |
145 | | |
146 | 1.15k | Py_DECREF(msg); |
147 | 1.15k | return 0; |
148 | 1.15k | } |
149 | | |
150 | | int |
151 | | _PyTokenizer_parser_warn(struct tok_state *tok, PyObject *category, const char *format, ...) |
152 | 10.6k | { |
153 | 10.6k | if (!tok->report_warnings) { |
154 | 0 | return 0; |
155 | 0 | } |
156 | | |
157 | 10.6k | PyObject *errmsg; |
158 | 10.6k | va_list vargs; |
159 | 10.6k | va_start(vargs, format); |
160 | 10.6k | errmsg = PyUnicode_FromFormatV(format, vargs); |
161 | 10.6k | va_end(vargs); |
162 | 10.6k | if (!errmsg) { |
163 | 0 | goto error; |
164 | 0 | } |
165 | | |
166 | 10.6k | if (PyErr_WarnExplicitObject(category, errmsg, tok->filename, |
167 | 10.6k | tok->lineno, NULL, NULL) < 0) { |
168 | 0 | if (PyErr_ExceptionMatches(category)) { |
169 | | /* Replace the DeprecationWarning exception with a SyntaxError |
170 | | to get a more accurate error report */ |
171 | 0 | PyErr_Clear(); |
172 | 0 | _PyTokenizer_syntaxerror(tok, "%U", errmsg); |
173 | 0 | } |
174 | 0 | goto error; |
175 | 0 | } |
176 | 10.6k | Py_DECREF(errmsg); |
177 | 10.6k | return 0; |
178 | | |
179 | 0 | error: |
180 | 0 | Py_XDECREF(errmsg); |
181 | 0 | tok->done = E_ERROR; |
182 | 0 | return -1; |
183 | 10.6k | } |
184 | | |
185 | | |
186 | | /* ############## STRING MANIPULATION ############## */ |
187 | | |
188 | | char * |
189 | | _PyTokenizer_new_string(const char *s, Py_ssize_t len, struct tok_state *tok) |
190 | 4.40k | { |
191 | 4.40k | char* result = (char *)PyMem_Malloc(len + 1); |
192 | 4.40k | if (!result) { |
193 | 0 | tok->done = E_NOMEM; |
194 | 0 | return NULL; |
195 | 0 | } |
196 | 4.40k | memcpy(result, s, len); |
197 | 4.40k | result[len] = '\0'; |
198 | 4.40k | return result; |
199 | 4.40k | } |
200 | | |
201 | | PyObject * |
202 | 4.33k | _PyTokenizer_translate_into_utf8(const char* str, const char* enc) { |
203 | 4.33k | PyObject *utf8; |
204 | 4.33k | PyObject* buf = PyUnicode_Decode(str, strlen(str), enc, NULL); |
205 | 4.33k | if (buf == NULL) |
206 | 1.78k | return NULL; |
207 | 2.55k | utf8 = PyUnicode_AsUTF8String(buf); |
208 | 2.55k | Py_DECREF(buf); |
209 | 2.55k | return utf8; |
210 | 4.33k | } |
211 | | |
212 | | char * |
213 | | _PyTokenizer_translate_newlines(const char *s, int exec_input, int preserve_crlf, |
214 | 23.4k | struct tok_state *tok) { |
215 | 23.4k | int skip_next_lf = 0; |
216 | 23.4k | size_t needed_length = strlen(s) + 2, final_length; |
217 | 23.4k | char *buf, *current; |
218 | 23.4k | char c = '\0'; |
219 | 23.4k | buf = PyMem_Malloc(needed_length); |
220 | 23.4k | if (buf == NULL) { |
221 | 0 | tok->done = E_NOMEM; |
222 | 0 | return NULL; |
223 | 0 | } |
224 | 6.98M | for (current = buf; *s; s++, current++) { |
225 | 6.96M | c = *s; |
226 | 6.96M | if (skip_next_lf) { |
227 | 12.4k | skip_next_lf = 0; |
228 | 12.4k | if (c == '\n') { |
229 | 261 | c = *++s; |
230 | 261 | if (!c) |
231 | 2 | break; |
232 | 261 | } |
233 | 12.4k | } |
234 | 6.96M | if (!preserve_crlf && c == '\r') { |
235 | 12.4k | skip_next_lf = 1; |
236 | 12.4k | c = '\n'; |
237 | 12.4k | } |
238 | 6.96M | *current = c; |
239 | 6.96M | } |
240 | | /* If this is exec input, add a newline to the end of the string if |
241 | | there isn't one already. */ |
242 | 23.4k | if (exec_input && c != '\n' && c != '\0') { |
243 | 22.7k | *current = '\n'; |
244 | 22.7k | current++; |
245 | 22.7k | } |
246 | 23.4k | *current = '\0'; |
247 | 23.4k | final_length = current - buf + 1; |
248 | 23.4k | if (final_length < needed_length && final_length) { |
249 | | /* should never fail */ |
250 | 677 | char* result = PyMem_Realloc(buf, final_length); |
251 | 677 | if (result == NULL) { |
252 | 0 | PyMem_Free(buf); |
253 | 0 | } |
254 | 677 | buf = result; |
255 | 677 | } |
256 | 23.4k | return buf; |
257 | 23.4k | } |
258 | | |
259 | | /* ############## ENCODING STUFF ############## */ |
260 | | |
261 | | |
262 | | /* See whether the file starts with a BOM. If it does, |
263 | | invoke the set_readline function with the new encoding. |
264 | | Return 1 on success, 0 on failure. */ |
265 | | int |
266 | | _PyTokenizer_check_bom(int get_char(struct tok_state *), |
267 | | void unget_char(int, struct tok_state *), |
268 | | int set_readline(struct tok_state *, const char *), |
269 | | struct tok_state *tok) |
270 | 23.3k | { |
271 | 23.3k | int ch1, ch2, ch3; |
272 | 23.3k | ch1 = get_char(tok); |
273 | 23.3k | tok->decoding_state = STATE_SEEK_CODING; |
274 | 23.3k | if (ch1 == EOF) { |
275 | 0 | return 1; |
276 | 23.3k | } else if (ch1 == 0xEF) { |
277 | 97 | ch2 = get_char(tok); |
278 | 97 | if (ch2 != 0xBB) { |
279 | 87 | unget_char(ch2, tok); |
280 | 87 | unget_char(ch1, tok); |
281 | 87 | return 1; |
282 | 87 | } |
283 | 10 | ch3 = get_char(tok); |
284 | 10 | if (ch3 != 0xBF) { |
285 | 4 | unget_char(ch3, tok); |
286 | 4 | unget_char(ch2, tok); |
287 | 4 | unget_char(ch1, tok); |
288 | 4 | return 1; |
289 | 4 | } |
290 | 23.2k | } else { |
291 | 23.2k | unget_char(ch1, tok); |
292 | 23.2k | return 1; |
293 | 23.2k | } |
294 | 6 | if (tok->encoding != NULL) |
295 | 0 | PyMem_Free(tok->encoding); |
296 | 6 | tok->encoding = _PyTokenizer_new_string("utf-8", 5, tok); |
297 | 6 | if (!tok->encoding) |
298 | 0 | return 0; |
299 | | /* No need to set_readline: input is already utf-8 */ |
300 | 6 | return 1; |
301 | 6 | } |
302 | | |
303 | | static const char * |
304 | | get_normal_name(const char *s) /* for utf-8 and latin-1 */ |
305 | 4.33k | { |
306 | 4.33k | char buf[13]; |
307 | 4.33k | int i; |
308 | 27.9k | for (i = 0; i < 12; i++) { |
309 | 27.6k | int c = s[i]; |
310 | 27.6k | if (c == '\0') |
311 | 4.09k | break; |
312 | 23.5k | else if (c == '_') |
313 | 686 | buf[i] = '-'; |
314 | 22.8k | else |
315 | 22.8k | buf[i] = Py_TOLOWER(c); |
316 | 27.6k | } |
317 | 4.33k | buf[i] = '\0'; |
318 | 4.33k | if (strcmp(buf, "utf-8") == 0 || |
319 | 4.33k | strncmp(buf, "utf-8-", 6) == 0) |
320 | 3 | return "utf-8"; |
321 | 4.33k | else if (strcmp(buf, "latin-1") == 0 || |
322 | 4.33k | strcmp(buf, "iso-8859-1") == 0 || |
323 | 4.33k | strcmp(buf, "iso-latin-1") == 0 || |
324 | 4.33k | strncmp(buf, "latin-1-", 8) == 0 || |
325 | 4.33k | strncmp(buf, "iso-8859-1-", 11) == 0 || |
326 | 4.33k | strncmp(buf, "iso-latin-1-", 12) == 0) |
327 | 15 | return "iso-8859-1"; |
328 | 4.32k | else |
329 | 4.32k | return s; |
330 | 4.33k | } |
331 | | |
332 | | /* Return the coding spec in S, or NULL if none is found. */ |
333 | | static int |
334 | | get_coding_spec(const char *s, char **spec, Py_ssize_t size, struct tok_state *tok) |
335 | 24.1k | { |
336 | 24.1k | Py_ssize_t i; |
337 | 24.1k | *spec = NULL; |
338 | | /* Coding spec must be in a comment, and that comment must be |
339 | | * the only statement on the source code line. */ |
340 | 30.3k | for (i = 0; i < size - 6; i++) { |
341 | 24.7k | if (s[i] == '#') |
342 | 4.64k | break; |
343 | 20.1k | if (s[i] != ' ' && s[i] != '\t' && s[i] != '\014') |
344 | 13.9k | return 1; |
345 | 20.1k | } |
346 | 206k | for (; i < size - 6; i++) { /* XXX inefficient search */ |
347 | 200k | const char* t = s + i; |
348 | 200k | if (memcmp(t, "coding", 6) == 0) { |
349 | 4.96k | const char* begin = NULL; |
350 | 4.96k | t += 6; |
351 | 4.96k | if (t[0] != ':' && t[0] != '=') |
352 | 222 | continue; |
353 | 5.46k | do { |
354 | 5.46k | t++; |
355 | 5.46k | } while (t[0] == ' ' || t[0] == '\t'); |
356 | | |
357 | 4.73k | begin = t; |
358 | 33.9k | while (Py_ISALNUM(t[0]) || |
359 | 33.9k | t[0] == '-' || t[0] == '_' || t[0] == '.') |
360 | 29.1k | t++; |
361 | | |
362 | 4.73k | if (begin < t) { |
363 | 4.33k | char* r = _PyTokenizer_new_string(begin, t - begin, tok); |
364 | 4.33k | const char* q; |
365 | 4.33k | if (!r) |
366 | 0 | return 0; |
367 | 4.33k | q = get_normal_name(r); |
368 | 4.33k | if (r != q) { |
369 | 18 | PyMem_Free(r); |
370 | 18 | r = _PyTokenizer_new_string(q, strlen(q), tok); |
371 | 18 | if (!r) |
372 | 0 | return 0; |
373 | 18 | } |
374 | 4.33k | *spec = r; |
375 | 4.33k | break; |
376 | 4.33k | } |
377 | 4.73k | } |
378 | 200k | } |
379 | 10.2k | return 1; |
380 | 10.2k | } |
381 | | |
382 | | /* Check whether the line contains a coding spec. If it does, |
383 | | invoke the set_readline function for the new encoding. |
384 | | This function receives the tok_state and the new encoding. |
385 | | Return 1 on success, 0 on failure. */ |
386 | | int |
387 | | _PyTokenizer_check_coding_spec(const char* line, Py_ssize_t size, struct tok_state *tok, |
388 | | int set_readline(struct tok_state *, const char *)) |
389 | 24.1k | { |
390 | 24.1k | char *cs; |
391 | 24.1k | if (tok->cont_line) { |
392 | | /* It's a continuation line, so it can't be a coding spec. */ |
393 | 0 | tok->decoding_state = STATE_NORMAL; |
394 | 0 | return 1; |
395 | 0 | } |
396 | 24.1k | if (!get_coding_spec(line, &cs, size, tok)) { |
397 | 0 | return 0; |
398 | 0 | } |
399 | 24.1k | if (!cs) { |
400 | 19.8k | Py_ssize_t i; |
401 | 26.2k | for (i = 0; i < size; i++) { |
402 | 25.7k | if (line[i] == '#' || line[i] == '\n' || line[i] == '\r') |
403 | 708 | break; |
404 | 25.0k | if (line[i] != ' ' && line[i] != '\t' && line[i] != '\014') { |
405 | | /* Stop checking coding spec after a line containing |
406 | | * anything except a comment. */ |
407 | 18.7k | tok->decoding_state = STATE_NORMAL; |
408 | 18.7k | break; |
409 | 18.7k | } |
410 | 25.0k | } |
411 | 19.8k | return 1; |
412 | 19.8k | } |
413 | 4.33k | tok->decoding_state = STATE_NORMAL; |
414 | 4.33k | if (tok->encoding == NULL) { |
415 | 4.33k | assert(tok->decoding_readline == NULL); |
416 | 4.33k | if (strcmp(cs, "utf-8") != 0 && !set_readline(tok, cs)) { |
417 | 0 | _PyTokenizer_error_ret(tok); |
418 | 0 | PyErr_Format(PyExc_SyntaxError, "encoding problem: %s", cs); |
419 | 0 | PyMem_Free(cs); |
420 | 0 | return 0; |
421 | 0 | } |
422 | 4.33k | tok->encoding = cs; |
423 | 4.33k | } else { /* then, compare cs with BOM */ |
424 | 4 | if (strcmp(tok->encoding, cs) != 0) { |
425 | 3 | _PyTokenizer_error_ret(tok); |
426 | 3 | PyErr_Format(PyExc_SyntaxError, |
427 | 3 | "encoding problem: %s with BOM", cs); |
428 | 3 | PyMem_Free(cs); |
429 | 3 | return 0; |
430 | 3 | } |
431 | 1 | PyMem_Free(cs); |
432 | 1 | } |
433 | 4.33k | return 1; |
434 | 4.33k | } |
435 | | |
436 | | /* Check whether the characters at s start a valid |
437 | | UTF-8 sequence. Return the number of characters forming |
438 | | the sequence if yes, 0 if not. The special cases match |
439 | | those in stringlib/codecs.h:utf8_decode. |
440 | | */ |
441 | | static int |
442 | | valid_utf8(const unsigned char* s) |
443 | 0 | { |
444 | 0 | int expected = 0; |
445 | 0 | int length; |
446 | 0 | if (*s < 0x80) { |
447 | | /* single-byte code */ |
448 | 0 | return 1; |
449 | 0 | } |
450 | 0 | else if (*s < 0xE0) { |
451 | | /* \xC2\x80-\xDF\xBF -- 0080-07FF */ |
452 | 0 | if (*s < 0xC2) { |
453 | | /* invalid sequence |
454 | | \x80-\xBF -- continuation byte |
455 | | \xC0-\xC1 -- fake 0000-007F */ |
456 | 0 | return 0; |
457 | 0 | } |
458 | 0 | expected = 1; |
459 | 0 | } |
460 | 0 | else if (*s < 0xF0) { |
461 | | /* \xE0\xA0\x80-\xEF\xBF\xBF -- 0800-FFFF */ |
462 | 0 | if (*s == 0xE0 && *(s + 1) < 0xA0) { |
463 | | /* invalid sequence |
464 | | \xE0\x80\x80-\xE0\x9F\xBF -- fake 0000-0800 */ |
465 | 0 | return 0; |
466 | 0 | } |
467 | 0 | else if (*s == 0xED && *(s + 1) >= 0xA0) { |
468 | | /* Decoding UTF-8 sequences in range \xED\xA0\x80-\xED\xBF\xBF |
469 | | will result in surrogates in range D800-DFFF. Surrogates are |
470 | | not valid UTF-8 so they are rejected. |
471 | | See https://www.unicode.org/versions/Unicode5.2.0/ch03.pdf |
472 | | (table 3-7) and http://www.rfc-editor.org/rfc/rfc3629.txt */ |
473 | 0 | return 0; |
474 | 0 | } |
475 | 0 | expected = 2; |
476 | 0 | } |
477 | 0 | else if (*s < 0xF5) { |
478 | | /* \xF0\x90\x80\x80-\xF4\x8F\xBF\xBF -- 10000-10FFFF */ |
479 | 0 | if (*(s + 1) < 0x90 ? *s == 0xF0 : *s == 0xF4) { |
480 | | /* invalid sequence -- one of: |
481 | | \xF0\x80\x80\x80-\xF0\x8F\xBF\xBF -- fake 0000-FFFF |
482 | | \xF4\x90\x80\x80- -- 110000- overflow */ |
483 | 0 | return 0; |
484 | 0 | } |
485 | 0 | expected = 3; |
486 | 0 | } |
487 | 0 | else { |
488 | | /* invalid start byte */ |
489 | 0 | return 0; |
490 | 0 | } |
491 | 0 | length = expected + 1; |
492 | 0 | for (; expected; expected--) |
493 | 0 | if (s[expected] < 0x80 || s[expected] >= 0xC0) |
494 | 0 | return 0; |
495 | 0 | return length; |
496 | 0 | } |
497 | | |
498 | | int |
499 | | _PyTokenizer_ensure_utf8(char *line, struct tok_state *tok) |
500 | 0 | { |
501 | 0 | int badchar = 0; |
502 | 0 | unsigned char *c; |
503 | 0 | int length; |
504 | 0 | for (c = (unsigned char *)line; *c; c += length) { |
505 | 0 | if (!(length = valid_utf8(c))) { |
506 | 0 | badchar = *c; |
507 | 0 | break; |
508 | 0 | } |
509 | 0 | } |
510 | 0 | if (badchar) { |
511 | 0 | PyErr_Format(PyExc_SyntaxError, |
512 | 0 | "Non-UTF-8 code starting with '\\x%.2x' " |
513 | 0 | "in file %U on line %i, " |
514 | 0 | "but no encoding declared; " |
515 | 0 | "see https://peps.python.org/pep-0263/ for details", |
516 | 0 | badchar, tok->filename, tok->lineno); |
517 | 0 | return 0; |
518 | 0 | } |
519 | 0 | return 1; |
520 | 0 | } |
521 | | |
522 | | |
523 | | /* ############## DEBUGGING STUFF ############## */ |
524 | | |
525 | | #ifdef Py_DEBUG |
526 | | void |
527 | | _PyTokenizer_print_escape(FILE *f, const char *s, Py_ssize_t size) |
528 | | { |
529 | | if (s == NULL) { |
530 | | fputs("NULL", f); |
531 | | return; |
532 | | } |
533 | | putc('"', f); |
534 | | while (size-- > 0) { |
535 | | unsigned char c = *s++; |
536 | | switch (c) { |
537 | | case '\n': fputs("\\n", f); break; |
538 | | case '\r': fputs("\\r", f); break; |
539 | | case '\t': fputs("\\t", f); break; |
540 | | case '\f': fputs("\\f", f); break; |
541 | | case '\'': fputs("\\'", f); break; |
542 | | case '"': fputs("\\\"", f); break; |
543 | | default: |
544 | | if (0x20 <= c && c <= 0x7f) |
545 | | putc(c, f); |
546 | | else |
547 | | fprintf(f, "\\x%02x", c); |
548 | | } |
549 | | } |
550 | | putc('"', f); |
551 | | } |
552 | | |
553 | | void |
554 | | _PyTokenizer_tok_dump(int type, char *start, char *end) |
555 | | { |
556 | | fprintf(stderr, "%s", _PyParser_TokenNames[type]); |
557 | | if (type == NAME || type == NUMBER || type == STRING || type == OP) |
558 | | fprintf(stderr, "(%.*s)", (int)(end - start), start); |
559 | | } |
560 | | #endif |