Skip to content

Commit

Permalink
fix named type read
Browse files Browse the repository at this point in the history
  • Loading branch information
chaokunyang committed Dec 24, 2024
1 parent d304d88 commit 2d1e402
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
10 changes: 9 additions & 1 deletion python/pyfury/_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ class ClassResolver:
"_dynamic_write_string_id",
"_dynamic_written_metastr",
"_ns_type_to_classinfo",
"_named_type_to_classinfo",
"namespace_encoder",
"namespace_decoder",
"typename_encoder",
Expand Down Expand Up @@ -173,6 +174,7 @@ def __init__(self, fury):
# hold python reference.
self._classes_info = dict()
self._ns_type_to_classinfo = dict()
self._named_type_to_classinfo = dict()
self.namespace_encoder = MetaStringEncoder(".", "_")
self.namespace_decoder = MetaStringDecoder(".", "_")
self.typename_encoder = MetaStringEncoder("$", "_")
Expand Down Expand Up @@ -344,7 +346,7 @@ def _register_type(
except BaseException:
serializer = serializer()
n_params = len({typename, type_id, None}) - 1
if n_params == 0:
if n_params == 0 and typename is None:
type_id = self._next_type_id()
if n_params == 2:
raise TypeError(
Expand Down Expand Up @@ -454,6 +456,7 @@ def __register_type(
classinfo = ClassInfo(
cls, type_id, serializer, ns_meta_bytes, type_meta_bytes, dynamic_type
)
self._named_type_to_classinfo[(namespace, typename)] = classinfo
self._ns_type_to_classinfo[(ns_meta_bytes, type_meta_bytes)] = classinfo
self._classes_info[cls] = classinfo
if type_id > 0 and (
Expand Down Expand Up @@ -574,6 +577,11 @@ def _load_metabytes_to_classinfo(self, ns_metabytes, type_metabytes):
return typeinfo
ns = ns_metabytes.decode(self.namespace_decoder)
typename = type_metabytes.decode(self.typename_decoder)
# the hash computed between languages may be different.
typeinfo = self._named_type_to_classinfo.get((ns, typename))
if typeinfo is not None:
self._ns_type_to_classinfo[(ns_metabytes, type_metabytes)] = typeinfo
return typeinfo
cls = load_class(ns + "#" + typename)
classinfo = self.get_classinfo(cls)
self._ns_type_to_classinfo[(ns_metabytes, type_metabytes)] = classinfo
Expand Down
9 changes: 4 additions & 5 deletions python/pyfury/tests/test_cross_language.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ def test_buffer(data_file_path):
assert buffer.read_int64() == 2**63 - 1
assert math.isclose(buffer.read_float(), -1.1, rel_tol=1e-03)
assert math.isclose(buffer.read_double(), -1.1, rel_tol=1e-03)
assert buffer.read_varint32() == 100
assert buffer.read_varuint32() == 100
binary = b"ab"
assert buffer.read_bytes(buffer.read_int32()) == binary
buffer.write_bool(True)
Expand All @@ -253,7 +253,7 @@ def test_buffer(data_file_path):
buffer.write_int64(2**63 - 1)
buffer.write_float(-1.1)
buffer.write_double(-1.1)
buffer.write_varint32(100)
buffer.write_varuint32(100)
buffer.write_int32(len(binary))
buffer.write_bytes(binary)
with open(data_file_path, "wb+") as f:
Expand Down Expand Up @@ -528,9 +528,8 @@ def test_register_serializer(data_file_path):
data_bytes = f.read()
buffer = pyfury.Buffer(data_bytes)
fury = pyfury.Fury(language=pyfury.Language.XLANG, ref_tracking=True)
fury.register_serializer(
ComplexObject1, ComplexObject1Serializer(fury, ComplexObject1)
)
fury.register_type(ComplexObject1, typename="test.ComplexObject1",
serializer=ComplexObject1Serializer(fury, ComplexObject1))
new_obj = fury.deserialize(buffer)
expected = ComplexObject1(*[None] * 12)
expected.f1, expected.f2, expected.f3 = True, "abc", ["abc", "abc"]
Expand Down

0 comments on commit 2d1e402

Please sign in to comment.