From bb3121d3ae2634f24a138ae6fefe2f2b430ffa73 Mon Sep 17 00:00:00 2001 From: Jason Paryani Date: Wed, 4 Dec 2013 11:42:57 -0800 Subject: [PATCH] Fix API changes in EventLoop --- capnp/capnp.pyx | 37 +++++++++++++++++----------------- capnp/capnp_cpp.pxd | 9 ++++++--- examples/example_capability.py | 7 ++++++- test/test_rpc.py | 6 ------ 4 files changed, 30 insertions(+), 29 deletions(-) diff --git a/capnp/capnp.pyx b/capnp/capnp.pyx index f22f1ca..d193098 100644 --- a/capnp/capnp.pyx +++ b/capnp/capnp.pyx @@ -9,7 +9,7 @@ cimport cython cimport capnp_cpp as capnp cimport schema_cpp -from capnp_cpp cimport Schema as C_Schema, StructSchema as C_StructSchema, InterfaceSchema as C_InterfaceSchema, DynamicStruct as C_DynamicStruct, DynamicValue as C_DynamicValue, Type as C_Type, DynamicList as C_DynamicList, fixMaybe, getEnumString, SchemaParser as C_SchemaParser, ParsedSchema as C_ParsedSchema, VOID, ArrayPtr, StringPtr, String, StringTree, DynamicOrphan as C_DynamicOrphan, ObjectPointer as C_DynamicObject, DynamicCapability as C_DynamicCapability, new_client, new_server, server_to_client, Request, Response, RemotePromise, convert_to_pypromise, UnixEventLoop, PyPromise, VoidPromise, CallContext, PyRestorer, RpcSystem, makeRpcServer, makeRpcClient, makeRpcClientWithRestorer, restoreHelper, Capability as C_Capability, TwoPartyVatNetwork as C_TwoPartyVatNetwork, Side, AsyncIoStream_wrapFd, AsyncIoStream, Own, makeTwoPartyVatNetwork, PromiseFulfillerPair as C_PromiseFulfillerPair, copyPromiseFulfillerPair, newPromiseAndFulfiller, reraise_kj_exception +from capnp_cpp cimport Schema as C_Schema, StructSchema as C_StructSchema, InterfaceSchema as C_InterfaceSchema, DynamicStruct as C_DynamicStruct, DynamicValue as C_DynamicValue, Type as C_Type, DynamicList as C_DynamicList, fixMaybe, getEnumString, SchemaParser as C_SchemaParser, ParsedSchema as C_ParsedSchema, VOID, ArrayPtr, StringPtr, String, StringTree, DynamicOrphan as C_DynamicOrphan, ObjectPointer as C_DynamicObject, DynamicCapability as C_DynamicCapability, new_client, new_server, server_to_client, Request, Response, RemotePromise, convert_to_pypromise, UnixEventLoop, PyPromise, VoidPromise, CallContext, PyRestorer, RpcSystem, makeRpcServer, makeRpcClient, makeRpcClientWithRestorer, restoreHelper, Capability as C_Capability, TwoPartyVatNetwork as C_TwoPartyVatNetwork, Side, AsyncIoStream, Own, makeTwoPartyVatNetwork, PromiseFulfillerPair as C_PromiseFulfillerPair, copyPromiseFulfillerPair, newPromiseAndFulfiller, reraise_kj_exception from schema_cpp cimport Node as C_Node, EnumNode as C_EnumNode from cython.operator cimport dereference as deref @@ -488,7 +488,7 @@ cdef class _DynamicListBuilder: cpdef adopt(self, index, _DynamicOrphan orphan): """A method for adopting Cap'n Proto orphans - Don't use this method unless you know what you're doing. Orphans are useful for dynamically allocating objects for an unkown sized list. + Don't use this method unless you know what you're doing. Orphans are useful for dynamically allocating objects for an unknown sized list. :type index: int :param index: The index of the element in the list to replace with the newly adopted object @@ -989,7 +989,7 @@ cdef class _DynamicStructBuilder: cpdef adopt(self, field, _DynamicOrphan orphan): """A method for adopting Cap'n Proto orphans - Don't use this method unless you know what you're doing. Orphans are useful for dynamically allocating objects for an unkown sized list. + Don't use this method unless you know what you're doing. Orphans are useful for dynamically allocating objects for an unknown sized list. :type field: str :param field: The field name in the struct @@ -1164,20 +1164,22 @@ cdef class _DynamicObjectBuilder: return _DynamicStructBuilder()._init(self.thisptr.getAs(s.thisptr), self._parent) cdef class _EventLoop: - cdef UnixEventLoop * thisptr + cdef Own[capnp.AsyncIoProvider] thisptr def __init__(self): self._init() cdef _init(self) except +reraise_kj_exception: - self.thisptr = new UnixEventLoop() + self.thisptr = capnp.setupIoEventLoop() - def __dealloc__(self): - self.remove() + cdef Own[AsyncIoStream] wrapSocketFd(self, int fd): + return deref(self.thisptr).wrapSocketFd(fd) - cpdef remove(self) except +reraise_kj_exception: - del self.thisptr - self.thisptr = NULL + # def __dealloc__(self): + # self.remove() + + # cpdef remove(self) except +reraise_kj_exception: + # self.thisptr = NULL # cpdef evalLater(self, func): # Py_INCREF(func) @@ -1211,13 +1213,7 @@ cdef class _EventLoop: # Py_INCREF(error_func) # return Promise()._init(capnp.there(self.thisptr, deref(promise.thisptr), func, error_func)) -DEFAULT_EVENT_LOOP = _EventLoop() - -def remove_event_loop(): - global DEFAULT_EVENT_LOOP - - DEFAULT_EVENT_LOOP.remove() - DEFAULT_EVENT_LOOP = None +cdef _EventLoop C_DEFAULT_EVENT_LOOP = _EventLoop() cdef class _CallContext: cdef CallContext * thisptr @@ -1612,7 +1608,10 @@ cdef class _FdAsyncIoStream: cdef Own[AsyncIoStream] thisptr def __init__(self, int fd): - self.thisptr = AsyncIoStream_wrapFd(fd) + self._init(fd) + + cdef _init(self, int fd) except +reraise_kj_exception: + self.thisptr = C_DEFAULT_EVENT_LOOP.wrapSocketFd(fd) cdef class PromiseFulfillerPair: cdef Own[C_PromiseFulfillerPair] thisptr @@ -2091,7 +2090,7 @@ cdef class _MessageBuilder: cpdef new_orphan(self, schema) except +reraise_kj_exception: """A method for instantiating Cap'n Proto orphans - Don't use this method unless you know what you're doing. Orphans are useful for dynamically allocating objects for an unkown sized list, ie:: + Don't use this method unless you know what you're doing. Orphans are useful for dynamically allocating objects for an unknown sized list, ie:: addressbook = capnp.load('addressbook.capnp') m = capnp._MallocMessageBuilder() diff --git a/capnp/capnp_cpp.pxd b/capnp/capnp_cpp.pxd index 654c22b..21e0764 100644 --- a/capnp/capnp_cpp.pxd +++ b/capnp/capnp_cpp.pxd @@ -59,8 +59,11 @@ cdef extern from "kj/array.h" namespace " ::kj": cdef extern from "kj/async-io.h" namespace " ::kj": cdef cppclass AsyncIoStream: pass - - Own[AsyncIoStream] AsyncIoStream_wrapFd" ::kj::AsyncIoStream::wrapFd"(int) + cdef cppclass AsyncIoProvider: + # Own[AsyncInputStream] wrapInputFd(int) + # Own[AsyncOutputStream] wrapOutputFd(int) + Own[AsyncIoStream] wrapSocketFd(int) + Own[AsyncIoProvider] setupIoEventLoop() cdef extern from "capnp/schema.h" namespace " ::capnp": cdef cppclass Schema: @@ -343,7 +346,7 @@ cdef extern from "capnp/schema-parser.h" namespace " ::capnp": ParsedSchema getNested(char * name) except +reraise_kj_exception cdef cppclass SchemaParser: SchemaParser() - ParsedSchema parseDiskFile(char * displayName, char * diskPath, ArrayPtr[StringPtr] importPath) except +reraise_kj_exception + ParsedSchema parseDiskFile(char * displayName, char * diskPath, ArrayPtr[StringPtr] importPath) cdef extern from "capnp/orphan.h" namespace " ::capnp": cdef cppclass DynamicOrphan" ::capnp::Orphan< ::capnp::DynamicValue>": diff --git a/examples/example_capability.py b/examples/example_capability.py index 9273024..c37c19c 100644 --- a/examples/example_capability.py +++ b/examples/example_capability.py @@ -32,4 +32,9 @@ def example_simple_rpc(): assert response.x == '125' -example_simple_rpc() \ No newline at end of file +# example_simple_rpc() +capnp.load('test.capnp') +for i in range(100): + capnp.load('test.capnp') + +capnp.load('test.capnp') \ No newline at end of file diff --git a/test/test_rpc.py b/test/test_rpc.py index 03eace2..19a096a 100644 --- a/test/test_rpc.py +++ b/test/test_rpc.py @@ -34,9 +34,3 @@ def test_simple_rpc(capability): response = remote.wait() assert response.x == '125' - -def test_custom_event_loop(capability): - capnp.remove_event_loop() - capnp.DEFAULT_EVENT_LOOP = capnp._EventLoop() - - test_simple_rpc(capability) \ No newline at end of file