Skip to content

Commit c8b2489

Browse files
committed
gh-153569: move tokenizer input state and relocation into the reader
1 parent 2ddc218 commit c8b2489

20 files changed

Lines changed: 154 additions & 247 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
@@ -207,15 +207,16 @@ _PyLexer_get_normal(struct tok_state *tok, ftstring_state *current, struct token
207207
}
208208
tok_backup(tok, c);
209209
if (c == '#' || c == '\n' || c == '\r') {
210+
int interactive = _PyTok_ReaderIsInteractive(tok);
210211
/* Lines with only whitespace and/or comments
211212
shouldn't affect the indentation and are
212213
not passed to the parser as NEWLINE tokens,
213214
except *totally* empty lines in interactive
214215
mode, which signal the end of a command group. */
215-
if (col == 0 && c == '\n' && tok->prompt != NULL) {
216+
if (col == 0 && c == '\n' && interactive) {
216217
blankline = 0; /* Let it through */
217218
}
218-
else if (tok->prompt != NULL && tok->lineno == 1) {
219+
else if (interactive && tok->lineno == 1) {
219220
/* In interactive mode, if the first line contains
220221
only spaces and/or a comment, let it through. */
221222
blankline = 0;

Parser/lexer/state.c

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,11 @@
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/helpers.h"
87
#include "../tokenizer/reader.h"
98

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

0 commit comments

Comments
 (0)