Skip to content

Commit 8f92f4e

Browse files
committed
gh-153569: move tokenizer input state and relocation into the reader
1 parent 0b9a33b commit 8f92f4e

20 files changed

Lines changed: 152 additions & 249 deletions

Lib/test/test_repl.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,9 +184,8 @@ def read_until(marker, start=0):
184184

185185
@cpython_only
186186
def test_lexer_buffer_realloc_with_null_start(self):
187-
# gh-144759: NULL pointer arithmetic in the lexer when start and
188-
# multi_line_start are NULL (uninitialized in tok_mode_stack[0])
189-
# and the lexer buffer is reallocated while parsing long input.
187+
# gh-144759: NULL pointer arithmetic when the lexer buffer grows
188+
# while parsing long input.
190189
long_value = "a" * 2000
191190
user_input = dedent(f"""\
192191
x = f'{{{long_value!r}}}'

Makefile.pre.in

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,6 @@ PEGEN_OBJS= \
394394
Parser/peg_api.o
395395

396396
TOKENIZER_OBJS= \
397-
Parser/lexer/buffer.o \
398397
Parser/lexer/lexer.o \
399398
Parser/lexer/number.o \
400399
Parser/lexer/state.o \
@@ -411,7 +410,6 @@ PEGEN_HEADERS= \
411410
$(srcdir)/Parser/string_parser.h
412411

413412
TOKENIZER_HEADERS= \
414-
Parser/lexer/buffer.h \
415413
Parser/lexer/lexer.h \
416414
Parser/lexer/lexer_internal.h \
417415
Parser/lexer/state.h \

PCbuild/_freeze_module.vcxproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,6 @@
181181
<ClCompile Include="..\Parser\action_helpers.c" />
182182
<ClCompile Include="..\Parser\string_parser.c" />
183183
<ClCompile Include="..\Parser\token.c" />
184-
<ClCompile Include="..\Parser\lexer\buffer.c" />
185184
<ClCompile Include="..\Parser\lexer\state.c" />
186185
<ClCompile Include="..\Parser\lexer\lexer.c" />
187186
<ClCompile Include="..\Parser\lexer\number.c" />

PCbuild/_freeze_module.vcxproj.filters

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -469,9 +469,6 @@
469469
<ClCompile Include="..\Parser\lexer\string.c">
470470
<Filter>Source Files</Filter>
471471
</ClCompile>
472-
<ClCompile Include="..\Parser\lexer\buffer.c">
473-
<Filter>Source Files</Filter>
474-
</ClCompile>
475472
<ClCompile Include="..\Parser\lexer\state.c">
476473
<Filter>Source Files</Filter>
477474
</ClCompile>

PCbuild/pythoncore.vcxproj

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -423,10 +423,9 @@
423423
<ClInclude Include="..\Parser\lexer\state.h" />
424424
<ClInclude Include="..\Parser\lexer\lexer.h" />
425425
<ClInclude Include="..\Parser\lexer\lexer_internal.h" />
426-
<ClInclude Include="..\Parser\lexer\buffer.h" />
427-
<ClInclude Include="..\Parser\tokenizer\cursor.h" />
428426
<ClInclude Include="..\Parser\tokenizer\reader.h" />
429427
<ClInclude Include="..\Parser\tokenizer\reader_internal.h" />
428+
<ClInclude Include="..\Parser\tokenizer\cursor.h" />
430429
<ClInclude Include="..\Parser\tokenizer\source.h" />
431430
<ClInclude Include="..\Parser\tokenizer\helpers.h" />
432431
<ClInclude Include="..\Parser\tokenizer\tokenizer.h" />
@@ -593,7 +592,6 @@
593592
<ClCompile Include="..\Parser\lexer\lexer.c" />
594593
<ClCompile Include="..\Parser\lexer\number.c" />
595594
<ClCompile Include="..\Parser\lexer\string.c" />
596-
<ClCompile Include="..\Parser\lexer\buffer.c" />
597595
<ClCompile Include="..\Parser\tokenizer\cursor.c" />
598596
<ClCompile Include="..\Parser\tokenizer\source.c" />
599597
<ClCompile Include="..\Parser\tokenizer\decoder.c" />

PCbuild/pythoncore.vcxproj.filters

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -330,18 +330,15 @@
330330
<ClInclude Include="..\Parser\lexer\lexer_internal.h">
331331
<Filter>Parser</Filter>
332332
</ClInclude>
333-
<ClInclude Include="..\Parser\lexer\buffer.h">
334-
<Filter>Parser</Filter>
335-
</ClInclude>
336-
<ClInclude Include="..\Parser\tokenizer\cursor.h">
337-
<Filter>Parser</Filter>
338-
</ClInclude>
339333
<ClInclude Include="..\Parser\tokenizer\reader.h">
340334
<Filter>Parser</Filter>
341335
</ClInclude>
342336
<ClInclude Include="..\Parser\tokenizer\reader_internal.h">
343337
<Filter>Parser</Filter>
344338
</ClInclude>
339+
<ClInclude Include="..\Parser\tokenizer\cursor.h">
340+
<Filter>Parser</Filter>
341+
</ClInclude>
345342
<ClInclude Include="..\Parser\tokenizer\source.h">
346343
<Filter>Parser</Filter>
347344
</ClInclude>
@@ -1361,9 +1358,6 @@
13611358
<ClCompile Include="..\Parser\lexer\state.c">
13621359
<Filter>Parser</Filter>
13631360
</ClCompile>
1364-
<ClCompile Include="..\Parser\lexer\buffer.c">
1365-
<Filter>Parser</Filter>
1366-
</ClCompile>
13671361
<ClCompile Include="..\Parser\tokenizer\cursor.c">
13681362
<Filter>Parser</Filter>
13691363
</ClCompile>

Parser/lexer/buffer.c

Lines changed: 0 additions & 29 deletions
This file was deleted.

Parser/lexer/buffer.h

Lines changed: 0 additions & 21 deletions
This file was deleted.

Parser/lexer/lexer.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,15 +204,16 @@ _PyLexer_get_normal_mode(struct tok_state *tok, tokenizer_mode* current_tok, str
204204
}
205205
tok_backup(tok, c);
206206
if (c == '#' || c == '\n' || c == '\r') {
207+
int interactive = _PyTok_ReaderIsInteractive(tok);
207208
/* Lines with only whitespace and/or comments
208209
shouldn't affect the indentation and are
209210
not passed to the parser as NEWLINE tokens,
210211
except *totally* empty lines in interactive
211212
mode, which signal the end of a command group. */
212-
if (col == 0 && c == '\n' && tok->prompt != NULL) {
213+
if (col == 0 && c == '\n' && interactive) {
213214
blankline = 0; /* Let it through */
214215
}
215-
else if (tok->prompt != NULL && tok->lineno == 1) {
216+
else if (interactive && tok->lineno == 1) {
216217
/* In interactive mode, if the first line contains
217218
only spaces and/or a comment, let it through. */
218219
blankline = 0;

Parser/lexer/state.c

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,10 @@
11
#include "Python.h"
2-
#include "pycore_pystate.h"
32
#include "pycore_token.h"
43
#include "errcode.h"
54

65
#include "state.h"
76
#include "../tokenizer/reader.h"
87

9-
/* Create and initialize a new tok_state structure */
10-
struct tok_state *
11-
_PyTokenizer_tok_new(void)
12-
{
13-
struct tok_state *tok = (struct tok_state *)PyMem_Calloc(
14-
1,
15-
sizeof(struct tok_state));
16-
if (tok == NULL) {
17-
PyErr_NoMemory();
18-
return NULL;
19-
}
20-
21-
tok->buf = tok->cur = tok->inp = NULL;
22-
tok->fp_interactive = 0;
23-
tok->interactive_src_start = NULL;
24-
tok->interactive_src_end = NULL;
25-
tok->start = NULL;
26-
tok->done = E_OK;
27-
tok->fp = NULL;
28-
tok->indent = 0;
29-
tok->indstack[0] = 0;
30-
tok->atbol = 1;
31-
tok->pendin = 0;
32-
tok->prompt = NULL;
33-
tok->lineno = 0;
34-
tok->start_loc = (_PyTok_Loc){-1, -1};
35-
tok->level = 0;
36-
tok->altindstack[0] = 0;
37-
tok->encoding = NULL;
38-
tok->filename = NULL;
39-
tok->module = NULL;
40-
tok->type_comments = 0;
41-
tok->interactive_underflow = IUNDERFLOW_NORMAL;
42-
tok->str = NULL;
43-
tok->report_warnings = 1;
44-
tok->tok_extra_tokens = 0;
45-
tok->comment_newline = 0;
46-
tok->implicit_newline = 0;
47-
_PyTok_SourceInit(&tok->source);
48-
tok->reader = NULL;
49-
tok->tok_mode_stack[0] = (tokenizer_mode){.kind =TOK_REGULAR_MODE, .quote='\0', .quote_size = 0, .in_debug=0};
50-
tok->tok_mode_stack_index = 0;
51-
#ifdef Py_DEBUG
52-
tok->debug = _Py_GetConfig()->parser_debug;
53-
#endif
54-
return tok;
55-
}
56-
578
/* Free a tok_state structure */
589
void
5910
_PyTokenizer_Free(struct tok_state *tok)

0 commit comments

Comments
 (0)