Skip to content

Commit 98b1e51

Browse files
authored
gh-145234: Normalize decoded CR in string tokenizer (#145281)
1 parent 171e0fa commit 98b1e51

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
@@ -239,6 +239,14 @@ def test_quiet(self):
239239
with self.assertRaises(py_compile.PyCompileError):
240240
py_compile.compile(bad_coding, self.pyc_path, doraise=True, quiet=1)
241241

242+
def test_utf7_decoded_cr_compiles(self):
243+
with open(self.source_path, 'wb') as file:
244+
file.write(b"#coding=U7+AA0''\n")
245+
246+
pyc_path = py_compile.compile(self.source_path, self.pyc_path, doraise=True)
247+
self.assertEqual(pyc_path, self.pyc_path)
248+
self.assertTrue(os.path.exists(self.pyc_path))
249+
242250

243251
class PyCompileTestsWithSourceEpoch(PyCompileTestsBase,
244252
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
@@ -108,6 +108,19 @@ decode_str(const char *input, int single, struct tok_state *tok, int preserve_cr
108108
else if (!_PyTokenizer_ensure_utf8(str, tok, 1)) {
109109
return _PyTokenizer_error_ret(tok);
110110
}
111+
if (utf8 != NULL) {
112+
char *translated = _PyTokenizer_translate_newlines(
113+
str, single, preserve_crlf, tok);
114+
if (translated == NULL) {
115+
Py_DECREF(utf8);
116+
return _PyTokenizer_error_ret(tok);
117+
}
118+
PyMem_Free(tok->input);
119+
tok->input = translated;
120+
str = translated;
121+
Py_CLEAR(utf8);
122+
}
123+
tok->str = str;
111124
assert(tok->decoding_buffer == NULL);
112125
tok->decoding_buffer = utf8; /* CAUTION */
113126
return str;

0 commit comments

Comments
 (0)