Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions Lib/plistlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,10 @@ def _escape(text):
if m is not None:
raise ValueError("strings can't contain control characters; "
"use bytes instead")
text = text.replace("\r\n", "\n") # convert DOS line endings
text = text.replace("\r", "\n") # convert Mac line endings
text = text.replace("&", "&") # escape '&'
text = text.replace("<", "&lt;") # escape '<'
text = text.replace(">", "&gt;") # escape '>'
text = text.replace("\r", "&#13;") # preserve CR via character reference
return text

class _PlistParser:
Expand Down
17 changes: 15 additions & 2 deletions Lib/test/test_plistlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -818,13 +818,26 @@ def test_controlcharacters(self):
if i >= 32 or c in "\r\n\t":
# \r, \n and \t are the only legal control chars in XML
data = plistlib.dumps(testString, fmt=plistlib.FMT_XML)
if c != "\r":
self.assertEqual(plistlib.loads(data), testString)
self.assertEqual(plistlib.loads(data), testString)
else:
with self.assertRaises(ValueError):
plistlib.dumps(testString, fmt=plistlib.FMT_XML)
plistlib.dumps(testString, fmt=plistlib.FMT_BINARY)

def test_cr_newline_roundtrip(self):
# gh-139423: Carriage returns should survive XML plist round-trip.
test_cases = [
"hello\rworld", # standalone CR
"hello\r\nworld", # CRLF
"a\rb\nc\r\nd", # mixed newlines
"\r", # bare CR
"\r\n", # bare CRLF
]
for s in test_cases:
with self.subTest(s=s):
data = plistlib.dumps(s, fmt=plistlib.FMT_XML)
self.assertEqual(plistlib.loads(data), s)

def test_non_bmp_characters(self):
pl = {'python': '\U0001f40d'}
for fmt in ALL_FORMATS:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Fixed :mod:`plistlib` to preserve carriage return characters (``\r``) during
XML plist round-trips. Previously, ``\r`` and ``\r\n`` were normalized to
``\n`` during serialization, causing data corruption. Carriage returns are now
encoded as ``&#13;`` XML character references, which the XML parser preserves.
Loading