Remove unused APIs

This commit is contained in:
Adeeb Shihadeh
2026-09-21 19:26:21 -07:00
parent 5befba15f3
commit 899416423f
9 changed files with 52 additions and 258 deletions

View File

@@ -21,6 +21,10 @@ packed serialization, file-descriptor I/O, segment APIs, borrowed Data views,
custom allocators, orphans/resizable lists, AnyPointer wrappers, type registration, custom allocators, orphans/resizable lists, AnyPointer wrappers, type registration,
the Python schema import hook, and the Cython code generator. Their examples, the Python schema import hook, and the Cython code generator. Their examples,
tests, docs, dependencies, and unsupported-platform CI were removed too. tests, docs, dependencies, and unsupported-platform CI were removed too.
Also removed are generated per-schema `Reader`/`Builder` classes, synthetic
`.Union` enums, `_has_by_field`/`_init_by_field`, allocation-size overrides,
`from_bytes(builder=True)`, and `to_dict` ordering/base64 options. Incoming base64
Data values in `from_dict` remain supported.
`remove_import_hook()` remains a no-op for cereal/opendbc compatibility. `remove_import_hook()` remains a no-op for cereal/opendbc compatibility.
This is intentionally not a full upstream API replacement. The import and This is intentionally not a full upstream API replacement. The import and

View File

@@ -146,9 +146,7 @@ cdef extern from "capnp/dynamic.h" namespace " ::capnp":
DynamicValueForward.Reader get(char *) except +reraise_kj_exception DynamicValueForward.Reader get(char *) except +reraise_kj_exception
DynamicValueForward.Reader getByField"get"(StructSchema.Field) except +reraise_kj_exception DynamicValueForward.Reader getByField"get"(StructSchema.Field) except +reraise_kj_exception
bint has(char *) except +reraise_kj_exception bint has(char *) except +reraise_kj_exception
bint hasByField"has"(StructSchema.Field) except +reraise_kj_exception
StructSchema getSchema() StructSchema getSchema()
uint64_t getId"getSchema().getProto().getId"()
Maybe[StructSchema.Field] which() Maybe[StructSchema.Field] which()
MessageSize totalSize() MessageSize totalSize()
@@ -159,15 +157,11 @@ cdef extern from "capnp/dynamic.h" namespace " ::capnp":
DynamicValueForward.Builder get(char *) except +reraise_kj_exception DynamicValueForward.Builder get(char *) except +reraise_kj_exception
DynamicValueForward.Builder getByField"get"(StructSchema.Field) except +reraise_kj_exception DynamicValueForward.Builder getByField"get"(StructSchema.Field) except +reraise_kj_exception
bint has(char *) except +reraise_kj_exception bint has(char *) except +reraise_kj_exception
bint hasByField"has"(StructSchema.Field) except +reraise_kj_exception
void set(char *, DynamicValueForward.Reader) except +reraise_kj_exception void set(char *, DynamicValueForward.Reader) except +reraise_kj_exception
void setByField"set"(StructSchema.Field, DynamicValueForward.Reader) except +reraise_kj_exception void setByField"set"(StructSchema.Field, DynamicValueForward.Reader) except +reraise_kj_exception
DynamicValueForward.Builder init(char *, uint size) except +reraise_kj_exception DynamicValueForward.Builder init(char *, uint size) except +reraise_kj_exception
DynamicValueForward.Builder init(char *) except +reraise_kj_exception DynamicValueForward.Builder init(char *) except +reraise_kj_exception
DynamicValueForward.Builder initByField"init"(StructSchema.Field, uint size) except +reraise_kj_exception
DynamicValueForward.Builder initByField"init"(StructSchema.Field) except +reraise_kj_exception
StructSchema getSchema() StructSchema getSchema()
uint64_t getId"getSchema().getProto().getId"()
Maybe[StructSchema.Field] which() Maybe[StructSchema.Field] which()
DynamicStruct.Reader asReader() DynamicStruct.Reader asReader()
MessageSize totalSize() MessageSize totalSize()

View File

@@ -82,7 +82,6 @@ cdef extern from "capnp/message.h" namespace " ::capnp":
cdef cppclass MallocMessageBuilder(MessageBuilder) nogil: cdef cppclass MallocMessageBuilder(MessageBuilder) nogil:
MallocMessageBuilder() MallocMessageBuilder()
MallocMessageBuilder(int)
cdef extern from "capnp/serialize.h" namespace " ::capnp": cdef extern from "capnp/serialize.h" namespace " ::capnp":
cdef cppclass FlatArrayMessageReader(MessageReader) nogil: cdef cppclass FlatArrayMessageReader(MessageReader) nogil:

View File

@@ -1,18 +1,4 @@
from cpython.ref cimport PyObject, Py_INCREF, Py_DECREF
from libc.stdint cimport * from libc.stdint cimport *
ctypedef unsigned int uint
ctypedef uint8_t byte
ctypedef uint8_t UInt8
ctypedef uint16_t UInt16
ctypedef uint32_t UInt32
ctypedef uint64_t UInt64
ctypedef int8_t Int8
ctypedef int16_t Int16
ctypedef int32_t Int32
ctypedef int64_t Int64
ctypedef char * Object
ctypedef bint Bool
ctypedef float Float32
ctypedef double Float64
from libcpp cimport bool as cbool from libcpp cimport bool as cbool
ctypedef unsigned int uint

View File

@@ -5,7 +5,7 @@ from capnp.includes cimport schema_cpp
from capnp.includes.capnp_cpp cimport ( from capnp.includes.capnp_cpp cimport (
Schema as C_Schema, StructSchema as C_StructSchema, Schema as C_Schema, StructSchema as C_StructSchema,
EnumSchema as C_EnumSchema, ListSchema as C_ListSchema, DynamicStruct as C_DynamicStruct, EnumSchema as C_EnumSchema, ListSchema as C_ListSchema, DynamicStruct as C_DynamicStruct,
DynamicValue as C_DynamicValue, Type as C_Type, DynamicList as C_DynamicList, DynamicValue as C_DynamicValue, DynamicList as C_DynamicList,
SchemaParser as C_SchemaParser, ParsedSchema as C_ParsedSchema, VOID, ArrayPtr, StringPtr, SchemaParser as C_SchemaParser, ParsedSchema as C_ParsedSchema, VOID, ArrayPtr, StringPtr,
String, StringTree, DynamicStruct_Builder String, StringTree, DynamicStruct_Builder
) )
@@ -47,8 +47,7 @@ cdef class _DynamicStructReader:
cpdef _DynamicEnumField _which(self) cpdef _DynamicEnumField _which(self)
cpdef _which_str(self) cpdef _which_str(self)
cpdef _get_by_field(self, _StructSchemaField field) cpdef _get_by_field(self, _StructSchemaField field)
cpdef _has_by_field(self, _StructSchemaField field) cpdef as_builder(self)
cpdef as_builder(self, num_first_segment_words=?)
cdef class _DynamicStructBuilder: cdef class _DynamicStructBuilder:
@@ -68,12 +67,10 @@ cdef class _DynamicStructBuilder:
cpdef init(self, field, size=?) cpdef init(self, field, size=?)
cpdef _get_by_field(self, _StructSchemaField field) cpdef _get_by_field(self, _StructSchemaField field)
cpdef _set_by_field(self, _StructSchemaField field, value) cpdef _set_by_field(self, _StructSchemaField field, value)
cpdef _has_by_field(self, _StructSchemaField field)
cpdef _init_by_field(self, _StructSchemaField field, size=?)
cpdef _DynamicEnumField _which(self) cpdef _DynamicEnumField _which(self)
cpdef _which_str(self) cpdef _which_str(self)
cpdef as_reader(self) cpdef as_reader(self)
cpdef copy(self, num_first_segment_words=?) cpdef copy(self)
cdef class _DynamicEnumField: cdef class _DynamicEnumField:
cdef object thisptr cdef object thisptr
@@ -115,10 +112,9 @@ cdef class _MessageBuilder:
cpdef set_root(self, value) cpdef set_root(self, value)
cdef to_python_reader(C_DynamicValue.Reader self, object parent) cdef to_python_reader(C_DynamicValue.Reader self, object parent)
cdef to_python_builder(C_DynamicValue.Builder self, object parent) cdef to_python_builder(C_DynamicValue.Builder self, object parent)
cdef _to_dict(msg, bint verbose, bint ordered, bint encode_bytes_as_base64=?) cdef _to_dict(msg, bint verbose)
cdef _from_list(_DynamicListBuilder msg, list d) cdef _from_list(_DynamicListBuilder msg, list d)
cdef _from_tuple(_DynamicListBuilder msg, tuple d) cdef _from_tuple(_DynamicListBuilder msg, tuple d)
cdef _setDynamicFieldWithField(DynamicStruct_Builder thisptr, _StructSchemaField field, value, parent) cdef _setDynamicFieldWithField(DynamicStruct_Builder thisptr, _StructSchemaField field, value, parent)
cdef _setDynamicFieldStatic(DynamicStruct_Builder thisptr, field, value, parent)
cdef api object wrap_kj_exception_for_reraise(capnp.Exception & exception) with gil cdef api object wrap_kj_exception_for_reraise(capnp.Exception & exception) with gil

View File

@@ -21,7 +21,6 @@ from libc.string cimport memcpy
from libcpp.utility cimport move from libcpp.utility cimport move
import collections as _collections
import contextlib import contextlib
import base64 import base64
import enum as _enum import enum as _enum
@@ -164,11 +163,6 @@ cdef schema_cpp.ReaderOptions make_reader_opts(traversal_limit_in_words, nesting
return opts return opts
ctypedef fused _DynamicStructReaderOrBuilder:
_DynamicStructReader
_DynamicStructBuilder
ctypedef fused _DynamicSetterClasses: ctypedef fused _DynamicSetterClasses:
C_DynamicList.Builder C_DynamicList.Builder
DynamicStruct_Builder DynamicStruct_Builder
@@ -627,119 +621,56 @@ cdef _setDynamicFieldWithField(DynamicStruct_Builder thisptr, _StructSchemaField
# TODO: Is this function used by anyone? Can it be removed? # TODO: Is this function used by anyone? Can it be removed?
cdef _setDynamicFieldStatic(DynamicStruct_Builder thisptr, field, value, parent):
cdef C_DynamicValue.Reader temp
value_type = type(value)
if value_type is int or value_type is long:
if value < 0:
temp = C_DynamicValue.Reader(<long long>value)
else:
temp = C_DynamicValue.Reader(<unsigned long long>value)
thisptr.set(field, temp)
elif value_type is float:
temp = C_DynamicValue.Reader(<double>value)
thisptr.set(field, temp)
elif value_type is bool:
temp = C_DynamicValue.Reader(<cbool>value)
thisptr.set(field, temp)
elif value_type is bytes:
_setBytes(thisptr, field, value)
elif isinstance(value, basestring):
_setBaseString(thisptr, field, value)
elif value_type is list:
ptr = thisptr.init(field, len(value))
builder = to_python_builder(ptr, parent)
_from_list(builder, value)
elif value_type is dict:
ptr = thisptr.get(field)
builder = to_python_builder(ptr, parent)
builder.from_dict(value)
elif value is None:
temp = C_DynamicValue.Reader(VOID)
thisptr.set(field, temp)
elif value_type is _DynamicStructBuilder:
thisptr.set(field, _extract_dynamic_struct_builder(value))
elif value_type is _DynamicStructReader:
thisptr.set(field, _extract_dynamic_struct_reader(value))
elif value_type is _DynamicListBuilder:
thisptr.set(field, _extract_dynamic_list_builder(value))
elif value_type is _DynamicListReader:
thisptr.set(field, _extract_dynamic_list_reader(value))
elif value_type is _DynamicEnum:
thisptr.set(field, _extract_dynamic_enum(value))
else:
raise KjException(
"Tried to set field: '{}' with a value of: '{}' which is an unsupported type: '{}'"
.format(field, str(value), str(type(value))))
cdef _DynamicListBuilder temp_list_b cdef _DynamicListBuilder temp_list_b
cdef _DynamicListReader temp_list_r cdef _DynamicListReader temp_list_r
cdef _DynamicStructBuilder temp_msg_b cdef _DynamicStructBuilder temp_msg_b
cdef _DynamicStructReader temp_msg_r cdef _DynamicStructReader temp_msg_r
cdef _to_dict(msg, bint verbose, bint ordered, bint encode_bytes_as_base64=False): cdef _to_dict(msg, bint verbose):
msg_type = type(msg) msg_type = type(msg)
if msg_type is _DynamicListBuilder: if msg_type is _DynamicListBuilder:
temp_list_b = msg temp_list_b = msg
return [_to_dict(temp_list_b._get(i), verbose, ordered, encode_bytes_as_base64) for i in range(len(msg))] return [_to_dict(temp_list_b._get(i), verbose) for i in range(len(msg))]
elif msg_type is _DynamicListReader: elif msg_type is _DynamicListReader:
temp_list_r = msg temp_list_r = msg
return [_to_dict(temp_list_r._get(i), verbose, ordered, encode_bytes_as_base64) for i in range(len(msg))] return [_to_dict(temp_list_r._get(i), verbose) for i in range(len(msg))]
if msg_type is _DynamicStructBuilder: if msg_type is _DynamicStructBuilder:
temp_msg_b = msg temp_msg_b = msg
if ordered:
ret = _collections.OrderedDict()
else:
ret = {} ret = {}
try: try:
which = temp_msg_b.which() which = temp_msg_b.which()
ret[which] = _to_dict(temp_msg_b._get(which), verbose, ordered, encode_bytes_as_base64) ret[which] = _to_dict(temp_msg_b._get(which), verbose)
except KjException: except KjException:
pass pass
for field in temp_msg_b.schema.non_union_fields: for field in temp_msg_b.schema.non_union_fields:
if verbose or temp_msg_b._has(field): if verbose or temp_msg_b._has(field):
ret[field] = _to_dict(temp_msg_b._get(field), verbose, ordered, encode_bytes_as_base64) ret[field] = _to_dict(temp_msg_b._get(field), verbose)
return ret return ret
elif msg_type is _DynamicStructReader: elif msg_type is _DynamicStructReader:
temp_msg_r = msg temp_msg_r = msg
if ordered:
ret = _collections.OrderedDict()
else:
ret = {} ret = {}
try: try:
which = temp_msg_r.which() which = temp_msg_r.which()
ret[which] = _to_dict(temp_msg_r._get(which), verbose, ordered, encode_bytes_as_base64) ret[which] = _to_dict(temp_msg_r._get(which), verbose)
except KjException: except KjException:
pass pass
for field in temp_msg_r.schema.non_union_fields: for field in temp_msg_r.schema.non_union_fields:
if verbose or temp_msg_r._has(field): if verbose or temp_msg_r._has(field):
ret[field] = _to_dict(temp_msg_r._get(field), verbose, ordered, encode_bytes_as_base64) ret[field] = _to_dict(temp_msg_r._get(field), verbose)
return ret return ret
if isinstance(msg, (_DynamicStructBuilder, _DynamicStructReader)): if isinstance(msg, (_DynamicStructBuilder, _DynamicStructReader)):
return msg.to_dict(verbose, ordered) return msg.to_dict(verbose)
if msg_type is _DynamicEnum: if msg_type is _DynamicEnum:
return str(msg) return str(msg)
if encode_bytes_as_base64 and msg_type is bytes:
# encode the message as base64 and return utf-8 string
return base64.b64encode(msg).decode('utf-8')
if msg_type is memoryview:
if encode_bytes_as_base64:
return base64.b64encode(bytes(msg)).decode('utf-8')
else:
return bytes(msg)
return msg return msg
@@ -890,9 +821,6 @@ cdef class _DynamicStructReader:
cpdef _has(self, field): cpdef _has(self, field):
return self.thisptr.has(field) return self.thisptr.has(field)
cpdef _has_by_field(self, _StructSchemaField field):
return self.thisptr.hasByField(field.thisptr)
cpdef _which_str(self): cpdef _which_str(self):
try: try:
return <char *>helpers.fixMaybe(self.thisptr.which()).getProto().getName().cStr() return <char *>helpers.fixMaybe(self.thisptr.which()).getProto().getName().cStr()
@@ -946,21 +874,18 @@ cdef class _DynamicStructReader:
def __repr__(self): def __repr__(self):
return '<%s reader %s>' % (self.schema.node.displayName, <char*>strStructReader(self.thisptr).cStr()) return '<%s reader %s>' % (self.schema.node.displayName, <char*>strStructReader(self.thisptr).cStr())
def to_dict(self, verbose=False, ordered=False, encode_bytes_as_base64=False): def to_dict(self, verbose=False):
return _to_dict(self, verbose, ordered, encode_bytes_as_base64) return _to_dict(self, verbose)
cpdef as_builder(self, num_first_segment_words=None): cpdef as_builder(self):
"""A method for casting this Reader to a Builder """A method for casting this Reader to a Builder
This is a copying operation with respect to the message's buffer. This is a copying operation with respect to the message's buffer.
Changes in the new builder will not reflect in the original reader. Changes in the new builder will not reflect in the original reader.
:type num_first_segment_words: int
:param num_first_segment_words: Size of the first segment to allocate (in words ie. 8 byte increments)
:rtype: :class:`_DynamicStructBuilder` :rtype: :class:`_DynamicStructBuilder`
""" """
builder = _MallocMessageBuilder(num_first_segment_words) builder = _MallocMessageBuilder()
return builder.set_root(self) return builder.set_root(self)
property total_size: property total_size:
@@ -1055,9 +980,6 @@ cdef class _DynamicStructBuilder:
cpdef _has(self, field): cpdef _has(self, field):
return self.thisptr.has(field) return self.thisptr.has(field)
cpdef _has_by_field(self, _StructSchemaField field):
return self.thisptr.hasByField(field.thisptr)
cpdef init(self, field, size=None): cpdef init(self, field, size=None):
"""Method for initializing fields that are of type union/struct/list """Method for initializing fields that are of type union/struct/list
@@ -1082,28 +1004,6 @@ cdef class _DynamicStructBuilder:
ptr = self.thisptr.init(field, size) ptr = self.thisptr.init(field, size)
return to_python_builder(ptr, self._parent) return to_python_builder(ptr, self._parent)
cpdef _init_by_field(self, _StructSchemaField field, size=None):
"""Method for initializing fields that are of type union/struct/list
Typically, you don't have to worry about initializing structs/unions, so this method is mainly for lists.
:type field: str
:param field: The field name to initialize
:type size: int
:param size: The size of the list to initiialize. This should be None for struct/union initialization.
:rtype: :class:`_DynamicStructBuilder` or :class:`_DynamicListBuilder`
:Raises: :exc:`KjException` if the field isn't in this struct
"""
if size is None:
ptr = self.thisptr.initByField(field.thisptr)
return to_python_builder(ptr, self._parent)
else:
ptr = self.thisptr.initByField(field.thisptr, size)
return to_python_builder(ptr, self._parent)
cpdef _which_str(self): cpdef _which_str(self):
try: try:
return <char *>helpers.fixMaybe(self.thisptr.which()).getProto().getName().cStr() return <char *>helpers.fixMaybe(self.thisptr.which()).getProto().getName().cStr()
@@ -1155,18 +1055,15 @@ cdef class _DynamicStructBuilder:
reader._obj_to_pin = self reader._obj_to_pin = self
return reader return reader
cpdef copy(self, num_first_segment_words=None): cpdef copy(self):
"""A method for copying this Builder """A method for copying this Builder
This is a copying operation with respect to the message's buffer. This is a copying operation with respect to the message's buffer.
Changes in the new builder will not reflect in the original reader. Changes in the new builder will not reflect in the original reader.
:type num_first_segment_words: int
:param num_first_segment_words: Size of the first segment to allocate (in words ie. 8 byte increments)
:rtype: :class:`_DynamicStructBuilder` :rtype: :class:`_DynamicStructBuilder`
""" """
builder = _MallocMessageBuilder(num_first_segment_words) builder = _MallocMessageBuilder()
return builder.set_root(self) return builder.set_root(self)
property schema: property schema:
@@ -1185,8 +1082,8 @@ cdef class _DynamicStructBuilder:
def __repr__(self): def __repr__(self):
return '<%s builder %s>' % (self.schema.node.displayName, <char*>strStructBuilder(self.thisptr).cStr()) return '<%s builder %s>' % (self.schema.node.displayName, <char*>strStructBuilder(self.thisptr).cStr())
def to_dict(self, verbose=False, ordered=False, encode_bytes_as_base64=False): def to_dict(self, verbose=False):
return _to_dict(self, verbose, ordered, encode_bytes_as_base64) return _to_dict(self, verbose)
def from_dict(self, dict d): def from_dict(self, dict d):
for key, val in d.iteritems(): for key, val in d.iteritems():
@@ -1415,15 +1312,9 @@ cdef class _ParsedSchema(_Schema):
return _ParsedSchema()._init_child(self.thisptr_child.getNested(name)) return _ParsedSchema()._init_child(self.thisptr_child.getNested(name))
class _StructABCMeta(type): cdef _new_message(self, kwargs):
"""A metaclass for the Type.Reader and Type.Builder ABCs."""
def __instancecheck__(cls, obj):
return isinstance(obj, cls.__base__) and obj.schema == cls._schema
cdef _new_message(self, kwargs, num_first_segment_words):
cdef _MessageBuilder builder cdef _MessageBuilder builder
builder = _MallocMessageBuilder(num_first_segment_words) builder = _MallocMessageBuilder()
msg = builder.init_root(self.schema) msg = builder.init_root(self.schema)
if kwargs is not None: if kwargs is not None:
msg.from_dict(kwargs) msg.from_dict(kwargs)
@@ -1458,14 +1349,6 @@ class _StructModule(object):
sub_module = _StructModuleWhich("StructModuleWhich", mapping) sub_module = _StructModuleWhich("StructModuleWhich", mapping)
setattr(sub_module, 'schema', raw_schema) setattr(sub_module, 'schema', raw_schema)
setattr(self, name, sub_module) setattr(self, name, sub_module)
if schema.union_fields and not schema.non_union_fields:
mapping = []
for union_field in schema.node.struct.fields:
name = union_field.name
name = name[0].upper() + name[1:]
mapping.append((name, union_field.discriminantValue))
sub_module = _StructModuleWhich("StructModuleWhich", mapping)
setattr(self, 'Union', sub_module)
def read_multiple_bytes(self, buf, traversal_limit_in_words=None, nesting_limit=None): def read_multiple_bytes(self, buf, traversal_limit_in_words=None, nesting_limit=None):
"""Returns an iterable, that when traversed will return Readers for messages. """Returns an iterable, that when traversed will return Readers for messages.
@@ -1485,7 +1368,7 @@ class _StructModule(object):
return reader return reader
@contextlib.contextmanager @contextlib.contextmanager
def from_bytes(self, buf, traversal_limit_in_words=None, nesting_limit=None, builder=False): def from_bytes(self, buf, traversal_limit_in_words=None, nesting_limit=None):
"""Returns a Reader for the unpacked object in buf. """Returns a Reader for the unpacked object in buf.
:type buf: buffer :type buf: buffer
@@ -1498,34 +1381,22 @@ class _StructModule(object):
:type nesting_limit: int :type nesting_limit: int
:param nesting_limit: Limits how many total words of data are allowed to be traversed. Default is 64. :param nesting_limit: Limits how many total words of data are allowed to be traversed. Default is 64.
:type builder: bool :rtype: :class:`_DynamicStructReader`
:param builder: If true, return a builder object.
Enabling `builder` returns a writable copy of the message.
:rtype: :class:`_DynamicStructReader` or :class:`_DynamicStructBuilder`
""" """
message = None message = None
try: try:
if builder:
message = _FlatArrayMessageReader(buf, traversal_limit_in_words, nesting_limit)
yield message.get_root(self.schema).as_builder()
else:
message = _FlatArrayMessageReader(buf, traversal_limit_in_words, nesting_limit) message = _FlatArrayMessageReader(buf, traversal_limit_in_words, nesting_limit)
yield message.get_root(self.schema) yield message.get_root(self.schema)
finally: finally:
if message: if message:
message.close() message.close()
def __call__(self, num_first_segment_words=None, **kwargs): def __call__(self, **kwargs):
return self.new_message(num_first_segment_words=num_first_segment_words, **kwargs) return self.new_message(**kwargs)
def new_message(self, num_first_segment_words=None, **kwargs): def new_message(self, **kwargs):
"""Returns a newly allocated builder message. """Returns a newly allocated builder message.
:type num_first_segment_words: int
:param num_first_segment_words: Size of the first segment to allocate (in words ie. 8 byte increments)
:type kwargs: dict :type kwargs: dict
:param kwargs: A list of fields and their values to initialize in the struct. :param kwargs: A list of fields and their values to initialize in the struct.
@@ -1534,7 +1405,7 @@ class _StructModule(object):
:rtype: :class:`_DynamicStructBuilder` :rtype: :class:`_DynamicStructBuilder`
""" """
return _new_message(self, kwargs, num_first_segment_words) return _new_message(self, kwargs)
class _EnumModule(object): class _EnumModule(object):
@@ -1641,27 +1512,6 @@ cdef class SchemaParser:
if proto.isStruct: if proto.isStruct:
local_module = _StructModule(schema.as_struct(), node.name) local_module = _StructModule(schema.as_struct(), node.name)
class Reader(_DynamicStructReader):
"""An abstract base class. Readers are 'instances' of this class."""
__metaclass__ = _StructABCMeta
__slots__ = []
_schema = local_module.schema
def __new__(self):
raise TypeError('This is an abstract base class')
class Builder(_DynamicStructBuilder):
"""An abstract base class. Builders are 'instances' of this class."""
__metaclass__ = _StructABCMeta
__slots__ = []
_schema = local_module.schema
def __new__(self):
raise TypeError('This is an abstract base class')
local_module.Reader = Reader
local_module.Builder = Builder
module.__dict__[node.name] = local_module module.__dict__[node.name] = local_module
elif proto.isConst: elif proto.isConst:
module.__dict__[node.name] = schema.as_const_value() module.__dict__[node.name] = schema.as_const_value()
@@ -1794,11 +1644,8 @@ cdef class _MallocMessageBuilder(_MessageBuilder):
... ...
data = person.to_bytes() data = person.to_bytes()
""" """
def __init__(self, size=None): def __init__(self):
if size is None:
self.thisptr = new schema_cpp.MallocMessageBuilder() self.thisptr = new schema_cpp.MallocMessageBuilder()
else:
self.thisptr = new schema_cpp.MallocMessageBuilder(size)
cdef class _MessageReader: cdef class _MessageReader:

View File

@@ -14,8 +14,8 @@ def blob_schema():
def test_blob_to_dict(blob_schema): def test_blob_to_dict(blob_schema):
blob_value = b"hello world" blob_value = b"hello world"
blob = blob_schema.BlobTest(blob=blob_value) blob = blob_schema.BlobTest(blob=blob_value)
blob_dict = blob.to_dict(encode_bytes_as_base64=True) assert blob.to_dict()["blob"] == blob_value
assert base64.b64decode(blob_dict["blob"]) == blob_value blob_dict = {"blob": base64.b64encode(blob_value).decode("ascii")}
msg = blob_schema.BlobTest.new_message() msg = blob_schema.BlobTest.new_message()
msg.from_dict(blob_dict) msg.from_dict(blob_dict)
assert blob.blob == blob_value assert msg.blob == blob_value

View File

@@ -143,16 +143,15 @@ def test_addressbook(addressbook):
def test_addressbook_explicit_fields(addressbook): def test_addressbook_explicit_fields(addressbook):
def writeAddressBook(file): def writeAddressBook(file):
addresses = addressbook.AddressBook.new_message() addresses = addressbook.AddressBook.new_message()
address_fields = addressbook.AddressBook.schema.fields
person_fields = addressbook.Person.schema.fields person_fields = addressbook.Person.schema.fields
phone_fields = addressbook.Person.PhoneNumber.schema.fields phone_fields = addressbook.Person.PhoneNumber.schema.fields
people = addresses._init_by_field(address_fields["people"], 2) people = addresses.init("people", 2)
alice = people[0] alice = people[0]
alice._set_by_field(person_fields["id"], 123) alice._set_by_field(person_fields["id"], 123)
alice._set_by_field(person_fields["name"], "Alice") alice._set_by_field(person_fields["name"], "Alice")
alice._set_by_field(person_fields["email"], "alice@example.com") alice._set_by_field(person_fields["email"], "alice@example.com")
alicePhones = alice._init_by_field(person_fields["phones"], 1) alicePhones = alice.init("phones", 1)
alicePhones[0]._set_by_field(phone_fields["number"], "555-1212") alicePhones[0]._set_by_field(phone_fields["number"], "555-1212")
alicePhones[0]._set_by_field(phone_fields["type"], "mobile") alicePhones[0]._set_by_field(phone_fields["type"], "mobile")
employment = alice._get_by_field(person_fields["employment"]) employment = alice._get_by_field(person_fields["employment"])
@@ -162,7 +161,7 @@ def test_addressbook_explicit_fields(addressbook):
bob._set_by_field(person_fields["id"], 456) bob._set_by_field(person_fields["id"], 456)
bob._set_by_field(person_fields["name"], "Bob") bob._set_by_field(person_fields["name"], "Bob")
bob._set_by_field(person_fields["email"], "bob@example.com") bob._set_by_field(person_fields["email"], "bob@example.com")
bobPhones = bob._init_by_field(person_fields["phones"], 2) bobPhones = bob.init("phones", 2)
bobPhones[0]._set_by_field(phone_fields["number"], "555-4567") bobPhones[0]._set_by_field(phone_fields["number"], "555-4567")
bobPhones[0]._set_by_field(phone_fields["type"], "home") bobPhones[0]._set_by_field(phone_fields["type"], "home")
bobPhones[1]._set_by_field(phone_fields["number"], "555-7654") bobPhones[1]._set_by_field(phone_fields["number"], "555-7654")
@@ -443,18 +442,6 @@ def test_build(all_types):
assert str(root) + "\n" == expectedText assert str(root) + "\n" == expectedText
def test_build_first_segment_size(all_types):
root = all_types.TestAllTypes.new_message(1)
init_all_types(root)
expectedText = open(os.path.join(this_dir, "all-types.txt"), "r", encoding="utf8").read()
assert str(root) + "\n" == expectedText
root = all_types.TestAllTypes.new_message(1024 * 1024)
init_all_types(root)
expectedText = open(os.path.join(this_dir, "all-types.txt"), "r", encoding="utf8").read()
assert str(root) + "\n" == expectedText
def test_binary_read(all_types): def test_binary_read(all_types):
f = open(os.path.join(this_dir, "all-types.binary"), "rb") f = open(os.path.join(this_dir, "all-types.binary"), "rb")
with all_types.TestAllTypes.from_bytes(f.read()) as reader: with all_types.TestAllTypes.from_bytes(f.read()) as reader:

View File

@@ -206,30 +206,24 @@ def test_set_dict_union(addressbook):
def test_union_enum(all_types): def test_union_enum(all_types):
assert all_types.UnionAllTypes.Union.UnionStructField1 == 0
assert all_types.UnionAllTypes.Union.UnionStructField2 == 1
msg = all_types.UnionAllTypes.new_message(**{"unionStructField1": {"textField": "foo"}}) msg = all_types.UnionAllTypes.new_message(**{"unionStructField1": {"textField": "foo"}})
assert msg.which == all_types.UnionAllTypes.Union.UnionStructField1
assert msg.which == "unionStructField1" assert msg.which == "unionStructField1"
assert msg.which == 0 assert msg.which == 0
msg = all_types.UnionAllTypes.new_message(**{"unionStructField2": {"textField": "foo"}}) msg = all_types.UnionAllTypes.new_message(**{"unionStructField2": {"textField": "foo"}})
assert msg.which == all_types.UnionAllTypes.Union.UnionStructField2
assert msg.which == "unionStructField2" assert msg.which == "unionStructField2"
assert msg.which == 1 assert msg.which == 1
assert all_types.GroupedUnionAllTypes.Union.G1 == 0
assert all_types.GroupedUnionAllTypes.Union.G2 == 1
msg = all_types.GroupedUnionAllTypes.new_message(**{"g1": {"unionStructField1": {"textField": "foo"}}}) msg = all_types.GroupedUnionAllTypes.new_message(**{"g1": {"unionStructField1": {"textField": "foo"}}})
assert msg.which == all_types.GroupedUnionAllTypes.Union.G1 assert msg.which() == "g1"
msg = all_types.GroupedUnionAllTypes.new_message(**{"g2": {"unionStructField2": {"textField": "foo"}}}) msg = all_types.GroupedUnionAllTypes.new_message(**{"g2": {"unionStructField2": {"textField": "foo"}}})
assert msg.which == all_types.GroupedUnionAllTypes.Union.G2 assert msg.which() == "g2"
msg = all_types.UnionAllTypes.new_message() msg = all_types.UnionAllTypes.new_message()
msg.unionStructField2 = msg.init(all_types.UnionAllTypes.Union.UnionStructField2) msg.init("unionStructField2")
assert msg.which() == "unionStructField2"
def isstr(s): def isstr(s):
@@ -258,14 +252,11 @@ def test_to_dict_verbose(addressbook):
assert person.to_dict(verbose=True)["phones"] == [] assert person.to_dict(verbose=True)["phones"] == []
if sys.version_info >= (2, 7):
assert person.to_dict(verbose=True, ordered=True)["phones"] == []
with pytest.raises(KeyError): with pytest.raises(KeyError):
assert person.to_dict()["phones"] == [] assert person.to_dict()["phones"] == []
def test_to_dict_ordered(addressbook): def test_to_dict_field_order(addressbook):
person = addressbook.Person.new_message( person = addressbook.Person.new_message(
**{ **{
"name": "Alice", "name": "Alice",
@@ -276,17 +267,7 @@ def test_to_dict_ordered(addressbook):
} }
) )
if sys.version_info >= (2, 7): assert list(person.to_dict().keys()) == ["id", "name", "email", "phones", "employment"]
assert list(person.to_dict(ordered=True).keys()) == [
"id",
"name",
"email",
"phones",
"employment",
]
else:
with pytest.raises(Exception):
person.to_dict(ordered=True)
def test_nested_list(addressbook): def test_nested_list(addressbook):