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

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

View File

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

View File

@@ -1,18 +1,4 @@
from cpython.ref cimport PyObject, Py_INCREF, Py_DECREF
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
ctypedef unsigned int uint

View File

@@ -5,7 +5,7 @@ from capnp.includes cimport schema_cpp
from capnp.includes.capnp_cpp cimport (
Schema as C_Schema, StructSchema as C_StructSchema,
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,
String, StringTree, DynamicStruct_Builder
)
@@ -47,8 +47,7 @@ cdef class _DynamicStructReader:
cpdef _DynamicEnumField _which(self)
cpdef _which_str(self)
cpdef _get_by_field(self, _StructSchemaField field)
cpdef _has_by_field(self, _StructSchemaField field)
cpdef as_builder(self, num_first_segment_words=?)
cpdef as_builder(self)
cdef class _DynamicStructBuilder:
@@ -68,12 +67,10 @@ cdef class _DynamicStructBuilder:
cpdef init(self, field, size=?)
cpdef _get_by_field(self, _StructSchemaField field)
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 _which_str(self)
cpdef as_reader(self)
cpdef copy(self, num_first_segment_words=?)
cpdef copy(self)
cdef class _DynamicEnumField:
cdef object thisptr
@@ -115,10 +112,9 @@ cdef class _MessageBuilder:
cpdef set_root(self, value)
cdef to_python_reader(C_DynamicValue.Reader 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_tuple(_DynamicListBuilder msg, tuple d)
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

View File

@@ -21,7 +21,6 @@ from libc.string cimport memcpy
from libcpp.utility cimport move
import collections as _collections
import contextlib
import base64
import enum as _enum
@@ -164,11 +163,6 @@ cdef schema_cpp.ReaderOptions make_reader_opts(traversal_limit_in_words, nesting
return opts
ctypedef fused _DynamicStructReaderOrBuilder:
_DynamicStructReader
_DynamicStructBuilder
ctypedef fused _DynamicSetterClasses:
C_DynamicList.Builder
DynamicStruct_Builder
@@ -627,119 +621,56 @@ cdef _setDynamicFieldWithField(DynamicStruct_Builder thisptr, _StructSchemaField
# 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 _DynamicListReader temp_list_r
cdef _DynamicStructBuilder temp_msg_b
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)
if msg_type is _DynamicListBuilder:
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:
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:
temp_msg_b = msg
if ordered:
ret = _collections.OrderedDict()
else:
ret = {}
ret = {}
try:
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:
pass
for field in temp_msg_b.schema.non_union_fields:
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
elif msg_type is _DynamicStructReader:
temp_msg_r = msg
if ordered:
ret = _collections.OrderedDict()
else:
ret = {}
ret = {}
try:
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:
pass
for field in temp_msg_r.schema.non_union_fields:
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
if isinstance(msg, (_DynamicStructBuilder, _DynamicStructReader)):
return msg.to_dict(verbose, ordered)
return msg.to_dict(verbose)
if msg_type is _DynamicEnum:
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
@@ -890,9 +821,6 @@ cdef class _DynamicStructReader:
cpdef _has(self, field):
return self.thisptr.has(field)
cpdef _has_by_field(self, _StructSchemaField field):
return self.thisptr.hasByField(field.thisptr)
cpdef _which_str(self):
try:
return <char *>helpers.fixMaybe(self.thisptr.which()).getProto().getName().cStr()
@@ -946,21 +874,18 @@ cdef class _DynamicStructReader:
def __repr__(self):
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):
return _to_dict(self, verbose, ordered, encode_bytes_as_base64)
def to_dict(self, verbose=False):
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
This is a copying operation with respect to the message's buffer.
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`
"""
builder = _MallocMessageBuilder(num_first_segment_words)
builder = _MallocMessageBuilder()
return builder.set_root(self)
property total_size:
@@ -1055,9 +980,6 @@ cdef class _DynamicStructBuilder:
cpdef _has(self, 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):
"""Method for initializing fields that are of type union/struct/list
@@ -1082,28 +1004,6 @@ cdef class _DynamicStructBuilder:
ptr = self.thisptr.init(field, size)
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):
try:
return <char *>helpers.fixMaybe(self.thisptr.which()).getProto().getName().cStr()
@@ -1155,18 +1055,15 @@ cdef class _DynamicStructBuilder:
reader._obj_to_pin = self
return reader
cpdef copy(self, num_first_segment_words=None):
cpdef copy(self):
"""A method for copying this Builder
This is a copying operation with respect to the message's buffer.
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`
"""
builder = _MallocMessageBuilder(num_first_segment_words)
builder = _MallocMessageBuilder()
return builder.set_root(self)
property schema:
@@ -1185,8 +1082,8 @@ cdef class _DynamicStructBuilder:
def __repr__(self):
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):
return _to_dict(self, verbose, ordered, encode_bytes_as_base64)
def to_dict(self, verbose=False):
return _to_dict(self, verbose)
def from_dict(self, dict d):
for key, val in d.iteritems():
@@ -1415,15 +1312,9 @@ cdef class _ParsedSchema(_Schema):
return _ParsedSchema()._init_child(self.thisptr_child.getNested(name))
class _StructABCMeta(type):
"""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 _new_message(self, kwargs):
cdef _MessageBuilder builder
builder = _MallocMessageBuilder(num_first_segment_words)
builder = _MallocMessageBuilder()
msg = builder.init_root(self.schema)
if kwargs is not None:
msg.from_dict(kwargs)
@@ -1458,14 +1349,6 @@ class _StructModule(object):
sub_module = _StructModuleWhich("StructModuleWhich", mapping)
setattr(sub_module, 'schema', raw_schema)
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):
"""Returns an iterable, that when traversed will return Readers for messages.
@@ -1485,7 +1368,7 @@ class _StructModule(object):
return reader
@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.
:type buf: buffer
@@ -1498,34 +1381,22 @@ class _StructModule(object):
:type nesting_limit: int
:param nesting_limit: Limits how many total words of data are allowed to be traversed. Default is 64.
:type builder: bool
:param builder: If true, return a builder object.
Enabling `builder` returns a writable copy of the message.
:rtype: :class:`_DynamicStructReader` or :class:`_DynamicStructBuilder`
:rtype: :class:`_DynamicStructReader`
"""
message = None
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)
yield message.get_root(self.schema)
message = _FlatArrayMessageReader(buf, traversal_limit_in_words, nesting_limit)
yield message.get_root(self.schema)
finally:
if message:
message.close()
def __call__(self, num_first_segment_words=None, **kwargs):
return self.new_message(num_first_segment_words=num_first_segment_words, **kwargs)
def __call__(self, **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.
: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
:param kwargs: A list of fields and their values to initialize in the struct.
@@ -1534,7 +1405,7 @@ class _StructModule(object):
:rtype: :class:`_DynamicStructBuilder`
"""
return _new_message(self, kwargs, num_first_segment_words)
return _new_message(self, kwargs)
class _EnumModule(object):
@@ -1641,27 +1512,6 @@ cdef class SchemaParser:
if proto.isStruct:
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
elif proto.isConst:
module.__dict__[node.name] = schema.as_const_value()
@@ -1794,11 +1644,8 @@ cdef class _MallocMessageBuilder(_MessageBuilder):
...
data = person.to_bytes()
"""
def __init__(self, size=None):
if size is None:
self.thisptr = new schema_cpp.MallocMessageBuilder()
else:
self.thisptr = new schema_cpp.MallocMessageBuilder(size)
def __init__(self):
self.thisptr = new schema_cpp.MallocMessageBuilder()
cdef class _MessageReader: