Page Menu
Home
Software Heritage
Search
Configure Global Search
Log In
Files
F7147877
D4568.id16224.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
1 KB
Subscribers
None
D4568.id16224.diff
View Options
diff --git a/swh/core/api/serializers.py b/swh/core/api/serializers.py
--- a/swh/core/api/serializers.py
+++ b/swh/core/api/serializers.py
@@ -90,6 +90,7 @@
class MsgpackExtTypeCodes(Enum):
LONG_INT = 1
+ LONG_NEG_INT = 2
def encode_data_client(data: Any, extra_encoders=None) -> bytes:
@@ -223,13 +224,17 @@
def encode_types(obj):
if isinstance(obj, int):
+ if obj > 0:
+ code = MsgpackExtTypeCodes.LONG_INT.value
+ else:
+ code = MsgpackExtTypeCodes.LONG_NEG_INT.value
+ obj = -obj
+
# integer overflowed while packing. Handle it as an extended type
length, rem = divmod(obj.bit_length(), 8)
if rem:
length += 1
- return msgpack.ExtType(
- MsgpackExtTypeCodes.LONG_INT.value, int.to_bytes(obj, length, "big")
- )
+ return msgpack.ExtType(code, int.to_bytes(obj, length, "big"))
if isinstance(obj, types.GeneratorType):
return list(obj)
@@ -259,6 +264,8 @@
def ext_hook(code, data):
if code == MsgpackExtTypeCodes.LONG_INT.value:
return int.from_bytes(data, "big")
+ elif code == MsgpackExtTypeCodes.LONG_NEG_INT.value:
+ return -int.from_bytes(data, "big")
raise ValueError("Unknown msgpack extended code %s" % code)
def decode_types(obj):
diff --git a/swh/core/api/tests/test_serializers.py b/swh/core/api/tests/test_serializers.py
--- a/swh/core/api/tests/test_serializers.py
+++ b/swh/core/api/tests/test_serializers.py
@@ -171,6 +171,7 @@
**DATA,
"none_dict_key": {None: 42},
"long_int_is_loooong": 10000000000000000000000000000000,
+ "long_negative_int_is_loooong": -10000000000000000000000000000000,
}
data = msgpack_dumps(expected_original_data)
actual_data = msgpack_loads(data)
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Thu, Jan 23, 2:33 AM (1 w, 2 d ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
3235039
Attached To
D4568: Add support for large negative integers in msgpack encoding
Event Timeline
Log In to Comment