Skip to content

gh-113537: support loads str in plistlib.loads #113582

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Jan 6, 2024
Merged
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
8 changes: 5 additions & 3 deletions Doc/library/plistlib.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ top level object is a dictionary.
To write out and to parse a plist file, use the :func:`dump` and
:func:`load` functions.

To work with plist data in bytes objects, use :func:`dumps`
To work with plist data in bytes or string objects, use :func:`dumps`
and :func:`loads`.

Values can be strings, integers, floats, booleans, tuples, lists, dictionaries
Expand Down Expand Up @@ -82,11 +82,13 @@ This module defines the following functions:

.. function:: loads(data, *, fmt=None, dict_type=dict)

Load a plist from a bytes object. See :func:`load` for an explanation of
the keyword arguments.
Load a plist from a bytes or string object. See :func:`load` for an
explanation of the keyword arguments.

.. versionadded:: 3.4

.. versionchanged:: 3.13
*data* can be a string when *fmt* equals :data:`FMT_XML`.

.. function:: dump(value, fp, *, fmt=FMT_XML, sort_keys=True, skipkeys=False)

Expand Down
5 changes: 5 additions & 0 deletions Lib/plistlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -888,6 +888,11 @@ def loads(value, *, fmt=None, dict_type=dict):
"""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)

Expand Down
13 changes: 13 additions & 0 deletions Lib/test/test_plistlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,19 @@ def test_bytes(self):
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)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Support loads ``str`` in :func:`plistlib.loads`.