mirror of
https://github.com/python/cpython.git
synced 2025-08-03 16:39:00 +00:00
gh-113537: support loads str in plistlib.loads (#113582)
Add support for loading XML plists from a string value instead of a only bytes value.
This commit is contained in:
parent
66f3964815
commit
bbf214df23
4 changed files with 24 additions and 3 deletions
|
@ -906,6 +906,11 @@ def loads(value, *, fmt=None, dict_type=dict, aware_datetime=False):
|
|||
"""Read a .plist file from a bytes object.
|
||||
Return the unpacked root object (which usually is a dictionary).
|
||||
"""
|
||||
if isinstance(value, str):
|
||||
if fmt == FMT_BINARY:
|
||||
raise TypeError("value must be bytes-like object when fmt is "
|
||||
"FMT_BINARY")
|
||||
value = value.encode()
|
||||
fp = BytesIO(value)
|
||||
return load(fp, fmt=fmt, dict_type=dict_type, aware_datetime=aware_datetime)
|
||||
|
||||
|
|
|
@ -510,6 +510,19 @@ class TestPlistlib(unittest.TestCase):
|
|||
data2 = plistlib.dumps(pl2)
|
||||
self.assertEqual(data, data2)
|
||||
|
||||
def test_loads_str_with_xml_fmt(self):
|
||||
pl = self._create()
|
||||
b = plistlib.dumps(pl)
|
||||
s = b.decode()
|
||||
self.assertIsInstance(s, str)
|
||||
pl2 = plistlib.loads(s)
|
||||
self.assertEqual(pl, pl2)
|
||||
|
||||
def test_loads_str_with_binary_fmt(self):
|
||||
msg = "value must be bytes-like object when fmt is FMT_BINARY"
|
||||
with self.assertRaisesRegex(TypeError, msg):
|
||||
plistlib.loads('test', fmt=plistlib.FMT_BINARY)
|
||||
|
||||
def test_indentation_array(self):
|
||||
data = [[[[[[[[{'test': b'aaaaaa'}]]]]]]]]
|
||||
self.assertEqual(plistlib.loads(plistlib.dumps(data)), data)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue