Skip to content

Commit 2daece9

Browse files
authored
[3.13] gh-145234: Normalize decoded CR in string tokenizer (GH-145281) (#145312)
1 parent 67f6368 commit 2daece9

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

Lib/test/test_py_compile.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,14 @@ def test_quiet(self):
207207
with self.assertRaises(py_compile.PyCompileError):
208208
py_compile.compile(bad_coding, doraise=True, quiet=1)
209209

210+
def test_utf7_decoded_cr_compiles(self):
211+
with open(self.source_path, 'wb') as file:
212+
file.write(b"#coding=U7+AA0''\n")
213+
214+
pyc_path = py_compile.compile(self.source_path, self.pyc_path, doraise=True)
215+
self.assertEqual(pyc_path, self.pyc_path)
216+
self.assertTrue(os.path.exists(self.pyc_path))
217+
210218

211219
class PyCompileTestsWithSourceEpoch(PyCompileTestsBase,
212220
unittest.TestCase,
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Fixed a ``SystemError`` in the parser when an encoding cookie (for example,
2+
UTF-7) decodes to carriage returns (``\r``). Newlines are now normalized after
3+
decoding in the string tokenizer.
4+
5+
Patch by Pablo Galindo.

Parser/tokenizer/string_tokenizer.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,19 @@ decode_str(const char *input, int single, struct tok_state *tok, int preserve_cr
102102
return _PyTokenizer_error_ret(tok);
103103
str = PyBytes_AS_STRING(utf8);
104104
}
105+
if (utf8 != NULL) {
106+
char *translated = _PyTokenizer_translate_newlines(
107+
str, single, preserve_crlf, tok);
108+
if (translated == NULL) {
109+
Py_DECREF(utf8);
110+
return _PyTokenizer_error_ret(tok);
111+
}
112+
PyMem_Free(tok->input);
113+
tok->input = translated;
114+
str = translated;
115+
Py_CLEAR(utf8);
116+
}
117+
tok->str = str;
105118
assert(tok->decoding_buffer == NULL);
106119
tok->decoding_buffer = utf8; /* CAUTION */
107120
return str;

0 commit comments

Comments
 (0)