FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-143346: Fix calculation of the line width for wrapped Base64 in plistlib by serhiy-storchaka · Pull Request #143347 · python/cpython · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) .rst  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
2 changes: 1 addition & 1 deletion Lib/plistlib.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ def write_bytes(self, data):
self._indent_level -= 1
maxlinelength = max(
16,
76 - len(self.indent.replace(b"\t", b" " * 8) * self._indent_level))
76 - len((self.indent * self._indent_level).expandtabs()))

for line in _encode_base64(data, maxlinelength).split(b"\n"):
if line:
Expand Down
64 changes: 63 additions & 1 deletion Lib/test/test_plistlib.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,69 @@ def test_bytes(self):
data2 = plistlib.dumps(pl2)
self.assertEqual(data, data2)

def test_bytes_indent(self):
header = (
b'<?xml version="1.0" encoding="UTF-8"?>\n'
b'<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">\n'
b'<plist version="1.0">\n')
data = [{'bytes': bytes(range(50))}]
pl = plistlib.dumps(data)
self.assertEqual(pl, header +
b'<array>\n'
b'\t<dict>\n'
b'\t\t<key>bytes</key>\n'
b'\t\t<data>\n'
b'\t\tAAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKiss\n'
b'\t\tLS4vMDE=\n'
b'\t\t</data>\n'
b'\t</dict>\n'
b'</array>\n'
b'</plist>\n')

def dumps_with_indent(data, indent):
fp = BytesIO()
writer = plistlib._PlistWriter(fp, indent=indent)
writer.write(data)
return fp.getvalue()

pl = dumps_with_indent(data, b' ')

serhiy-storchaka Jan 2, 2026 •
edited
Loading

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

This tests non-default indentation.

self.assertEqual(pl, header +
b'<array>\n'
b' <dict>\n'
b' <key>bytes</key>\n'
b' <data>\n'
b' AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDE=\n'
b' </data>\n'
b' </dict>\n'
b'</array>\n'
b'</plist>\n')

pl = dumps_with_indent(data, b' \t')

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

This catches the original bug.

self.assertEqual(pl, header +
b'<array>\n'
b' \t<dict>\n'
b' \t \t<key>bytes</key>\n'
b' \t \t<data>\n'
b' \t \tAAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKiss\n'
b' \t \tLS4vMDE=\n'
b' \t \t</data>\n'
b' \t</dict>\n'
b'</array>\n'
b'</plist>\n')

pl = dumps_with_indent(data, b'\t ')

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

This catches a bug in my initial fix in #143216.

self.assertEqual(pl, header +
b'<array>\n'
b'\t <dict>\n'
b'\t \t <key>bytes</key>\n'
b'\t \t <data>\n'
b'\t \t AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygp\n'
b'\t \t KissLS4vMDE=\n'
b'\t \t </data>\n'
b'\t </dict>\n'
b'</array>\n'
b'</plist>\n')

def test_loads_str_with_xml_fmt(self):
pl = self._create()
b = plistlib.dumps(pl)
Expand Down Expand Up @@ -581,7 +644,6 @@ def test_appleformatting(self):
self.assertEqual(data, TESTDATA[fmt],
"generated data was not identical to Apple's output")


def test_appleformattingfromliteral(self):
self.maxDiff = None
for fmt in ALL_FORMATS:
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix incorrect wrapping of the Base64 data in :class:`!plistlib._PlistWriter`
when the indent contains a mix of tabs and spaces.
Loading

Back | FazBrowse Home | New Git URL