Skip to content

Commit 18c6ce8

Browse files
committed
pythongh-113537: support loads str in plistlib.loads
1 parent 88cb972 commit 18c6ce8

File tree

4 files changed

+14
-3
lines changed

4 files changed

+14
-3
lines changed

Doc/library/plistlib.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ top level object is a dictionary.
2727
To write out and to parse a plist file, use the :func:`dump` and
2828
:func:`load` functions.
2929

30-
To work with plist data in bytes objects, use :func:`dumps`
30+
To work with plist data in bytes or string objects, use :func:`dumps`
3131
and :func:`loads`.
3232

3333
Values can be strings, integers, floats, booleans, tuples, lists, dictionaries
@@ -82,8 +82,8 @@ This module defines the following functions:
8282

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

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

8888
.. versionadded:: 3.4
8989

Lib/plistlib.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -888,6 +888,8 @@ def loads(value, *, fmt=None, dict_type=dict):
888888
"""Read a .plist file from a bytes object.
889889
Return the unpacked root object (which usually is a dictionary).
890890
"""
891+
if type(value) == str:
892+
value = value.encode()
891893
fp = BytesIO(value)
892894
return load(fp, fmt=fmt, dict_type=dict_type)
893895

Lib/test/test_plistlib.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,14 @@ def test_bytes(self):
508508
data2 = plistlib.dumps(pl2)
509509
self.assertEqual(data, data2)
510510

511+
def test_loads_str(self):
512+
pl = self._create()
513+
b = plistlib.dumps(pl)
514+
s = b.decode()
515+
self.assertEqual(type(s), str)
516+
pl2 = plistlib.loads(s)
517+
self.assertEqual(pl, pl2)
518+
511519
def test_indentation_array(self):
512520
data = [[[[[[[[{'test': b'aaaaaa'}]]]]]]]]
513521
self.assertEqual(plistlib.loads(plistlib.dumps(data)), data)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Support loads ``str`` in :func:`plistlib.loads`.

0 commit comments

Comments
 (0)