Fix up various bugs that arose when trying to cimport capnp

This commit is contained in:
Jason Paryani
2014-09-03 23:25:39 -07:00
parent b11e461a9e
commit 18fce3fe78
5 changed files with 98 additions and 33 deletions

View File

@@ -6,11 +6,11 @@ from non_circular cimport reraise_kj_exception
from cpython.ref cimport PyObject
cdef extern from "../helpers/fixMaybe.h":
cdef extern from "capnp/helpers/fixMaybe.h":
EnumSchema.Enumerant fixMaybe(Maybe[EnumSchema.Enumerant]) except +reraise_kj_exception
StructSchema.Field fixMaybe(Maybe[StructSchema.Field]) except +reraise_kj_exception
cdef extern from "../helpers/capabilityHelper.h":
cdef extern from "capnp/helpers/capabilityHelper.h":
# PyPromise evalLater(EventLoop &, PyObject * func)
# PyPromise there(EventLoop & loop, PyPromise & promise, PyObject * func, PyObject * error_func)
PyPromise then(PyPromise & promise, PyObject * func, PyObject * error_func)
@@ -24,7 +24,7 @@ cdef extern from "../helpers/capabilityHelper.h":
PyPromise convert_to_pypromise(VoidPromise&)
VoidPromise convert_to_voidpromise(PyPromise&)
cdef extern from "../helpers/rpcHelper.h":
cdef extern from "capnp/helpers/rpcHelper.h":
Capability.Client restoreHelper(RpcSystem&)
Capability.Client restoreHelper(RpcSystem&, MessageBuilder&)
Capability.Client restoreHelper(RpcSystem&, MessageReader&)
@@ -33,10 +33,10 @@ cdef extern from "../helpers/rpcHelper.h":
RpcSystem makeRpcClientWithRestorer(TwoPartyVatNetwork&, PyRestorer&)
PyPromise connectServer(TaskSet &, PyRestorer &, AsyncIoContext *, StringPtr)
cdef extern from "../helpers/serialize.h":
cdef extern from "capnp/helpers/serialize.h":
ByteArray messageToPackedBytes(MessageBuilder &, size_t wordCount)
cdef extern from "../helpers/asyncHelper.h":
cdef extern from "capnp/helpers/asyncHelper.h":
void waitNeverDone(WaitScope&)
Response * waitRemote(RemotePromise *, WaitScope&)
PyObject * waitPyPromise(PyPromise *, WaitScope&)

View File

@@ -1,20 +1,20 @@
from cpython.ref cimport PyObject
cdef extern from "../helpers/capabilityHelper.h":
cdef extern from "capnp/helpers/capabilityHelper.h":
cppclass PythonInterfaceDynamicImpl:
PythonInterfaceDynamicImpl(PyObject *)
cdef extern from "../helpers/capabilityHelper.h":
cdef extern from "capnp/helpers/capabilityHelper.h":
void reraise_kj_exception()
cdef cppclass PyRefCounter:
PyRefCounter(PyObject *)
cdef extern from "../helpers/rpcHelper.h":
cdef extern from "capnp/helpers/rpcHelper.h":
cdef cppclass PyRestorer:
PyRestorer(PyObject *)
cdef cppclass ErrorHandler:
pass
cdef extern from "../helpers/asyncHelper.h":
cdef extern from "capnp/helpers/asyncHelper.h":
cdef cppclass PyEventPort:
PyEventPort(PyObject *)

View File

@@ -1,7 +1,7 @@
# schema.capnp.cpp.pyx
# distutils: language = c++
# distutils: extra_compile_args = --std=c++11
cdef extern from "../helpers/checkCompiler.h":
cdef extern from "capnp/helpers/checkCompiler.h":
pass
from schema_cpp cimport Node, Data, StructNode, EnumNode, InterfaceNode, MessageBuilder, MessageReader

View File

@@ -6,6 +6,23 @@ from .capnp.includes.types cimport *
from .capnp.helpers.non_circular cimport reraise_kj_exception
from .capnp.helpers cimport helpers
cdef class _StructSchemaField:
cdef C_StructSchema.Field thisptr
cdef object _parent
cdef _init(self, C_StructSchema.Field other, parent=?)
cdef class _DynamicOrphan:
cdef C_DynamicOrphan thisptr
cdef public object _parent
cdef _init(self, C_DynamicOrphan other, object parent)
cdef C_DynamicOrphan move(self)
cpdef get(self)
cdef class _DynamicStructReader:
cdef C_DynamicStruct.Reader thisptr
cdef public object _parent
@@ -18,5 +35,50 @@ cdef class _DynamicStructReader:
cpdef _get(self, field)
cpdef _has(self, field)
cpdef _which(self)
cpdef _get_by_field(self, _StructSchemaField field)
cpdef _has_by_field(self, _StructSchemaField field)
cpdef as_builder(self, num_first_segment_words=?)
cdef class _DynamicStructBuilder:
cdef DynamicStruct_Builder thisptr
cdef public object _parent
cdef public bint is_root
cdef bint _is_written
cdef object _schema
cdef _init(self, DynamicStruct_Builder other, object parent, bint isRoot=?)
cdef _check_write(self)
cpdef to_bytes(_DynamicStructBuilder self)
cpdef _to_bytes_packed_helper(_DynamicStructBuilder self, word_count)
cpdef to_bytes_packed(_DynamicStructBuilder self)
cpdef _get(self, field)
cpdef _set(self, field, value)
cpdef _has(self, field)
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 init_resizable_list(self, field)
cpdef _which(self)
cpdef adopt(self, field, _DynamicOrphan orphan)
cpdef disown(self, field)
cpdef as_reader(self)
cpdef copy(self, num_first_segment_words=?)
cdef class _Schema:
cdef C_Schema thisptr
cdef _init(self, C_Schema other)
cpdef as_const_value(self)
cpdef as_struct(self)
cpdef as_interface(self)
cpdef as_enum(self)
cpdef get_dependency(self, id)
cpdef get_proto(self)

