Skip to content

Commit 41e14d8

Browse files
committed
gh-153569: initialize file encoding without tokenizing
Encoding lookup only needs the BOM and first two physical lines. Run the reader initialization step directly instead of creating tokens and suppressing warnings during unrelated work. With lookup no longer entering the lexer, warning suppression is unnecessary and ordinary tokenizer warnings keep a single path.
1 parent ae87ad8 commit 41e14d8

3 files changed

Lines changed: 4 additions & 19 deletions

File tree

Parser/lexer/state.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,11 @@ struct tok_state {
9090
/* Stuff for PEP 0263 */
9191
char *encoding; /* Source encoding. */
9292
const char* line_start; /* pointer to start of current line */
93-
9493
_PyTok_SourceText source;
9594
struct _PyTok_Reader *reader;
9695

9796
int type_comments; /* Whether to look for type comments */
9897

99-
int report_warnings;
100-
// TODO: Factor this into its own thing
10198
tokenizer_mode tok_mode_stack[MAXFSTRINGLEVEL];
10299
int tok_mode_stack_index;
103100
int tok_extra_tokens;

Parser/tokenizer/helpers.c

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,6 @@ _PyTokenizer_indenterror(struct tok_state *tok)
9898
int
9999
_PyTokenizer_warn_invalid_escape_sequence(struct tok_state *tok, int first_invalid_escape_char)
100100
{
101-
if (!tok->report_warnings) {
102-
return 0;
103-
}
104-
105101
PyObject *msg = PyUnicode_FromFormat(
106102
"\"\\%c\" is an invalid escape sequence. "
107103
"Such sequences will not work in the future. "
@@ -187,10 +183,6 @@ _PyTokenizer_raise_init_error(PyObject *filename)
187183
int
188184
_PyTokenizer_parser_warn(struct tok_state *tok, PyObject *category, const char *format, ...)
189185
{
190-
if (!tok->report_warnings) {
191-
return 0;
192-
}
193-
194186
PyObject *errmsg;
195187
va_list vargs;
196188
va_start(vargs, format);

Parser/tokenizer/reader.c

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
#include "helpers.h"
66
#include "reader.h"
77
#include "reader_internal.h"
8-
#include "../lexer/lexer.h"
98
#include "../lexer/state.h"
109

1110
#ifdef HAVE_UNISTD_H
@@ -865,13 +864,10 @@ _PyTokenizer_FindEncodingFilename(int fd, PyObject *filename)
865864
_PyTokenizer_Free(tok);
866865
return NULL;
867866
}
868-
/* Reporting a warning here could recursively ask for the encoding. */
869-
tok->report_warnings = 0;
870-
while (tok->lineno < 2 && tok->done == E_OK) {
871-
struct token token;
872-
_PyToken_Init(&token);
873-
_PyTokenizer_Get(tok, &token);
874-
_PyToken_Free(&token);
867+
if (initialize_file(tok) < 0) {
868+
fclose(fp);
869+
_PyTokenizer_Free(tok);
870+
return NULL;
875871
}
876872
fclose(fp);
877873
char *encoding = tok->encoding == NULL

0 commit comments

Comments
 (0)