Skip to content

Commit

Permalink
Support bytes (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
gaogaotiantian authored May 3, 2024
1 parent 17d551e commit 5ef347b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/coredumpy/py_object_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ def dump_object(cls, obj):
return {"type": "None"}
elif isinstance(obj, (int, float, str, bool)):
return {"type": type(obj).__name__, "value": obj}
elif isinstance(obj, bytes):
return {"type": type(obj).__name__, "value": obj.hex()}
elif isinstance(obj, (list, tuple, set)):
for item in obj:
cls._add_object(item)
Expand All @@ -87,6 +89,8 @@ def load_object(cls, id, data=None):
proxy = None
elif data["type"] in ("int", "float", "str", "bool"):
proxy = data["value"]
elif data["type"] == "bytes":
proxy = bytes.fromhex(data["value"])
elif data["type"] == "list":
proxy = [cls.load_object(item_id, cls._objects.get(item_id)) for item_id in data["value"]]
elif data["type"] == "tuple":
Expand Down
5 changes: 5 additions & 0 deletions tests/test_py_object_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ def test_bool(self):
proxy = self.convert_object(obj)
self.assertEqual(proxy, True)

def test_bytes(self):
obj = b"hello"
proxy = self.convert_object(obj)
self.assertEqual(proxy, b"hello")

def test_builtins(self):
obj = object()
proxy = self.convert_object(obj)
Expand Down

0 comments on commit 5ef347b

Please sign in to comment.