From 6db6653bbc0841600248e4d7b7542591067c4157 Mon Sep 17 00:00:00 2001 From: Ronald Oussoren Date: Wed, 15 Jan 2014 11:32:35 +0100 Subject: Issue #14455: Fix some issues with plistlib * Negative integer support in binary plists was broken * Better exception for invalid data * Fix the versionadded/versionchanged markup in the documentation * Add the interface cleanup to what's new for 3.4 --- Doc/library/plistlib.rst | 19 +++- Doc/whatsnew/3.4.rst | 2 + Lib/plistlib.py | 22 ++++- Lib/test/test_plistlib.py | 156 +++++++++++++++++++------------- Mac/Tools/plistlib_generate_testdata.py | 8 ++ Misc/NEWS | 2 + 6 files changed, 138 insertions(+), 71 deletions(-) diff --git a/Doc/library/plistlib.rst b/Doc/library/plistlib.rst index 3cf8086..03cd3c1 100644 --- a/Doc/library/plistlib.rst +++ b/Doc/library/plistlib.rst @@ -79,6 +79,8 @@ This module defines the following functions: Load a plist from a bytes object. See :func:`load` for an explanation of the keyword arguments. + .. versionadded:: 3.4 + .. function:: dump(value, fp, \*, fmt=FMT_XML, sort_keys=True, skipkeys=False) @@ -102,8 +104,17 @@ This module defines the following functions: A :exc:`TypeError` will be raised if the object is of an unsupported type or a container that contains objects of unsupported types. - .. versionchanged:: 3.4 - Added the *fmt*, *sort_keys* and *skipkeys* arguments. + An :exc:`OverflowError` will be raised for integer values that cannot + be represented in (binary) plist files. + + .. warning:: + + For compatibility with Apple's libraries it is possible to write + an integer in the range from 2 ** 63 upto (and including) 2 ** 64 + to binary plists, even though these will be read back as negative + values. + + .. versionadded: 3.4 .. function:: dumps(value, \*, fmt=FMT_XML, sort_keys=True, skipkeys=False) @@ -112,6 +123,7 @@ This module defines the following functions: the documentation for :func:`dump` for an explanation of the keyword arguments of this function. + .. versionadded: 3.4 The following functions are deprecated: @@ -162,9 +174,6 @@ The following functions are deprecated: .. deprecated:: 3.4 Use :func:`dumps` instead. - .. versionchanged:: 3.4 - Added the *fmt*, *sort_keys* and *skipkeys* arguments. - The following classes are available: diff --git a/Doc/whatsnew/3.4.rst b/Doc/whatsnew/3.4.rst index d543973..6ace6d6 100644 --- a/Doc/whatsnew/3.4.rst +++ b/Doc/whatsnew/3.4.rst @@ -132,6 +132,8 @@ Significantly Improved Library Modules: a new :mod:`~email.message.Message` subclass (:class:`~email.contentmanager.EmailMessage`) that :ref:`simplify MIME handling ` (:issue:`18891`). +* :mod:`plistlib` has a cleaned up interface and support for binary + plist files (:issue:`14455`) CPython implementation improvements: diff --git a/Lib/plistlib.py b/Lib/plistlib.py index 277ce62..8286456 100644 --- a/Lib/plistlib.py +++ b/Lib/plistlib.py @@ -478,7 +478,10 @@ class _PlistWriter(_DumbXMLWriter): self.simple_element("false") elif isinstance(value, int): - self.simple_element("integer", "%d" % value) + if -1 << 63 <= value < 1 << 64: + self.simple_element("integer", "%d" % value) + else: + raise OverflowError(value) elif isinstance(value, float): self.simple_element("real", repr(value)) @@ -665,7 +668,8 @@ class _BinaryPlistParser: return b'' elif tokenH == 0x10: # int - return int.from_bytes(self._fp.read(1 << tokenL), 'big') + return int.from_bytes(self._fp.read(1 << tokenL), + 'big', signed=tokenL >= 3) elif token == 0x22: # real return struct.unpack('>f', self._fp.read(4))[0] @@ -871,14 +875,22 @@ class _BinaryPlistWriter (object): self._fp.write(b'\x09') elif isinstance(value, int): - if value < 1 << 8: + if value < 0: + try: + self._fp.write(struct.pack('>Bq', 0x13, value)) + except struct.error: + raise OverflowError(value) + elif value < 1 << 8: self._fp.write(struct.pack('>BB', 0x10, value)) elif value < 1 << 16: self._fp.write(struct.pack('>BH', 0x11, value)) elif value < 1 << 32: self._fp.write(struct.pack('>BL', 0x12, value)) else: - self._fp.write(struct.pack('>BQ', 0x13, value)) + try: + self._fp.write(struct.pack('>BQ', 0x13, value)) + except struct.error: + raise OverflowError(value) elif isinstance(value, float): self._fp.write(struct.pack('>Bd', 0x23, value)) @@ -933,7 +945,7 @@ class _BinaryPlistWriter (object): self._fp.write(struct.pack('>' + self._ref_format * s, *valRefs)) else: - raise InvalidFileException() + raise TypeError(value) def _is_fmt_binary(header): diff --git a/Lib/test/test_plistlib.py b/Lib/test/test_plistlib.py index abb1d19..a191608 100644 --- a/Lib/test/test_plistlib.py +++ b/Lib/test/test_plistlib.py @@ -7,6 +7,7 @@ import datetime import codecs import binascii import collections +import struct from test import support from io import BytesIO @@ -19,68 +20,74 @@ TESTDATA={ PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiPz4KPCFET0NU WVBFIHBsaXN0IFBVQkxJQyAiLS8vQXBwbGUvL0RURCBQTElTVCAxLjAvL0VO IiAiaHR0cDovL3d3dy5hcHBsZS5jb20vRFREcy9Qcm9wZXJ0eUxpc3QtMS4w - LmR0ZCI+CjxwbGlzdCB2ZXJzaW9uPSIxLjAiPgo8ZGljdD4KCTxrZXk+YURh - dGU8L2tleT4KCTxkYXRlPjIwMDQtMTAtMjZUMTA6MzM6MzNaPC9kYXRlPgoJ - PGtleT5hRGljdDwva2V5PgoJPGRpY3Q+CgkJPGtleT5hRmFsc2VWYWx1ZTwv - a2V5PgoJCTxmYWxzZS8+CgkJPGtleT5hVHJ1ZVZhbHVlPC9rZXk+CgkJPHRy - dWUvPgoJCTxrZXk+YVVuaWNvZGVWYWx1ZTwva2V5PgoJCTxzdHJpbmc+TcOk - c3NpZywgTWHDnzwvc3RyaW5nPgoJCTxrZXk+YW5vdGhlclN0cmluZzwva2V5 - PgoJCTxzdHJpbmc+Jmx0O2hlbGxvICZhbXA7ICdoaScgdGhlcmUhJmd0Ozwv - c3RyaW5nPgoJCTxrZXk+ZGVlcGVyRGljdDwva2V5PgoJCTxkaWN0PgoJCQk8 - a2V5PmE8L2tleT4KCQkJPGludGVnZXI+MTc8L2ludGVnZXI+CgkJCTxrZXk+ - Yjwva2V5PgoJCQk8cmVhbD4zMi41PC9yZWFsPgoJCQk8a2V5PmM8L2tleT4K - CQkJPGFycmF5PgoJCQkJPGludGVnZXI+MTwvaW50ZWdlcj4KCQkJCTxpbnRl - Z2VyPjI8L2ludGVnZXI+CgkJCQ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wZXJEaWN0CAlrAE0A5ABzAHMAaQBnACwAIABNAGEA318QFTxoZWxs + byAmICdoaScgdGhlcmUhPtMdHh8gISJRYVFiUWMQESNAQEAAAAAAAKMjJCUQ + ARACVHRleHQjP+AAAAAAAAClKCkqIStRQVFCEAyjIyQsEAMT////7V+g4AAT + //////////tWRG9vZGFo0KARAtihNE8Q+jxsb3RzIG9mIGJpbmFyeSBndW5r + PgABAgM8bG90cyBvZiBiaW5hcnkgZ3Vuaz4AAQIDPGxvdHMgb2YgYmluYXJ5 + IGd1bms+AAECAzxsb3RzIG9mIGJpbmFyeSBndW5rPgABAgM8bG90cyBvZiBi + aW5hcnkgZ3Vuaz4AAQIDPGxvdHMgb2YgYmluYXJ5IGd1bms+AAECAzxsb3Rz + IG9mIGJpbmFyeSBndW5rPgABAgM8bG90cyBvZiBiaW5hcnkgZ3Vuaz4AAQID + PGxvdHMgb2YgYmluYXJ5IGd1bms+AAECAzxsb3RzIG9mIGJpbmFyeSBndW5r + PgABAgNNPGJpbmFyeSBndW5rPl8QF1RoYXQgd2FzIGEgdW5pY29kZSBrZXku + AAgAKQAxADcAPQBEAEoAXABpAHEAfQCJAI8AmgCjALAAvwDIANEA3ADoAPMB + AQEPARoBGwEcATMBSwFSAVQBVgFYAVoBYwFnAWkBawFwAXkBfwGBAYMBhQGJ + AYsBlAGdAaQBpQGmAakBqwKoArYAAAAAAAACAQAAAAAAAAA3AAAAAAAAAAAA + AAAAAAAC0A=='''), } @@ -98,6 +105,9 @@ class TestPlistlib(unittest.TestCase): aList=["A", "B", 12, 32.5, [1, 2, 3]], aFloat = 0.5, anInt = 728, + aBigInt = 2 ** 63 - 44, + aNegativeInt = -5, + aNegativeBigInt = -80000000000, aDict=dict( anotherString="", aUnicodeValue='M\xe4ssig, Ma\xdf', @@ -133,6 +143,30 @@ class TestPlistlib(unittest.TestCase): self.assertRaises(AttributeError, plistlib.dump, pl, 'filename') self.assertRaises(AttributeError, plistlib.load, 'filename') + def test_invalid_type(self): + pl = [ object() ] + + for fmt in ALL_FORMATS: + with self.subTest(fmt=fmt): + self.assertRaises(TypeError, plistlib.dumps, pl, fmt=fmt) + + def test_int(self): + for pl in [0, 2**8-1, 2**8, 2**16-1, 2**16, 2**32-1, 2**32, + 2**63-1, 1, -2**63]: + for fmt in ALL_FORMATS: + with self.subTest(pl=pl, fmt=fmt): + data = plistlib.dumps(pl, fmt=fmt) + pl2 = plistlib.loads(data) + self.assertIsInstance(pl2, int) + self.assertEqual(pl, pl2) + data2 = plistlib.dumps(pl2, fmt=fmt) + self.assertEqual(data, data2) + + for fmt in ALL_FORMATS: + for pl in (2 ** 64 + 1, 2 ** 127-1, -2**64, -2 ** 127): + with self.subTest(pl=pl, fmt=fmt): + self.assertRaises(OverflowError, plistlib.dumps, + pl, fmt=fmt) def test_bytes(self): pl = self._create() diff --git a/Mac/Tools/plistlib_generate_testdata.py b/Mac/Tools/plistlib_generate_testdata.py index da1d976..68d4d74 100644 --- a/Mac/Tools/plistlib_generate_testdata.py +++ b/Mac/Tools/plistlib_generate_testdata.py @@ -23,7 +23,13 @@ def nsstr(value): def main(): pl = OrderedDict() + # Note: pl is an OrderedDict to control the order + # of keys, and hence have some control on the structure + # of the output file. + # New keys should be added in alphabetical order. + seconds = datetime.datetime(2004, 10, 26, 10, 33, 33, tzinfo=datetime.timezone(datetime.timedelta(0))).timestamp() + pl[nsstr('aBigInt')] = 2 ** 63 - 44 pl[nsstr('aDate')] = NSDate.dateWithTimeIntervalSince1970_(seconds) pl[nsstr('aDict')] = d = OrderedDict() @@ -52,6 +58,8 @@ def main(): aa.append(2) aa.append(3) + pl[nsstr('aNegativeBigInt')] = -80000000000 + pl[nsstr('aNegativeInt')] = -5 pl[nsstr('aString')] = nsstr('Doodah') pl[nsstr('anEmptyDict')] = NSMutableDictionary.alloc().init() diff --git a/Misc/NEWS b/Misc/NEWS index ea31879..f3ea917 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -72,6 +72,8 @@ Library - Issue #20229: Avoid plistlib deprecation warning in platform.mac_ver(). +- Issue #14455: Fix some problems with the new binary plist support in plistlib. + IDLE ---- -- cgit v0.12