MCPcopy Create free account
hub / github.com/Bitmessage/PyBitmessage / _unpack_map

Function _unpack_map

src/fallback/umsgpack/umsgpack.py:735–769  ·  view source on GitHub ↗
(code, fp, options)

Source from the content-addressed store, hash-verified

733
734
735def _unpack_map(code, fp, options):
736 if (ord(code) & 0xf0) == 0x80:
737 length = (ord(code) & ~0xf0)
738 elif code == b'\xde':
739 length = struct.unpack(">H", _read_except(fp, 2))[0]
740 elif code == b'\xdf':
741 length = struct.unpack(">I", _read_except(fp, 4))[0]
742 else:
743 raise Exception("logic error, not map: 0x%02x" % ord(code))
744
745 d = {} if not options.get('use_ordered_dict') \
746 else collections.OrderedDict()
747 for _ in xrange(length):
748 # Unpack key
749 k = _unpack(fp, options)
750
751 if isinstance(k, list):
752 # Attempt to convert list into a hashable tuple
753 k = _deep_list_to_tuple(k)
754 elif not isinstance(k, collections.Hashable):
755 raise UnhashableKeyException(
756 "encountered unhashable key: %s, %s" % (str(k), str(type(k))))
757 elif k in d:
758 raise DuplicateKeyException(
759 "encountered duplicate key: %s, %s" % (str(k), str(type(k))))
760
761 # Unpack value
762 v = _unpack(fp, options)
763
764 try:
765 d[k] = v
766 except TypeError:
767 raise UnhashableKeyException(
768 "encountered unhashable key: %s" % str(k))
769 return d
770
771
772def _unpack(fp, options):

Callers

nothing calls this directly

Calls 6

_read_exceptFunction · 0.85
_unpackFunction · 0.85
_deep_list_to_tupleFunction · 0.85
getMethod · 0.45

Tested by

no test coverage detected