From 41140b3d5fd1867e98991c5b1015201b19f88b1b Mon Sep 17 00:00:00 2001 From: Lasse Blaauwbroek Date: Fri, 13 Oct 2023 11:33:28 +0200 Subject: [PATCH] Upgrade Cython to version 3 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit While looking at #333, I hypothesized that upgrading Cython might solve the issue. It didn't. But upgrading should still happen at some point. This is my work in progress on that. The tests pass, but there are two main things missing: Problem (1): Starting with Cython 3, you can only do `except+` or `except +reraise_kj_exception` on `extern` functions coming from C++. (This makes sense, and the way things were declared in Pycapnp wasn't too good.) As a result, I had to remove a lot of these declaration. This results in some segmentation faults, because Cython no longer detects C++ exceptions and converts them to Python exceptions in some places. To solve this, all `extern` declarations in `.pxd` files have to be examined and `except +reraise_kj_exception` clauses need to be added to anything that might throw. Previously, this was done really inconsistently. The lazy solution would be to just add the clause everywhere, but I'm not sure what the performance implications are. Problem (2): The compilation output of `python setup.py build_ext --inplace` is now full of messages like these: ``` capnp/lib/capnp.cpp: In function ‘PyObject* __pyx_f_5capnp_3lib_5capnp_18_DynamicListReader__get(__pyx_obj_5capnp_3lib_5capnp__DynamicListReader*, int64_t, int)’: capnp/lib/capnp.cpp:4871:51: warning: moving a temporary object prevents copy elision [-Wpessimizing-move] 4871 | #define __PYX_STD_MOVE_IF_SUPPORTED(x) std::move(x) | ~~~~~~~~~^~~ capnp/lib/capnp.cpp:20944:59: note: in expansion of macro ‘__PYX_STD_MOVE_IF_SUPPORTED’ 20944 | __pyx_t_2 = __pyx_f_5capnp_3lib_5capnp_to_python_reader(__PYX_STD_MOVE_IF_SUPPORTED((( ::capnp::DynamicValue::Reader)__pyx_t_7)), __pyx_t_1); if (unlikely(!__pyx_t_2)) __PYX_ERR(0, 419, __pyx_L1_error) | ^~~~~~~~~~~~~~~~~~~~~~~~~~~ capnp/lib/capnp.cpp:4871:51: note: remove ‘std::move’ call 4871 | #define __PYX_STD_MOVE_IF_SUPPORTED(x) std::move(x) | ~~~~~~~~~^~~ capnp/lib/capnp.cpp:20944:59: note: in expansion of macro ‘__PYX_STD_MOVE_IF_SUPPORTED’ 20944 | __pyx_t_2 = __pyx_f_5capnp_3lib_5capnp_to_python_reader(__PYX_STD_MOVE_IF_SUPPORTED((( ::capnp::DynamicValue::Reader)__pyx_t_7)), __pyx_t_1); if (unlikely(!__pyx_t_2)) __PYX_ERR(0, 419, __pyx_L1_error) | ^~~~~~~~~~~~~~~~~~~~~~~~~~~ ``` There are to many `move` calls inserted. I'm not sure if this is a Cython issue, or if we are somehow annotating things wrong. Might be worth asking the Cython people. I'm not planning on working on this further in the short term. If someone wants to take over on this, feel free. --- capnp/helpers/helpers.pxd | 9 +-- capnp/helpers/rpcHelper.h | 8 +-- capnp/includes/capnp_cpp.pxd | 22 ++----- capnp/includes/schema_cpp.pxd | 12 ++-- capnp/lib/capnp.pxd | 24 ++++---- capnp/lib/capnp.pyx | 106 +++++++++++++++++----------------- pyproject.toml | 2 +- requirements.txt | 2 +- tox.ini | 2 +- 9 files changed, 84 insertions(+), 103 deletions(-) diff --git a/capnp/helpers/helpers.pxd b/capnp/helpers/helpers.pxd index dde61f00..fe98ebf3 100644 --- a/capnp/helpers/helpers.pxd +++ b/capnp/helpers/helpers.pxd @@ -16,20 +16,17 @@ cdef extern from "capnp/helpers/fixMaybe.h": cdef extern from "capnp/helpers/capabilityHelper.h": PyPromise then(PyPromise promise, Own[PyRefCounter] func, Own[PyRefCounter] error_func) - DynamicCapability.Client new_client(InterfaceSchema&, PyObject *) - DynamicValue.Reader new_server(InterfaceSchema&, PyObject *) - Capability.Client server_to_client(InterfaceSchema&, PyObject *) PyPromise convert_to_pypromise(RemotePromise) PyPromise convert_to_pypromise(VoidPromise) VoidPromise taskToPromise(Own[PyRefCounter] coroutine, PyObject* callback) void init_capnp_api() cdef extern from "capnp/helpers/rpcHelper.h": - Capability.Client bootstrapHelper(RpcSystem&) - Capability.Client bootstrapHelperServer(RpcSystem&) + Own[Capability.Client] bootstrapHelper(RpcSystem&) except +reraise_kj_exception + Own[Capability.Client] bootstrapHelperServer(RpcSystem&) except +reraise_kj_exception cdef extern from "capnp/helpers/serialize.h": - ByteArray messageToPackedBytes(MessageBuilder &, size_t wordCount) + ByteArray messageToPackedBytes(MessageBuilder &, size_t wordCount) except +reraise_kj_exception cdef extern from "capnp/helpers/deserialize.h": Node.Reader toReader(DynamicStruct.Reader reader) except +reraise_kj_exception diff --git a/capnp/helpers/rpcHelper.h b/capnp/helpers/rpcHelper.h index 0681039c..c017ba3a 100644 --- a/capnp/helpers/rpcHelper.h +++ b/capnp/helpers/rpcHelper.h @@ -6,16 +6,16 @@ #include "Python.h" #include "capabilityHelper.h" -capnp::Capability::Client bootstrapHelper(capnp::RpcSystem& client) { +kj::Own bootstrapHelper(capnp::RpcSystem& client) { capnp::MallocMessageBuilder hostIdMessage(8); auto hostId = hostIdMessage.initRoot(); hostId.setSide(capnp::rpc::twoparty::Side::SERVER); - return client.bootstrap(hostId); + return kj::heap(client.bootstrap(hostId)); } -capnp::Capability::Client bootstrapHelperServer(capnp::RpcSystem& client) { +kj::Own bootstrapHelperServer(capnp::RpcSystem& client) { capnp::MallocMessageBuilder hostIdMessage(8); auto hostId = hostIdMessage.initRoot(); hostId.setSide(capnp::rpc::twoparty::Side::CLIENT); - return client.bootstrap(hostId); + return kj::heap(client.bootstrap(hostId)); } diff --git a/capnp/includes/capnp_cpp.pxd b/capnp/includes/capnp_cpp.pxd index 335b1726..9e559b79 100644 --- a/capnp/includes/capnp_cpp.pxd +++ b/capnp/includes/capnp_cpp.pxd @@ -57,26 +57,12 @@ cdef extern from "kj/async.h" namespace " ::kj": cdef cppclass Promise[T] nogil: Promise(Promise) Promise(T) - T wait(WaitScope) except +reraise_kj_exception - bool poll(WaitScope) except +reraise_kj_exception - # ForkedPromise fork() - # Promise exclusiveJoin(Promise&& other) - # Promise[T] eagerlyEvaluate() - # void detach(ErrorFunc) String trace() Promise[T] attach(Own[PyRefCounter] &) Promise[T] attach(Own[PyRefCounter] &, Own[PyRefCounter] &) Promise[T] attach(Own[PyRefCounter] &, Own[PyRefCounter] &, Own[PyRefCounter] &) Promise[T] attach(Own[PyRefCounter] &, Own[PyRefCounter] &, Own[PyRefCounter] &, Own[PyRefCounter] &) - cdef cppclass Canceler nogil: - Canceler() - Promise[T] wrap[T](Promise[T] promise) - void cancel(StringPtr cancelReason) - void cancel(Exception& exception) - void release() - bool isEmpty() - ctypedef Promise[Own[PyRefCounter]] PyPromise ctypedef Promise[void] VoidPromise @@ -109,8 +95,8 @@ cdef extern from "kj/array.h" namespace " ::kj": cdef extern from "kj/async-io.h" namespace " ::kj": cdef cppclass AsyncIoStream nogil: - Promise[size_t] read(void*, size_t, size_t) - Promise[void] write(const void*, size_t) + Promise[size_t] read(void*, size_t, size_t) except +reraise_kj_exception + Promise[void] write(const void*, size_t) except +reraise_kj_exception cdef extern from "capnp/schema.capnp.h" namespace " ::capnp": enum TypeWhich" ::capnp::schema::Type::Which": @@ -297,7 +283,7 @@ cdef extern from "capnp/dynamic.h" namespace " ::capnp": Client() Client(Client&) Client(Own[PythonInterfaceDynamicImpl]) - Client upcast(InterfaceSchema requestedSchema) + Client upcast(InterfaceSchema requestedSchema) except +reraise_kj_exception DynamicCapability.Client castAs"castAs< ::capnp::DynamicCapability>"(InterfaceSchema) InterfaceSchema getSchema() Request newRequest(char * methodName) @@ -447,7 +433,7 @@ cdef extern from "capnp/dynamic.h" namespace " ::capnp": Type getType() cdef extern from "capnp/schema-loader.h" namespace " ::capnp": - cdef cppclass SchemaLoader: + cdef cppclass SchemaLoader nogil: SchemaLoader() Schema load(Node.Reader reader) except +reraise_kj_exception Schema get(uint64_t id_) except +reraise_kj_exception diff --git a/capnp/includes/schema_cpp.pxd b/capnp/includes/schema_cpp.pxd index e5520890..6f8d559a 100644 --- a/capnp/includes/schema_cpp.pxd +++ b/capnp/includes/schema_cpp.pxd @@ -673,7 +673,7 @@ cdef extern from "capnp/message.h" namespace " ::capnp": Annotation.Builder getRootAnnotation'getRoot< ::capnp::schema::Annotation>'() Annotation.Builder initRootAnnotation'initRoot< ::capnp::schema::Annotation>'() - DynamicStruct_Builder getRootDynamicStruct'getRoot< ::capnp::DynamicStruct>'(StructSchema) + DynamicStruct_Builder getRootDynamicStruct'getRoot< ::capnp::DynamicStruct>'(StructSchema) except +reraise_kj_exception DynamicStruct_Builder initRootDynamicStruct'initRoot< ::capnp::DynamicStruct>'(StructSchema) void setRootDynamicStruct'setRoot< ::capnp::DynamicStruct::Reader>'(DynamicStruct.Reader) @@ -696,7 +696,7 @@ cdef extern from "capnp/message.h" namespace " ::capnp": StructNode.Reader getRootStructNode'getRoot< ::capnp::schema::StructNode>'() Annotation.Reader getRootAnnotation'getRoot< ::capnp::schema::Annotation>'() - DynamicStruct.Reader getRootDynamicStruct'getRoot< ::capnp::DynamicStruct>'(StructSchema) + DynamicStruct.Reader getRootDynamicStruct'getRoot< ::capnp::DynamicStruct>'(StructSchema) except +reraise_kj_exception AnyPointer.Reader getRootAnyPointer'getRoot< ::capnp::AnyPointer>'() cdef cppclass MallocMessageBuilder(MessageBuilder) nogil: @@ -798,7 +798,7 @@ cdef extern from "capnp/serialize.h" namespace " ::capnp": FlatArrayMessageReader(WordArrayPtr array, ReaderOptions) except +reraise_kj_exception const word* getEnd() const - void writeMessageToFd(int, MessageBuilder&) nogil except +reraise_kj_exception + void writeMessageToFd(int, MessageBuilder&) except +reraise_kj_exception nogil WordArray messageToFlatArray(MessageBuilder &) nogil @@ -816,6 +816,6 @@ cdef extern from "capnp/serialize-packed.h" namespace " ::capnp": PackedFdMessageReader(int) except +reraise_kj_exception PackedFdMessageReader(int, ReaderOptions) except +reraise_kj_exception - void writePackedMessage(BufferedOutputStream&, MessageBuilder&) nogil except +reraise_kj_exception - void writePackedMessage(OutputStream&, MessageBuilder&) nogil except +reraise_kj_exception - void writePackedMessageToFd(int, MessageBuilder&) nogil except +reraise_kj_exception + void writePackedMessage(BufferedOutputStream&, MessageBuilder&) except +reraise_kj_exception nogil + void writePackedMessage(OutputStream&, MessageBuilder&) except +reraise_kj_exception nogil + void writePackedMessageToFd(int, MessageBuilder&) except +reraise_kj_exception nogil diff --git a/capnp/lib/capnp.pxd b/capnp/lib/capnp.pxd index 1ddd6b8a..bb235a31 100644 --- a/capnp/lib/capnp.pxd +++ b/capnp/lib/capnp.pxd @@ -28,7 +28,7 @@ cdef class _StringArrayPtr: cdef StringPtr * thisptr cdef object parent cdef size_t size - cdef ArrayPtr[StringPtr] asArrayPtr(self) except +reraise_kj_exception + cdef ArrayPtr[StringPtr] asArrayPtr(self) cdef class SchemaLoader: cdef C_SchemaLoader * thisptr @@ -38,7 +38,7 @@ cdef class SchemaParser: cdef public dict modules_by_id cdef list _all_imports cdef _StringArrayPtr _last_import_array - cpdef _parse_disk_file(self, displayName, diskPath, imports) except +reraise_kj_exception + cpdef _parse_disk_file(self, displayName, diskPath, imports) cdef class _DynamicOrphan: cdef C_DynamicOrphan thisptr @@ -79,10 +79,10 @@ cdef class _DynamicStructBuilder: cdef _init(self, DynamicStruct_Builder other, object parent, bint isRoot=?, bint tryRegistry=?) cdef _check_write(self) - cpdef to_bytes(_DynamicStructBuilder self) except +reraise_kj_exception - cpdef to_segments(_DynamicStructBuilder self) except +reraise_kj_exception - cpdef _to_bytes_packed_helper(_DynamicStructBuilder self, word_count) except +reraise_kj_exception - cpdef to_bytes_packed(_DynamicStructBuilder self) except +reraise_kj_exception + cpdef to_bytes(_DynamicStructBuilder self) + cpdef to_segments(_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) @@ -128,7 +128,7 @@ cdef class _DynamicEnum: cdef public object _parent cdef _init(self, capnp.DynamicEnum other, object parent) - cpdef _as_str(self) except +reraise_kj_exception + cpdef _as_str(self) cdef class _DynamicListBuilder: cdef C_DynamicList.Builder thisptr @@ -146,11 +146,11 @@ cdef class _DynamicListBuilder: cdef class _MessageBuilder: cdef schema_cpp.MessageBuilder * thisptr cpdef init_root(self, schema) - cpdef get_root(self, schema) except +reraise_kj_exception - cpdef get_root_as_any(self) except +reraise_kj_exception - cpdef set_root(self, value) except +reraise_kj_exception - cpdef get_segments_for_output(self) except +reraise_kj_exception - cpdef new_orphan(self, schema) except +reraise_kj_exception + cpdef get_root(self, schema) + cpdef get_root_as_any(self) + cpdef set_root(self, value) + cpdef get_segments_for_output(self) + cpdef new_orphan(self, schema) cdef to_python_reader(C_DynamicValue.Reader self, object parent) cdef to_python_builder(C_DynamicValue.Builder self, object parent) diff --git a/capnp/lib/capnp.pyx b/capnp/lib/capnp.pyx index dbb92b74..850f9bbc 100644 --- a/capnp/lib/capnp.pyx +++ b/capnp/lib/capnp.pyx @@ -10,11 +10,12 @@ cimport cython # noqa: E402 from capnp.helpers.helpers cimport init_capnp_api -from capnp.includes.capnp_cpp cimport AsyncIoStream, WaitScope, PyPromise, VoidPromise, EventPort, EventLoop, Canceler, PyAsyncIoStream, PromiseFulfiller, VoidPromiseFulfiller, tryReadMessage, writeMessage, makeException, PythonInterfaceDynamicImpl +from capnp.includes.capnp_cpp cimport AsyncIoStream, WaitScope, PyPromise, VoidPromise, EventPort, EventLoop, PyAsyncIoStream, PromiseFulfiller, VoidPromiseFulfiller, tryReadMessage, writeMessage, makeException, PythonInterfaceDynamicImpl from capnp.includes.schema_cpp cimport (MessageReader,) -from cpython cimport array, Py_buffer, PyObject_CheckBuffer, memoryview, buffer -from cpython.buffer cimport PyBUF_SIMPLE, PyBUF_WRITABLE +from cpython cimport array, Py_buffer, PyObject_CheckBuffer +from cpython.buffer cimport PyBUF_SIMPLE, PyBUF_WRITABLE, PyBUF_WRITE, PyBUF_READ +from cpython.memoryview cimport PyMemoryView_FromMemory from cpython.exc cimport PyErr_Clear from cython.operator cimport dereference as deref from libc.stdlib cimport malloc, free @@ -120,10 +121,10 @@ def fill_context(method_name, context, returned_data): for arg_name, arg_val in zip(names, returned_data): setattr(results, arg_name, arg_val) -cdef api VoidPromise * call_server_method(object server, +cdef api Promise[void]* call_server_method(object server, char * _method_name, CallContext & _context, - object _kj_loop) except * with gil: + object _kj_loop) except* with gil: method_name = _method_name kj_loop = <_EventLoop>_kj_loop kj_loop.check() @@ -290,7 +291,7 @@ cdef api object get_exception_info(object exc_type, object exc_obj, object exc_t return (b'', 0, b"Couldn't determine python exception") -cdef schema_cpp.ReaderOptions make_reader_opts(traversal_limit_in_words, nesting_limit) with gil: +cdef schema_cpp.ReaderOptions make_reader_opts(traversal_limit_in_words, nesting_limit): cdef schema_cpp.ReaderOptions opts if traversal_limit_in_words is not None: opts.traversalLimitInWords = traversal_limit_in_words @@ -992,7 +993,7 @@ cdef class _DynamicEnum: self._parent = parent return self - cpdef _as_str(self) except +reraise_kj_exception: + cpdef _as_str(self): return helpers.fixMaybe(self.thisptr.getEnumerant()).getProto().getName().cStr() property raw: @@ -1302,7 +1303,7 @@ cdef class _DynamicStructBuilder: _write_packed_message_to_fd(file.fileno(), self._parent) self._is_written = True - cpdef to_bytes(_DynamicStructBuilder self) except +reraise_kj_exception: + cpdef to_bytes(_DynamicStructBuilder self): """Returns the struct's containing message as a Python bytes object in the unpacked binary format. This is inefficient; it makes several copies. @@ -1319,7 +1320,7 @@ cdef class _DynamicStructBuilder: self._is_written = True return ret - cpdef to_segments(_DynamicStructBuilder self) except +reraise_kj_exception: + cpdef to_segments(_DynamicStructBuilder self): """Returns the struct's containing message as a Python list of Python bytes objects. This avoids making copies. @@ -1333,14 +1334,14 @@ cdef class _DynamicStructBuilder: segments = builder.get_segments_for_output() return segments - cpdef _to_bytes_packed_helper(_DynamicStructBuilder self, word_count) except +reraise_kj_exception: + cpdef _to_bytes_packed_helper(_DynamicStructBuilder self, word_count): cdef _MessageBuilder builder = self._parent array = helpers.messageToPackedBytes(deref(builder.thisptr), word_count) cdef const char* ptr = array.begin() cdef bytes ret = ptr[:array.size()] return ret - cpdef to_bytes_packed(_DynamicStructBuilder self) except +reraise_kj_exception: + cpdef to_bytes_packed(_DynamicStructBuilder self): self._check_write() word_count = self.total_size.word_count + 2 @@ -1610,7 +1611,7 @@ cdef class _DynamicStructPipeline: def __dealloc__(self): del self.thisptr - cpdef _get(self, field) except +reraise_kj_exception: + cpdef _get(self, field): cdef int type = (self.thisptr.get(field)).getType() if type == capnp.TYPE_CAPABILITY: return _DynamicCapabilityClient()._init( @@ -1680,7 +1681,7 @@ cdef class _DynamicObjectReader: self._parent = parent return self - cpdef as_struct(self, schema) except +reraise_kj_exception: + cpdef as_struct(self, schema): cdef _StructSchema s if hasattr(schema, 'schema'): s = schema.schema @@ -1689,7 +1690,7 @@ cdef class _DynamicObjectReader: return _DynamicStructReader()._init(self.thisptr.getAs(s._thisptr()), self._parent) - cpdef as_interface(self, schema) except +reraise_kj_exception: + cpdef as_interface(self, schema): cdef _InterfaceSchema s if hasattr(schema, 'schema'): s = schema.schema @@ -1698,7 +1699,7 @@ cdef class _DynamicObjectReader: return _DynamicCapabilityClient()._init(self.thisptr.getAsCapability(s.thisptr), self._parent) - cpdef as_list(self, schema) except +reraise_kj_exception: + cpdef as_list(self, schema): cdef _ListSchema s if hasattr(schema, 'schema'): s = schema.schema @@ -1707,7 +1708,7 @@ cdef class _DynamicObjectReader: return _DynamicListReader()._init(self.thisptr.getAsList(s.thisptr), self._parent) - cpdef as_text(self) except +reraise_kj_exception: + cpdef as_text(self): return (self.thisptr.getAsText().cStr())[:] @@ -1723,7 +1724,7 @@ cdef class _DynamicObjectBuilder: def __dealloc__(self): del self.thisptr - cpdef as_struct(self, schema) except +reraise_kj_exception: + cpdef as_struct(self, schema): cdef _StructSchema s if hasattr(schema, 'schema'): s = schema.schema @@ -1732,7 +1733,7 @@ cdef class _DynamicObjectBuilder: return _DynamicStructBuilder()._init(self.thisptr.getAs(s._thisptr()), self._parent) - cpdef as_interface(self, schema) except +reraise_kj_exception: + cpdef as_interface(self, schema): cdef _InterfaceSchema s if hasattr(schema, 'schema'): s = schema.schema @@ -1741,7 +1742,7 @@ cdef class _DynamicObjectBuilder: return _DynamicCapabilityClient()._init(self.thisptr.getAsCapability(s.thisptr), self._parent) - cpdef as_list(self, schema) except +reraise_kj_exception: + cpdef as_list(self, schema): cdef _ListSchema s if hasattr(schema, 'schema'): s = schema.schema @@ -1767,13 +1768,13 @@ cdef class _DynamicObjectBuilder: return _DynamicListBuilder()._init(self.thisptr.initAsList(s.thisptr, size), self._parent) - cpdef as_text(self) except +reraise_kj_exception: + cpdef as_text(self): return (self.thisptr.getAsText().cStr())[:] cpdef as_reader(self): return _DynamicObjectReader()._init(self.thisptr.asReader(), self._parent) -cdef void kjloop_runnable_callback(void* data) with gil: +cdef kjloop_runnable_callback(void* data): cdef AsyncIoEventPort *port = data assert port.runHandle is not None port.kjLoop.run() @@ -1823,7 +1824,7 @@ cdef class _EventLoop: cdef object active_tasks cdef cbool closed - cdef _init(self, asyncio_loop) except +reraise_kj_exception: + cdef _init(self, asyncio_loop): self.event_port = capnp.heap[AsyncIoEventPort](asyncio_loop) kj_loop = deref(self.event_port).getKjLoop() self.wait_scope = capnp.heap[WaitScope](deref(kj_loop)) @@ -1941,7 +1942,7 @@ cdef class _Promise: self.thisptr = capnp.heap[PyPromise](move(other)) return self - cpdef cancel(self) except +reraise_kj_exception: + cpdef cancel(self): self.thisptr = Own[PyPromise]() @@ -1956,7 +1957,7 @@ cdef class _RemotePromise: self._parent = parent return self - cdef void _check_consumed(self) except*: + cdef _check_consumed(self): if self.thisptr.get() == NULL: raise KjException( "Promise was already used in a consuming operation. You can no longer use this Promise object") @@ -1969,7 +1970,7 @@ cdef class _RemotePromise: .attach(capnp.heap[PyRefCounter](self._parent)) ).__await__() - cpdef _get(self, field) except +reraise_kj_exception: + cpdef _get(self, field): self._check_consumed() cdef int type = (self.thisptr.get().get(field)).getType() if type == capnp.TYPE_CAPABILITY: @@ -2002,7 +2003,7 @@ cdef class _RemotePromise: def to_dict(self, verbose=False, ordered=False): return _to_dict(self, verbose, ordered) - cpdef cancel(self) except +reraise_kj_exception: + cpdef cancel(self): self.thisptr = Own[RemotePromise]() self._parent = None # We don't need parent anymore. Setting to none allows quicker garbage collection @@ -2105,7 +2106,7 @@ cdef class _DynamicCapabilityClient: for key, val in kwargs.items(): _setDynamicField(deref(request), key, val, self) - cpdef _send_helper(self, name, word_count, args, kwargs) except +reraise_kj_exception: + cpdef _send_helper(self, name, word_count, args, kwargs): # if word_count is None: # word_count = 0 C_DEFAULT_EVENT_LOOP_GETTER() # Make sure the event loop is running @@ -2115,7 +2116,7 @@ cdef class _DynamicCapabilityClient: return _RemotePromise()._init(request.send(), self) - cpdef _request_helper(self, name, firstSegmentWordSize, args, kwargs) except +reraise_kj_exception: + cpdef _request_helper(self, name, firstSegmentWordSize, args, kwargs): # if word_count is None: # word_count = 0 cdef _Request req = _Request()._init_child(self.thisptr.newRequest(name), self) @@ -2144,7 +2145,7 @@ cdef class _DynamicCapabilityClient: except KjException as e: raise e._to_python(), None, _sys.exc_info()[2] - cpdef upcast(self, schema) except +reraise_kj_exception: + cpdef upcast(self, schema): cdef _InterfaceSchema s if hasattr(schema, 'schema'): s = schema.schema @@ -2153,7 +2154,7 @@ cdef class _DynamicCapabilityClient: return _DynamicCapabilityClient()._init(self.thisptr.upcast(s.thisptr), self._parent) - cpdef cast_as(self, schema) except +reraise_kj_exception: + cpdef cast_as(self, schema): cdef _InterfaceSchema s if hasattr(schema, 'schema'): s = schema.schema @@ -2173,24 +2174,21 @@ cdef class _DynamicCapabilityClient: cdef class _CapabilityClient: - cdef C_Capability.Client * thisptr + cdef Own[C_Capability.Client] thisptr cdef public object _parent - cdef _init(self, C_Capability.Client other, object parent): - self.thisptr = new C_Capability.Client(other) + cdef _init(self, Own[C_Capability.Client] other, object parent): + self.thisptr = move(other) self._parent = parent return self - def __dealloc__(self): - del self.thisptr - cpdef cast_as(self, schema): cdef _InterfaceSchema s if hasattr(schema, 'schema'): s = schema.schema else: s = schema - return _DynamicCapabilityClient()._init(self.thisptr.castAs(s.thisptr), self._parent) + return _DynamicCapabilityClient()._init(deref(self.thisptr).castAs(s.thisptr), self._parent) cdef class _TwoPartyVatNetwork: @@ -2205,7 +2203,7 @@ cdef class _TwoPartyVatNetwork: self.thisptr = capnp.heap[C_TwoPartyVatNetwork](deref(stream.thisptr), side, opts) return self - cpdef on_disconnect(self) except +reraise_kj_exception: + cpdef on_disconnect(self): return _voidpromise_to_asyncio(deref(self.thisptr).onDisconnect()) @@ -2244,12 +2242,12 @@ cdef class TwoPartyClient: self.thisptr = capnp.heap[RpcSystem](makeRpcClient(deref(self._network.thisptr))) - cpdef bootstrap(self) except +reraise_kj_exception: + cpdef bootstrap(self): if self.closed: raise RuntimeError("This client is closed") return _CapabilityClient()._init(helpers.bootstrapHelper(deref(self.thisptr)), self) - cpdef on_disconnect(self) except +reraise_kj_exception: + cpdef on_disconnect(self): if self.closed: raise RuntimeError("This client is closed") return self._network.on_disconnect() @@ -2299,12 +2297,12 @@ cdef class TwoPartyServer: capnp.heap[PyRefCounter](bootstrap), capnp.heap[PyRefCounter](loop))))) - cpdef bootstrap(self) except +reraise_kj_exception: + cpdef bootstrap(self): if self.closed: raise RuntimeError("This server is closed") return _CapabilityClient()._init(helpers.bootstrapHelperServer(deref(self.thisptr)), self) - cpdef on_disconnect(self) except +reraise_kj_exception: + cpdef on_disconnect(self): if self.closed: raise RuntimeError("This server is closed") return _voidpromise_to_asyncio(deref(self._network.thisptr).onDisconnect() @@ -2496,7 +2494,7 @@ cdef class _PyAsyncIoStreamProtocol(DummyBaseClass, asyncio.BufferedProtocol): self.read_overflow_buffer_current = bytearray(size) return self.read_overflow_buffer_current else: - return memoryview.PyMemoryView_FromMemory(self.read_buffer, self.read_max_bytes, buffer.PyBUF_WRITE) + return PyMemoryView_FromMemory(self.read_buffer, self.read_max_bytes, PyBUF_WRITE) def buffer_updated(self, size): if self.read_buffer == NULL: # Should not happen, but for SSL it does, see comment above @@ -2531,7 +2529,7 @@ cdef class _PyAsyncIoStreamProtocol(DummyBaseClass, asyncio.BufferedProtocol): self.write_index = i break piece = &self.write_pieces[i] - view = memoryview.PyMemoryView_FromMemory(piece.begin(), piece.size(), buffer.PyBUF_READ) + view = PyMemoryView_FromMemory(piece.begin(), piece.size(), PyBUF_READ) self.transport.write(view) if not self.write_paused: self.write_fulfiller.fulfill() @@ -2582,7 +2580,7 @@ cdef api void _asyncio_stream_read_start( # Begin of draining the overflow buffer, which is created because of a bug in SSL, see comment above. # Can be removed once Python < 3.11 is not longer supported. if self.read_overflow_buffer: - to_copy = min(len(self.read_overflow_buffer), max_bytes) + to_copy = min(len(self.read_overflow_buffer), max_bytes) memcpy(buffer, self.read_overflow_buffer, to_copy) del self.read_overflow_buffer[:to_copy] self.read_buffer += to_copy @@ -3316,7 +3314,7 @@ cdef class _StringArrayPtr: def __dealloc__(self): free(self.thisptr) - cdef ArrayPtr[StringPtr] asArrayPtr(self) except +reraise_kj_exception: + cdef ArrayPtr[StringPtr] asArrayPtr(self): return ArrayPtr[StringPtr](self.thisptr, self.size) @@ -3363,7 +3361,7 @@ cdef class SchemaParser: def __dealloc__(self): del self.thisptr - cpdef _parse_disk_file(self, displayName, diskPath, imports) except +reraise_kj_exception: + cpdef _parse_disk_file(self, displayName, diskPath, imports): cdef _StringArrayPtr importArray if self._last_import_array and self._last_import_array.parent == imports: @@ -3531,7 +3529,7 @@ cdef class _MessageBuilder: s = schema return _DynamicStructBuilder()._init(self.thisptr.initRootDynamicStruct(s._thisptr()), self, True) - cpdef get_root(self, schema) except +reraise_kj_exception: + cpdef get_root(self, schema): """A method for instantiating Cap'n Proto structs, from an already pre-written buffer Don't use this method unless you know what you're doing. You probably @@ -3556,7 +3554,7 @@ cdef class _MessageBuilder: s = schema return _DynamicStructBuilder()._init(self.thisptr.getRootDynamicStruct(s._thisptr()), self, True) - cpdef get_root_as_any(self) except +reraise_kj_exception: + cpdef get_root_as_any(self): """A method for getting a Cap'n Proto AnyPointer, from an already pre-written buffer Don't use this method unless you know what you're doing. @@ -3566,7 +3564,7 @@ cdef class _MessageBuilder: """ return _DynamicObjectBuilder()._init(self.thisptr.getRootAnyPointer(), self) - cpdef set_root(self, value) except +reraise_kj_exception: + cpdef set_root(self, value): """A method for instantiating Cap'n Proto structs by copying from an existing struct :type value: :class:`_DynamicStructReader` @@ -3582,7 +3580,7 @@ cdef class _MessageBuilder: self.thisptr.setRootDynamicStruct((<_DynamicStructReader>value).thisptr) return self.get_root(value.schema) - cpdef get_segments_for_output(self) except +reraise_kj_exception: + cpdef get_segments_for_output(self): segments = self.thisptr.getSegmentsForOutput() res = [] cdef const char* ptr @@ -3594,7 +3592,7 @@ cdef class _MessageBuilder: res.append(segment_bytes) return res - cpdef new_orphan(self, schema) except +reraise_kj_exception: + cpdef new_orphan(self, schema): """A method for instantiating Cap'n Proto orphans Don't use this method unless you know what you're doing. @@ -3652,7 +3650,7 @@ cdef class _MessageReader: def __init__(self): raise NotImplementedError("This is an abstract base class") - cpdef get_root(self, schema) except +reraise_kj_exception: + cpdef get_root(self, schema): """A method for instantiating Cap'n Proto structs You will need to pass in a schema to specify which struct to @@ -3676,7 +3674,7 @@ cdef class _MessageReader: s = schema return _DynamicStructReader()._init(self.thisptr.getRootDynamicStruct(s._thisptr()), self) - cpdef get_root_as_any(self) except +reraise_kj_exception: + cpdef get_root_as_any(self): """A method for getting a Cap'n Proto AnyPointer, from an already pre-written buffer Don't use this method unless you know what you're doing. diff --git a/pyproject.toml b/pyproject.toml index 1551bd21..a59ee2be 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,5 +1,5 @@ [build-system] -requires = ["setuptools", "wheel", "pkgconfig", "cython<3"] +requires = ["setuptools", "wheel", "pkgconfig", "cython>=3.0"] build-backend = "backend" backend-path = ["_custom_build"] diff --git a/requirements.txt b/requirements.txt index ccda7ce6..c7330dd9 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,6 +1,6 @@ jinja2 black -cython<3 +cython>=3 flake8 setuptools pkgconfig diff --git a/tox.ini b/tox.ini index 71d1c9ac..7a7e0d11 100644 --- a/tox.ini +++ b/tox.ini @@ -8,7 +8,7 @@ deps= Jinja2 pytest pytest-asyncio - cython<3 + cython>=3 commands = pip install .