Update plist.py from pymodules

This commit is contained in:
CorpNewt 2022-09-20 18:51:38 -05:00
parent 3e49ef255a
commit afef3f3139

View File

@ -91,8 +91,18 @@ def load(fp, fmt=None, use_builtin_types=None, dict_type=dict):
# Monkey patch!
def end_integer():
d = p.get_data()
p.add_object(int(d,16) if d.lower().startswith("0x") else int(d))
value = int(d,16) if d.lower().startswith("0x") else int(d)
if -1 << 63 <= value < 1 << 63:
p.add_object(value)
else:
raise OverflowError("Integer overflow at line {}".format(p.parser.CurrentLineNumber))
def end_data():
try:
p.add_object(plistlib._decode_base64(p.get_data()))
except Exception as e:
raise Exception("Data error at line {}: {}".format(p.parser.CurrentLineNumber,e))
p.end_integer = end_integer
p.end_data = end_data
return p.parse(fp)
elif not _is_binary(fp):
# Is not binary - assume a string - and try to load
@ -103,6 +113,10 @@ def load(fp, fmt=None, use_builtin_types=None, dict_type=dict):
# Create a new PlistParser object - then we need to set up
# the values and parse.
p = plistlib.PlistParser()
parser = ParserCreate()
parser.StartElementHandler = p.handleBeginElement
parser.EndElementHandler = p.handleEndElement
parser.CharacterDataHandler = p.handleData
# We also need to monkey patch this to allow for other dict_types
def begin_dict(attrs):
d = dict_type()
@ -110,13 +124,19 @@ def load(fp, fmt=None, use_builtin_types=None, dict_type=dict):
p.stack.append(d)
def end_integer():
d = p.getData()
p.addObject(int(d,16) if d.lower().startswith("0x") else int(d))
value = int(d,16) if d.lower().startswith("0x") else int(d)
if -1 << 63 <= value < 1 << 63:
p.addObject(value)
else:
raise OverflowError("Integer overflow at line {}".format(parser.CurrentLineNumber))
def end_data():
try:
p.addObject(plistlib.Data.fromBase64(p.getData()))
except Exception as e:
raise Exception("Data error at line {}: {}".format(parser.CurrentLineNumber,e))
p.begin_dict = begin_dict
p.end_integer = end_integer
parser = ParserCreate()
parser.StartElementHandler = p.handleBeginElement
parser.EndElementHandler = p.handleEndElement
parser.CharacterDataHandler = p.handleData
p.end_data = end_data
if isinstance(fp, unicode):
# Encode unicode -> string; use utf-8 for safety
fp = fp.encode("utf-8")