From cff551c774a15db469b65f0a5f248bb6d981cc1e Mon Sep 17 00:00:00 2001 From: Jason Paryani Date: Sun, 8 Dec 2013 17:27:59 -0800 Subject: [PATCH] Refactor layout of pxd files --- capnp/__init__.pxd | 0 capnp/capnp.pyx | 13 ++++++++---- capnp/helpers/__init__.pxd | 0 capnp/{ => helpers}/capabilityHelper.h | 0 capnp/helpers/checkCompiler.h | 11 ++++++++++ capnp/{ => helpers}/fixMaybe.h | 2 -- capnp/helpers/helpers.pxd | 29 ++++++++++++++++++++++++++ capnp/helpers/non_circular.pxd | 8 +++++++ capnp/{ => helpers}/rpcHelper.h | 0 capnp/includes/__init__.pxd | 0 capnp/{ => includes}/capnp_cpp.pxd | 15 +++++++------ capnp/{ => includes}/schema_cpp.pxd | 2 +- setup.py | 2 +- 13 files changed, 66 insertions(+), 16 deletions(-) create mode 100644 capnp/__init__.pxd create mode 100644 capnp/helpers/__init__.pxd rename capnp/{ => helpers}/capabilityHelper.h (100%) create mode 100644 capnp/helpers/checkCompiler.h rename capnp/{ => helpers}/fixMaybe.h (72%) create mode 100644 capnp/helpers/helpers.pxd create mode 100644 capnp/helpers/non_circular.pxd rename capnp/{ => helpers}/rpcHelper.h (100%) create mode 100644 capnp/includes/__init__.pxd rename capnp/{ => includes}/capnp_cpp.pxd (98%) rename capnp/{ => includes}/schema_cpp.pxd (99%) diff --git a/capnp/__init__.pxd b/capnp/__init__.pxd new file mode 100644 index 0000000..e69de29 diff --git a/capnp/capnp.pyx b/capnp/capnp.pyx index ead0041..db6ada4 100644 --- a/capnp/capnp.pyx +++ b/capnp/capnp.pyx @@ -7,11 +7,16 @@ # cython: embedsignature = True 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, AnyPointer as C_DynamicObject, DynamicCapability as C_DynamicCapability, new_client, new_server, server_to_client, Request, Response, RemotePromise, convert_to_pypromise, 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 .capnp.includes cimport capnp_cpp as capnp +from .capnp.includes cimport schema_cpp +from .capnp.includes.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, AnyPointer as C_DynamicObject, DynamicCapability as C_DynamicCapability, new_client, new_server, server_to_client, Request, Response, RemotePromise, convert_to_pypromise, PyPromise, VoidPromise, CallContext, PyRestorer, RpcSystem, makeRpcServer, makeRpcClient, restoreHelper, Capability as C_Capability, TwoPartyVatNetwork as C_TwoPartyVatNetwork, Side, AsyncIoStream, Own, makeTwoPartyVatNetwork, PromiseFulfillerPair as C_PromiseFulfillerPair, copyPromiseFulfillerPair, newPromiseAndFulfiller, reraise_kj_exception +from .capnp.includes.schema_cpp cimport Node as C_Node, EnumNode as C_EnumNode + +from .capnp.helpers.helpers cimport makeRpcClientWithRestorer + + + from cython.operator cimport dereference as deref from cpython.ref cimport PyObject, Py_INCREF, Py_DECREF diff --git a/capnp/helpers/__init__.pxd b/capnp/helpers/__init__.pxd new file mode 100644 index 0000000..e69de29 diff --git a/capnp/capabilityHelper.h b/capnp/helpers/capabilityHelper.h similarity index 100% rename from capnp/capabilityHelper.h rename to capnp/helpers/capabilityHelper.h diff --git a/capnp/helpers/checkCompiler.h b/capnp/helpers/checkCompiler.h new file mode 100644 index 0000000..7250885 --- /dev/null +++ b/capnp/helpers/checkCompiler.h @@ -0,0 +1,11 @@ +#ifdef __GNUC__ + #if __clang__ + #if __cplusplus >= 201103L && !__has_include() + #warning "Your compiler supports C++11 but your C++ standard library does not. If your system has libc++ installed (as should be the case on e.g. Mac OSX), try adding -stdlib=libc++ to your CFLAGS (ignore the other warning that says to use CXXFLAGS)." + #endif + #endif +#endif + +#include "capnp/dynamic.h" + +static_assert(CAPNP_VERSION >= 3000, "Version of Cap'n Proto C++ Library is too old. Please upgrade to a version >= 0.3 and then re-install this python library"); diff --git a/capnp/fixMaybe.h b/capnp/helpers/fixMaybe.h similarity index 72% rename from capnp/fixMaybe.h rename to capnp/helpers/fixMaybe.h index 0f23106..705edda 100644 --- a/capnp/fixMaybe.h +++ b/capnp/helpers/fixMaybe.h @@ -1,8 +1,6 @@ #include "kj/common.h" #include -static_assert(CAPNP_VERSION >= 3000, "Version of Cap'n Proto C++ Library is too old. Please upgrade to a version >= 0.3 and then re-install this python library"); - template T fixMaybe(::kj::Maybe val) { KJ_IF_MAYBE(new_val, val) { diff --git a/capnp/helpers/helpers.pxd b/capnp/helpers/helpers.pxd new file mode 100644 index 0000000..679fd2b --- /dev/null +++ b/capnp/helpers/helpers.pxd @@ -0,0 +1,29 @@ +from .capnp.includes.capnp_cpp cimport Maybe, DynamicStruct, Request, PyPromise, VoidPromise, RemotePromise, DynamicCapability, InterfaceSchema, EnumSchema, StructSchema, DynamicValue, Capability, RpcSystem, MessageBuilder, MessageReader, TwoPartyVatNetwork + +from non_circular cimport reraise_kj_exception + +from cpython.ref cimport PyObject + +cdef extern from "helpers/fixMaybe.h": + EnumSchema.Enumerant fixMaybe(Maybe[EnumSchema.Enumerant]) except +reraise_kj_exception + char * getEnumString(DynamicStruct.Reader val) + char * getEnumString(DynamicStruct.Builder val) + char * getEnumString(Request val) + +cdef extern from "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) + VoidPromise then(RemotePromise & promise, PyObject * func, PyObject * error_func) + PyPromise then(VoidPromise & promise, PyObject * func, PyObject * 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&) + +cdef extern from "helpers/rpcHelper.h": + cdef cppclass PyRestorer: + PyRestorer(PyObject *, StructSchema&) + Capability.Client restoreHelper(RpcSystem&, MessageBuilder&) + Capability.Client restoreHelper(RpcSystem&, MessageReader&) + RpcSystem makeRpcClientWithRestorer(TwoPartyVatNetwork&, PyRestorer&) diff --git a/capnp/helpers/non_circular.pxd b/capnp/helpers/non_circular.pxd new file mode 100644 index 0000000..f5deb43 --- /dev/null +++ b/capnp/helpers/non_circular.pxd @@ -0,0 +1,8 @@ +from cpython.ref cimport PyObject + +cdef extern from "helpers/capabilityHelper.h": + cppclass PythonInterfaceDynamicImpl: + PythonInterfaceDynamicImpl(PyObject *) + +cdef extern from "helpers/capabilityHelper.h": + void reraise_kj_exception() \ No newline at end of file diff --git a/capnp/rpcHelper.h b/capnp/helpers/rpcHelper.h similarity index 100% rename from capnp/rpcHelper.h rename to capnp/helpers/rpcHelper.h diff --git a/capnp/includes/__init__.pxd b/capnp/includes/__init__.pxd new file mode 100644 index 0000000..e69de29 diff --git a/capnp/capnp_cpp.pxd b/capnp/includes/capnp_cpp.pxd similarity index 98% rename from capnp/capnp_cpp.pxd rename to capnp/includes/capnp_cpp.pxd index 1dfbbe1..0ddd5b6 100644 --- a/capnp/capnp_cpp.pxd +++ b/capnp/includes/capnp_cpp.pxd @@ -1,7 +1,11 @@ # schema.capnp.cpp.pyx # distutils: language = c++ # distutils: extra_compile_args = --std=c++11 +cdef extern from "helpers/checkCompiler.h": + pass + from schema_cpp cimport Node, Data, StructNode, EnumNode, InterfaceNode, MessageBuilder, MessageReader +from .capnp.helpers.non_circular cimport PythonInterfaceDynamicImpl, reraise_kj_exception from cpython.ref cimport PyObject from libc.stdint cimport * @@ -17,9 +21,6 @@ cdef extern from "kj/async.h" namespace " ::kj": ctypedef Promise[PyObject *] PyPromise ctypedef Promise[void] VoidPromise -cdef extern from "capabilityHelper.h": - void reraise_kj_exception() - cdef extern from "capnp/common.h" namespace " ::capnp": enum Void: VOID " ::capnp::VOID" @@ -252,26 +253,24 @@ cdef extern from "capnp/any.h" namespace " ::capnp": Builder(Builder) DynamicStruct.Builder getAs"getAs< ::capnp::DynamicStruct>"(StructSchema) -cdef extern from "fixMaybe.h": +cdef extern from "helpers/fixMaybe.h": EnumSchema.Enumerant fixMaybe(Maybe[EnumSchema.Enumerant]) except +reraise_kj_exception char * getEnumString(DynamicStruct.Reader val) char * getEnumString(DynamicStruct.Builder val) char * getEnumString(Request val) -cdef extern from "capabilityHelper.h": +cdef extern from "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) VoidPromise then(RemotePromise & promise, PyObject * func, PyObject * error_func) PyPromise then(VoidPromise & promise, PyObject * func, PyObject * error_func) - cppclass PythonInterfaceDynamicImpl: - PythonInterfaceDynamicImpl(PyObject *) DynamicCapability.Client new_client(InterfaceSchema&, PyObject *) DynamicValueForward.Reader new_server(InterfaceSchema&, PyObject *) Capability.Client server_to_client(InterfaceSchema&, PyObject *) PyPromise convert_to_pypromise(RemotePromise&) -cdef extern from "rpcHelper.h": +cdef extern from "helpers/rpcHelper.h": cdef cppclass PyRestorer: PyRestorer(PyObject *, StructSchema&) Capability.Client restoreHelper(RpcSystem&, MessageBuilder&) diff --git a/capnp/schema_cpp.pxd b/capnp/includes/schema_cpp.pxd similarity index 99% rename from capnp/schema_cpp.pxd rename to capnp/includes/schema_cpp.pxd index 265fce2..750aa94 100644 --- a/capnp/schema_cpp.pxd +++ b/capnp/includes/schema_cpp.pxd @@ -718,7 +718,7 @@ cdef extern from "kj/array.h" namespace " ::kj": word* begin() size_t size() -cdef extern from "capabilityHelper.h": +cdef extern from "helpers/capabilityHelper.h": void reraise_kj_exception() cdef extern from "kj/array.h" namespace " ::kj": diff --git a/setup.py b/setup.py index bf2ed7d..43790f3 100644 --- a/setup.py +++ b/setup.py @@ -48,7 +48,7 @@ setup( name="pycapnp", packages=["capnp"], version=VERSION, - package_data={'capnp': ['*.pxd', '*.pyx', '*.h']}, + package_data={'capnp': ['*.pxd', '*.pyx', '*.h', 'helpers/*.pxd', 'helpers/*.h', 'includes/*.pxd']}, ext_modules=cythonize('capnp/*.pyx', language="c++"), install_requires=[ 'cython > 0.19',