Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
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
2 changes: 1 addition & 1 deletion 2 Doc/library/plistlib.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ and XML plist files.

The property list (``.plist``) file format is a simple serialization supporting
basic object types, like dictionaries, lists, numbers and strings. Usually the
top level object is a dictionary.
top level object is a dictionary or a frozen dictionary.

To write out and to parse a plist file, use the :func:`dump` and
:func:`load` functions.
Expand Down
3 changes: 2 additions & 1 deletion 3 Doc/whatsnew/3.15.rst
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,8 @@ For example::

The following standard library modules have been updated to accept
:class:`!frozendict`: :mod:`copy`, :mod:`decimal`, :mod:`json`, :mod:`marshal`,
:mod:`pickle`, :mod:`pprint` and :mod:`xml.etree.ElementTree`.
:mod:`plistlib` (only for serialization), :mod:`pickle`, :mod:`pprint` and
:mod:`xml.etree.ElementTree`.

:func:`eval` and :func:`exec` accept :class:`!frozendict` for *globals*, and
:func:`type` and :meth:`str.maketrans` accept :class:`!frozendict` for *dict*.
Expand Down
16 changes: 8 additions & 8 deletions 16 Lib/plistlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

The property list (.plist) file format is a simple XML pickle supporting
basic object types, like dictionaries, lists, numbers and strings.
Usually the top level object is a dictionary.
Usually the top level object is a dictionary or a frozen dictionary.

To write out a plist file, use the dump(value, file)
function. 'value' is the top level object, 'file' is
Expand Down Expand Up @@ -357,7 +357,7 @@ def write_value(self, value):
elif isinstance(value, float):
self.simple_element("real", repr(value))

elif isinstance(value, dict):
elif isinstance(value, (dict, frozendict)):
self.write_dict(value)

elif isinstance(value, (bytes, bytearray)):
Expand Down Expand Up @@ -715,7 +715,7 @@ def _flatten(self, value):
self._objidtable[id(value)] = refnum

# And finally recurse into containers
if isinstance(value, dict):
if isinstance(value, (dict, frozendict)):
keys = []
values = []
items = value.items()
Expand Down Expand Up @@ -836,7 +836,7 @@ def _write_object(self, value):
self._write_size(0xA0, s)
self._fp.write(struct.pack('>' + self._ref_format * s, *refs))

elif isinstance(value, dict):
elif isinstance(value, (dict, frozendict)):
keyRefs, valRefs = [], []

if self._sort_keys:
Expand Down Expand Up @@ -869,18 +869,18 @@ def _is_fmt_binary(header):
# Generic bits
#

_FORMATS={
FMT_XML: dict(
_FORMATS=frozendict({
FMT_XML: frozendict(
detect=_is_fmt_xml,
parser=_PlistParser,
writer=_PlistWriter,
),
FMT_BINARY: dict(
FMT_BINARY: frozendict(
detect=_is_fmt_binary,
parser=_BinaryPlistParser,
writer=_BinaryPlistWriter,
)
}
})


def load(fp, *, fmt=None, dict_type=dict, aware_datetime=False):
Expand Down
19 changes: 19 additions & 0 deletions 19 Lib/test/test_plistlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -792,6 +792,25 @@ def test_dict_members(self):
})
self.assertIsNot(pl2['first'], pl2['second'])

def test_frozendict(self):
pl = frozendict(
aString="Doodah",
anInt=728,
aDict=frozendict(
anotherString="hello",
aTrueValue=True,
),
aList=["A", "B", 12],
)

for fmt in ALL_FORMATS:
with self.subTest(fmt=fmt):
data = plistlib.dumps(pl, fmt=fmt)
pl2 = plistlib.loads(data)
self.assertEqual(pl2, dict(pl))
self.assertIsInstance(pl2, dict)
self.assertIsInstance(pl2['aDict'], dict)

def test_controlcharacters(self):
for i in range(128):
c = chr(i)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Support :class:`frozendict` in :mod:`plistlib`, for serialization only.
Patch by Hugo van Kemenade.
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.