View File

@@ -2,6 +2,7 @@
# distutils: language = c++
# distutils: extra_compile_args = --std=c++11
# distutils: libraries = capnpc capnp capnp-rpc
# distutils: include_dirs = .
# cython: c_string_type = str
# cython: c_string_encoding = default
# cython: embedsignature = True
@@ -874,15 +875,15 @@ cdef class _DynamicStructReader:
return to_python_reader(self.thisptr.get(field), self._parent)
def __getattr__(self, field):
return to_python_reader(self.thisptr.get(field), self._parent)
return self._get(field)
def _get_by_field(self, _StructSchemaField field):
cpdef _get_by_field(self, _StructSchemaField field):
return to_python_reader(self.thisptr.getByField(field.thisptr), self._parent)
cpdef _has(self, field):
return self.thisptr.has(field)
def _has_by_field(self, _StructSchemaField field):
cpdef _has_by_field(self, _StructSchemaField field):
return self.thisptr.hasByField(field.thisptr)
cpdef _which(self):
@@ -965,11 +966,6 @@ cdef class _DynamicStructBuilder:
setattr(person, 'field-with-hyphens', 'foo') # for names that are invalid for python, use setattr
print getattr(person, 'field-with-hyphens') # for names that are invalid for python, use getattr
"""
cdef DynamicStruct_Builder thisptr
cdef public object _parent
cdef public bint is_root
cdef bint _is_written
cdef object _schema
cdef _init(self, DynamicStruct_Builder other, object parent, bint isRoot = False):
self.thisptr = other
self._parent = parent
@@ -1061,23 +1057,21 @@ cdef class _DynamicStructBuilder:
cpdef _get(self, field):
return to_python_builder(self.thisptr.get(field), self._parent)
def _get_by_field(self, _StructSchemaField field):
cpdef _get_by_field(self, _StructSchemaField field):
return to_python_builder(self.thisptr.getByField(field.thisptr), self._parent)
def __getattr__(self, field):
cdef C_DynamicValue.Builder value = self.thisptr.get(field)
return to_python_builder(value, self._parent)
return self._get(field)
cpdef _set(self, field, value):
_setDynamicField(self.thisptr, field, value, self._parent)
def _set_by_field(self, _StructSchemaField field, value):
cpdef _set_by_field(self, _StructSchemaField field, value):
# TODO: make this faster
_setDynamicField(self.thisptr, field.proto.name, value, self._parent)
def __setattr__(self, field, value):
_setDynamicField(self.thisptr, field, value, self._parent)
self._set(field, value)
cpdef _has(self, field):
return self.thisptr.has(field)
@@ -1105,7 +1099,7 @@ cdef class _DynamicStructBuilder:
else:
return to_python_builder(self.thisptr.init(field, size), self._parent)
def _init_by_field(self, _StructSchemaField field, size=None):
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.
@@ -1295,8 +1289,6 @@ cdef class _DynamicStructPipeline:
return _to_dict(self, verbose)
cdef class _DynamicOrphan:
cdef C_DynamicOrphan thisptr
cdef public object _parent
cdef _init(self, C_DynamicOrphan other, object parent):
self.thisptr = moveOrphan(other)
self._parent = parent
@@ -2146,7 +2138,6 @@ cdef class PromiseFulfillerPair:
deref(deref(self.thisptr).fulfiller).fulfill()
cdef class _Schema:
cdef C_Schema thisptr
cdef _init(self, C_Schema other):
self.thisptr = other
return self
@@ -2176,13 +2167,16 @@ cdef class _Schema:
cdef class _StructSchema:
cdef C_StructSchema thisptr
cdef object __fieldnames, __union_fields, __non_union_fields, __fields
cdef object __fieldnames, __union_fields, __non_union_fields, __fields, __getters
cdef list __fields_list
cdef _init(self, C_StructSchema other):
self.thisptr = other
self.__fieldnames = None
self.__union_fields = None
self.__non_union_fields = None
self.__fields = None
self.__fields_list = None
self.__getters = None
return self
property fieldnames:
@@ -2219,7 +2213,7 @@ cdef class _StructSchema:
return self.__non_union_fields
property fields:
"""A tuple of the field names in the struct."""
"""All of the _StructSchemaField in this schema as a dict"""
def __get__(self):
if self.__fields is not None:
return self.__fields
@@ -2229,6 +2223,17 @@ cdef class _StructSchema:
for i in xrange(nfields)}
return self.__fields
property fields_list:
"""All of the _StructSchemaField in this schema as a list"""
def __get__(self):
if self.__fields_list is not None:
return self.__fields_list
fieldlist = self.thisptr.getFields()
nfields = fieldlist.size()
self.__fields_list = [_StructSchemaField()._init(fieldlist[i], self)
for i in xrange(nfields)]
return self.__fields_list
property node:
"""The raw schema node"""
def __get__(self):
@@ -2249,8 +2254,6 @@ cdef class _StructSchema:
return '<schema for %s>' % self.node.displayName
cdef class _StructSchemaField:
cdef C_StructSchema.Field thisptr
cdef object _parent
cdef _init(self, C_StructSchema.Field other, parent=None):
self.thisptr = other
self._parent = parent