Skip to content

Commit 73741c4

Browse files
committed
gh-153569: simplify tokenizer input state
1 parent 9d3e7ce commit 73741c4

4 files changed

Lines changed: 15 additions & 13 deletions

File tree

Modules/_testinternalcapi/tokenizer.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,11 @@ test_tokenizer_source(PyObject *Py_UNUSED(module),
3131
}
3232

3333
_PyTok_SourceClear(&source);
34-
if (_PyTok_SourceAppend(&source, "tail", 4) < 0) {
34+
if (check(_PyTok_SourceAppend(&source, "tail", 4) == 0,
35+
"cannot reuse source") < 0 ||
36+
check(source.len == 4 &&
37+
memcmp(source.bytes, "tail\0", 5) == 0,
38+
"wrong reused source contents") < 0) {
3539
goto error;
3640
}
3741

Parser/lexer/state.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ struct tok_state {
7474
_PyTok_Off buf_offset; /* Logical offset of buf[0]. */
7575
const char *start; /* Start of current token if not NULL */
7676
int done; /* E_OK normally, E_EOF at EOF, otherwise error code */
77-
/* NB If done != E_OK, cur must be == inp!!! */
7877
FILE *fp; /* Rest of input; NULL if tokenizing a string */
7978
int indent; /* Current indentation index */
8079
int indstack[MAXINDENT]; /* Stack of indents */
@@ -146,7 +145,7 @@ _PyLexer_BufferOffset(const struct tok_state *tok, const char *position)
146145
return tok->buf_offset + offset;
147146
}
148147

149-
static inline char *
148+
static inline const char *
150149
_PyLexer_BufferPointer(const struct tok_state *tok, _PyTok_Off offset)
151150
{
152151
assert(tok->buf != NULL);

Parser/tokenizer/decoder.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,9 @@ _PyTok_NormalizeNewlines(const char *data, Py_ssize_t len, int preserve_crlf,
103103
}
104104
result[write] = '\0';
105105
*out_len = write;
106-
*implicit_newline = implicit;
106+
if (implicit_newline != NULL) {
107+
*implicit_newline = implicit;
108+
}
107109
return result;
108110
}
109111

Parser/tokenizer/reader.c

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,11 @@ chunk_is_line(const _PyTok_Chunk *chunk)
191191
static _PyTok_ReadResult
192192
next_prepared(struct tok_state *tok, _PyTok_Chunk *chunk)
193193
{
194-
const char *source_end = _PyTok_SourceData(&tok->source) + tok->source.len;
194+
if (tok->source.len == 0) {
195+
return _PYTOK_READ_EOF;
196+
}
197+
assert(tok->source.bytes != NULL);
198+
const char *source_end = tok->source.bytes + tok->source.len;
195199
if (tok->inp == source_end) {
196200
return _PYTOK_READ_EOF;
197201
}
@@ -553,18 +557,15 @@ next_interactive(struct tok_state *tok, _PyTok_Chunk *chunk)
553557
_PyTok_ChunkClear(&decoded);
554558
return _PYTOK_READ_ERROR;
555559
}
556-
int implicit_newline;
557560
chunk->data = _PyTok_NormalizeNewlines(
558561
decoded.data, decoded.len, 0, 0,
559-
&chunk->len, &implicit_newline);
562+
&chunk->len, NULL);
560563
_PyTok_ChunkClear(&decoded);
561564
if (chunk->data == NULL) {
562-
PyErr_NoMemory();
563565
tok->done = E_NOMEM;
564566
return _PYTOK_READ_ERROR;
565567
}
566568
chunk->ownership = _PYTOK_CHUNK_PYMEM;
567-
chunk->implicit_newline = implicit_newline;
568569
return _PYTOK_READ_LINE;
569570
}
570571

@@ -656,10 +657,6 @@ _PyTok_ReaderUnderflow(struct tok_state *tok)
656657
}
657658

658659
Py_ssize_t scan_len = chunk.len;
659-
if (kind == _PYTOK_READER_INTERACTIVE &&
660-
chunk.implicit_newline) {
661-
scan_len--;
662-
}
663660
if (streaming) {
664661
if (reset_buffer) {
665662
reset_streaming_buffer(tok);

0 commit comments

Comments
 (0)