blob: fc8ac0f802bfdf2d2f3b7fb6138b85c3b0cf4394 [file]
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
import datetime
import os
import platform
import time
import cython
from libc.stdint cimport int32_t, int64_t, uint8_t, uint64_t
from libc.stdint cimport *
from libcpp cimport bool as c_bool
from libcpp.utility cimport pair
from libcpp.vector cimport vector
from cython.operator cimport dereference as deref
from cpython cimport PyObject
from cpython.object cimport PyTypeObject, PyObject_GetAttr, PyObject_SetAttr
from cpython.dict cimport PyDict_Next
from cpython.list cimport PyList_New, PyList_SET_ITEM
from cpython.tuple cimport PyTuple_New, PyTuple_SET_ITEM
from cpython.ref cimport Py_INCREF, Py_XDECREF
from cpython.bytes cimport PyBytes_AS_STRING, PyBytes_GET_SIZE
from libc.string cimport memcmp
from pyfory.includes.libflat_hash_map cimport flat_hash_map
from pyfory.includes.libutil cimport FlatIntMap
from pyfory._fory import (
NO_USER_TYPE_ID,
NOT_NULL_INT64_FLAG,
)
from pyfory.error import TypeUnregisteredError
from pyfory.meta.typedef_decoder import decode_typedef
from pyfory.meta.typedef import is_struct_typedef_kind
from pyfory.meta.metastring import MetaStringDecoder
from pyfory.policy import DEFAULT_POLICY
from pyfory.resolver import NULL_FLAG, NOT_NULL_VALUE_FLAG
from pyfory.type_util import normalize_fory_type
from pyfory.includes.libserialization cimport (
IdentityKind,
TypeId,
is_user_type,
is_typed_user_type,
is_unbound_user_type,
is_namespaced_type,
is_type_share_meta,
Fory_IsInternalTypeId,
Fory_CanUsePrimitiveCollectionFastpath,
Fory_PyPrimitiveCollectionWriteToBuffer,
Fory_PyPrimitiveCollectionReadFromBuffer,
Fory_PyWriteBasicFieldToBuffer,
Fory_PyReadBasicFieldFromBuffer,
)
cdef extern from *:
"""
#define int2obj(obj_addr) ((PyObject *)(obj_addr))
#define obj2int(obj_ref) (Py_INCREF(obj_ref), ((int64_t)(obj_ref)))
#define fory_sequence_get_items(collection) \
(PyList_CheckExact(collection) ? ((PyListObject *)(collection))->ob_item : \
(PyTuple_CheckExact(collection) ? ((PyTupleObject *)(collection))->ob_item : NULL))
"""
object int2obj(int64_t obj_addr)
int64_t obj2int(object obj_ref)
PyObject **fory_sequence_get_items(object collection)
dict _PyDict_NewPresized(Py_ssize_t minused)
Py_ssize_t Py_SIZE(object obj)
ENABLE_FORY_CYTHON_SERIALIZATION = os.environ.get(
"ENABLE_FORY_CYTHON_SERIALIZATION", "True"
).lower() in ("true", "1")
cdef int32_t NOT_NULL_BOOL_FLAG = (NOT_NULL_VALUE_FLAG & 0xFF) | (<int32_t>TypeId.BOOL << 8)
cdef int32_t NOT_NULL_STRING_FLAG = (NOT_NULL_VALUE_FLAG & 0xFF) | (<int32_t>TypeId.STRING << 8)
cdef int32_t NOT_NULL_FLOAT64_FLAG = (NOT_NULL_VALUE_FLAG & 0xFF) | (<int32_t>TypeId.FLOAT64 << 8)
_PRIMITIVE_TYPE_IDS = frozenset(range(1, 21)) - {16}
def _is_primitive_type(type_):
if type(type_) is int:
return type_ in _PRIMITIVE_TYPE_IDS
return type_ in (bool, int, float)
@cython.final
cdef class Config:
"""
Immutable runtime configuration shared by `Fory`, `TypeResolver`, and the
directional read/write contexts.
The Cython runtime treats this object as the single source of truth for
execution-mode and maximum-depth flags. Higher-level facades may expose
convenience accessors, but runtime code should read these values from the
config instance instead of mirroring them onto other owners.
Attributes:
xlang: Selects xlang wire format instead of Python native mode.
track_ref: Enables reference tracking for shared and circular object graphs.
strict: Requires type registration before serialization/deserialization.
compatible: Enables compatible mode and schema-evolution metadata paths.
meta_share: Enables shared type metadata on the resolver/type-info path.
scoped_meta_share_enabled: Enables per-operation meta-share state.
max_depth: Maximum allowed nesting depth during deserialization.
max_type_fields: Maximum accepted field count in one received struct TypeDef.
max_type_meta_bytes: Maximum accepted body size in one received TypeDef.
max_schema_versions_per_type: Maximum cached remote metadata versions for one logical type.
max_average_schema_versions_per_type: Average cached remote schema versions across cached remote types.
max_graph_memory_bytes: Approximate graph-memory gate per root deserialization.
Mainly covers materialized collections, maps, arrays, structs, and objects. Leaf values
are gated by unread input bytes instead, and actual process memory can be higher.
Defaults to 128 MiB and must be a positive byte limit.
max_unbacked_container_items: Maximum repeated collection/map reads not backed
by input bytes per root. Defaults to 8192 and must be non-negative.
field_nullable: Treats struct/dataclass fields as nullable by default.
policy: Deserialization policy used for security-sensitive checks.
meta_compressor: Optional typedef/meta compressor implementation.
"""
cdef public bint xlang
cdef public bint track_ref
cdef public bint strict
cdef public bint compatible
cdef public bint meta_share
cdef public bint scoped_meta_share_enabled
cdef public int32_t max_depth
cdef public int32_t max_type_fields
cdef public int32_t max_type_meta_bytes
cdef public int32_t max_schema_versions_per_type
cdef public int32_t max_average_schema_versions_per_type
cdef public int64_t max_graph_memory_bytes
cdef public int64_t max_unbacked_container_items
cdef public bint field_nullable
cdef public object policy
cdef public object meta_compressor
def __init__(
self,
*,
xlang,
track_ref,
strict,
compatible,
meta_share,
scoped_meta_share_enabled,
max_depth,
max_type_fields,
max_type_meta_bytes,
max_schema_versions_per_type,
max_average_schema_versions_per_type,
max_graph_memory_bytes,
max_unbacked_container_items,
field_nullable,
policy,
meta_compressor,
):
"""
Build a runtime config object for one Python or Cython Fory instance.
Args:
xlang: Select xlang wire format.
track_ref: Enable reference tracking for object graphs.
strict: Require registered types on dynamic resolution paths.
compatible: Enable compatible mode and meta-share flows.
meta_share: Enable shared type metadata on resolver/type-info paths.
scoped_meta_share_enabled: Enable per-operation meta-share state.
max_depth: Maximum allowed read depth before failing deserialization.
max_type_fields: Maximum accepted field count in one received struct TypeDef.
max_type_meta_bytes: Maximum accepted body size in one received TypeDef.
max_schema_versions_per_type: Maximum cached remote metadata versions for one logical type.
max_average_schema_versions_per_type: Average cached remote schema versions across cached remote types.
max_graph_memory_bytes: Approximate graph-memory gate per root deserialization.
Mainly covers materialized collections, maps, arrays, structs, and objects. Leaf
values are gated by unread input bytes instead, and actual process memory can be
higher. Defaults to 128 MiB and must be a positive byte limit.
max_unbacked_container_items: Maximum repeated collection/map reads not backed
by input bytes per root. Defaults to 8192 and must be non-negative.
field_nullable: Treat all struct fields as nullable by default.
policy: Deserialization policy implementation.
meta_compressor: Optional typedef/meta compressor.
"""
self.xlang = xlang
self.track_ref = track_ref
self.strict = strict
self.compatible = compatible
self.meta_share = meta_share
self.scoped_meta_share_enabled = scoped_meta_share_enabled
self.max_depth = max_depth
if max_type_fields <= 0:
raise ValueError("max_type_fields must be a positive integer")
if max_type_meta_bytes <= 0:
raise ValueError("max_type_meta_bytes must be a positive integer")
if max_schema_versions_per_type <= 0:
raise ValueError("max_schema_versions_per_type must be a positive integer")
if max_average_schema_versions_per_type <= 0:
raise ValueError("max_average_schema_versions_per_type must be a positive integer")
if (
not isinstance(max_graph_memory_bytes, int)
or max_graph_memory_bytes <= 0
or max_graph_memory_bytes > 9223372036854775807
):
raise ValueError("max_graph_memory_bytes must be in range [1, 9223372036854775807]")
if (
not isinstance(max_unbacked_container_items, int)
or max_unbacked_container_items < 0
or max_unbacked_container_items > 9223372036854775807
):
raise ValueError("max_unbacked_container_items must be in range [0, 9223372036854775807]")
self.max_type_fields = max_type_fields
self.max_type_meta_bytes = max_type_meta_bytes
self.max_schema_versions_per_type = max_schema_versions_per_type
self.max_average_schema_versions_per_type = max_average_schema_versions_per_type
self.max_graph_memory_bytes = max_graph_memory_bytes
self.max_unbacked_container_items = max_unbacked_container_items
self.field_nullable = field_nullable
self.policy = policy
self.meta_compressor = meta_compressor
cdef inline bint _is_struct_type_id(uint8_t type_id):
return (
type_id == <uint8_t>TypeId.STRUCT
or type_id == <uint8_t>TypeId.TYPED_STRUCT
or type_id == <uint8_t>TypeId.NAMED_STRUCT
or type_id == <uint8_t>TypeId.COMPATIBLE_STRUCT
)
cdef class WriteContext
cdef class ReadContext
@cython.final
cdef class TypeResolver:
"""
Cython accelerator for type-info lookup and wire-level type metadata IO.
The source of truth for registration and non-hotpath bookkeeping remains the
Python `pyfory.registry.TypeResolver`. This Cython companion caches the hot
lookup tables needed by serialization and deserialization so active runtime
paths can avoid Python-level dispatch where the layout is stable.
"""
cdef object resolver
cdef readonly Config config
cdef readonly object shared_registry
cdef readonly bint xlang
cdef readonly bint track_ref
cdef readonly bint strict
cdef readonly bint compatible
cdef readonly bint field_nullable
cdef readonly object policy
cdef readonly bint meta_share
cdef public bint _frozen
cdef object _unknown_struct_cls
cdef readonly dict _types_info
cdef readonly dict _type_id_to_type_info
cdef readonly dict _user_type_id_to_type_info
cdef readonly dict _ns_type_to_type_info
cdef readonly dict _local_type_info_by_hash
cdef readonly dict _meta_shared_type_info
cdef readonly dict _checked_unbound_type_defs
cdef vector[PyObject *] _c_registered_id_to_type_info
cdef flat_hash_map[uint32_t, PyObject *] _c_user_type_id_to_type_info
cdef flat_hash_map[uint64_t, PyObject *] _c_types_info
cdef flat_hash_map[pair[int64_t, int64_t], PyObject *] _c_meta_hash_to_type_info
def __init__(self, Config config, *, shared_registry):
"""
Build the Cython resolver and its hot caches.
Args:
config: Runtime configuration shared by the owning `Fory`.
shared_registry: Shared encoded meta-string registry.
"""
from pyfory.registry import TypeResolver as PyTypeResolver
resolver = PyTypeResolver(
config,
shared_registry=shared_registry,
)
self.resolver = resolver
self.config = config
self.shared_registry = resolver.shared_registry
self.xlang = resolver.xlang
self.track_ref = resolver.track_ref
self.strict = resolver.strict
self.compatible = resolver.compatible
self.field_nullable = resolver.field_nullable
self.policy = resolver.policy
self.meta_share = resolver.meta_share
self._frozen = False
self._unknown_struct_cls = None
self._types_info = resolver._types_info
self._type_id_to_type_info = resolver._type_id_to_type_info
self._user_type_id_to_type_info = resolver._user_type_id_to_type_info
self._ns_type_to_type_info = resolver._ns_type_to_type_info
self._local_type_info_by_hash = resolver._local_type_info_by_hash
self._meta_shared_type_info = resolver._meta_shared_type_info
self._checked_unbound_type_defs = resolver._checked_unbound_type_defs
for typeinfo in resolver._types_info.values():
self._populate_type_info(typeinfo)
def initialize(self):
cdef object typeinfo
from pyfory.struct import UnknownStruct
self._unknown_struct_cls = UnknownStruct
self.resolver._set_actual_resolver(self)
self.resolver.initialize()
for typeinfo in self.resolver._types_info.values():
self._populate_type_info(typeinfo)
cpdef inline void freeze(self):
self._frozen = True
self.resolver._frozen = True
def register_type(
self,
cls,
*,
type_id: int = None,
name: str = None,
serializer=None,
):
cdef TypeInfo typeinfo = self.resolver.register_type(
cls,
type_id=type_id,
name=name,
serializer=serializer,
)
self._populate_type_info(typeinfo)
return typeinfo
def register_union(
self,
cls,
*,
type_id: int = None,
name: str = None,
serializer=None,
):
cdef TypeInfo typeinfo = self.resolver.register_union(
cls,
type_id=type_id,
name=name,
serializer=serializer,
)
self._populate_type_info(typeinfo)
return typeinfo
def register_serializer(self, cls, serializer):
cdef TypeInfo typeinfo
cdef uint8_t previous_type_id
cdef uint32_t previous_user_type_id
cdef uint8_t previous_identity_kind
self.resolver._check_not_frozen()
typeinfo = self.resolver.get_type_info(cls, create=False)
if typeinfo is None:
self.resolver.register_serializer(cls, serializer)
return
previous_type_id = typeinfo.type_id
previous_user_type_id = typeinfo.user_type_id
previous_identity_kind = typeinfo.identity_kind
self.resolver.register_serializer(cls, serializer)
typeinfo = self.resolver.get_type_info(cls)
if (
previous_type_id != typeinfo.type_id
or previous_user_type_id != typeinfo.user_type_id
or previous_identity_kind != typeinfo.identity_kind
):
if (
is_typed_user_type(<TypeId>previous_type_id)
or (
previous_type_id == <uint8_t>TypeId.COMPATIBLE_STRUCT
and previous_identity_kind == <uint8_t>IdentityKind.BY_ID
)
):
if previous_user_type_id != <uint32_t>NO_USER_TYPE_ID:
self._c_user_type_id_to_type_info[previous_user_type_id] = NULL
elif previous_type_id > 0 and previous_type_id < self._c_registered_id_to_type_info.size():
if self._c_registered_id_to_type_info[previous_type_id] == <PyObject*>typeinfo:
self._c_registered_id_to_type_info[previous_type_id] = NULL
self._populate_type_info(typeinfo)
cpdef inline TypeInfo get_type_info(self, cls, create=True):
cdef pair[uint64_t, PyObject *] *entry = self._c_types_info.find(<uintptr_t> <PyObject *> cls)
cdef PyObject * typeinfo_ptr = NULL
cdef TypeInfo typeinfo
if entry != NULL:
typeinfo_ptr = deref(entry).second
if typeinfo_ptr != NULL:
typeinfo = <TypeInfo> typeinfo_ptr
if typeinfo.serializer is None:
typeinfo = self.resolver.get_type_info(cls, create=create)
self._populate_type_info(typeinfo)
return typeinfo
if not create:
return None
typeinfo = self.resolver.get_type_info(cls, create=create)
self._populate_type_info(typeinfo)
return typeinfo
cpdef TypeInfo resolve_target_type_info(self, object target_type):
cdef TypeInfo typeinfo = self.resolver.resolve_target_type_info(target_type)
if typeinfo is not None and typeinfo._target_key is typeinfo.cls:
self._populate_type_info(typeinfo)
return typeinfo
cpdef TypeInfo resolve_field_target_type_info(self, object target_type):
cdef TypeInfo typeinfo = self.resolver.resolve_field_target_type_info(target_type)
if typeinfo is not None and typeinfo._target_key is typeinfo.cls:
self._populate_type_info(typeinfo)
return typeinfo
cpdef inline Serializer get_serializer(self, cls):
cdef TypeInfo typeinfo = self.get_type_info(cls)
return None if typeinfo is None else typeinfo.serializer
def get_type_info_by_id(self, type_id, user_type_id=NO_USER_TYPE_ID):
return self.resolver.get_type_info_by_id(type_id, user_type_id=user_type_id)
def get_type_info_by_name(self, namespace, typename):
return self.resolver.get_type_info_by_name(namespace, typename)
def _get_type_by_python_name(self, module, qualname):
return self.resolver._get_type_by_python_name(module, qualname)
def get_registered_name(self, cls):
return self.resolver.get_registered_name(cls)
def get_registered_type_ids(self, cls):
return self.resolver.get_registered_type_ids(cls)
def is_registered_by_name(self, cls):
return self.resolver.is_registered_by_name(cls)
cpdef inline write_type_info(self, WriteContext write_context, TypeInfo typeinfo):
cdef uint8_t type_id
if typeinfo.dynamic_type:
return
type_id = typeinfo.type_id
write_context.write_uint8(type_id)
if type_id == <uint8_t>TypeId.COMPATIBLE_STRUCT:
self.write_shared_type_meta(write_context, typeinfo)
return
if Fory_IsInternalTypeId(type_id):
return
if is_typed_user_type(<TypeId>type_id):
if typeinfo.user_type_id == <uint32_t>NO_USER_TYPE_ID:
raise TypeError(f"user_type_id required for type_id {type_id}")
write_context.write_var_uint32(typeinfo.user_type_id)
return
if is_namespaced_type(<TypeId>type_id):
if self.meta_share:
self.write_shared_type_meta(write_context, typeinfo)
else:
write_context.meta_string_writer.write_encoded_meta_string(
write_context.buffer, typeinfo.namespace_bytes
)
write_context.meta_string_writer.write_encoded_meta_string(
write_context.buffer, typeinfo.typename_bytes
)
cpdef inline TypeInfo read_type_info(
self, ReadContext read_context, TypeInfo expected_typeinfo=None
):
cdef Buffer buffer = read_context.buffer
cdef uint8_t type_id = buffer.read_uint8()
cdef uint32_t user_type_id
cdef object ns_metabytes
cdef object type_metabytes
cdef PyObject * typeinfo_ptr = NULL
cdef TypeInfo typeinfo
cdef pair[uint32_t, PyObject *] *entry
if type_id == <uint8_t>TypeId.COMPATIBLE_STRUCT:
return self.read_shared_type_meta(
read_context,
type_id=type_id,
expected_typeinfo=expected_typeinfo,
)
if Fory_IsInternalTypeId(type_id):
# Hot type-id reads must stay on the C caches.
if expected_typeinfo is not None:
if expected_typeinfo.type_id == type_id and not expected_typeinfo.is_registered:
return expected_typeinfo
if (
is_user_type(<TypeId>expected_typeinfo.type_id)
and expected_typeinfo.cls is not self._unknown_struct_cls
):
raise TypeError(
f"Expected target {expected_typeinfo.cls}, got wire type_id {type_id}"
)
if type_id >= self._c_registered_id_to_type_info.size():
raise ValueError(f"Unexpected type_id {type_id}")
typeinfo_ptr = self._c_registered_id_to_type_info[type_id]
if typeinfo_ptr == NULL:
raise ValueError(f"Unexpected type_id {type_id}")
return <TypeInfo>typeinfo_ptr
if is_namespaced_type(<TypeId>type_id):
if self.meta_share:
return self.read_shared_type_meta(
read_context,
type_id=type_id,
expected_typeinfo=expected_typeinfo,
)
ns_metabytes = read_context.meta_string_reader.read_encoded_meta_string(
buffer
)
type_metabytes = read_context.meta_string_reader.read_encoded_meta_string(
buffer
)
if expected_typeinfo is None:
return self._load_bytes_to_type_info(ns_metabytes, type_metabytes)
return self.resolver._resolve_named_non_meta_share_type_info(
type_id, ns_metabytes, type_metabytes, expected_typeinfo
)
if is_typed_user_type(<TypeId>type_id):
user_type_id = buffer.read_var_uint32()
entry = self._c_user_type_id_to_type_info.find(user_type_id)
if entry != NULL and deref(entry).second != NULL:
typeinfo = <TypeInfo>deref(entry).second
if expected_typeinfo is None or typeinfo.cls is expected_typeinfo.cls:
if typeinfo.serializer is None:
self.resolver._set_type_info(typeinfo)
return typeinfo
return self.resolver._resolve_typed_user_type_info(
type_id, user_type_id, expected_typeinfo
)
if is_unbound_user_type(<TypeId>type_id):
if (
expected_typeinfo is not None
and expected_typeinfo.type_id == type_id
and expected_typeinfo.cls is not self._unknown_struct_cls
and expected_typeinfo.serializer is not None
):
return expected_typeinfo
return self.resolver._resolve_unbound_non_evolving_type_info(
type_id, expected_typeinfo
)
raise ValueError(f"Unexpected type_id {type_id}")
def is_registered_by_id(self, cls=None, type_id=None, user_type_id=NO_USER_TYPE_ID):
return self.resolver.is_registered_by_id(
cls=cls,
type_id=type_id,
user_type_id=user_type_id,
)
cdef _populate_type_info(self, TypeInfo typeinfo):
cdef uint8_t type_id = typeinfo.type_id
cdef object canonical_typeinfo
if typeinfo.is_registered:
if (
is_typed_user_type(<TypeId>type_id)
or (
type_id == <uint8_t>TypeId.COMPATIBLE_STRUCT
and typeinfo.identity_kind == <uint8_t>IdentityKind.BY_ID
)
):
if typeinfo.user_type_id != NO_USER_TYPE_ID:
canonical_typeinfo = self._user_type_id_to_type_info.get(typeinfo.user_type_id)
if canonical_typeinfo is None or canonical_typeinfo is typeinfo:
self._c_user_type_id_to_type_info[typeinfo.user_type_id] = <PyObject *> typeinfo
elif type_id > 0 and not is_user_type(<TypeId>type_id):
if type_id >= self._c_registered_id_to_type_info.size():
self._c_registered_id_to_type_info.resize(type_id * 2, NULL)
canonical_typeinfo = self._type_id_to_type_info.get(type_id)
if canonical_typeinfo is None or canonical_typeinfo is typeinfo:
self._c_registered_id_to_type_info[type_id] = <PyObject *> typeinfo
if typeinfo.typename_bytes is not None:
self._c_meta_hash_to_type_info[
pair[int64_t, int64_t](
typeinfo.namespace_bytes.hashcode,
typeinfo.typename_bytes.hashcode,
)
] = <PyObject *>typeinfo
if typeinfo._target_key is typeinfo.cls:
self._c_types_info[<uintptr_t> <PyObject *> typeinfo.cls] = <PyObject *> typeinfo
if self._c_types_info.size() * 10 >= self._c_types_info.bucket_count() * 5:
self._c_types_info.rehash(self._c_types_info.size() * 2)
cpdef inline write_shared_type_meta(self, WriteContext write_context, TypeInfo typeinfo):
cdef MetaShareWriteContext meta_context = write_context.meta_share_context
cdef object type_key
cdef uint8_t type_id
cdef object type_def
cdef uint64_t type_addr
cdef pair[uint64_t, int32_t] *entry
cdef int32_t index
if meta_context is None:
raise AssertionError(
"Meta share write context must be set when compatible mode is enabled"
)
type_key = typeinfo._target_key
type_id = typeinfo.type_id
if not is_type_share_meta(<TypeId>type_id):
write_context.write_var_uint32(0)
write_context.write_bytes(typeinfo.type_def.encoded)
return
type_addr = <uint64_t> <PyObject *> type_key
entry = meta_context.class_map.find(type_addr)
if entry != NULL:
write_context.write_var_uint32((deref(entry).second << 1) | 1)
return
index = meta_context.class_map.size()
meta_context.class_map[type_addr] = index
write_context.write_var_uint32(index << 1)
type_def = typeinfo.type_def
if type_def is None:
self.resolver._set_type_info(typeinfo)
type_def = typeinfo.type_def
write_context.write_bytes(type_def.encoded)
cpdef _write_unknown_struct_type_info(
self, WriteContext write_context, object type_def
):
cdef MetaShareWriteContext meta_context
cdef object encoded
cdef uint8_t type_id
cdef uint64_t type_addr
cdef pair[uint64_t, int32_t] *entry
cdef int32_t index
if type_def.type_id != <uint8_t>TypeId.COMPATIBLE_STRUCT:
raise TypeError("UnknownStruct requires a shared Struct TypeDef")
encoded = type_def.encoded
if type(encoded) is not bytes or len(encoded) < 8:
raise TypeError("UnknownStruct requires an encoded TypeDef")
meta_context = write_context.meta_share_context
if meta_context is None:
raise TypeError("UnknownStruct requires compatible metadata sharing")
type_id = type_def.type_id
type_addr = <uint64_t><PyObject *>type_def
write_context.write_uint8(type_id)
entry = meta_context.class_map.find(type_addr)
if entry != NULL:
write_context.write_var_uint32((deref(entry).second << 1) | 1)
return
index = meta_context.class_map.size()
meta_context.class_map[type_addr] = index
write_context.write_var_uint32(index << 1)
write_context.write_bytes(encoded)
cpdef inline TypeInfo read_shared_type_meta(
self,
ReadContext read_context,
type_id=None,
TypeInfo expected_typeinfo=None,
):
cdef MetaShareReadContext meta_context = read_context.meta_share_context
cdef uint32_t index_marker
cdef uint32_t index
cdef TypeInfo typeinfo
if meta_context is None:
raise AssertionError(
"Meta share read context must be set when compatible mode is enabled"
)
if type_id is None:
type_id = read_context.read_uint8()
index_marker = read_context.read_var_uint32()
index = index_marker >> 1
if index_marker & 1:
typeinfo = <TypeInfo>meta_context.read_type_infos[index]
if expected_typeinfo is None:
if typeinfo.identity_kind == <uint8_t>IdentityKind.UNBOUND:
raise TypeUnregisteredError(
"Unregistered UNBOUND type cannot be deserialized without a target type"
)
if (
not typeinfo.is_registered
and typeinfo.type_def is not None
and typeinfo.cls is not self._unknown_struct_cls
):
return self.resolver._resolve_non_unbound_typedef_for_target(
typeinfo.type_def, None
)
return typeinfo
if typeinfo._target_key is expected_typeinfo._target_key:
return typeinfo
return self.resolver._resolve_meta_backref_slow(typeinfo, expected_typeinfo)
typeinfo = self._read_and_build_type_info(
read_context.buffer, expected_typeinfo
)
meta_context.read_type_infos.append(typeinfo)
return typeinfo
cdef inline TypeInfo _read_and_build_type_info(
self, Buffer buffer, TypeInfo expected_typeinfo=None
):
cdef int64_t header = buffer.read_int64()
cdef uint64_t hash_key = _typedef_hash_key(header)
cdef TypeInfo typeinfo
cdef object unbound_td
if expected_typeinfo is not None:
if expected_typeinfo.local_header_hash == hash_key and hash_key != 0:
# L0 local target hit: single integer compare skips body with zero lookup/parse/quota.
_skip_typedef_fast(buffer, header)
return expected_typeinfo
typeinfo = expected_typeinfo.get_adapted_type_info(hash_key)
if typeinfo is not None:
# L2 per-target adapted cache hit.
_skip_typedef_fast(buffer, header)
return typeinfo
typeinfo = self._local_type_info_by_hash.get(hash_key)
if typeinfo is not None:
_skip_typedef_fast(buffer, header)
if expected_typeinfo is None or typeinfo._target_key is expected_typeinfo._target_key:
return typeinfo
return self.resolver._resolve_existing_type_info_for_target(
typeinfo, expected_typeinfo
)
typeinfo = self._meta_shared_type_info.get(hash_key)
if typeinfo is not None:
# L1 checked remote cache hit for BY_ID/BY_NAME. Top 52 bits are protocol identity.
_skip_typedef_fast(buffer, header)
if expected_typeinfo is None or typeinfo._target_key is expected_typeinfo._target_key:
return typeinfo
return self.resolver._resolve_existing_type_info_for_target(
typeinfo, expected_typeinfo
)
unbound_td = self._checked_unbound_type_defs.get(hash_key)
if unbound_td is not None:
_skip_typedef_fast(buffer, header)
return self.resolver._resolve_unbound_typedef_for_target(
unbound_td, expected_typeinfo
)
return self.resolver._read_uncached_type_info(buffer, header, expected_typeinfo)
cdef inline TypeInfo _load_bytes_to_type_info(
self, object ns_metabytes, object type_metabytes
):
cdef pair[int64_t, int64_t] hash_key = pair[int64_t, int64_t](
ns_metabytes.hashcode,
type_metabytes.hashcode,
)
cdef pair[pair[int64_t, int64_t], PyObject *] *entry = (
self._c_meta_hash_to_type_info.find(hash_key)
)
cdef TypeInfo typeinfo
# Slow resolution may populate and rehash this map, invalidating entry.
cdef bint cache_slot_empty = (
entry == NULL or deref(entry).second == NULL
)
if entry != NULL and deref(entry).second != NULL:
typeinfo = <TypeInfo>deref(entry).second
if (
_encoded_meta_string_matches(
ns_metabytes,
typeinfo.namespace_bytes,
)
and _encoded_meta_string_matches(
type_metabytes,
typeinfo.typename_bytes,
)
):
return typeinfo
typeinfo = self.resolver._load_metabytes_to_type_info(ns_metabytes, type_metabytes)
if (
cache_slot_empty
# The Python resolver owns the bounded accepted-alias set. The
# compiled mirror must not retain aliases that owner declined.
and self._ns_type_to_type_info.get(
(ns_metabytes, type_metabytes)
) is typeinfo
and _encoded_meta_string_matches(
ns_metabytes,
typeinfo.namespace_bytes,
)
and _encoded_meta_string_matches(
type_metabytes,
typeinfo.typename_bytes,
)
):
self._c_meta_hash_to_type_info[hash_key] = <PyObject *>typeinfo
return typeinfo
cdef inline bint _encoded_meta_string_matches(object left, object right):
if left is right:
return True
if left is None or right is None:
return False
return (
left.hashcode == right.hashcode
and left.encoding == right.encoding
and left.length == right.length
and left.data == right.data
)
cdef inline uint64_t _typedef_hash_key(int64_t header):
# Cast before shifting so signed headers keep their protocol-defined top 52 bits.
return (<uint64_t>header) >> 12
cdef inline void _skip_typedef_fast(Buffer buffer, int64_t header):
cdef uint32_t meta_size = <uint32_t>(header & 0xFF)
cdef uint32_t extended_size
if meta_size == 0xFF:
extended_size = buffer.read_var_uint32()
if extended_size > <uint32_t>(UINT32_MAX - meta_size):
raise ValueError("Invalid TypeDef metadata size")
meta_size += extended_size
# Cache-hit skip must not materialize the opaque body. In stream mode,
# read_bytes would allocate after filling, while skip proves/consumes bytes
# without retaining attacker-controlled metadata.
buffer.skip(meta_size)
namespace_decoder = MetaStringDecoder(".", "_")
typename_decoder = MetaStringDecoder("$", "_")
include "buffer.pxi"
cdef inline object _wrap_buffer(shared_ptr[CBuffer] c_buffer):
return Buffer.wrap(c_buffer)
cdef class Serializer:
"""
Base serializer contract for the active Cython runtime.
Concrete Cython serializers and Python serializers used in Cython mode both
implement this API: `write(write_context, value)` and `read(read_context)`.
Serializers must remain stateless with respect to one serialization call and
must not retain `Fory`, `WriteContext`, or `ReadContext`.
"""
cdef readonly TypeResolver type_resolver
cdef readonly object type_
cdef public bint need_to_write_ref
cdef public bint read_data_always_advances
def __init__(self, TypeResolver type_resolver, type_):
"""
Initialize a serializer for one declared Python type.
Args:
type_resolver: Active Cython resolver for type lookup and configuration.
type_: Declared Python type handled by this serializer.
"""
self.type_resolver = type_resolver
type_ = normalize_fory_type(type_)
self.type_ = type_
self.need_to_write_ref = self.type_resolver.track_ref and not _is_primitive_type(type_)
self.read_data_always_advances = False
cpdef write(self, WriteContext write_context, value):
raise NotImplementedError(f"write method not implemented in {type(self)}")
cpdef read(self, ReadContext read_context):
raise NotImplementedError(f"read method not implemented in {type(self)}")
@classmethod
def support_subclass(cls) -> bool:
return False
cdef class _PrimitiveSerializer(Serializer):
def __init__(self, type_resolver, type_):
super().__init__(type_resolver, type_)
self.read_data_always_advances = True
@cython.final
cdef class EnumSerializer(Serializer):
cdef tuple _members
cdef dict _wire_value_by_member
cdef dict _member_by_wire_value
def __init__(self, TypeResolver type_resolver, type_):
cdef dict explicit_wire_values
cdef bint use_explicit_ids
cdef object member
cdef object raw_value
cdef uint32_t wire_value
super().__init__(type_resolver, type_)
self.need_to_write_ref = False
self.read_data_always_advances = True
self._members = tuple(type_)
self._wire_value_by_member = {member: idx for idx, member in enumerate(self._members)}
self._member_by_wire_value = {idx: member for idx, member in enumerate(self._members)}
if type_resolver.xlang:
explicit_wire_values = {}
use_explicit_ids = True
for member in self._members:
raw_value = member.value
if isinstance(raw_value, bool) or not isinstance(raw_value, int) or raw_value < 0:
use_explicit_ids = False
break
wire_value = raw_value
if wire_value in explicit_wire_values:
use_explicit_ids = False
break
explicit_wire_values[wire_value] = member
if use_explicit_ids:
self._wire_value_by_member = {
member: int(member.value) for member in self._members
}
self._member_by_wire_value = explicit_wire_values
@classmethod
def support_subclass(cls) -> bool:
return True
cpdef inline write(self, WriteContext write_context, value):
write_context.write_var_uint32(self._wire_value_by_member[value])
cpdef inline read(self, ReadContext read_context):
cdef uint32_t wire_value = read_context.read_var_uint32()
cdef object value = self._member_by_wire_value.get(wire_value)
if value is None:
raise ValueError(
f"Unknown enum value {wire_value} for {self.type_.__qualname__}"
)
return value
@cython.final
cdef class SliceSerializer(Serializer):
def __init__(self, type_resolver, type_):
super().__init__(type_resolver, type_)
self.read_data_always_advances = True
cpdef inline write(self, WriteContext write_context, v):
cdef slice value = v
start, stop, step = value.start, value.stop, value.step
if type(start) is int:
write_context.write_int16(NOT_NULL_INT64_FLAG)
write_context.write_varint64(start)
else:
if start is None:
write_context.write_int8(NULL_FLAG)
else:
write_context.write_int8(NOT_NULL_VALUE_FLAG)
write_context.write_no_ref(start)
if type(stop) is int:
write_context.write_int16(NOT_NULL_INT64_FLAG)
write_context.write_varint64(stop)
else:
if stop is None:
write_context.write_int8(NULL_FLAG)
else:
write_context.write_int8(NOT_NULL_VALUE_FLAG)
write_context.write_no_ref(stop)
if type(step) is int:
write_context.write_int16(NOT_NULL_INT64_FLAG)
write_context.write_varint64(step)
else:
if step is None:
write_context.write_int8(NULL_FLAG)
else:
write_context.write_int8(NOT_NULL_VALUE_FLAG)
write_context.write_no_ref(step)
cpdef inline read(self, ReadContext read_context):
cdef object start
cdef object stop
cdef object step
if read_context.read_int8() == NULL_FLAG:
start = None
else:
start = read_context.read_no_ref()
if read_context.read_int8() == NULL_FLAG:
stop = None
else:
stop = read_context.read_no_ref()
if read_context.read_int8() == NULL_FLAG:
step = None
else:
step = read_context.read_no_ref()
return slice(start, stop, step)
@cython.final
cdef class TypeInfo:
cdef public object cls
cdef public uint8_t type_id
cdef public uint32_t user_type_id
cdef public Serializer serializer
cdef public object namespace_bytes
cdef public object typename_bytes
cdef public bint dynamic_type
cdef public object type_def
cdef public uint8_t identity_kind
cdef public bint is_registered
cdef public uint64_t local_header_hash
cdef public object _target_key
cdef public object _type_hints_override
cdef uint64_t _last_adapted_hash
cdef TypeInfo _last_adapted_info
cdef vector[uint64_t] _adapted_hashes
cdef list _adapted_infos
def __init__(
self,
cls=None,
type_id: int = 0,
user_type_id: int = 0xFFFFFFFF,
serializer=None,
namespace_bytes=None,
typename_bytes=None,
dynamic_type: bool = False,
type_def: object = None,
identity_kind: int = 1,
is_registered: bool = True,
local_header_hash: int = 0,
target_key=None,
type_hints_override=None,
):
self.cls = cls
self.type_id = <uint8_t>(0 if type_id is None or type_id < 0 else type_id)
self.user_type_id = <uint32_t>user_type_id
self.serializer = serializer
self.namespace_bytes = namespace_bytes
self.typename_bytes = typename_bytes
self.dynamic_type = dynamic_type
self.type_def = type_def
self.identity_kind = <uint8_t>identity_kind
self.is_registered = is_registered
if local_header_hash:
self.local_header_hash = <uint64_t>local_header_hash
elif type_def is not None:
self.local_header_hash = <uint64_t>type_def.header_hash
else:
self.local_header_hash = 0
self._target_key = cls if target_key is None else target_key
self._type_hints_override = type_hints_override
self._last_adapted_hash = 0
self._last_adapted_info = None
self._adapted_infos = None
cpdef inline TypeInfo get_adapted_type_info(self, uint64_t hash_key):
cdef size_t i
cdef size_t n
cdef TypeInfo info
if hash_key != 0 and self._last_adapted_hash == hash_key:
return self._last_adapted_info
n = self._adapted_hashes.size()
if n > 0:
for i in range(n):
if self._adapted_hashes[i] == hash_key:
info = <TypeInfo>self._adapted_infos[i]
self._last_adapted_hash = hash_key
self._last_adapted_info = info
return info
return None
cpdef cache_adapted_type_info(self, uint64_t hash_key, TypeInfo info, int32_t max_versions=0):
cdef size_t i
cdef size_t n
if hash_key == 0:
return
if self._adapted_infos is None:
self._adapted_infos = [info]
self._adapted_hashes.push_back(hash_key)
self._last_adapted_hash = hash_key
self._last_adapted_info = info
return
n = self._adapted_hashes.size()
for i in range(n):
if self._adapted_hashes[i] == hash_key:
self._adapted_infos[i] = info
self._last_adapted_hash = hash_key
self._last_adapted_info = info
return
if max_versions > 0 and n >= <size_t>max_versions:
return
self._adapted_hashes.push_back(hash_key)
self._adapted_infos.append(info)
self._last_adapted_hash = hash_key
self._last_adapted_info = info
def __repr__(self):
return (
f"TypeInfo(cls={self.cls}, type_id={self.type_id}, "
f"user_type_id={self.user_type_id}, serializer={self.serializer})"
)
cpdef str decode_namespace(self):
if self.namespace_bytes is None:
return ""
return self.namespace_bytes.decode(namespace_decoder)
cpdef str decode_typename(self):
if self.typename_bytes is None:
return ""
return self.typename_bytes.decode(typename_decoder)
include "context.pxi"
@cython.final
cdef class Fory:
"""
High-performance serialization facade for the active Cython runtime.
`Fory` owns the immutable runtime config, the Python registration resolver,
the Cython resolver cache, and one reusable read/write context pair. It is
the root entry point for top-level serialize/deserialize operations; nested
runtime state lives on `WriteContext` and `ReadContext`.
"""
cdef public bint xlang
cdef public bint track_ref
cdef public bint strict
cdef public bint compatible
cdef public bint field_nullable
cdef public int32_t max_depth
cdef public int64_t max_graph_memory_bytes
cdef public int64_t max_unbacked_container_items
cdef public object policy
cdef public Config config
cdef public TypeResolver type_resolver
cdef public WriteContext write_context
cdef public ReadContext read_context
cdef public Buffer buffer
def __init__(
self,
xlang=True,
ref=False,
strict=True,
compatible=None,
max_depth=50,
max_type_fields=512,
max_type_meta_bytes=4096,
max_schema_versions_per_type=10,
max_average_schema_versions_per_type=3,
max_graph_memory_bytes=128 * 1024 * 1024,
max_unbacked_container_items=8192,
policy=None,
field_nullable=False,
meta_compressor=None,
):
"""
Initialize a Cython-backed Fory runtime instance.
Args:
xlang: Select xlang wire format.
ref: Enable reference tracking for shared and circular references.
strict: Require registered types on dynamic resolution paths.
compatible: Enable compatible mode and meta-share type exchange. Defaults to
compatible mode.
max_depth: Maximum allowed read depth before rejecting payloads.
max_type_fields: Maximum accepted field count in one received struct TypeDef.
max_type_meta_bytes: Maximum accepted body size in one received TypeDef.
max_schema_versions_per_type: Maximum cached remote metadata versions for one logical type.
max_average_schema_versions_per_type: Average cached remote schema versions across cached remote types.
max_graph_memory_bytes: Approximate graph-memory gate per root deserialization.
Mainly covers materialized collections, maps, arrays, structs, and objects. Leaf
values are gated by unread input bytes instead, and actual process memory can be
higher. Defaults to 128 MiB and must be a positive byte limit.
max_unbacked_container_items: Maximum repeated collection/map reads not backed
by input bytes per root. Defaults to 8192 and must be non-negative.
policy: Optional deserialization policy implementation.
field_nullable: Treat struct fields as nullable by default.
meta_compressor: Optional typedef/meta compressor implementation.
"""
compatible = True if compatible is None else compatible
self.xlang = xlang
self.track_ref = ref
self.strict = strict
if strict is not True:
from pyfory._fory import _ENABLE_TYPE_REGISTRATION_FORCIBLY
if _ENABLE_TYPE_REGISTRATION_FORCIBLY:
self.strict = True
self.policy = DEFAULT_POLICY if policy is None else policy
self.compatible = compatible
self.field_nullable = field_nullable
self.max_depth = max_depth
if (
not isinstance(max_graph_memory_bytes, int)
or max_graph_memory_bytes <= 0
or max_graph_memory_bytes > 9223372036854775807
):
raise ValueError("max_graph_memory_bytes must be in range [1, 9223372036854775807]")
if (
not isinstance(max_unbacked_container_items, int)
or max_unbacked_container_items < 0
or max_unbacked_container_items > 9223372036854775807
):
raise ValueError("max_unbacked_container_items must be in range [0, 9223372036854775807]")
self.max_graph_memory_bytes = max_graph_memory_bytes
self.max_unbacked_container_items = max_unbacked_container_items
self.config = Config(
xlang=xlang,
track_ref=ref,
strict=self.strict,
compatible=compatible,
meta_share=compatible,
scoped_meta_share_enabled=compatible,
max_depth=max_depth,
max_type_fields=max_type_fields,
max_type_meta_bytes=max_type_meta_bytes,
max_schema_versions_per_type=max_schema_versions_per_type,
max_average_schema_versions_per_type=max_average_schema_versions_per_type,
max_graph_memory_bytes=max_graph_memory_bytes,
max_unbacked_container_items=max_unbacked_container_items,
field_nullable=field_nullable,
policy=self.policy,
meta_compressor=meta_compressor,
)
from pyfory.registry import SharedRegistry
shared_registry = SharedRegistry()
self.type_resolver = TypeResolver(
self.config,
shared_registry=shared_registry,
)
self.type_resolver.initialize()
self.write_context = WriteContext(self.config, self.type_resolver)
self.read_context = ReadContext(self.config, self.type_resolver)
self.buffer = Buffer.allocate(32)
def register(
self,
cls,
*,
type_id=None,
name=None,
serializer=None,
):
return self.register_type(
cls,
type_id=type_id,
name=name,
serializer=serializer,
)
def register_type(
self,
cls,
*,
type_id: int = None,
name: str = None,
serializer=None,
):
return self.type_resolver.register_type(
cls,
type_id=type_id,
name=name,
serializer=serializer,
)
def register_union(
self,
cls,
*,
type_id: int = None,
name: str = None,
serializer=None,
):
return self.type_resolver.register_union(
cls,
type_id=type_id,
name=name,
serializer=serializer,
)
def register_serializer(self, cls, serializer):
self.type_resolver.register_serializer(cls, serializer)
def dumps(
self,
obj,
buffer=None,
buffer_callback=None,
unsupported_callback=None,
):
return self.serialize(
obj,
buffer=buffer,
buffer_callback=buffer_callback,
unsupported_callback=unsupported_callback,
)
def dump(self, obj, stream):
try:
self.buffer.set_writer_index(0)
self.buffer.bind_output_stream(Buffer.wrap_output_stream(stream))
self._serialize(
obj,
self.buffer,
buffer_callback=None,
unsupported_callback=None,
)
self.force_flush()
finally:
self.buffer.bind_output_stream(None)
self.reset_write()
def loads(self, buffer, target_type=None, *, buffers=None, unsupported_objects=None):
return self.deserialize(
buffer,
target_type,
buffers=buffers,
unsupported_objects=unsupported_objects,
)
def serialize(self, obj, Buffer buffer=None, buffer_callback=None, unsupported_callback=None):
cdef Buffer write_buffer
try:
write_buffer = self._serialize(
obj,
buffer,
buffer_callback=buffer_callback,
unsupported_callback=unsupported_callback,
)
if write_buffer is not self.buffer:
return write_buffer
if write_buffer.get_output_stream() is not None:
return write_buffer
return write_buffer.to_bytes(0, write_buffer.get_writer_index())
finally:
self.reset_write()
cdef Buffer _serialize(self, obj, Buffer buffer=None, buffer_callback=None, unsupported_callback=None):
cdef WriteContext write_context = self.write_context
cdef int32_t mask_index
cdef uint8_t bitmap
if not self.type_resolver._frozen:
self.type_resolver.freeze()
if buffer is None:
self.buffer.set_writer_index(0)
buffer = self.buffer
# Keep the root context setup inline. Top-level serialize is a hot path,
# so it should not pay an extra method call just to bind the active buffer.
write_context.buffer = buffer
write_context.c_buffer = buffer.c_buffer
write_context.buffer_callback = buffer_callback
write_context.unsupported_callback = unsupported_callback
mask_index = buffer.get_writer_index()
buffer.grow(1)
buffer.set_writer_index(mask_index + 1)
bitmap = 0x41 if self.xlang else 0x40
if buffer_callback is not None:
bitmap |= 2
buffer.put_int8(mask_index, <int8_t>bitmap)
write_context.write_ref(obj)
return buffer
def deserialize(self, buffer, target_type=None, *, buffers=None, unsupported_objects=None):
try:
return self._deserialize(
buffer,
target_type=target_type,
buffers=buffers,
unsupported_objects=unsupported_objects,
)
finally:
self.reset_read()
cdef object _deserialize(self, buffer, target_type=None, buffers=None, unsupported_objects=None):
cdef ReadContext read_context = self.read_context
cdef Buffer read_buffer
cdef int32_t reader_index
cdef uint8_t bitmap
cdef bint peer_out_of_band_enabled
cdef TypeInfo expected_typeinfo
if not self.type_resolver._frozen:
self.type_resolver.freeze()
if isinstance(buffer, bytes):
buffer = Buffer(buffer)
read_buffer = buffer
reader_index = read_buffer.get_reader_index()
read_buffer.set_reader_index(reader_index + 1)
bitmap = <uint8_t>read_buffer.get_int8(reader_index)
if (bitmap & 0xC0) != 0x40:
raise ValueError(
f"Unsupported Fory protocol generation in root header bitmap 0x{bitmap:02x}"
)
if bitmap & 0x3C:
raise ValueError(f"Unsupported root header bitmap 0x{bitmap:02x}")
if ((bitmap & 1) != 0) != self.xlang:
raise ValueError("Header bitmap mismatch at xlang bit")
peer_out_of_band_enabled = (bitmap & 2) != 0
if peer_out_of_band_enabled and buffers is None:
raise ValueError("Out-of-band buffers are required by the root header")
if not peer_out_of_band_enabled and buffers is not None:
raise ValueError("Out-of-band buffers were provided for an in-band root payload")
# Keep the root context setup inline. Top-level deserialize is a hot path,
# so it should not pay an extra method call just to bind the active buffer.
read_context.buffer = read_buffer
read_context.c_buffer = read_buffer.c_buffer
read_context.buffers = iter(buffers) if buffers is not None else None
read_context.unsupported_objects = (
iter(unsupported_objects) if unsupported_objects is not None else None
)
read_context.peer_out_of_band_enabled = peer_out_of_band_enabled
read_context.remaining_graph_memory_bytes = self.max_graph_memory_bytes
read_context.remaining_unbacked_container_items = self.max_unbacked_container_items
read_context.depth = 0
if target_type is not None:
if target_type is read_context._last_root_target:
expected_typeinfo = read_context._last_root_target_info
else:
expected_typeinfo = self.type_resolver.resolve_target_type_info(target_type)
read_context._last_root_target = target_type
read_context._last_root_target_info = expected_typeinfo
return read_context.read_ref_expected(expected_typeinfo)
return read_context.read_ref()
cpdef enter_flush_barrier(self):
self.write_context.enter_flush_barrier()
cpdef exit_flush_barrier(self):
self.write_context.exit_flush_barrier()
cpdef try_flush(self):
self.write_context.try_flush()
cpdef force_flush(self):
self.write_context.force_flush()
cpdef reset_write(self):
self.write_context.reset()
cpdef reset_read(self):
self.read_context.reset()
cpdef reset(self):
self.reset_write()
self.reset_read()
include "primitive.pxi"
include "number.pxi"
include "collection.pxi"
include "struct.pxi"
cpdef inline write_nullable_pybool(buffer, value):
if value is None:
buffer.write_int8(NULL_FLAG)
else:
buffer.write_int8(NOT_NULL_VALUE_FLAG)
buffer.write_bool(value)
cpdef inline write_nullable_int8(buffer, value):
if value is None:
buffer.write_int8(NULL_FLAG)
else:
buffer.write_int8(NOT_NULL_VALUE_FLAG)
buffer.write_int8(value)
cpdef inline write_nullable_int16(buffer, value):
if value is None:
buffer.write_int8(NULL_FLAG)
else:
buffer.write_int8(NOT_NULL_VALUE_FLAG)
buffer.write_int16(value)
cpdef inline write_nullable_int32(buffer, value):
if value is None:
buffer.write_int8(NULL_FLAG)
else:
buffer.write_int8(NOT_NULL_VALUE_FLAG)
buffer.write_varint32(value)
cpdef inline write_nullable_pyint64(buffer, value):
if value is None:
buffer.write_int8(NULL_FLAG)
else:
buffer.write_int8(NOT_NULL_VALUE_FLAG)
buffer.write_varint64(value)
cpdef inline write_nullable_float32(buffer, value):
if value is None:
buffer.write_int8(NULL_FLAG)
else:
buffer.write_int8(NOT_NULL_VALUE_FLAG)
buffer.write_float32(value)
cpdef inline write_nullable_pyfloat64(buffer, value):
if value is None:
buffer.write_int8(NULL_FLAG)
else:
buffer.write_int8(NOT_NULL_VALUE_FLAG)
buffer.write_double(value)
cpdef inline write_nullable_pystr(buffer, value):
if value is None:
buffer.write_int8(NULL_FLAG)
else:
buffer.write_int8(NOT_NULL_VALUE_FLAG)
buffer.write_string(value)
cpdef inline read_nullable_pybool(buffer):
if buffer.read_int8() == NOT_NULL_VALUE_FLAG:
return buffer.read_bool()
return None
cpdef inline read_nullable_int8(buffer):
if buffer.read_int8() == NOT_NULL_VALUE_FLAG:
return buffer.read_int8()
return None
cpdef inline read_nullable_int16(buffer):
if buffer.read_int8() == NOT_NULL_VALUE_FLAG:
return buffer.read_int16()
return None
cpdef inline read_nullable_int32(buffer):
if buffer.read_int8() == NOT_NULL_VALUE_FLAG:
return buffer.read_varint32()
return None
cpdef inline read_nullable_pyint64(buffer):
if buffer.read_int8() == NOT_NULL_VALUE_FLAG:
return buffer.read_varint64()
return None
cpdef inline read_nullable_float32(buffer):
if buffer.read_int8() == NOT_NULL_VALUE_FLAG:
return buffer.read_float32()
return None
cpdef inline read_nullable_pyfloat64(buffer):
if buffer.read_int8() == NOT_NULL_VALUE_FLAG:
return buffer.read_double()
return None
cpdef inline read_nullable_pystr(buffer):
if buffer.read_int8() == NOT_NULL_VALUE_FLAG:
return buffer.read_string()
return None