MCPcopy
hub / github.com/corpnewt/GenSMBIOS / load

Function load

Scripts/plist.py:87–183  ·  view source on GitHub ↗
(fp, fmt=None, use_builtin_types=None, dict_type=dict)

Source from the content-addressed store, hash-verified

85### ###
86
87def load(fp, fmt=None, use_builtin_types=None, dict_type=dict):
88 if _is_binary(fp):
89 use_builtin_types = False if use_builtin_types is None else use_builtin_types
90 try:
91 p = _BinaryPlistParser(use_builtin_types=use_builtin_types, dict_type=dict_type)
92 except:
93 # Python 3.9 removed use_builtin_types
94 p = _BinaryPlistParser(dict_type=dict_type)
95 return p.parse(fp)
96 elif _check_py3():
97 offset = _seek_past_whitespace(fp)
98 use_builtin_types = True if use_builtin_types is None else use_builtin_types
99 # We need to monkey patch this to allow for hex integers - code taken/modified from
100 # https://github.com/python/cpython/blob/3.8/Lib/plistlib.py
101 if fmt is None:
102 header = fp.read(32)
103 fp.seek(offset)
104 for info in plistlib._FORMATS.values():
105 if info['detect'](header):
106 P = info['parser']
107 break
108 else:
109 raise plistlib.InvalidFileException()
110 else:
111 P = plistlib._FORMATS[fmt]['parser']
112 try:
113 p = P(use_builtin_types=use_builtin_types, dict_type=dict_type)
114 except:
115 # Python 3.9 removed use_builtin_types
116 p = P(dict_type=dict_type)
117 if isinstance(p,plistlib._PlistParser):
118 # Monkey patch!
119 def end_integer():
120 d = p.get_data()
121 value = int(d,16) if d.lower().startswith("0x") else int(d)
122 if -1 << 63 <= value < 1 << 64:
123 p.add_object(value)
124 else:
125 raise OverflowError("Integer overflow at line {}".format(p.parser.CurrentLineNumber))
126 def end_data():
127 try:
128 p.add_object(plistlib._decode_base64(p.get_data()))
129 except Exception as e:
130 raise Exception("Data error at line {}: {}".format(p.parser.CurrentLineNumber,e))
131 p.end_integer = end_integer
132 p.end_data = end_data
133 return p.parse(fp)
134 else:
135 offset = _seek_past_whitespace(fp)
136 # Is not binary - assume a string - and try to load
137 # We avoid using readPlistFromString() as that uses
138 # cStringIO and fails when Unicode strings are detected
139 # Don't subclass - keep the parser local
140 from xml.parsers.expat import ParserCreate
141 # Create a new PlistParser object - then we need to set up
142 # the values and parse.
143 p = plistlib.PlistParser()
144 parser = ParserCreate()

Callers 2

readPlistFunction · 0.85
loadsFunction · 0.85

Calls 5

_is_binaryFunction · 0.85
_BinaryPlistParserClass · 0.85
_check_py3Function · 0.85
_seek_past_whitespaceFunction · 0.85
parseMethod · 0.80

Tested by

no test coverage detected