blob: cebfadc8e89f3040cdb685a9761dbfd9428623fb [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002/* Tokenizer implementation */
3
Serhiy Storchaka0cc6b5e2020-02-12 12:17:00 +02004#define PY_SSIZE_T_CLEAN
Jack Jansen7b8c7542002-04-14 20:12:41 +00005#include "Python.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +00006
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00007#include <ctype.h>
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00008#include <assert.h>
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00009
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000010#include "tokenizer.h"
11#include "errcode.h"
12
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +000013#include "unicodeobject.h"
Christian Heimes2c9c7a52008-05-26 13:42:13 +000014#include "bytesobject.h"
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +000015#include "fileobject.h"
16#include "codecs.h"
17#include "abstract.h"
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +000018
Victor Stinnerf2ddc6a2017-11-17 01:25:47 -080019/* Alternate tab spacing */
20#define ALTTABSIZE 1
21
Martin v. Löwis5b222132007-06-10 09:51:05 +000022#define is_potential_identifier_start(c) (\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000023 (c >= 'a' && c <= 'z')\
24 || (c >= 'A' && c <= 'Z')\
25 || c == '_'\
26 || (c >= 128))
Martin v. Löwis5b222132007-06-10 09:51:05 +000027
28#define is_potential_identifier_char(c) (\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000029 (c >= 'a' && c <= 'z')\
30 || (c >= 'A' && c <= 'Z')\
31 || (c >= '0' && c <= '9')\
32 || c == '_'\
33 || (c >= 128))
Martin v. Löwis5b222132007-06-10 09:51:05 +000034
Guido van Rossumf4b1a641994-08-29 12:43:07 +000035
Guido van Rossum4fe87291992-02-26 15:24:44 +000036/* Don't ever change this -- it would break the portability of Python code */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000037#define TABSIZE 8
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000038
Guido van Rossum3f5da241990-12-20 15:06:42 +000039/* Forward */
Tim Petersdbd9ba62000-07-09 03:09:57 +000040static struct tok_state *tok_new(void);
41static int tok_nextc(struct tok_state *tok);
42static void tok_backup(struct tok_state *tok, int c);
Guido van Rossum3f5da241990-12-20 15:06:42 +000043
Brett Cannond5ec98c2007-10-20 02:54:14 +000044
Guido van Rossumdcfcd142019-01-31 03:40:27 -080045/* Spaces in this constant are treated as "zero or more spaces or tabs" when
46 tokenizing. */
47static const char* type_comment_prefix = "# type: ";
48
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000049/* Create and initialize a new tok_state structure */
50
51static struct tok_state *
Thomas Wouters23c9e002000-07-22 19:20:54 +000052tok_new(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000053{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000054 struct tok_state *tok = (struct tok_state *)PyMem_MALLOC(
55 sizeof(struct tok_state));
56 if (tok == NULL)
57 return NULL;
Andy Lester384f3c52020-02-27 20:44:52 -060058 tok->buf = tok->cur = tok->inp = NULL;
59 tok->start = NULL;
60 tok->end = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000061 tok->done = E_OK;
62 tok->fp = NULL;
63 tok->input = NULL;
64 tok->tabsize = TABSIZE;
65 tok->indent = 0;
66 tok->indstack[0] = 0;
Yury Selivanov75445082015-05-11 22:57:16 -040067
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000068 tok->atbol = 1;
69 tok->pendin = 0;
70 tok->prompt = tok->nextprompt = NULL;
71 tok->lineno = 0;
72 tok->level = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000073 tok->altindstack[0] = 0;
74 tok->decoding_state = STATE_INIT;
75 tok->decoding_erred = 0;
76 tok->read_coding_spec = 0;
77 tok->enc = NULL;
78 tok->encoding = NULL;
79 tok->cont_line = 0;
Victor Stinner7f2fee32011-04-05 00:39:01 +020080 tok->filename = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000081 tok->decoding_readline = NULL;
82 tok->decoding_buffer = NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -080083 tok->type_comments = 0;
Yury Selivanov96ec9342015-07-23 15:01:58 +030084
Guido van Rossum495da292019-03-07 12:38:08 -080085 tok->async_hacks = 0;
86 tok->async_def = 0;
87 tok->async_def_indent = 0;
88 tok->async_def_nl = 0;
89
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000090 return tok;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000091}
92
Benjamin Petersonaeaa5922009-11-13 00:17:59 +000093static char *
Benjamin Peterson2dbfd882013-07-15 19:15:34 -070094new_string(const char *s, Py_ssize_t len, struct tok_state *tok)
Benjamin Petersonaeaa5922009-11-13 00:17:59 +000095{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000096 char* result = (char *)PyMem_MALLOC(len + 1);
Benjamin Peterson2dbfd882013-07-15 19:15:34 -070097 if (!result) {
98 tok->done = E_NOMEM;
99 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000100 }
Benjamin Peterson2dbfd882013-07-15 19:15:34 -0700101 memcpy(result, s, len);
102 result[len] = '\0';
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000103 return result;
Benjamin Petersonaeaa5922009-11-13 00:17:59 +0000104}
105
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000106static char *
107error_ret(struct tok_state *tok) /* XXX */
108{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000109 tok->decoding_erred = 1;
110 if (tok->fp != NULL && tok->buf != NULL) /* see PyTokenizer_Free */
111 PyMem_FREE(tok->buf);
Andy Lester384f3c52020-02-27 20:44:52 -0600112 tok->buf = tok->cur = tok->inp = NULL;
113 tok->start = NULL;
114 tok->end = NULL;
Serhiy Storchaka0d441112015-11-14 15:10:35 +0200115 tok->done = E_DECODE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000116 return NULL; /* as if it were EOF */
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000117}
118
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000119
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200120static const char *
121get_normal_name(const char *s) /* for utf-8 and latin-1 */
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000122{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000123 char buf[13];
124 int i;
125 for (i = 0; i < 12; i++) {
126 int c = s[i];
127 if (c == '\0')
128 break;
129 else if (c == '_')
130 buf[i] = '-';
131 else
132 buf[i] = tolower(c);
133 }
134 buf[i] = '\0';
135 if (strcmp(buf, "utf-8") == 0 ||
136 strncmp(buf, "utf-8-", 6) == 0)
137 return "utf-8";
138 else if (strcmp(buf, "latin-1") == 0 ||
139 strcmp(buf, "iso-8859-1") == 0 ||
140 strcmp(buf, "iso-latin-1") == 0 ||
141 strncmp(buf, "latin-1-", 8) == 0 ||
142 strncmp(buf, "iso-8859-1-", 11) == 0 ||
143 strncmp(buf, "iso-latin-1-", 12) == 0)
144 return "iso-8859-1";
145 else
146 return s;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000147}
148
149/* Return the coding spec in S, or NULL if none is found. */
150
Benjamin Peterson2dbfd882013-07-15 19:15:34 -0700151static int
152get_coding_spec(const char *s, char **spec, Py_ssize_t size, struct tok_state *tok)
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000153{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000154 Py_ssize_t i;
Benjamin Peterson2dbfd882013-07-15 19:15:34 -0700155 *spec = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000156 /* Coding spec must be in a comment, and that comment must be
157 * the only statement on the source code line. */
158 for (i = 0; i < size - 6; i++) {
159 if (s[i] == '#')
160 break;
161 if (s[i] != ' ' && s[i] != '\t' && s[i] != '\014')
Benjamin Peterson2dbfd882013-07-15 19:15:34 -0700162 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000163 }
164 for (; i < size - 6; i++) { /* XXX inefficient search */
165 const char* t = s + i;
166 if (strncmp(t, "coding", 6) == 0) {
167 const char* begin = NULL;
168 t += 6;
169 if (t[0] != ':' && t[0] != '=')
170 continue;
171 do {
172 t++;
173 } while (t[0] == '\x20' || t[0] == '\t');
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000174
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000175 begin = t;
176 while (Py_ISALNUM(t[0]) ||
177 t[0] == '-' || t[0] == '_' || t[0] == '.')
178 t++;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000179
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000180 if (begin < t) {
Benjamin Peterson2dbfd882013-07-15 19:15:34 -0700181 char* r = new_string(begin, t - begin, tok);
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200182 const char* q;
Benjamin Peterson2dbfd882013-07-15 19:15:34 -0700183 if (!r)
184 return 0;
Benjamin Peterson265fba42013-07-15 20:50:22 -0700185 q = get_normal_name(r);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000186 if (r != q) {
187 PyMem_FREE(r);
Benjamin Peterson2dbfd882013-07-15 19:15:34 -0700188 r = new_string(q, strlen(q), tok);
189 if (!r)
190 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000191 }
Benjamin Peterson2dbfd882013-07-15 19:15:34 -0700192 *spec = r;
Serhiy Storchakae431d3c2016-03-20 23:36:29 +0200193 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000194 }
195 }
196 }
Benjamin Peterson2dbfd882013-07-15 19:15:34 -0700197 return 1;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000198}
199
200/* Check whether the line contains a coding spec. If it does,
201 invoke the set_readline function for the new encoding.
202 This function receives the tok_state and the new encoding.
203 Return 1 on success, 0 on failure. */
204
205static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000206check_coding_spec(const char* line, Py_ssize_t size, struct tok_state *tok,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000207 int set_readline(struct tok_state *, const char *))
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000208{
Benjamin Peterson2dbfd882013-07-15 19:15:34 -0700209 char *cs;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000210 int r = 1;
Tim Peters17db21f2002-09-03 15:39:58 +0000211
Serhiy Storchaka768c16c2014-01-09 18:36:09 +0200212 if (tok->cont_line) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000213 /* It's a continuation line, so it can't be a coding spec. */
Serhiy Storchaka768c16c2014-01-09 18:36:09 +0200214 tok->read_coding_spec = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000215 return 1;
Serhiy Storchaka768c16c2014-01-09 18:36:09 +0200216 }
Benjamin Peterson2dbfd882013-07-15 19:15:34 -0700217 if (!get_coding_spec(line, &cs, size, tok))
218 return 0;
Serhiy Storchaka768c16c2014-01-09 18:36:09 +0200219 if (!cs) {
220 Py_ssize_t i;
221 for (i = 0; i < size; i++) {
222 if (line[i] == '#' || line[i] == '\n' || line[i] == '\r')
223 break;
224 if (line[i] != ' ' && line[i] != '\t' && line[i] != '\014') {
225 /* Stop checking coding spec after a line containing
226 * anything except a comment. */
227 tok->read_coding_spec = 1;
228 break;
229 }
230 }
Benjamin Peterson2dbfd882013-07-15 19:15:34 -0700231 return 1;
Serhiy Storchaka768c16c2014-01-09 18:36:09 +0200232 }
Benjamin Peterson2dbfd882013-07-15 19:15:34 -0700233 tok->read_coding_spec = 1;
234 if (tok->encoding == NULL) {
235 assert(tok->decoding_state == STATE_RAW);
236 if (strcmp(cs, "utf-8") == 0) {
237 tok->encoding = cs;
238 } else {
239 r = set_readline(tok, cs);
240 if (r) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000241 tok->encoding = cs;
Benjamin Peterson2dbfd882013-07-15 19:15:34 -0700242 tok->decoding_state = STATE_NORMAL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000243 }
Benjamin Peterson2dbfd882013-07-15 19:15:34 -0700244 else {
Serhiy Storchaka3af14aa2013-06-09 16:51:52 +0300245 PyErr_Format(PyExc_SyntaxError,
Benjamin Peterson2dbfd882013-07-15 19:15:34 -0700246 "encoding problem: %s", cs);
247 PyMem_FREE(cs);
248 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000249 }
Benjamin Peterson2dbfd882013-07-15 19:15:34 -0700250 } else { /* then, compare cs with BOM */
251 r = (strcmp(tok->encoding, cs) == 0);
252 if (!r)
253 PyErr_Format(PyExc_SyntaxError,
254 "encoding problem: %s with BOM", cs);
255 PyMem_FREE(cs);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000256 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000257 return r;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000258}
259
260/* See whether the file starts with a BOM. If it does,
261 invoke the set_readline function with the new encoding.
262 Return 1 on success, 0 on failure. */
263
264static int
265check_bom(int get_char(struct tok_state *),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000266 void unget_char(int, struct tok_state *),
267 int set_readline(struct tok_state *, const char *),
268 struct tok_state *tok)
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000269{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000270 int ch1, ch2, ch3;
271 ch1 = get_char(tok);
272 tok->decoding_state = STATE_RAW;
273 if (ch1 == EOF) {
274 return 1;
275 } else if (ch1 == 0xEF) {
276 ch2 = get_char(tok);
277 if (ch2 != 0xBB) {
278 unget_char(ch2, tok);
279 unget_char(ch1, tok);
280 return 1;
281 }
282 ch3 = get_char(tok);
283 if (ch3 != 0xBF) {
284 unget_char(ch3, tok);
285 unget_char(ch2, tok);
286 unget_char(ch1, tok);
287 return 1;
288 }
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000289#if 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000290 /* Disable support for UTF-16 BOMs until a decision
291 is made whether this needs to be supported. */
292 } else if (ch1 == 0xFE) {
293 ch2 = get_char(tok);
294 if (ch2 != 0xFF) {
295 unget_char(ch2, tok);
296 unget_char(ch1, tok);
297 return 1;
298 }
299 if (!set_readline(tok, "utf-16-be"))
300 return 0;
301 tok->decoding_state = STATE_NORMAL;
302 } else if (ch1 == 0xFF) {
303 ch2 = get_char(tok);
304 if (ch2 != 0xFE) {
305 unget_char(ch2, tok);
306 unget_char(ch1, tok);
307 return 1;
308 }
309 if (!set_readline(tok, "utf-16-le"))
310 return 0;
311 tok->decoding_state = STATE_NORMAL;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000312#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000313 } else {
314 unget_char(ch1, tok);
315 return 1;
316 }
317 if (tok->encoding != NULL)
318 PyMem_FREE(tok->encoding);
Benjamin Peterson2dbfd882013-07-15 19:15:34 -0700319 tok->encoding = new_string("utf-8", 5, tok);
320 if (!tok->encoding)
321 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000322 /* No need to set_readline: input is already utf-8 */
323 return 1;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000324}
325
326/* Read a line of text from TOK into S, using the stream in TOK.
Walter Dörwaldc1f5fff2005-07-12 21:53:43 +0000327 Return NULL on failure, else S.
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000328
Walter Dörwaldc1f5fff2005-07-12 21:53:43 +0000329 On entry, tok->decoding_buffer will be one of:
330 1) NULL: need to call tok->decoding_readline to get a new line
331 2) PyUnicodeObject *: decoding_feof has called tok->decoding_readline and
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000332 stored the result in tok->decoding_buffer
Christian Heimes9c4756e2008-05-26 13:22:05 +0000333 3) PyByteArrayObject *: previous call to fp_readl did not have enough room
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000334 (in the s buffer) to copy entire contents of the line read
335 by tok->decoding_readline. tok->decoding_buffer has the overflow.
336 In this case, fp_readl is called in a loop (with an expanded buffer)
337 until the buffer ends with a '\n' (or until the end of the file is
338 reached): see tok_nextc and its calls to decoding_fgets.
Walter Dörwaldc1f5fff2005-07-12 21:53:43 +0000339*/
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000340
341static char *
342fp_readl(char *s, int size, struct tok_state *tok)
343{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000344 PyObject* bufobj;
345 const char *buf;
346 Py_ssize_t buflen;
Walter Dörwaldc1f5fff2005-07-12 21:53:43 +0000347
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000348 /* Ask for one less byte so we can terminate it */
349 assert(size > 0);
350 size--;
Walter Dörwaldc1f5fff2005-07-12 21:53:43 +0000351
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000352 if (tok->decoding_buffer) {
353 bufobj = tok->decoding_buffer;
354 Py_INCREF(bufobj);
355 }
356 else
357 {
Victor Stinnera5ed5f02016-12-06 18:45:50 +0100358 bufobj = _PyObject_CallNoArg(tok->decoding_readline);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000359 if (bufobj == NULL)
360 goto error;
361 }
362 if (PyUnicode_CheckExact(bufobj))
363 {
Serhiy Storchaka06515832016-11-20 09:13:07 +0200364 buf = PyUnicode_AsUTF8AndSize(bufobj, &buflen);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000365 if (buf == NULL) {
366 goto error;
367 }
368 }
369 else
370 {
371 buf = PyByteArray_AsString(bufobj);
372 if (buf == NULL) {
373 goto error;
374 }
375 buflen = PyByteArray_GET_SIZE(bufobj);
376 }
Amaury Forgeot d'Arc65f9ace2007-11-15 23:19:43 +0000377
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000378 Py_XDECREF(tok->decoding_buffer);
379 if (buflen > size) {
380 /* Too many chars, the rest goes into tok->decoding_buffer */
381 tok->decoding_buffer = PyByteArray_FromStringAndSize(buf+size,
382 buflen-size);
383 if (tok->decoding_buffer == NULL)
384 goto error;
385 buflen = size;
386 }
387 else
388 tok->decoding_buffer = NULL;
Amaury Forgeot d'Arc65f9ace2007-11-15 23:19:43 +0000389
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000390 memcpy(s, buf, buflen);
391 s[buflen] = '\0';
392 if (buflen == 0) /* EOF */
393 s = NULL;
394 Py_DECREF(bufobj);
395 return s;
Neal Norwitz41eaedd2007-08-12 00:03:22 +0000396
397error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000398 Py_XDECREF(bufobj);
399 return error_ret(tok);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000400}
401
402/* Set the readline function for TOK to a StreamReader's
403 readline function. The StreamReader is named ENC.
404
405 This function is called from check_bom and check_coding_spec.
406
407 ENC is usually identical to the future value of tok->encoding,
408 except for the (currently unsupported) case of UTF-16.
409
410 Return 1 on success, 0 on failure. */
411
412static int
413fp_setreadl(struct tok_state *tok, const char* enc)
414{
Benjamin Peterson35ee9482016-09-12 22:06:58 -0700415 PyObject *readline, *io, *stream;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200416 _Py_IDENTIFIER(open);
417 _Py_IDENTIFIER(readline);
Victor Stinner22a351a2010-10-14 12:04:34 +0000418 int fd;
Serhiy Storchaka768c16c2014-01-09 18:36:09 +0200419 long pos;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000420
Victor Stinner22a351a2010-10-14 12:04:34 +0000421 fd = fileno(tok->fp);
Serhiy Storchaka768c16c2014-01-09 18:36:09 +0200422 /* Due to buffering the file offset for fd can be different from the file
Martin v. Löwis815b41b2014-02-28 15:27:29 +0100423 * position of tok->fp. If tok->fp was opened in text mode on Windows,
424 * its file position counts CRLF as one char and can't be directly mapped
425 * to the file offset for fd. Instead we step back one byte and read to
426 * the end of line.*/
Serhiy Storchaka768c16c2014-01-09 18:36:09 +0200427 pos = ftell(tok->fp);
Martin v. Löwis815b41b2014-02-28 15:27:29 +0100428 if (pos == -1 ||
429 lseek(fd, (off_t)(pos > 0 ? pos - 1 : pos), SEEK_SET) == (off_t)-1) {
Victor Stinner22a351a2010-10-14 12:04:34 +0000430 PyErr_SetFromErrnoWithFilename(PyExc_OSError, NULL);
Benjamin Peterson35ee9482016-09-12 22:06:58 -0700431 return 0;
Victor Stinner22a351a2010-10-14 12:04:34 +0000432 }
433
Benjamin Peterson35ee9482016-09-12 22:06:58 -0700434 io = PyImport_ImportModuleNoBlock("io");
435 if (io == NULL)
436 return 0;
437
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200438 stream = _PyObject_CallMethodId(io, &PyId_open, "isisOOO",
Victor Stinner22a351a2010-10-14 12:04:34 +0000439 fd, "r", -1, enc, Py_None, Py_None, Py_False);
Benjamin Peterson35ee9482016-09-12 22:06:58 -0700440 Py_DECREF(io);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000441 if (stream == NULL)
Benjamin Peterson35ee9482016-09-12 22:06:58 -0700442 return 0;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000443
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +0200444 readline = _PyObject_GetAttrId(stream, &PyId_readline);
Benjamin Peterson35ee9482016-09-12 22:06:58 -0700445 Py_DECREF(stream);
446 if (readline == NULL)
447 return 0;
Serhiy Storchaka48842712016-04-06 09:45:48 +0300448 Py_XSETREF(tok->decoding_readline, readline);
Benjamin Peterson35ee9482016-09-12 22:06:58 -0700449
Martin v. Löwis815b41b2014-02-28 15:27:29 +0100450 if (pos > 0) {
Victor Stinnera5ed5f02016-12-06 18:45:50 +0100451 PyObject *bufobj = _PyObject_CallNoArg(readline);
Benjamin Peterson35ee9482016-09-12 22:06:58 -0700452 if (bufobj == NULL)
453 return 0;
454 Py_DECREF(bufobj);
Martin v. Löwis815b41b2014-02-28 15:27:29 +0100455 }
Guido van Rossum9cbfffd2007-06-07 00:54:15 +0000456
Benjamin Peterson35ee9482016-09-12 22:06:58 -0700457 return 1;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000458}
459
460/* Fetch the next byte from TOK. */
461
462static int fp_getc(struct tok_state *tok) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000463 return getc(tok->fp);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000464}
465
466/* Unfetch the last byte back into TOK. */
467
468static void fp_ungetc(int c, struct tok_state *tok) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000469 ungetc(c, tok->fp);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000470}
471
Martin v. Löwis447d33e2007-07-29 18:10:01 +0000472/* Check whether the characters at s start a valid
473 UTF-8 sequence. Return the number of characters forming
474 the sequence if yes, 0 if not. */
475static int valid_utf8(const unsigned char* s)
476{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000477 int expected = 0;
478 int length;
479 if (*s < 0x80)
480 /* single-byte code */
481 return 1;
482 if (*s < 0xc0)
483 /* following byte */
484 return 0;
485 if (*s < 0xE0)
486 expected = 1;
487 else if (*s < 0xF0)
488 expected = 2;
489 else if (*s < 0xF8)
490 expected = 3;
491 else
492 return 0;
493 length = expected + 1;
494 for (; expected; expected--)
495 if (s[expected] < 0x80 || s[expected] >= 0xC0)
496 return 0;
497 return length;
Martin v. Löwis447d33e2007-07-29 18:10:01 +0000498}
499
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000500/* Read a line of input from TOK. Determine encoding
501 if necessary. */
502
503static char *
504decoding_fgets(char *s, int size, struct tok_state *tok)
505{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000506 char *line = NULL;
507 int badchar = 0;
508 for (;;) {
509 if (tok->decoding_state == STATE_NORMAL) {
510 /* We already have a codec associated with
511 this input. */
512 line = fp_readl(s, size, tok);
513 break;
514 } else if (tok->decoding_state == STATE_RAW) {
515 /* We want a 'raw' read. */
516 line = Py_UniversalNewlineFgets(s, size,
517 tok->fp, NULL);
518 break;
519 } else {
520 /* We have not yet determined the encoding.
521 If an encoding is found, use the file-pointer
522 reader functions from now on. */
523 if (!check_bom(fp_getc, fp_ungetc, fp_setreadl, tok))
524 return error_ret(tok);
525 assert(tok->decoding_state != STATE_INIT);
526 }
527 }
528 if (line != NULL && tok->lineno < 2 && !tok->read_coding_spec) {
529 if (!check_coding_spec(line, strlen(line), tok, fp_setreadl)) {
530 return error_ret(tok);
531 }
532 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000533 /* The default encoding is UTF-8, so make sure we don't have any
534 non-UTF-8 sequences in it. */
535 if (line && !tok->encoding) {
536 unsigned char *c;
537 int length;
538 for (c = (unsigned char *)line; *c; c += length)
539 if (!(length = valid_utf8(c))) {
540 badchar = *c;
541 break;
542 }
543 }
544 if (badchar) {
545 /* Need to add 1 to the line number, since this line
546 has not been counted, yet. */
Jesus Ceac1935d22011-04-25 04:03:58 +0200547 PyErr_Format(PyExc_SyntaxError,
548 "Non-UTF-8 code starting with '\\x%.2x' "
549 "in file %U on line %i, "
550 "but no encoding declared; "
551 "see http://python.org/dev/peps/pep-0263/ for details",
552 badchar, tok->filename, tok->lineno + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000553 return error_ret(tok);
554 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000555 return line;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000556}
557
558static int
559decoding_feof(struct tok_state *tok)
560{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000561 if (tok->decoding_state != STATE_NORMAL) {
562 return feof(tok->fp);
563 } else {
564 PyObject* buf = tok->decoding_buffer;
565 if (buf == NULL) {
Victor Stinnera5ed5f02016-12-06 18:45:50 +0100566 buf = _PyObject_CallNoArg(tok->decoding_readline);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000567 if (buf == NULL) {
568 error_ret(tok);
569 return 1;
570 } else {
571 tok->decoding_buffer = buf;
572 }
573 }
574 return PyObject_Length(buf) == 0;
575 }
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000576}
577
578/* Fetch a byte from TOK, using the string buffer. */
579
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000580static int
581buf_getc(struct tok_state *tok) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000582 return Py_CHARMASK(*tok->str++);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000583}
584
585/* Unfetch a byte from TOK, using the string buffer. */
586
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000587static void
588buf_ungetc(int c, struct tok_state *tok) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000589 tok->str--;
590 assert(Py_CHARMASK(*tok->str) == c); /* tok->cur may point to read-only segment */
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000591}
592
593/* Set the readline function for TOK to ENC. For the string-based
594 tokenizer, this means to just record the encoding. */
595
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000596static int
597buf_setreadl(struct tok_state *tok, const char* enc) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000598 tok->enc = enc;
599 return 1;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000600}
601
602/* Return a UTF-8 encoding Python string object from the
603 C byte string STR, which is encoded with ENC. */
604
605static PyObject *
606translate_into_utf8(const char* str, const char* enc) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000607 PyObject *utf8;
608 PyObject* buf = PyUnicode_Decode(str, strlen(str), enc, NULL);
609 if (buf == NULL)
610 return NULL;
611 utf8 = PyUnicode_AsUTF8String(buf);
612 Py_DECREF(buf);
613 return utf8;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000614}
615
Benjamin Petersonaeaa5922009-11-13 00:17:59 +0000616
617static char *
618translate_newlines(const char *s, int exec_input, struct tok_state *tok) {
Victor Stinner79697732013-06-05 00:44:00 +0200619 int skip_next_lf = 0;
620 size_t needed_length = strlen(s) + 2, final_length;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000621 char *buf, *current;
622 char c = '\0';
623 buf = PyMem_MALLOC(needed_length);
624 if (buf == NULL) {
625 tok->done = E_NOMEM;
626 return NULL;
627 }
628 for (current = buf; *s; s++, current++) {
629 c = *s;
630 if (skip_next_lf) {
631 skip_next_lf = 0;
632 if (c == '\n') {
633 c = *++s;
634 if (!c)
635 break;
636 }
637 }
638 if (c == '\r') {
639 skip_next_lf = 1;
640 c = '\n';
641 }
642 *current = c;
643 }
644 /* If this is exec input, add a newline to the end of the string if
645 there isn't one already. */
646 if (exec_input && c != '\n') {
647 *current = '\n';
648 current++;
649 }
650 *current = '\0';
651 final_length = current - buf + 1;
Pablo Galindocb90c892019-03-19 17:17:58 +0000652 if (final_length < needed_length && final_length) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000653 /* should never fail */
Pablo Galindocb90c892019-03-19 17:17:58 +0000654 char* result = PyMem_REALLOC(buf, final_length);
655 if (result == NULL) {
656 PyMem_FREE(buf);
657 }
658 buf = result;
659 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000660 return buf;
Benjamin Petersonaeaa5922009-11-13 00:17:59 +0000661}
662
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000663/* Decode a byte string STR for use as the buffer of TOK.
664 Look for encoding declarations inside STR, and record them
665 inside TOK. */
666
Andy Lester384f3c52020-02-27 20:44:52 -0600667static char *
Benjamin Petersonaeaa5922009-11-13 00:17:59 +0000668decode_str(const char *input, int single, struct tok_state *tok)
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000669{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000670 PyObject* utf8 = NULL;
Andy Lester384f3c52020-02-27 20:44:52 -0600671 char *str;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000672 const char *s;
673 const char *newl[2] = {NULL, NULL};
674 int lineno = 0;
675 tok->input = str = translate_newlines(input, single, tok);
676 if (str == NULL)
677 return NULL;
678 tok->enc = NULL;
679 tok->str = str;
680 if (!check_bom(buf_getc, buf_ungetc, buf_setreadl, tok))
681 return error_ret(tok);
682 str = tok->str; /* string after BOM if any */
683 assert(str);
684 if (tok->enc != NULL) {
685 utf8 = translate_into_utf8(str, tok->enc);
686 if (utf8 == NULL)
687 return error_ret(tok);
688 str = PyBytes_AsString(utf8);
689 }
690 for (s = str;; s++) {
691 if (*s == '\0') break;
692 else if (*s == '\n') {
693 assert(lineno < 2);
694 newl[lineno] = s;
695 lineno++;
696 if (lineno == 2) break;
697 }
698 }
699 tok->enc = NULL;
700 /* need to check line 1 and 2 separately since check_coding_spec
701 assumes a single line as input */
702 if (newl[0]) {
703 if (!check_coding_spec(str, newl[0] - str, tok, buf_setreadl))
704 return error_ret(tok);
Serhiy Storchaka768c16c2014-01-09 18:36:09 +0200705 if (tok->enc == NULL && !tok->read_coding_spec && newl[1]) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000706 if (!check_coding_spec(newl[0]+1, newl[1] - newl[0],
707 tok, buf_setreadl))
708 return error_ret(tok);
709 }
710 }
711 if (tok->enc != NULL) {
712 assert(utf8 == NULL);
713 utf8 = translate_into_utf8(str, tok->enc);
714 if (utf8 == NULL)
715 return error_ret(tok);
716 str = PyBytes_AS_STRING(utf8);
717 }
718 assert(tok->decoding_buffer == NULL);
719 tok->decoding_buffer = utf8; /* CAUTION */
720 return str;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000721}
722
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000723/* Set up tokenizer for string */
724
725struct tok_state *
Benjamin Petersonaeaa5922009-11-13 00:17:59 +0000726PyTokenizer_FromString(const char *str, int exec_input)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000727{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000728 struct tok_state *tok = tok_new();
Andy Lester384f3c52020-02-27 20:44:52 -0600729 char *decoded;
730
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000731 if (tok == NULL)
732 return NULL;
Andy Lester384f3c52020-02-27 20:44:52 -0600733 decoded = decode_str(str, exec_input, tok);
734 if (decoded == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000735 PyTokenizer_Free(tok);
736 return NULL;
737 }
Neal Norwitzdee2fd52005-11-16 05:12:59 +0000738
Andy Lester384f3c52020-02-27 20:44:52 -0600739 tok->buf = tok->cur = tok->inp = decoded;
740 tok->end = decoded;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000741 return tok;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000742}
743
Benjamin Petersonf5b52242009-03-02 23:31:26 +0000744struct tok_state *
Benjamin Petersonaeaa5922009-11-13 00:17:59 +0000745PyTokenizer_FromUTF8(const char *str, int exec_input)
Benjamin Petersonf5b52242009-03-02 23:31:26 +0000746{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000747 struct tok_state *tok = tok_new();
Andy Lester384f3c52020-02-27 20:44:52 -0600748 char *translated;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000749 if (tok == NULL)
750 return NULL;
Andy Lester384f3c52020-02-27 20:44:52 -0600751 tok->input = translated = translate_newlines(str, exec_input, tok);
752 if (translated == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000753 PyTokenizer_Free(tok);
754 return NULL;
755 }
756 tok->decoding_state = STATE_RAW;
757 tok->read_coding_spec = 1;
758 tok->enc = NULL;
Andy Lester384f3c52020-02-27 20:44:52 -0600759 tok->str = translated;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000760 tok->encoding = (char *)PyMem_MALLOC(6);
761 if (!tok->encoding) {
762 PyTokenizer_Free(tok);
763 return NULL;
764 }
765 strcpy(tok->encoding, "utf-8");
Benjamin Petersonf5b52242009-03-02 23:31:26 +0000766
Andy Lester384f3c52020-02-27 20:44:52 -0600767 tok->buf = tok->cur = tok->inp = translated;
768 tok->end = translated;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000769 return tok;
Benjamin Petersonf5b52242009-03-02 23:31:26 +0000770}
771
Guido van Rossum8c11a5c1991-07-27 21:42:56 +0000772/* Set up tokenizer for file */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000773
774struct tok_state *
Serhiy Storchakac6792272013-10-19 21:03:34 +0300775PyTokenizer_FromFile(FILE *fp, const char* enc,
776 const char *ps1, const char *ps2)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000777{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000778 struct tok_state *tok = tok_new();
779 if (tok == NULL)
780 return NULL;
781 if ((tok->buf = (char *)PyMem_MALLOC(BUFSIZ)) == NULL) {
782 PyTokenizer_Free(tok);
783 return NULL;
784 }
785 tok->cur = tok->inp = tok->buf;
786 tok->end = tok->buf + BUFSIZ;
787 tok->fp = fp;
788 tok->prompt = ps1;
789 tok->nextprompt = ps2;
790 if (enc != NULL) {
791 /* Must copy encoding declaration since it
792 gets copied into the parse tree. */
793 tok->encoding = PyMem_MALLOC(strlen(enc)+1);
794 if (!tok->encoding) {
795 PyTokenizer_Free(tok);
796 return NULL;
797 }
798 strcpy(tok->encoding, enc);
799 tok->decoding_state = STATE_NORMAL;
800 }
801 return tok;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000802}
803
804
805/* Free a tok_state structure */
806
807void
Thomas Wouters23c9e002000-07-22 19:20:54 +0000808PyTokenizer_Free(struct tok_state *tok)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000809{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000810 if (tok->encoding != NULL)
811 PyMem_FREE(tok->encoding);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000812 Py_XDECREF(tok->decoding_readline);
813 Py_XDECREF(tok->decoding_buffer);
Victor Stinner7f2fee32011-04-05 00:39:01 +0200814 Py_XDECREF(tok->filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000815 if (tok->fp != NULL && tok->buf != NULL)
816 PyMem_FREE(tok->buf);
817 if (tok->input)
Andy Lester384f3c52020-02-27 20:44:52 -0600818 PyMem_FREE(tok->input);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000819 PyMem_FREE(tok);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000820}
821
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000822/* Get next char, updating state; error code goes into tok->done */
823
824static int
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200825tok_nextc(struct tok_state *tok)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000826{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000827 for (;;) {
828 if (tok->cur != tok->inp) {
829 return Py_CHARMASK(*tok->cur++); /* Fast path */
830 }
831 if (tok->done != E_OK)
832 return EOF;
833 if (tok->fp == NULL) {
834 char *end = strchr(tok->inp, '\n');
835 if (end != NULL)
836 end++;
837 else {
838 end = strchr(tok->inp, '\0');
839 if (end == tok->inp) {
840 tok->done = E_EOF;
841 return EOF;
842 }
843 }
844 if (tok->start == NULL)
845 tok->buf = tok->cur;
846 tok->line_start = tok->cur;
847 tok->lineno++;
848 tok->inp = end;
849 return Py_CHARMASK(*tok->cur++);
850 }
851 if (tok->prompt != NULL) {
852 char *newtok = PyOS_Readline(stdin, stdout, tok->prompt);
Victor Stinner89e34362011-01-07 18:47:22 +0000853 if (newtok != NULL) {
854 char *translated = translate_newlines(newtok, 0, tok);
855 PyMem_FREE(newtok);
856 if (translated == NULL)
857 return EOF;
858 newtok = translated;
859 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000860 if (tok->encoding && newtok && *newtok) {
861 /* Recode to UTF-8 */
862 Py_ssize_t buflen;
863 const char* buf;
864 PyObject *u = translate_into_utf8(newtok, tok->encoding);
865 PyMem_FREE(newtok);
866 if (!u) {
867 tok->done = E_DECODE;
868 return EOF;
869 }
870 buflen = PyBytes_GET_SIZE(u);
871 buf = PyBytes_AS_STRING(u);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000872 newtok = PyMem_MALLOC(buflen+1);
Zackery Spytz4c49da02018-12-07 03:11:30 -0700873 if (newtok == NULL) {
874 Py_DECREF(u);
875 tok->done = E_NOMEM;
876 return EOF;
877 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000878 strcpy(newtok, buf);
879 Py_DECREF(u);
880 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000881 if (tok->nextprompt != NULL)
882 tok->prompt = tok->nextprompt;
883 if (newtok == NULL)
884 tok->done = E_INTR;
885 else if (*newtok == '\0') {
886 PyMem_FREE(newtok);
887 tok->done = E_EOF;
888 }
889 else if (tok->start != NULL) {
890 size_t start = tok->start - tok->buf;
891 size_t oldlen = tok->cur - tok->buf;
892 size_t newlen = oldlen + strlen(newtok);
Pablo Galindo5ec91f72020-01-06 15:59:09 +0000893 Py_ssize_t cur_multi_line_start = tok->multi_line_start - tok->buf;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000894 char *buf = tok->buf;
895 buf = (char *)PyMem_REALLOC(buf, newlen+1);
896 tok->lineno++;
897 if (buf == NULL) {
898 PyMem_FREE(tok->buf);
899 tok->buf = NULL;
900 PyMem_FREE(newtok);
901 tok->done = E_NOMEM;
902 return EOF;
903 }
904 tok->buf = buf;
905 tok->cur = tok->buf + oldlen;
Pablo Galindo5ec91f72020-01-06 15:59:09 +0000906 tok->multi_line_start = tok->buf + cur_multi_line_start;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000907 tok->line_start = tok->cur;
908 strcpy(tok->buf + oldlen, newtok);
909 PyMem_FREE(newtok);
910 tok->inp = tok->buf + newlen;
911 tok->end = tok->inp + 1;
912 tok->start = tok->buf + start;
913 }
914 else {
915 tok->lineno++;
916 if (tok->buf != NULL)
917 PyMem_FREE(tok->buf);
918 tok->buf = newtok;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000919 tok->cur = tok->buf;
920 tok->line_start = tok->buf;
921 tok->inp = strchr(tok->buf, '\0');
922 tok->end = tok->inp + 1;
923 }
924 }
925 else {
926 int done = 0;
927 Py_ssize_t cur = 0;
928 char *pt;
929 if (tok->start == NULL) {
930 if (tok->buf == NULL) {
931 tok->buf = (char *)
932 PyMem_MALLOC(BUFSIZ);
933 if (tok->buf == NULL) {
934 tok->done = E_NOMEM;
935 return EOF;
936 }
937 tok->end = tok->buf + BUFSIZ;
938 }
939 if (decoding_fgets(tok->buf, (int)(tok->end - tok->buf),
940 tok) == NULL) {
Serhiy Storchaka0d441112015-11-14 15:10:35 +0200941 if (!tok->decoding_erred)
942 tok->done = E_EOF;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000943 done = 1;
944 }
945 else {
946 tok->done = E_OK;
947 tok->inp = strchr(tok->buf, '\0');
Benjamin Peterson26d998c2016-09-18 23:41:11 -0700948 done = tok->inp == tok->buf || tok->inp[-1] == '\n';
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000949 }
950 }
951 else {
952 cur = tok->cur - tok->buf;
953 if (decoding_feof(tok)) {
954 tok->done = E_EOF;
955 done = 1;
956 }
957 else
958 tok->done = E_OK;
959 }
960 tok->lineno++;
961 /* Read until '\n' or EOF */
962 while (!done) {
963 Py_ssize_t curstart = tok->start == NULL ? -1 :
964 tok->start - tok->buf;
Anthony Sottile5b94f352019-07-29 06:59:13 -0700965 Py_ssize_t cur_multi_line_start = tok->multi_line_start - tok->buf;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000966 Py_ssize_t curvalid = tok->inp - tok->buf;
967 Py_ssize_t newsize = curvalid + BUFSIZ;
968 char *newbuf = tok->buf;
969 newbuf = (char *)PyMem_REALLOC(newbuf,
970 newsize);
971 if (newbuf == NULL) {
972 tok->done = E_NOMEM;
973 tok->cur = tok->inp;
974 return EOF;
975 }
976 tok->buf = newbuf;
Serhiy Storchaka0d441112015-11-14 15:10:35 +0200977 tok->cur = tok->buf + cur;
Anthony Sottile5b94f352019-07-29 06:59:13 -0700978 tok->multi_line_start = tok->buf + cur_multi_line_start;
Serhiy Storchaka0d441112015-11-14 15:10:35 +0200979 tok->line_start = tok->cur;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000980 tok->inp = tok->buf + curvalid;
981 tok->end = tok->buf + newsize;
982 tok->start = curstart < 0 ? NULL :
983 tok->buf + curstart;
984 if (decoding_fgets(tok->inp,
985 (int)(tok->end - tok->inp),
986 tok) == NULL) {
987 /* Break out early on decoding
988 errors, as tok->buf will be NULL
989 */
990 if (tok->decoding_erred)
991 return EOF;
992 /* Last line does not end in \n,
993 fake one */
Anthony Sottileabea73b2019-05-18 11:27:17 -0700994 if (tok->inp[-1] != '\n')
995 strcpy(tok->inp, "\n");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000996 }
997 tok->inp = strchr(tok->inp, '\0');
998 done = tok->inp[-1] == '\n';
999 }
1000 if (tok->buf != NULL) {
1001 tok->cur = tok->buf + cur;
1002 tok->line_start = tok->cur;
1003 /* replace "\r\n" with "\n" */
1004 /* For Mac leave the \r, giving a syntax error */
1005 pt = tok->inp - 2;
1006 if (pt >= tok->buf && *pt == '\r') {
1007 *pt++ = '\n';
1008 *pt = '\0';
1009 tok->inp = pt;
1010 }
1011 }
1012 }
1013 if (tok->done != E_OK) {
1014 if (tok->prompt != NULL)
1015 PySys_WriteStderr("\n");
1016 tok->cur = tok->inp;
1017 return EOF;
1018 }
1019 }
1020 /*NOTREACHED*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001021}
1022
1023
1024/* Back-up one character */
1025
1026static void
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001027tok_backup(struct tok_state *tok, int c)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001028{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001029 if (c != EOF) {
Victor Stinner9e5d30c2020-03-07 00:54:20 +01001030 if (--tok->cur < tok->buf) {
Victor Stinner87d3b9d2020-03-25 19:27:36 +01001031 Py_FatalError("tokenizer beginning of buffer");
Victor Stinner9e5d30c2020-03-07 00:54:20 +01001032 }
1033 if (*tok->cur != c) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001034 *tok->cur = c;
Victor Stinner9e5d30c2020-03-07 00:54:20 +01001035 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001036 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001037}
1038
1039
Guido van Rossum926f13a1998-04-09 21:38:06 +00001040static int
Serhiy Storchakacf7303e2018-07-09 15:09:35 +03001041syntaxerror(struct tok_state *tok, const char *format, ...)
1042{
Serhiy Storchaka0cc6b5e2020-02-12 12:17:00 +02001043 PyObject *errmsg, *errtext, *args;
Serhiy Storchakacf7303e2018-07-09 15:09:35 +03001044 va_list vargs;
1045#ifdef HAVE_STDARG_PROTOTYPES
1046 va_start(vargs, format);
1047#else
1048 va_start(vargs);
1049#endif
Serhiy Storchaka0cc6b5e2020-02-12 12:17:00 +02001050 errmsg = PyUnicode_FromFormatV(format, vargs);
Serhiy Storchakacf7303e2018-07-09 15:09:35 +03001051 va_end(vargs);
Serhiy Storchaka0cc6b5e2020-02-12 12:17:00 +02001052 if (!errmsg) {
1053 goto error;
1054 }
1055
1056 errtext = PyUnicode_DecodeUTF8(tok->line_start, tok->cur - tok->line_start,
1057 "replace");
1058 if (!errtext) {
1059 goto error;
1060 }
1061 int offset = (int)PyUnicode_GET_LENGTH(errtext);
1062 Py_ssize_t line_len = strcspn(tok->line_start, "\n");
1063 if (line_len != tok->cur - tok->line_start) {
1064 Py_DECREF(errtext);
1065 errtext = PyUnicode_DecodeUTF8(tok->line_start, line_len,
1066 "replace");
1067 }
1068 if (!errtext) {
1069 goto error;
1070 }
1071
1072 args = Py_BuildValue("(O(OiiN))", errmsg,
1073 tok->filename, tok->lineno, offset, errtext);
1074 if (args) {
1075 PyErr_SetObject(PyExc_SyntaxError, args);
1076 Py_DECREF(args);
1077 }
1078
1079error:
1080 Py_XDECREF(errmsg);
Serhiy Storchakacf7303e2018-07-09 15:09:35 +03001081 tok->done = E_ERROR;
Serhiy Storchakacf7303e2018-07-09 15:09:35 +03001082 return ERRORTOKEN;
1083}
1084
1085static int
Thomas Wouters23c9e002000-07-22 19:20:54 +00001086indenterror(struct tok_state *tok)
Guido van Rossum926f13a1998-04-09 21:38:06 +00001087{
Victor Stinnerf2ddc6a2017-11-17 01:25:47 -08001088 tok->done = E_TABSPACE;
1089 tok->cur = tok->inp;
1090 return ERRORTOKEN;
Guido van Rossum926f13a1998-04-09 21:38:06 +00001091}
1092
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001093/* Verify that the identifier follows PEP 3131.
1094 All identifier strings are guaranteed to be "ready" unicode objects.
1095 */
Martin v. Löwis47383402007-08-15 07:32:56 +00001096static int
Victor Stinner52f6dd72010-03-12 14:45:56 +00001097verify_identifier(struct tok_state *tok)
Martin v. Löwis47383402007-08-15 07:32:56 +00001098{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001099 PyObject *s;
Benjamin Petersond73aca72015-04-21 12:05:19 -04001100 if (tok->decoding_erred)
1101 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001102 s = PyUnicode_DecodeUTF8(tok->start, tok->cur - tok->start, NULL);
Zackery Spytz5061a742018-09-10 00:27:31 -06001103 if (s == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001104 if (PyErr_ExceptionMatches(PyExc_UnicodeDecodeError)) {
Serhiy Storchaka74ea6b52020-05-12 12:42:04 +03001105 tok->done = E_DECODE;
1106 }
1107 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001108 tok->done = E_ERROR;
1109 }
1110 return 0;
1111 }
Serhiy Storchaka74ea6b52020-05-12 12:42:04 +03001112 Py_ssize_t invalid = _PyUnicode_ScanIdentifier(s);
1113 if (invalid < 0) {
1114 Py_DECREF(s);
1115 tok->done = E_ERROR;
1116 return 0;
Victor Stinnerf3e7ea52020-02-11 14:29:33 +01001117 }
Serhiy Storchaka74ea6b52020-05-12 12:42:04 +03001118 assert(PyUnicode_GET_LENGTH(s) > 0);
1119 if (invalid < PyUnicode_GET_LENGTH(s)) {
1120 Py_UCS4 ch = PyUnicode_READ_CHAR(s, invalid);
1121 if (invalid + 1 < PyUnicode_GET_LENGTH(s)) {
1122 /* Determine the offset in UTF-8 encoded input */
1123 Py_SETREF(s, PyUnicode_Substring(s, 0, invalid + 1));
1124 if (s != NULL) {
1125 Py_SETREF(s, PyUnicode_AsUTF8String(s));
1126 }
1127 if (s == NULL) {
1128 tok->done = E_ERROR;
1129 return 0;
1130 }
1131 tok->cur = (char *)tok->start + PyBytes_GET_SIZE(s);
1132 }
1133 Py_DECREF(s);
1134 // PyUnicode_FromFormatV() does not support %X
1135 char hex[9];
1136 snprintf(hex, sizeof(hex), "%04X", ch);
1137 if (Py_UNICODE_ISPRINTABLE(ch)) {
1138 syntaxerror(tok, "invalid character '%c' (U+%s)", ch, hex);
1139 }
1140 else {
1141 syntaxerror(tok, "invalid non-printable character U+%s", hex);
1142 }
1143 return 0;
1144 }
1145 Py_DECREF(s);
1146 return 1;
Martin v. Löwis47383402007-08-15 07:32:56 +00001147}
Guido van Rossum926f13a1998-04-09 21:38:06 +00001148
Brett Cannona721aba2016-09-09 14:57:09 -07001149static int
1150tok_decimal_tail(struct tok_state *tok)
1151{
1152 int c;
1153
1154 while (1) {
1155 do {
1156 c = tok_nextc(tok);
1157 } while (isdigit(c));
1158 if (c != '_') {
1159 break;
1160 }
1161 c = tok_nextc(tok);
1162 if (!isdigit(c)) {
Brett Cannona721aba2016-09-09 14:57:09 -07001163 tok_backup(tok, c);
Serhiy Storchakacf7303e2018-07-09 15:09:35 +03001164 syntaxerror(tok, "invalid decimal literal");
Brett Cannona721aba2016-09-09 14:57:09 -07001165 return 0;
1166 }
1167 }
1168 return c;
1169}
1170
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001171/* Get next token, after space stripping etc. */
1172
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001173static int
Andy Lester384f3c52020-02-27 20:44:52 -06001174tok_get(struct tok_state *tok, const char **p_start, const char **p_end)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001175{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001176 int c;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001177 int blankline, nonascii;
Guido van Rossum8c11a5c1991-07-27 21:42:56 +00001178
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001179 *p_start = *p_end = NULL;
Guido van Rossum8c11a5c1991-07-27 21:42:56 +00001180 nextline:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001181 tok->start = NULL;
1182 blankline = 0;
Guido van Rossum8c11a5c1991-07-27 21:42:56 +00001183
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001184 /* Get indentation level */
1185 if (tok->atbol) {
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001186 int col = 0;
1187 int altcol = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001188 tok->atbol = 0;
1189 for (;;) {
1190 c = tok_nextc(tok);
Brett Cannona721aba2016-09-09 14:57:09 -07001191 if (c == ' ') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001192 col++, altcol++;
Brett Cannona721aba2016-09-09 14:57:09 -07001193 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001194 else if (c == '\t') {
Victor Stinnerf2ddc6a2017-11-17 01:25:47 -08001195 col = (col / tok->tabsize + 1) * tok->tabsize;
1196 altcol = (altcol / ALTTABSIZE + 1) * ALTTABSIZE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001197 }
Brett Cannona721aba2016-09-09 14:57:09 -07001198 else if (c == '\014') {/* Control-L (formfeed) */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001199 col = altcol = 0; /* For Emacs users */
Brett Cannona721aba2016-09-09 14:57:09 -07001200 }
1201 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001202 break;
Brett Cannona721aba2016-09-09 14:57:09 -07001203 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001204 }
1205 tok_backup(tok, c);
1206 if (c == '#' || c == '\n') {
1207 /* Lines with only whitespace and/or comments
1208 shouldn't affect the indentation and are
1209 not passed to the parser as NEWLINE tokens,
1210 except *totally* empty lines in interactive
1211 mode, which signal the end of a command group. */
Brett Cannona721aba2016-09-09 14:57:09 -07001212 if (col == 0 && c == '\n' && tok->prompt != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001213 blankline = 0; /* Let it through */
Brett Cannona721aba2016-09-09 14:57:09 -07001214 }
Batuhan Taşkaya109fc272019-12-09 07:36:27 +03001215 else if (tok->prompt != NULL && tok->lineno == 1) {
1216 /* In interactive mode, if the first line contains
1217 only spaces and/or a comment, let it through. */
1218 blankline = 0;
1219 col = altcol = 0;
1220 }
Brett Cannona721aba2016-09-09 14:57:09 -07001221 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001222 blankline = 1; /* Ignore completely */
Brett Cannona721aba2016-09-09 14:57:09 -07001223 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001224 /* We can't jump back right here since we still
1225 may need to skip to the end of a comment */
1226 }
1227 if (!blankline && tok->level == 0) {
1228 if (col == tok->indstack[tok->indent]) {
1229 /* No change */
1230 if (altcol != tok->altindstack[tok->indent]) {
Victor Stinnerf2ddc6a2017-11-17 01:25:47 -08001231 return indenterror(tok);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001232 }
1233 }
1234 else if (col > tok->indstack[tok->indent]) {
1235 /* Indent -- always one */
1236 if (tok->indent+1 >= MAXINDENT) {
1237 tok->done = E_TOODEEP;
1238 tok->cur = tok->inp;
1239 return ERRORTOKEN;
1240 }
1241 if (altcol <= tok->altindstack[tok->indent]) {
Victor Stinnerf2ddc6a2017-11-17 01:25:47 -08001242 return indenterror(tok);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001243 }
1244 tok->pendin++;
1245 tok->indstack[++tok->indent] = col;
1246 tok->altindstack[tok->indent] = altcol;
1247 }
1248 else /* col < tok->indstack[tok->indent] */ {
1249 /* Dedent -- any number, must be consistent */
1250 while (tok->indent > 0 &&
1251 col < tok->indstack[tok->indent]) {
1252 tok->pendin--;
1253 tok->indent--;
1254 }
1255 if (col != tok->indstack[tok->indent]) {
1256 tok->done = E_DEDENT;
1257 tok->cur = tok->inp;
1258 return ERRORTOKEN;
1259 }
1260 if (altcol != tok->altindstack[tok->indent]) {
Victor Stinnerf2ddc6a2017-11-17 01:25:47 -08001261 return indenterror(tok);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001262 }
1263 }
1264 }
1265 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001266
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001267 tok->start = tok->cur;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001268
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001269 /* Return pending indents/dedents */
1270 if (tok->pendin != 0) {
1271 if (tok->pendin < 0) {
1272 tok->pendin++;
1273 return DEDENT;
1274 }
1275 else {
1276 tok->pendin--;
1277 return INDENT;
1278 }
1279 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001280
Guido van Rossum495da292019-03-07 12:38:08 -08001281 /* Peek ahead at the next character */
1282 c = tok_nextc(tok);
1283 tok_backup(tok, c);
1284 /* Check if we are closing an async function */
1285 if (tok->async_def
1286 && !blankline
1287 /* Due to some implementation artifacts of type comments,
1288 * a TYPE_COMMENT at the start of a function won't set an
1289 * indentation level and it will produce a NEWLINE after it.
1290 * To avoid spuriously ending an async function due to this,
1291 * wait until we have some non-newline char in front of us. */
1292 && c != '\n'
1293 && tok->level == 0
1294 /* There was a NEWLINE after ASYNC DEF,
1295 so we're past the signature. */
1296 && tok->async_def_nl
1297 /* Current indentation level is less than where
1298 the async function was defined */
1299 && tok->async_def_indent >= tok->indent)
1300 {
1301 tok->async_def = 0;
1302 tok->async_def_indent = 0;
1303 tok->async_def_nl = 0;
1304 }
1305
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001306 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001307 tok->start = NULL;
1308 /* Skip spaces */
1309 do {
1310 c = tok_nextc(tok);
1311 } while (c == ' ' || c == '\t' || c == '\014');
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001312
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001313 /* Set start of current token */
1314 tok->start = tok->cur - 1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001315
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001316 /* Skip comment, unless it's a type comment */
Brett Cannona721aba2016-09-09 14:57:09 -07001317 if (c == '#') {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001318 const char *prefix, *p, *type_start;
1319
Brett Cannona721aba2016-09-09 14:57:09 -07001320 while (c != EOF && c != '\n') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001321 c = tok_nextc(tok);
Brett Cannona721aba2016-09-09 14:57:09 -07001322 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001323
1324 if (tok->type_comments) {
1325 p = tok->start;
1326 prefix = type_comment_prefix;
1327 while (*prefix && p < tok->cur) {
1328 if (*prefix == ' ') {
1329 while (*p == ' ' || *p == '\t') {
1330 p++;
1331 }
1332 } else if (*prefix == *p) {
1333 p++;
1334 } else {
1335 break;
1336 }
1337
1338 prefix++;
1339 }
1340
1341 /* This is a type comment if we matched all of type_comment_prefix. */
1342 if (!*prefix) {
1343 int is_type_ignore = 1;
Michael J. Sullivan933e1502019-05-22 07:54:20 -07001344 const char *ignore_end = p + 6;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001345 tok_backup(tok, c); /* don't eat the newline or EOF */
1346
1347 type_start = p;
1348
Michael J. Sullivand8320ec2019-05-11 11:17:24 -07001349 /* A TYPE_IGNORE is "type: ignore" followed by the end of the token
Michael J. Sullivand8a82e22019-05-22 13:43:37 -07001350 * or anything ASCII and non-alphanumeric. */
Michael J. Sullivand8320ec2019-05-11 11:17:24 -07001351 is_type_ignore = (
Michael J. Sullivan933e1502019-05-22 07:54:20 -07001352 tok->cur >= ignore_end && memcmp(p, "ignore", 6) == 0
Michael J. Sullivand8a82e22019-05-22 13:43:37 -07001353 && !(tok->cur > ignore_end
1354 && ((unsigned char)ignore_end[0] >= 128 || Py_ISALNUM(ignore_end[0]))));
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001355
1356 if (is_type_ignore) {
Andy Lester384f3c52020-02-27 20:44:52 -06001357 *p_start = ignore_end;
Michael J. Sullivan933e1502019-05-22 07:54:20 -07001358 *p_end = tok->cur;
1359
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001360 /* If this type ignore is the only thing on the line, consume the newline also. */
1361 if (blankline) {
1362 tok_nextc(tok);
1363 tok->atbol = 1;
1364 }
1365 return TYPE_IGNORE;
1366 } else {
Andy Lester384f3c52020-02-27 20:44:52 -06001367 *p_start = type_start; /* after type_comment_prefix */
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001368 *p_end = tok->cur;
1369 return TYPE_COMMENT;
1370 }
1371 }
1372 }
Brett Cannona721aba2016-09-09 14:57:09 -07001373 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001374
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001375 /* Check for EOF and errors now */
1376 if (c == EOF) {
1377 return tok->done == E_EOF ? ENDMARKER : ERRORTOKEN;
1378 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001379
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001380 /* Identifier (most frequent token!) */
1381 nonascii = 0;
1382 if (is_potential_identifier_start(c)) {
Berker Peksag6f805622017-02-05 04:32:39 +03001383 /* Process the various legal combinations of b"", r"", u"", and f"". */
Eric V. Smith235a6f02015-09-19 14:51:32 -04001384 int saw_b = 0, saw_r = 0, saw_u = 0, saw_f = 0;
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01001385 while (1) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04001386 if (!(saw_b || saw_u || saw_f) && (c == 'b' || c == 'B'))
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01001387 saw_b = 1;
Armin Ronacher6ecf77b2012-03-04 12:04:06 +00001388 /* Since this is a backwards compatibility support literal we don't
1389 want to support it in arbitrary order like byte literals. */
Brett Cannona721aba2016-09-09 14:57:09 -07001390 else if (!(saw_b || saw_u || saw_r || saw_f)
1391 && (c == 'u'|| c == 'U')) {
Armin Ronacher6ecf77b2012-03-04 12:04:06 +00001392 saw_u = 1;
Brett Cannona721aba2016-09-09 14:57:09 -07001393 }
Christian Heimes0b3847d2012-06-20 11:17:58 +02001394 /* ur"" and ru"" are not supported */
Brett Cannona721aba2016-09-09 14:57:09 -07001395 else if (!(saw_r || saw_u) && (c == 'r' || c == 'R')) {
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01001396 saw_r = 1;
Brett Cannona721aba2016-09-09 14:57:09 -07001397 }
1398 else if (!(saw_f || saw_b || saw_u) && (c == 'f' || c == 'F')) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04001399 saw_f = 1;
Brett Cannona721aba2016-09-09 14:57:09 -07001400 }
1401 else {
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01001402 break;
Brett Cannona721aba2016-09-09 14:57:09 -07001403 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001404 c = tok_nextc(tok);
Brett Cannona721aba2016-09-09 14:57:09 -07001405 if (c == '"' || c == '\'') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001406 goto letter_quote;
Brett Cannona721aba2016-09-09 14:57:09 -07001407 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001408 }
1409 while (is_potential_identifier_char(c)) {
Brett Cannona721aba2016-09-09 14:57:09 -07001410 if (c >= 128) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001411 nonascii = 1;
Brett Cannona721aba2016-09-09 14:57:09 -07001412 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001413 c = tok_nextc(tok);
1414 }
1415 tok_backup(tok, c);
Brett Cannona721aba2016-09-09 14:57:09 -07001416 if (nonascii && !verify_identifier(tok)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001417 return ERRORTOKEN;
Brett Cannona721aba2016-09-09 14:57:09 -07001418 }
Pablo Galindo11a7f152020-04-21 01:53:04 +01001419
1420 *p_start = tok->start;
1421 *p_end = tok->cur;
1422
Guido van Rossum495da292019-03-07 12:38:08 -08001423 /* async/await parsing block. */
1424 if (tok->cur - tok->start == 5 && tok->start[0] == 'a') {
1425 /* May be an 'async' or 'await' token. For Python 3.7 or
1426 later we recognize them unconditionally. For Python
1427 3.5 or 3.6 we recognize 'async' in front of 'def', and
1428 either one inside of 'async def'. (Technically we
1429 shouldn't recognize these at all for 3.4 or earlier,
1430 but there's no *valid* Python 3.4 code that would be
1431 rejected, and async functions will be rejected in a
1432 later phase.) */
1433 if (!tok->async_hacks || tok->async_def) {
1434 /* Always recognize the keywords. */
1435 if (memcmp(tok->start, "async", 5) == 0) {
1436 return ASYNC;
1437 }
1438 if (memcmp(tok->start, "await", 5) == 0) {
1439 return AWAIT;
1440 }
1441 }
1442 else if (memcmp(tok->start, "async", 5) == 0) {
1443 /* The current token is 'async'.
1444 Look ahead one token to see if that is 'def'. */
1445
1446 struct tok_state ahead_tok;
Andy Lester384f3c52020-02-27 20:44:52 -06001447 const char *ahead_tok_start = NULL;
1448 const char *ahead_tok_end = NULL;
Guido van Rossum495da292019-03-07 12:38:08 -08001449 int ahead_tok_kind;
1450
1451 memcpy(&ahead_tok, tok, sizeof(ahead_tok));
1452 ahead_tok_kind = tok_get(&ahead_tok, &ahead_tok_start,
1453 &ahead_tok_end);
1454
1455 if (ahead_tok_kind == NAME
1456 && ahead_tok.cur - ahead_tok.start == 3
1457 && memcmp(ahead_tok.start, "def", 3) == 0)
1458 {
1459 /* The next token is going to be 'def', so instead of
1460 returning a plain NAME token, return ASYNC. */
1461 tok->async_def_indent = tok->indent;
1462 tok->async_def = 1;
1463 return ASYNC;
1464 }
1465 }
1466 }
1467
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001468 return NAME;
1469 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001470
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001471 /* Newline */
1472 if (c == '\n') {
1473 tok->atbol = 1;
Brett Cannona721aba2016-09-09 14:57:09 -07001474 if (blankline || tok->level > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001475 goto nextline;
Brett Cannona721aba2016-09-09 14:57:09 -07001476 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001477 *p_start = tok->start;
1478 *p_end = tok->cur - 1; /* Leave '\n' out of the string */
1479 tok->cont_line = 0;
Guido van Rossum495da292019-03-07 12:38:08 -08001480 if (tok->async_def) {
1481 /* We're somewhere inside an 'async def' function, and
1482 we've encountered a NEWLINE after its signature. */
1483 tok->async_def_nl = 1;
1484 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001485 return NEWLINE;
1486 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001487
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001488 /* Period or number starting with period? */
1489 if (c == '.') {
1490 c = tok_nextc(tok);
1491 if (isdigit(c)) {
1492 goto fraction;
1493 } else if (c == '.') {
1494 c = tok_nextc(tok);
1495 if (c == '.') {
1496 *p_start = tok->start;
1497 *p_end = tok->cur;
1498 return ELLIPSIS;
Brett Cannona721aba2016-09-09 14:57:09 -07001499 }
1500 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001501 tok_backup(tok, c);
1502 }
1503 tok_backup(tok, '.');
Brett Cannona721aba2016-09-09 14:57:09 -07001504 }
1505 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001506 tok_backup(tok, c);
1507 }
1508 *p_start = tok->start;
1509 *p_end = tok->cur;
1510 return DOT;
1511 }
Guido van Rossumf595fde1996-01-12 01:31:58 +00001512
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001513 /* Number */
1514 if (isdigit(c)) {
1515 if (c == '0') {
1516 /* Hex, octal or binary -- maybe. */
1517 c = tok_nextc(tok);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001518 if (c == 'x' || c == 'X') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001519 /* Hex */
1520 c = tok_nextc(tok);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001521 do {
Brett Cannona721aba2016-09-09 14:57:09 -07001522 if (c == '_') {
1523 c = tok_nextc(tok);
1524 }
1525 if (!isxdigit(c)) {
Brett Cannona721aba2016-09-09 14:57:09 -07001526 tok_backup(tok, c);
Serhiy Storchakacf7303e2018-07-09 15:09:35 +03001527 return syntaxerror(tok, "invalid hexadecimal literal");
Brett Cannona721aba2016-09-09 14:57:09 -07001528 }
1529 do {
1530 c = tok_nextc(tok);
1531 } while (isxdigit(c));
1532 } while (c == '_');
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001533 }
1534 else if (c == 'o' || c == 'O') {
1535 /* Octal */
1536 c = tok_nextc(tok);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001537 do {
Brett Cannona721aba2016-09-09 14:57:09 -07001538 if (c == '_') {
1539 c = tok_nextc(tok);
1540 }
1541 if (c < '0' || c >= '8') {
Brett Cannona721aba2016-09-09 14:57:09 -07001542 tok_backup(tok, c);
Serhiy Storchakacf7303e2018-07-09 15:09:35 +03001543 if (isdigit(c)) {
1544 return syntaxerror(tok,
1545 "invalid digit '%c' in octal literal", c);
1546 }
1547 else {
1548 return syntaxerror(tok, "invalid octal literal");
1549 }
Brett Cannona721aba2016-09-09 14:57:09 -07001550 }
1551 do {
1552 c = tok_nextc(tok);
1553 } while ('0' <= c && c < '8');
1554 } while (c == '_');
Serhiy Storchakacf7303e2018-07-09 15:09:35 +03001555 if (isdigit(c)) {
1556 return syntaxerror(tok,
1557 "invalid digit '%c' in octal literal", c);
1558 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001559 }
1560 else if (c == 'b' || c == 'B') {
1561 /* Binary */
1562 c = tok_nextc(tok);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001563 do {
Brett Cannona721aba2016-09-09 14:57:09 -07001564 if (c == '_') {
1565 c = tok_nextc(tok);
1566 }
1567 if (c != '0' && c != '1') {
Brett Cannona721aba2016-09-09 14:57:09 -07001568 tok_backup(tok, c);
Serhiy Storchakacf7303e2018-07-09 15:09:35 +03001569 if (isdigit(c)) {
1570 return syntaxerror(tok,
1571 "invalid digit '%c' in binary literal", c);
1572 }
1573 else {
1574 return syntaxerror(tok, "invalid binary literal");
1575 }
Brett Cannona721aba2016-09-09 14:57:09 -07001576 }
1577 do {
1578 c = tok_nextc(tok);
1579 } while (c == '0' || c == '1');
1580 } while (c == '_');
Serhiy Storchakacf7303e2018-07-09 15:09:35 +03001581 if (isdigit(c)) {
1582 return syntaxerror(tok,
1583 "invalid digit '%c' in binary literal", c);
1584 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001585 }
1586 else {
1587 int nonzero = 0;
1588 /* maybe old-style octal; c is first char of it */
1589 /* in any case, allow '0' as a literal */
Brett Cannona721aba2016-09-09 14:57:09 -07001590 while (1) {
1591 if (c == '_') {
1592 c = tok_nextc(tok);
1593 if (!isdigit(c)) {
Brett Cannona721aba2016-09-09 14:57:09 -07001594 tok_backup(tok, c);
Serhiy Storchakacf7303e2018-07-09 15:09:35 +03001595 return syntaxerror(tok, "invalid decimal literal");
Brett Cannona721aba2016-09-09 14:57:09 -07001596 }
1597 }
1598 if (c != '0') {
1599 break;
1600 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001601 c = tok_nextc(tok);
1602 }
Brett Cannona721aba2016-09-09 14:57:09 -07001603 if (isdigit(c)) {
1604 nonzero = 1;
1605 c = tok_decimal_tail(tok);
1606 if (c == 0) {
1607 return ERRORTOKEN;
1608 }
1609 }
1610 if (c == '.') {
1611 c = tok_nextc(tok);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001612 goto fraction;
Brett Cannona721aba2016-09-09 14:57:09 -07001613 }
1614 else if (c == 'e' || c == 'E') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001615 goto exponent;
Brett Cannona721aba2016-09-09 14:57:09 -07001616 }
1617 else if (c == 'j' || c == 'J') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001618 goto imaginary;
Brett Cannona721aba2016-09-09 14:57:09 -07001619 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001620 else if (nonzero) {
Brett Cannona721aba2016-09-09 14:57:09 -07001621 /* Old-style octal: now disallowed. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001622 tok_backup(tok, c);
Serhiy Storchakacf7303e2018-07-09 15:09:35 +03001623 return syntaxerror(tok,
1624 "leading zeros in decimal integer "
1625 "literals are not permitted; "
1626 "use an 0o prefix for octal integers");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001627 }
1628 }
1629 }
1630 else {
1631 /* Decimal */
Brett Cannona721aba2016-09-09 14:57:09 -07001632 c = tok_decimal_tail(tok);
1633 if (c == 0) {
1634 return ERRORTOKEN;
1635 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001636 {
1637 /* Accept floating point numbers. */
1638 if (c == '.') {
Brett Cannona721aba2016-09-09 14:57:09 -07001639 c = tok_nextc(tok);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001640 fraction:
1641 /* Fraction */
Brett Cannona721aba2016-09-09 14:57:09 -07001642 if (isdigit(c)) {
1643 c = tok_decimal_tail(tok);
1644 if (c == 0) {
1645 return ERRORTOKEN;
1646 }
1647 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001648 }
1649 if (c == 'e' || c == 'E') {
Benjamin Petersonc4161622014-06-07 12:36:39 -07001650 int e;
1651 exponent:
1652 e = c;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001653 /* Exponent part */
1654 c = tok_nextc(tok);
Benjamin Petersonc4161622014-06-07 12:36:39 -07001655 if (c == '+' || c == '-') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001656 c = tok_nextc(tok);
Benjamin Petersonc4161622014-06-07 12:36:39 -07001657 if (!isdigit(c)) {
Benjamin Petersonc4161622014-06-07 12:36:39 -07001658 tok_backup(tok, c);
Serhiy Storchakacf7303e2018-07-09 15:09:35 +03001659 return syntaxerror(tok, "invalid decimal literal");
Benjamin Petersonc4161622014-06-07 12:36:39 -07001660 }
1661 } else if (!isdigit(c)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001662 tok_backup(tok, c);
Benjamin Petersonc4161622014-06-07 12:36:39 -07001663 tok_backup(tok, e);
1664 *p_start = tok->start;
1665 *p_end = tok->cur;
1666 return NUMBER;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001667 }
Brett Cannona721aba2016-09-09 14:57:09 -07001668 c = tok_decimal_tail(tok);
1669 if (c == 0) {
1670 return ERRORTOKEN;
1671 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001672 }
Brett Cannona721aba2016-09-09 14:57:09 -07001673 if (c == 'j' || c == 'J') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001674 /* Imaginary part */
1675 imaginary:
1676 c = tok_nextc(tok);
Brett Cannona721aba2016-09-09 14:57:09 -07001677 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001678 }
1679 }
1680 tok_backup(tok, c);
1681 *p_start = tok->start;
1682 *p_end = tok->cur;
1683 return NUMBER;
1684 }
Guido van Rossum24dacb31997-04-06 03:46:20 +00001685
1686 letter_quote:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001687 /* String */
1688 if (c == '\'' || c == '"') {
1689 int quote = c;
1690 int quote_size = 1; /* 1 or 3 */
1691 int end_quote_size = 0;
Guido van Rossumcf171a72007-11-16 00:51:45 +00001692
Anthony Sottile995d9b92019-01-12 20:05:13 -08001693 /* Nodes of type STRING, especially multi line strings
1694 must be handled differently in order to get both
1695 the starting line number and the column offset right.
1696 (cf. issue 16806) */
1697 tok->first_lineno = tok->lineno;
1698 tok->multi_line_start = tok->line_start;
1699
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001700 /* Find the quote size and start of string */
1701 c = tok_nextc(tok);
1702 if (c == quote) {
1703 c = tok_nextc(tok);
Brett Cannona721aba2016-09-09 14:57:09 -07001704 if (c == quote) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001705 quote_size = 3;
Brett Cannona721aba2016-09-09 14:57:09 -07001706 }
1707 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001708 end_quote_size = 1; /* empty string found */
Brett Cannona721aba2016-09-09 14:57:09 -07001709 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001710 }
Brett Cannona721aba2016-09-09 14:57:09 -07001711 if (c != quote) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001712 tok_backup(tok, c);
Brett Cannona721aba2016-09-09 14:57:09 -07001713 }
Guido van Rossumcf171a72007-11-16 00:51:45 +00001714
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001715 /* Get rest of string */
1716 while (end_quote_size != quote_size) {
1717 c = tok_nextc(tok);
1718 if (c == EOF) {
Brett Cannona721aba2016-09-09 14:57:09 -07001719 if (quote_size == 3) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001720 tok->done = E_EOFS;
Brett Cannona721aba2016-09-09 14:57:09 -07001721 }
1722 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001723 tok->done = E_EOLS;
Brett Cannona721aba2016-09-09 14:57:09 -07001724 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001725 tok->cur = tok->inp;
1726 return ERRORTOKEN;
1727 }
1728 if (quote_size == 1 && c == '\n') {
1729 tok->done = E_EOLS;
1730 tok->cur = tok->inp;
1731 return ERRORTOKEN;
1732 }
Brett Cannona721aba2016-09-09 14:57:09 -07001733 if (c == quote) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001734 end_quote_size += 1;
Brett Cannona721aba2016-09-09 14:57:09 -07001735 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001736 else {
1737 end_quote_size = 0;
Brett Cannona721aba2016-09-09 14:57:09 -07001738 if (c == '\\') {
Christian Heimesc6cc23d2016-09-09 00:09:45 +02001739 tok_nextc(tok); /* skip escaped char */
Brett Cannona721aba2016-09-09 14:57:09 -07001740 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001741 }
1742 }
Guido van Rossumcf171a72007-11-16 00:51:45 +00001743
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001744 *p_start = tok->start;
1745 *p_end = tok->cur;
1746 return STRING;
1747 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001748
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001749 /* Line continuation */
1750 if (c == '\\') {
1751 c = tok_nextc(tok);
1752 if (c != '\n') {
1753 tok->done = E_LINECONT;
1754 tok->cur = tok->inp;
1755 return ERRORTOKEN;
1756 }
Anthony Sottileabea73b2019-05-18 11:27:17 -07001757 c = tok_nextc(tok);
1758 if (c == EOF) {
1759 tok->done = E_EOF;
1760 tok->cur = tok->inp;
1761 return ERRORTOKEN;
1762 } else {
1763 tok_backup(tok, c);
1764 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001765 tok->cont_line = 1;
1766 goto again; /* Read next line */
1767 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001768
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001769 /* Check for two-character token */
1770 {
1771 int c2 = tok_nextc(tok);
1772 int token = PyToken_TwoChars(c, c2);
1773 if (token != OP) {
1774 int c3 = tok_nextc(tok);
1775 int token3 = PyToken_ThreeChars(c, c2, c3);
1776 if (token3 != OP) {
1777 token = token3;
Brett Cannona721aba2016-09-09 14:57:09 -07001778 }
1779 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001780 tok_backup(tok, c3);
1781 }
1782 *p_start = tok->start;
1783 *p_end = tok->cur;
1784 return token;
1785 }
1786 tok_backup(tok, c2);
1787 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001788
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001789 /* Keep track of parentheses nesting level */
1790 switch (c) {
1791 case '(':
1792 case '[':
1793 case '{':
Serhiy Storchaka94cf3082018-12-17 17:34:14 +02001794 if (tok->level >= MAXLEVEL) {
1795 return syntaxerror(tok, "too many nested parentheses");
1796 }
1797 tok->parenstack[tok->level] = c;
1798 tok->parenlinenostack[tok->level] = tok->lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001799 tok->level++;
1800 break;
1801 case ')':
1802 case ']':
1803 case '}':
Serhiy Storchaka94cf3082018-12-17 17:34:14 +02001804 if (!tok->level) {
1805 return syntaxerror(tok, "unmatched '%c'", c);
1806 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001807 tok->level--;
Serhiy Storchaka94cf3082018-12-17 17:34:14 +02001808 int opening = tok->parenstack[tok->level];
1809 if (!((opening == '(' && c == ')') ||
1810 (opening == '[' && c == ']') ||
1811 (opening == '{' && c == '}')))
1812 {
1813 if (tok->parenlinenostack[tok->level] != tok->lineno) {
1814 return syntaxerror(tok,
1815 "closing parenthesis '%c' does not match "
1816 "opening parenthesis '%c' on line %d",
1817 c, opening, tok->parenlinenostack[tok->level]);
1818 }
1819 else {
1820 return syntaxerror(tok,
1821 "closing parenthesis '%c' does not match "
1822 "opening parenthesis '%c'",
1823 c, opening);
1824 }
1825 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001826 break;
1827 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001828
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001829 /* Punctuation character */
1830 *p_start = tok->start;
1831 *p_end = tok->cur;
1832 return PyToken_OneChar(c);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001833}
1834
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001835int
Andy Lester384f3c52020-02-27 20:44:52 -06001836PyTokenizer_Get(struct tok_state *tok, const char **p_start, const char **p_end)
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001837{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001838 int result = tok_get(tok, p_start, p_end);
1839 if (tok->decoding_erred) {
1840 result = ERRORTOKEN;
1841 tok->done = E_DECODE;
1842 }
1843 return result;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001844}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001845
Victor Stinnerfe7c5b52011-04-05 01:48:03 +02001846/* Get the encoding of a Python file. Check for the coding cookie and check if
1847 the file starts with a BOM.
Guido van Rossumce3a72a2007-10-19 23:16:50 +00001848
Victor Stinnerfe7c5b52011-04-05 01:48:03 +02001849 PyTokenizer_FindEncodingFilename() returns NULL when it can't find the
1850 encoding in the first or second line of the file (in which case the encoding
1851 should be assumed to be UTF-8).
Brett Cannone4539892007-10-20 03:46:49 +00001852
Victor Stinnerfe7c5b52011-04-05 01:48:03 +02001853 The char* returned is malloc'ed via PyMem_MALLOC() and thus must be freed
1854 by the caller. */
1855
Guido van Rossumce3a72a2007-10-19 23:16:50 +00001856char *
Victor Stinnerfe7c5b52011-04-05 01:48:03 +02001857PyTokenizer_FindEncodingFilename(int fd, PyObject *filename)
Guido van Rossum40d20bc2007-10-22 00:09:51 +00001858{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001859 struct tok_state *tok;
1860 FILE *fp;
Andy Lester384f3c52020-02-27 20:44:52 -06001861 const char *p_start = NULL;
1862 const char *p_end = NULL;
1863 char *encoding = NULL;
Guido van Rossumce3a72a2007-10-19 23:16:50 +00001864
Victor Stinnerdaf45552013-08-28 00:53:59 +02001865 fd = _Py_dup(fd);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001866 if (fd < 0) {
1867 return NULL;
1868 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02001869
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001870 fp = fdopen(fd, "r");
1871 if (fp == NULL) {
1872 return NULL;
1873 }
1874 tok = PyTokenizer_FromFile(fp, NULL, NULL, NULL);
1875 if (tok == NULL) {
1876 fclose(fp);
1877 return NULL;
1878 }
Victor Stinnerfe7c5b52011-04-05 01:48:03 +02001879 if (filename != NULL) {
1880 Py_INCREF(filename);
1881 tok->filename = filename;
1882 }
1883 else {
1884 tok->filename = PyUnicode_FromString("<string>");
1885 if (tok->filename == NULL) {
1886 fclose(fp);
1887 PyTokenizer_Free(tok);
1888 return encoding;
1889 }
1890 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001891 while (tok->lineno < 2 && tok->done == E_OK) {
1892 PyTokenizer_Get(tok, &p_start, &p_end);
1893 }
1894 fclose(fp);
1895 if (tok->encoding) {
1896 encoding = (char *)PyMem_MALLOC(strlen(tok->encoding) + 1);
1897 if (encoding)
Hansraj Das69f37bc2019-08-15 21:49:07 +05301898 strcpy(encoding, tok->encoding);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001899 }
1900 PyTokenizer_Free(tok);
1901 return encoding;
Guido van Rossumce3a72a2007-10-19 23:16:50 +00001902}
Thomas Wouters89d996e2007-09-08 17:39:28 +00001903
Victor Stinnerfe7c5b52011-04-05 01:48:03 +02001904char *
1905PyTokenizer_FindEncoding(int fd)
1906{
1907 return PyTokenizer_FindEncodingFilename(fd, NULL);
1908}
1909
Guido van Rossum408027e1996-12-30 16:17:54 +00001910#ifdef Py_DEBUG
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001911
1912void
Thomas Wouters23c9e002000-07-22 19:20:54 +00001913tok_dump(int type, char *start, char *end)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001914{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001915 printf("%s", _PyParser_TokenNames[type]);
1916 if (type == NAME || type == NUMBER || type == STRING || type == OP)
1917 printf("(%.*s)", (int)(end - start), start);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001918}
1919
1920#endif