Skip to content

Commit d701ca0

Browse files
committed
gh-145234: Normalize decoded CR in string tokenizer
1 parent 7eb00ad commit d701ca0

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-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: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
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.

Parser/tokenizer/string_tokenizer.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,18 @@ 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+
}
111123
assert(tok->decoding_buffer == NULL);
112124
tok->decoding_buffer = utf8; /* CAUTION */
113125
return str;

0 commit comments

Comments
 (0)