From 7e8162ce85d566a488839f364dfebe82a23f2d60 Mon Sep 17 00:00:00 2001 From: Mingye Wang Date: Sun, 23 Feb 2020 22:33:57 +0800 Subject: [PATCH 01/16] bpo-39732: encode UIDs in XML as CF$UID This commit lets the XML writer encode UIDs in XML as a dictionary with a single key CF$UID pointing to an integer value. This is how Apple handles UIDs when writing XMLs. It appears significantly harder to get the parser to replace a value it has just written, so I am not attempting it yet. I mean, I know how to replace it when the parent is a list, but when it is a dictionary it seems that I will need some extra key-saving along the stack. --- Lib/plistlib.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Lib/plistlib.py b/Lib/plistlib.py index 3292c30d5fb29b9..29d46557d88ac5f 100644 --- a/Lib/plistlib.py +++ b/Lib/plistlib.py @@ -365,6 +365,9 @@ def write_value(self, value): elif isinstance(value, (tuple, list)): self.write_array(value) + elif isinstance(value, plist.UID): + self.write_dict({"CF$UID": int(value.data)}) + else: raise TypeError("unsupported type: %s" % type(value)) From 393bf6a30002fa58c13d47cebe87c06f786ab9d3 Mon Sep 17 00:00:00 2001 From: Mingye Wang Date: Mon, 24 Feb 2020 12:36:41 +0800 Subject: [PATCH 02/16] plistlib: add XML UID unmarshal; do XML test on UID --- Lib/plistlib.py | 14 +++++++++++++- Lib/test/test_plistlib.py | 2 ++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/Lib/plistlib.py b/Lib/plistlib.py index 29d46557d88ac5f..bca929e2fbe3051 100644 --- a/Lib/plistlib.py +++ b/Lib/plistlib.py @@ -175,6 +175,7 @@ def __init__(self, dict_type): self.stack = [] self.current_key = None self.root = None + self.previous_dict_key = None self._dict_type = dict_type def parse(self, fileobj): @@ -231,6 +232,7 @@ def get_data(self): def begin_dict(self, attrs): d = self._dict_type() + self.previous_dict_key = self.current_key self.add_object(d) self.stack.append(d) @@ -238,7 +240,17 @@ def end_dict(self): if self.current_key: raise ValueError("missing value for key '%s' at line %d" % (self.current_key,self.parser.CurrentLineNumber)) - self.stack.pop() + d = self.stack.pop() + # Unmarshal CF$UID to UID and replace the dict node + if len(d) == 1 and "CF$UID" in d and isinstance(d["CF$UID"], int): + uid = UID(d["CF$UID"]) + if self.previous_dict_key: + stack[-1][self.previous_dict_key] = uid + elif not self.stack: + self.root = uid + else: + assert isinstance(self.stack[-1], type([])) + self.stack[-1][-1] = uid def end_key(self): if self.current_key or not isinstance(self.stack[-1], dict): diff --git a/Lib/test/test_plistlib.py b/Lib/test/test_plistlib.py index b08ababa341cfe5..0c1e815eac9b2a7 100644 --- a/Lib/test/test_plistlib.py +++ b/Lib/test/test_plistlib.py @@ -523,6 +523,7 @@ def test_indentation_dict_mix(self): def test_uid(self): data = UID(1) self.assertEqual(plistlib.loads(plistlib.dumps(data, fmt=plistlib.FMT_BINARY)), data) + self.assertEqual(plistlib.loads(plistlib.dumps(data, fmt=plistlib.FMT_XML)), data) dict_data = { 'uid0': UID(0), 'uid2': UID(2), @@ -532,6 +533,7 @@ def test_uid(self): 'uid63': UID(2 ** 63) } self.assertEqual(plistlib.loads(plistlib.dumps(dict_data, fmt=plistlib.FMT_BINARY)), dict_data) + self.assertEqual(plistlib.loads(plistlib.dumps(dict_data, fmt=plistlib.FMT_XML)), data) def test_uid_data(self): uid = UID(1) From 098ff1de3c1e6ea7d1169a66f581ff834ea8247d Mon Sep 17 00:00:00 2001 From: Mingye Wang Date: Mon, 24 Feb 2020 12:40:05 +0800 Subject: [PATCH 03/16] plistlib: add UID see-also The apple ref does not mention UID at all. Use a source that does. --- Doc/library/plistlib.rst | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Doc/library/plistlib.rst b/Doc/library/plistlib.rst index 732ef3536863cc8..a56bc7fda407215 100644 --- a/Doc/library/plistlib.rst +++ b/Doc/library/plistlib.rst @@ -48,6 +48,8 @@ or :class:`datetime.datetime` objects. `PList manual page `_ Apple's documentation of the file format. + `Property list `_ + Wikipedia's description of the format and archiers. This module defines the following functions: @@ -130,7 +132,7 @@ The following classes are available: .. class:: UID(data) Wraps an :class:`int`. This is used when reading or writing NSKeyedArchiver - encoded data, which contains UID (see PList manual). + encoded data, which contains UID (see Wikipedia). It has one attribute, :attr:`data`, which can be used to retrieve the int value of the UID. :attr:`data` must be in the range ``0 <= data < 2**64``. From b3f36208e0d2e41e5446f056446cd294dfcd8b87 Mon Sep 17 00:00:00 2001 From: Mingye Wang Date: Mon, 24 Feb 2020 12:49:19 +0800 Subject: [PATCH 04/16] plistlib: add news --- Doc/library/plistlib.rst | 3 ++- .../next/Library/2020-02-24-12-48-58.bpo-39732.4qZX4h.rst | 2 ++ 2 files changed, 4 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2020-02-24-12-48-58.bpo-39732.4qZX4h.rst diff --git a/Doc/library/plistlib.rst b/Doc/library/plistlib.rst index a56bc7fda407215..7672535fa76b425 100644 --- a/Doc/library/plistlib.rst +++ b/Doc/library/plistlib.rst @@ -42,7 +42,8 @@ or :class:`datetime.datetime` objects. by NSKeyedArchiver and NSKeyedUnarchiver. .. versionchanged:: 3.9 - Old API removed. + Old API removed. Support added for reading and writing :class:`UID` tokens in XML + plists. .. seealso:: diff --git a/Misc/NEWS.d/next/Library/2020-02-24-12-48-58.bpo-39732.4qZX4h.rst b/Misc/NEWS.d/next/Library/2020-02-24-12-48-58.bpo-39732.4qZX4h.rst new file mode 100644 index 000000000000000..bb5656cd11c5bdf --- /dev/null +++ b/Misc/NEWS.d/next/Library/2020-02-24-12-48-58.bpo-39732.4qZX4h.rst @@ -0,0 +1,2 @@ +Add support for reading and writing :class:`plistlib.UID` tokens in XML +plists. From 306a62e87fcbcafe86dd7446f4c7261c704a7a71 Mon Sep 17 00:00:00 2001 From: Mingye Wang Date: Mon, 24 Feb 2020 12:52:17 +0800 Subject: [PATCH 05/16] plistlib: fixup! class name oops --- Lib/plistlib.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/plistlib.py b/Lib/plistlib.py index bca929e2fbe3051..a57373bdea701e1 100644 --- a/Lib/plistlib.py +++ b/Lib/plistlib.py @@ -377,7 +377,7 @@ def write_value(self, value): elif isinstance(value, (tuple, list)): self.write_array(value) - elif isinstance(value, plist.UID): + elif isinstance(value, UID): self.write_dict({"CF$UID": int(value.data)}) else: From 96ad451f8d219f0892c4e8445563fd46059cffa0 Mon Sep 17 00:00:00 2001 From: Mingye Wang Date: Mon, 24 Feb 2020 13:15:09 +0800 Subject: [PATCH 06/16] fixup! Update Lib/plistlib.py --- Lib/plistlib.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/plistlib.py b/Lib/plistlib.py index a57373bdea701e1..95282f0ed6130fd 100644 --- a/Lib/plistlib.py +++ b/Lib/plistlib.py @@ -245,7 +245,7 @@ def end_dict(self): if len(d) == 1 and "CF$UID" in d and isinstance(d["CF$UID"], int): uid = UID(d["CF$UID"]) if self.previous_dict_key: - stack[-1][self.previous_dict_key] = uid + self.stack[-1][self.previous_dict_key] = uid elif not self.stack: self.root = uid else: From cfcb93ff13554ca4dd480693672adc9eb83a825d Mon Sep 17 00:00:00 2001 From: Mingye Wang Date: Mon, 24 Feb 2020 14:15:22 +0800 Subject: [PATCH 07/16] Update Lib/test/test_plistlib.py --- Lib/test/test_plistlib.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_plistlib.py b/Lib/test/test_plistlib.py index 0c1e815eac9b2a7..d55ade1b85bd82e 100644 --- a/Lib/test/test_plistlib.py +++ b/Lib/test/test_plistlib.py @@ -533,7 +533,7 @@ def test_uid(self): 'uid63': UID(2 ** 63) } self.assertEqual(plistlib.loads(plistlib.dumps(dict_data, fmt=plistlib.FMT_BINARY)), dict_data) - self.assertEqual(plistlib.loads(plistlib.dumps(dict_data, fmt=plistlib.FMT_XML)), data) + self.assertEqual(plistlib.loads(plistlib.dumps(dict_data, fmt=plistlib.FMT_XML)), dict_data) def test_uid_data(self): uid = UID(1) From 5e23322133961b5a2c313937a20f89cd2191a4b3 Mon Sep 17 00:00:00 2001 From: Mingye Wang Date: Mon, 30 Nov 2020 13:10:25 +0800 Subject: [PATCH 08/16] Appwy suggestions from code review --- Doc/library/plistlib.rst | 7 +++++-- Lib/plistlib.py | 9 ++++++--- Lib/test/test_plistlib.py | 10 ++++++---- 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/Doc/library/plistlib.rst b/Doc/library/plistlib.rst index 7672535fa76b425..c616e81053d550d 100644 --- a/Doc/library/plistlib.rst +++ b/Doc/library/plistlib.rst @@ -42,7 +42,10 @@ or :class:`datetime.datetime` objects. by NSKeyedArchiver and NSKeyedUnarchiver. .. versionchanged:: 3.9 - Old API removed. Support added for reading and writing :class:`UID` tokens in XML + Old API removed. + +.. versionchanged:: 3.10 + Support added for reading and writing :class:`UID` tokens in XML plists. .. seealso:: @@ -50,7 +53,7 @@ or :class:`datetime.datetime` objects. `PList manual page `_ Apple's documentation of the file format. `Property list `_ - Wikipedia's description of the format and archiers. + Wikipedia's description of the format and archivers. This module defines the following functions: diff --git a/Lib/plistlib.py b/Lib/plistlib.py index 95282f0ed6130fd..e4ae43251ed0f26 100644 --- a/Lib/plistlib.py +++ b/Lib/plistlib.py @@ -112,6 +112,9 @@ def __hash__(self): """ +# CF$UID KEY +CFUID = "CF$UID" + # Regex to find any control chars, except for \t \n and \r _controlCharPat = re.compile( @@ -242,14 +245,14 @@ def end_dict(self): (self.current_key,self.parser.CurrentLineNumber)) d = self.stack.pop() # Unmarshal CF$UID to UID and replace the dict node - if len(d) == 1 and "CF$UID" in d and isinstance(d["CF$UID"], int): - uid = UID(d["CF$UID"]) + if len(d) == 1 and CFUID in d and isinstance(d[CFUID], int): + uid = UID(d[CFUID]) if self.previous_dict_key: self.stack[-1][self.previous_dict_key] = uid elif not self.stack: self.root = uid else: - assert isinstance(self.stack[-1], type([])) + # self.stack[-1] can only be a list self.stack[-1][-1] = uid def end_key(self): diff --git a/Lib/test/test_plistlib.py b/Lib/test/test_plistlib.py index d55ade1b85bd82e..abc7c2838b4498a 100644 --- a/Lib/test/test_plistlib.py +++ b/Lib/test/test_plistlib.py @@ -522,8 +522,9 @@ def test_indentation_dict_mix(self): def test_uid(self): data = UID(1) - self.assertEqual(plistlib.loads(plistlib.dumps(data, fmt=plistlib.FMT_BINARY)), data) - self.assertEqual(plistlib.loads(plistlib.dumps(data, fmt=plistlib.FMT_XML)), data) + for fmt in ALL_FORMATS: + self.assertEqual(plistlib.loads(plistlib.dumps(data, fmt=fmt)), data) + dict_data = { 'uid0': UID(0), 'uid2': UID(2), @@ -532,8 +533,9 @@ def test_uid(self): 'uid32': UID(2 ** 32), 'uid63': UID(2 ** 63) } - self.assertEqual(plistlib.loads(plistlib.dumps(dict_data, fmt=plistlib.FMT_BINARY)), dict_data) - self.assertEqual(plistlib.loads(plistlib.dumps(dict_data, fmt=plistlib.FMT_XML)), dict_data) + for fmt in ALL_FORMATS: + self.assertEqual(plistlib.loads(plistlib.dumps(dict_data, fmt=fmt)), dict_data) + def test_uid_data(self): uid = UID(1) From 7acc2e5f0f6d4105ba17d89c1f18e20c4b494683 Mon Sep 17 00:00:00 2001 From: Mingye Wang Date: Sat, 5 Dec 2020 14:06:14 +0800 Subject: [PATCH 09/16] fixup! Add CFUID to __all__ --- Lib/plistlib.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/plistlib.py b/Lib/plistlib.py index e4ae43251ed0f26..2b3bccee8fc7271 100644 --- a/Lib/plistlib.py +++ b/Lib/plistlib.py @@ -55,7 +55,7 @@ print(pl["foo"]) """ __all__ = [ - "InvalidFileException", "FMT_XML", "FMT_BINARY", "load", "dump", "loads", "dumps", "UID" + "InvalidFileException", "FMT_XML", "FMT_BINARY", "load", "dump", "loads", "dumps", "UID", "CFUID" ] import binascii From 03a0c27ec220c27a712fc2f06823e1199c024eb8 Mon Sep 17 00:00:00 2001 From: Oleg Iarygin Date: Wed, 19 Apr 2023 15:17:56 +0400 Subject: [PATCH 10/16] Address the review on Wikipedia --- Doc/library/plistlib.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/plistlib.rst b/Doc/library/plistlib.rst index c616e81053d550d..49198873b96fca7 100644 --- a/Doc/library/plistlib.rst +++ b/Doc/library/plistlib.rst @@ -136,7 +136,7 @@ The following classes are available: .. class:: UID(data) Wraps an :class:`int`. This is used when reading or writing NSKeyedArchiver - encoded data, which contains UID (see Wikipedia). + encoded data, which contains UID (see Swift foundation source). It has one attribute, :attr:`data`, which can be used to retrieve the int value of the UID. :attr:`data` must be in the range ``0 <= data < 2**64``. From 22e6ec38bccc6a4c78ebd692c2b78e7185c6830c Mon Sep 17 00:00:00 2001 From: Oleg Iarygin Date: Wed, 19 Apr 2023 15:19:26 +0400 Subject: [PATCH 11/16] Update `versionchanged` --- Doc/library/plistlib.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/plistlib.rst b/Doc/library/plistlib.rst index 49198873b96fca7..3b78a9d094887e5 100644 --- a/Doc/library/plistlib.rst +++ b/Doc/library/plistlib.rst @@ -44,7 +44,7 @@ or :class:`datetime.datetime` objects. .. versionchanged:: 3.9 Old API removed. -.. versionchanged:: 3.10 +.. versionchanged:: 3.13 Support added for reading and writing :class:`UID` tokens in XML plists. From c36153f082aeb6dbee0c8968f06fd2153a06d189 Mon Sep 17 00:00:00 2001 From: Mingye Wang Date: Thu, 20 Apr 2023 15:33:13 +0800 Subject: [PATCH 12/16] Do the CFUID_KEY thing --- Lib/plistlib.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Lib/plistlib.py b/Lib/plistlib.py index 2b3bccee8fc7271..f744481b6462703 100644 --- a/Lib/plistlib.py +++ b/Lib/plistlib.py @@ -55,7 +55,7 @@ print(pl["foo"]) """ __all__ = [ - "InvalidFileException", "FMT_XML", "FMT_BINARY", "load", "dump", "loads", "dumps", "UID", "CFUID" + "InvalidFileException", "FMT_XML", "FMT_BINARY", "load", "dump", "loads", "dumps", "UID", "CFUID_KEY" ] import binascii @@ -113,7 +113,7 @@ def __hash__(self): """ # CF$UID KEY -CFUID = "CF$UID" +CFUID_KEY = "CF$UID" # Regex to find any control chars, except for \t \n and \r @@ -245,8 +245,8 @@ def end_dict(self): (self.current_key,self.parser.CurrentLineNumber)) d = self.stack.pop() # Unmarshal CF$UID to UID and replace the dict node - if len(d) == 1 and CFUID in d and isinstance(d[CFUID], int): - uid = UID(d[CFUID]) + if len(d) == 1 and CFUID_KEY in d and isinstance(d[CFUID_KEY], int): + uid = UID(d[CFUID_KEY]) if self.previous_dict_key: self.stack[-1][self.previous_dict_key] = uid elif not self.stack: From 72e1e891f6df321643a308e1fb5b8ab6b8891ba8 Mon Sep 17 00:00:00 2001 From: Mingye Wang Date: Thu, 20 Apr 2023 15:38:27 +0800 Subject: [PATCH 13/16] Document CFUID_KEY --- Doc/library/plistlib.rst | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/Doc/library/plistlib.rst b/Doc/library/plistlib.rst index 3b78a9d094887e5..201d6bc88df9016 100644 --- a/Doc/library/plistlib.rst +++ b/Doc/library/plistlib.rst @@ -155,11 +155,18 @@ The following constants are available: .. data:: FMT_BINARY - The binary format for plist files - + The binary format for plist files. + .. versionadded:: 3.4 +.. data:: CFUID_KEY + + The dictionary key used to represent a UID in XML-format plist files. + + .. versionadded :: 3.13 + + Examples -------- From e147e1828ef8a726ccb95a12e05fb3ddf6714c2f Mon Sep 17 00:00:00 2001 From: Mingye Wang Date: Thu, 20 Apr 2023 15:48:52 +0800 Subject: [PATCH 14/16] fixup! whitespace issue :( --- Doc/library/plistlib.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Doc/library/plistlib.rst b/Doc/library/plistlib.rst index 201d6bc88df9016..d74c6e536c175b5 100644 --- a/Doc/library/plistlib.rst +++ b/Doc/library/plistlib.rst @@ -156,14 +156,14 @@ The following constants are available: .. data:: FMT_BINARY The binary format for plist files. - + .. versionadded:: 3.4 .. data:: CFUID_KEY The dictionary key used to represent a UID in XML-format plist files. - + .. versionadded :: 3.13 From fe7f65bc2c9c33498b952df26d39a12bcc3ecfea Mon Sep 17 00:00:00 2001 From: Mingye Wang Date: Fri, 14 Jul 2023 17:12:42 +0800 Subject: [PATCH 15/16] M/T/plistlib_generate_testdata.py: prepare XML test case No idea if it works. It's all NS to me! --- Mac/Tools/plistlib_generate_testdata.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Mac/Tools/plistlib_generate_testdata.py b/Mac/Tools/plistlib_generate_testdata.py index 3349c604a37a4cc..122a7d8bb0ddc00 100755 --- a/Mac/Tools/plistlib_generate_testdata.py +++ b/Mac/Tools/plistlib_generate_testdata.py @@ -92,6 +92,10 @@ def main(): keyed_archive_data = NSKeyedArchiver.archivedDataWithRootObject_("KeyArchive UID Test") print(" 'KEYED_ARCHIVE': binascii.a2b_base64(b'''\n %s''')," % (_encode_base64(bytes(keyed_archive_data)).decode('ascii')[:-1])) + nska_xml = NSKeyedArchiver.init() + nska_xml.outputFormat = NSPropertyListXMLFormat_v1_0 + keyed_archive_data_xml = NSKeyedArchiver.archivedDataWithRootObject_("KeyArchive UID Test") + print(" 'KEYED_ARCHIVE_XML': binascii.a2b_base64(b'''\n %s''')," % (_encode_base64(bytes(keyed_archive_data)).decode('ascii')[:-1])) print("}") print() From 7d887ac71d12b29047b5963baa01e93f530f4b11 Mon Sep 17 00:00:00 2001 From: Mingye Wang Date: Sat, 21 Nov 2026 12:15:01 +0800 Subject: [PATCH 16/16] gh-83913: Support UID tokens in XML plists Encode UID objects as a dictionary with a single CFUID_KEY key when writing XML, and convert such dictionaries back to UID objects when parsing. Track the key of the enclosing dictionary per dict so that nested structures are handled correctly. Also document CFUID_KEY, add XML tests, and drop a broken, unused fragment from the macOS testdata generator. Co-Authored-By: Skadi "Skadeep" Deepseek --- Doc/library/plistlib.rst | 14 ++++---- Doc/whatsnew/3.16.rst | 9 +++++ Lib/plistlib.py | 20 ++++++----- Lib/test/test_plistlib.py | 34 +++++++++++++++++++ Mac/Tools/plistlib_generate_testdata.py | 4 --- ...-02-24-12-48-58.gh-issue-83913.4qZX4h.rst} | 4 +-- 6 files changed, 64 insertions(+), 21 deletions(-) rename Misc/NEWS.d/next/Library/{2020-02-24-12-48-58.bpo-39732.4qZX4h.rst => 2020-02-24-12-48-58.gh-issue-83913.4qZX4h.rst} (97%) diff --git a/Doc/library/plistlib.rst b/Doc/library/plistlib.rst index cc61c5b8cb63492..6e661e391f42358 100644 --- a/Doc/library/plistlib.rst +++ b/Doc/library/plistlib.rst @@ -40,7 +40,7 @@ or :class:`datetime.datetime` objects. .. versionchanged:: 3.9 Old API removed. -.. versionchanged:: 3.13 +.. versionchanged:: 3.16 Support added for reading and writing :class:`UID` tokens in XML plists. @@ -48,8 +48,6 @@ or :class:`datetime.datetime` objects. `PList manual page `_ Apple's documentation of the file format. - `Property list `_ - Wikipedia's description of the format and archivers. This module defines the following functions: @@ -147,7 +145,10 @@ The following classes are available: .. class:: UID(data) Wraps an :class:`int`. This is used when reading or writing NSKeyedArchiver - encoded data, which contains UID (see Swift foundation source). + encoded data, which contains UID (see PList manual). + + In XML plists, a UID is written as a dictionary with a single + :data:`CFUID_KEY` key. .. attribute:: data @@ -174,9 +175,10 @@ The following constants are available: .. data:: CFUID_KEY - The dictionary key used to represent a UID in XML-format plist files. + The dictionary key used to represent a :class:`UID` in XML-format plist + files. - .. versionadded :: 3.13 + .. versionadded:: 3.16 The module defines the following exceptions: diff --git a/Doc/whatsnew/3.16.rst b/Doc/whatsnew/3.16.rst index 7995da012858245..2a9583ce653881c 100644 --- a/Doc/whatsnew/3.16.rst +++ b/Doc/whatsnew/3.16.rst @@ -618,6 +618,15 @@ pickle :class:`!WeakMethod` and :class:`!WeakSet` can now also be copied. (Contributed by Serhiy Storchaka in :gh:`74876`.) +plistlib +-------- + +* :mod:`plistlib` now supports reading and writing :class:`plistlib.UID` + tokens in XML plists. In XML, a UID is represented as a dictionary with a + single :data:`plistlib.CFUID_KEY` key, matching the format written by + Apple's ``NSKeyedArchiver``. + (Contributed by Mingye Wang in :gh:`83913`.) + quopri ------ diff --git a/Lib/plistlib.py b/Lib/plistlib.py index 517b024c0f1f8d7..93f8f4a87398264 100644 --- a/Lib/plistlib.py +++ b/Lib/plistlib.py @@ -198,7 +198,8 @@ def __init__(self, dict_type, aware_datetime=False): self.stack = [] self.current_key = None self.root = None - self.previous_dict_key = None + # Key under which each dict was added to its parent. + self._dict_keys = [] self._dict_type = dict_type self._aware_datetime = aware_datetime @@ -256,7 +257,7 @@ def get_data(self): def begin_dict(self, attrs): d = self._dict_type() - self.previous_dict_key = self.current_key + self._dict_keys.append(self.current_key) self.add_object(d) self.stack.append(d) @@ -265,15 +266,16 @@ def end_dict(self): raise ValueError("missing value for key '%s' at line %d" % (self.current_key,self.parser.CurrentLineNumber)) d = self.stack.pop() - # Unmarshal CF$UID to UID and replace the dict node - if len(d) == 1 and CFUID_KEY in d and isinstance(d[CFUID_KEY], int): + key = self._dict_keys.pop() + # A dict with a single CF$UID key is how XML plists encode a UID. + if (len(d) == 1 and CFUID_KEY in d + and type(d[CFUID_KEY]) is int): uid = UID(d[CFUID_KEY]) - if self.previous_dict_key: - self.stack[-1][self.previous_dict_key] = uid - elif not self.stack: + if not self.stack: self.root = uid + elif isinstance(self.stack[-1], dict): + self.stack[-1][key] = uid else: - # self.stack[-1] can only be a list self.stack[-1][-1] = uid def end_key(self): @@ -405,7 +407,7 @@ def write_value(self, value): self.write_array(value) elif isinstance(value, UID): - self.write_dict({"CF$UID": int(value.data)}) + self.write_dict({CFUID_KEY: int(value.data)}) else: raise TypeError("unsupported type: %s" % type(value)) diff --git a/Lib/test/test_plistlib.py b/Lib/test/test_plistlib.py index 4331d4f6684ad57..5b14201f17edda8 100644 --- a/Lib/test/test_plistlib.py +++ b/Lib/test/test_plistlib.py @@ -17,6 +17,7 @@ from test import support from test.support import os_helper from io import BytesIO +from xml.etree import ElementTree from plistlib import UID @@ -613,6 +614,39 @@ def test_uid(self): for fmt in ALL_FORMATS: self.assertEqual(plistlib.loads(plistlib.dumps(dict_data, fmt=fmt)), dict_data) + def test_uid_xml_format(self): + # XML plists encode a UID as a dict with a single CFUID_KEY key. + data = plistlib.dumps({'uid': UID(42)}, fmt=plistlib.FMT_XML) + root = ElementTree.fromstring(data) + uid_dict = root.find('dict/dict') + self.assertIsNotNone(uid_dict) + self.assertEqual(uid_dict.find('key').text, plistlib.CFUID_KEY) + self.assertEqual(uid_dict.find('integer').text, '42') + + def test_uid_xml_parse(self): + uid_xml = b'CF$UID1' + + def wrap(body): + return b'' + body + b'' + + self.assertEqual(plistlib.loads(wrap(uid_xml)), UID(1)) + self.assertEqual(plistlib.loads(wrap(b'' + uid_xml + b'')), + [UID(1)]) + self.assertEqual( + plistlib.loads(wrap(b'ab' + + uid_xml + b'')), + {'a': {'b': UID(1)}}) + self.assertEqual( + plistlib.loads(wrap(b'' + uid_xml + b'')), + {'': UID(1)}) + # Only dicts with a single integer CF$UID key are UIDs. + self.assertEqual( + plistlib.loads(wrap(b'CF$UID1' + b'x')), + {'CF$UID': 1, 'x': True}) + self.assertEqual( + plistlib.loads(wrap(b'CF$UID')), + {'CF$UID': True}) def test_uid_data(self): uid = UID(1) diff --git a/Mac/Tools/plistlib_generate_testdata.py b/Mac/Tools/plistlib_generate_testdata.py index 122a7d8bb0ddc00..3349c604a37a4cc 100755 --- a/Mac/Tools/plistlib_generate_testdata.py +++ b/Mac/Tools/plistlib_generate_testdata.py @@ -92,10 +92,6 @@ def main(): keyed_archive_data = NSKeyedArchiver.archivedDataWithRootObject_("KeyArchive UID Test") print(" 'KEYED_ARCHIVE': binascii.a2b_base64(b'''\n %s''')," % (_encode_base64(bytes(keyed_archive_data)).decode('ascii')[:-1])) - nska_xml = NSKeyedArchiver.init() - nska_xml.outputFormat = NSPropertyListXMLFormat_v1_0 - keyed_archive_data_xml = NSKeyedArchiver.archivedDataWithRootObject_("KeyArchive UID Test") - print(" 'KEYED_ARCHIVE_XML': binascii.a2b_base64(b'''\n %s''')," % (_encode_base64(bytes(keyed_archive_data)).decode('ascii')[:-1])) print("}") print() diff --git a/Misc/NEWS.d/next/Library/2020-02-24-12-48-58.bpo-39732.4qZX4h.rst b/Misc/NEWS.d/next/Library/2020-02-24-12-48-58.gh-issue-83913.4qZX4h.rst similarity index 97% rename from Misc/NEWS.d/next/Library/2020-02-24-12-48-58.bpo-39732.4qZX4h.rst rename to Misc/NEWS.d/next/Library/2020-02-24-12-48-58.gh-issue-83913.4qZX4h.rst index bb5656cd11c5bdf..457a12230a60c1d 100644 --- a/Misc/NEWS.d/next/Library/2020-02-24-12-48-58.bpo-39732.4qZX4h.rst +++ b/Misc/NEWS.d/next/Library/2020-02-24-12-48-58.gh-issue-83913.4qZX4h.rst @@ -1,2 +1,2 @@ -Add support for reading and writing :class:`plistlib.UID` tokens in XML -plists. +Add support for reading and writing :class:`plistlib.UID` tokens in XML +plists.