Skip to content

Commit b73a1a6

Browse files
committed
Merge branch 'gh-153569-tokenizer-reader-cutover' into gh-153569-tokenizer-offset-state
# Conflicts: # Parser/tokenizer/decoder.c # Parser/tokenizer/reader.c # Parser/tokenizer/reader_internal.h
2 parents 1b94006 + 12a9a35 commit b73a1a6

3 files changed

Lines changed: 22 additions & 10 deletions

File tree

Parser/tokenizer/decoder.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -279,10 +279,10 @@ _PyTok_DetectEncoding(struct _PyTokenizer *tok, const _PyTok_Chunk *first,
279279

280280
int
281281
_PyTok_DecodeOnce(struct _PyTokenizer *tok, _PyTok_Chunk *chunk,
282-
const char *encoding)
282+
const char *encoding, const char *errors)
283283
{
284284
PyObject *unicode = PyUnicode_Decode(
285-
chunk->data, chunk->len, encoding, NULL);
285+
chunk->data, chunk->len, encoding, errors);
286286
if (unicode == NULL) {
287287
_PyTok_RecordPending(tok, _PYTOK_ERR_DECODE);
288288
return -1;
@@ -399,7 +399,8 @@ _PyTok_PrepareString(struct _PyTokenizer *tok, const char *input,
399399
.ownership = _PYTOK_CHUNK_BORROWED,
400400
};
401401
if (tok->reader->encoding != NULL && strcmp(tok->reader->encoding, "utf-8") != 0) {
402-
if (_PyTok_DecodeOnce(tok, &decoded, tok->reader->encoding) < 0) {
402+
if (_PyTok_DecodeOnce(
403+
tok, &decoded, tok->reader->encoding, NULL) < 0) {
403404
return -1;
404405
}
405406
}

Parser/tokenizer/reader.c

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -376,15 +376,24 @@ next_readline(struct _PyTokenizer *tok, _PyTok_Chunk *chunk)
376376
Py_DECREF(raw);
377377
return _PYTOK_READ_ERROR;
378378
}
379-
if (_PyTok_StartDecoder(tok, "replace") < 0) {
380-
Py_DECREF(raw);
381-
return _PYTOK_READ_ERROR;
382-
}
383379
input.owner = raw;
384380
input.data = PyBytes_AS_STRING(raw);
385381
input.len = PyBytes_GET_SIZE(raw);
386382
input.ownership = _PYTOK_CHUNK_PYOBJECT;
387-
if (input.len > 0 && _PyTok_DecodeChunk(tok, &input, 0) < 0) {
383+
int decoded;
384+
if (reader->decoder == NULL &&
385+
strcmp(reader->encoding, "utf-8") == 0 &&
386+
chunk_is_line(&input)) {
387+
decoded = _PyTok_DecodeOnce(
388+
tok, &input, "utf-8", "replace");
389+
}
390+
else {
391+
decoded = _PyTok_StartDecoder(tok, "replace");
392+
if (decoded == 0 && input.len > 0) {
393+
decoded = _PyTok_DecodeChunk(tok, &input, 0);
394+
}
395+
}
396+
if (decoded < 0) {
388397
_PyTok_ChunkClear(&input);
389398
return _PYTOK_READ_ERROR;
390399
}
@@ -468,7 +477,8 @@ next_interactive(struct _PyTokenizer *tok, _PyTok_Chunk *chunk)
468477
.ownership = _PYTOK_CHUNK_PYMEM,
469478
};
470479
if (reader->encoding != NULL &&
471-
_PyTok_DecodeOnce(tok, &decoded, reader->encoding) < 0) {
480+
_PyTok_DecodeOnce(
481+
tok, &decoded, reader->encoding, NULL) < 0) {
472482
_PyTok_ChunkClear(&decoded);
473483
return _PYTOK_READ_ERROR;
474484
}

Parser/tokenizer/reader_internal.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ struct _PyTok_Reader {
6868
struct _PyTokenizer;
6969

7070
char *_PyTok_CopyBytes(const char *, Py_ssize_t);
71-
int _PyTok_DecodeOnce(struct _PyTokenizer *, _PyTok_Chunk *, const char *);
71+
int _PyTok_DecodeOnce(
72+
struct _PyTokenizer *, _PyTok_Chunk *, const char *, const char *);
7273
char *_PyTok_NormalizeNewlines(
7374
const char *, Py_ssize_t, int, int, Py_ssize_t *, int *);
7475
void _PyTok_ChunkClear(_PyTok_Chunk *);

0 commit comments

Comments
 (0)