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

Add support for loading XML plists from a string value instead of a only bytes value.
This commit is contained in:
AN Long 2024-01-06 17:26:59 +08:00 • committed by GitHub
parent 66f3964815
commit bbf214df23
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 24 additions and 3 deletions

View file

@ -906,6 +906,11 @@ def loads(value, *, fmt=None, dict_type=dict, aware_datetime=False):
"""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, aware_datetime=aware_datetime)