Add then function for VoidPromise

This commit is contained in:
Jason Paryani 2013-12-04 15:59:22 -08:00
parent f5bd65bf7b
commit 6fd17ff4d9
3 changed files with 25 additions and 14 deletions

View file

@ -70,6 +70,14 @@ PyObject * wrapPyFunc(PyObject * func, PyObject * arg) {
return result;
}
PyObject * wrapPyFuncNoArg(PyObject * func) {
PyObject * result = PyObject_CallFunctionObjArgs(func, NULL);
Py_DECREF(func);
check_py_error();
return result;
}
void wrapRemoteCall(PyObject * func, capnp::Response<capnp::DynamicStruct> & arg) {
wrap_remote_call(func, arg);
@ -92,6 +100,14 @@ void wrapRemoteCall(PyObject * func, capnp::Response<capnp::DynamicStruct> & arg
, [error_func](kj::Exception arg) { wrapPyFunc(error_func, wrap_kj_exception(arg)); } );
}
::kj::Promise<PyObject *> then(kj::Promise<void> & promise, PyObject * func, PyObject * error_func) {
if(error_func == Py_None)
return promise.then([func]() { return wrapPyFuncNoArg(func); } );
else
return promise.then([func]() { return wrapPyFuncNoArg(func); }
, [error_func](kj::Exception arg) { return wrapPyFunc(error_func, wrap_kj_exception(arg)); } );
}
class PythonInterfaceDynamicImpl final: public capnp::DynamicCapability::Server {
public:
PyObject * py_server;

View file

@ -1255,7 +1255,7 @@ cdef class _Promise:
if self.is_consumed:
raise ValueError('Promise was already used in a consuming operation. You can no longer use this Promise object')
ret = <object>self.thisptr.wait()
ret = <object>self.thisptr.wait() # TODO: make sure refcount is fine here...
self.is_consumed = True
return ret
@ -1292,14 +1292,14 @@ cdef class _VoidPromise:
self.is_consumed = True
# cpdef then(self, func, error_func=None) except +reraise_kj_exception:
# if self.is_consumed:
# raise RuntimeError('Promise was already used in a consuming operation. You can no longer use this Promise object')
cpdef then(self, func, error_func=None) except +reraise_kj_exception:
if self.is_consumed:
raise RuntimeError('Promise was already used in a consuming operation. You can no longer use this Promise object')
# Py_INCREF(func)
# Py_INCREF(error_func)
Py_INCREF(func)
Py_INCREF(error_func)
# return Promise()._init(capnp.then(deref(self.thisptr), <PyObject *>func, <PyObject *>error_func))
return _Promise()._init(capnp.then(deref(self.thisptr), <PyObject *>func, <PyObject *>error_func))
cdef class _RemotePromise:
cdef RemotePromise * thisptr
@ -1332,7 +1332,7 @@ cdef class _RemotePromise:
cpdef then(self, func, error_func=None) except +reraise_kj_exception:
if self.is_consumed:
raise ValueError('Promise was already used in a consuming operation. You can no longer use this Promise object')
raise RuntimeError('Promise was already used in a consuming operation. You can no longer use this Promise object')
Py_INCREF(func)
Py_INCREF(error_func)
@ -1361,12 +1361,6 @@ cdef class _RemotePromise:
def __dir__(self):
return list(self.schema.fieldnames)
# def __str__(self):
# return printStructReader(self.thisptr).flatten().cStr()
# def __repr__(self):
# return '<%s reader %s>' % (self.schema.node.displayName, strStructReader(self.thisptr).cStr())
def to_dict(self, verbose=False):
return _to_dict(self, verbose)

View file

@ -244,6 +244,7 @@ cdef extern from "capabilityHelper.h":
# 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 *)