Fixup exception handling for capabilities

This commit is contained in:
Jason Paryani
2013-10-20 17:24:59 -07:00
parent a9ad0e6b85
commit 5e00534842
4 changed files with 110 additions and 16 deletions

View File

@@ -13,9 +13,27 @@ extern "C" {
PyObject * wrapPyFunc(PyObject * func, PyObject * arg) {
PyObject * result = PyObject_CallFunctionObjArgs(func, arg, NULL);
Py_DECREF(func);
PyObject * err = PyErr_Occurred();
if(err) {
char * errorMsg = PyString_AsString(PyObject_Repr(err));
// PyErr_Clear();
throw std::invalid_argument(errorMsg);
}
return result;
}
void wrapRemoteCall(PyObject * func, capnp::Response<capnp::DynamicStruct> & arg) {
wrap_remote_call(func, arg);
PyObject * err = PyErr_Occurred();
if(err) {
char * errorMsg = PyString_AsString(PyObject_Repr(err));
// PyErr_Clear();
throw std::invalid_argument(errorMsg);
}
}
::kj::Promise<PyObject *> evalLater(kj::EventLoop & loop, PyObject * func) {
return loop.evalLater([func]() { return wrapPyFunc(func, NULL); } );
}
@@ -38,9 +56,9 @@ PyObject * wrapPyFunc(PyObject * func, PyObject * arg) {
::kj::Promise<void> then(::capnp::RemotePromise< ::capnp::DynamicStruct> & promise, PyObject * func, PyObject * error_func) {
if(error_func == Py_None)
return promise.then([func](capnp::Response<capnp::DynamicStruct>&& arg) { wrap_remote_call(func, arg); } );
return promise.then([func](capnp::Response<capnp::DynamicStruct>&& arg) { wrapRemoteCall(func, arg); } );
else
return promise.then([func](capnp::Response<capnp::DynamicStruct>&& arg) { wrap_remote_call(func, arg); }
return promise.then([func](capnp::Response<capnp::DynamicStruct>&& arg) { wrapRemoteCall(func, arg); }
, [error_func](kj::Exception arg) { wrapPyFunc(error_func, wrap_kj_exception(arg)); } );
}
@@ -60,7 +78,19 @@ public:
kj::Promise<void> call(capnp::InterfaceSchema::Method method,
capnp::CallContext< capnp::DynamicStruct, capnp::DynamicStruct> context) {
auto methodName = method.getProto().getName();
kj::Promise<void> * promise = call_server_method(py_server, const_cast<char *>(methodName.cStr()), context);
PyObject * err = PyErr_Occurred();
if(err) {
PyObject *ptype, *pvalue, *ptraceback;
PyErr_Fetch(&ptype, &pvalue, &ptraceback);
char * errorMsg = PyString_AsString(pvalue);
PyErr_Clear();
throw std::invalid_argument(errorMsg);
}
if(promise == nullptr)
return kj::READY_NOW;

View File

@@ -39,30 +39,37 @@ import os as _os
import sys as _sys
import imp as _imp
from functools import partial as _partial
import warnings as _warnings
import inspect as _inspect
# By making it public, we'll be able to call it from capabilityHelper.h
cdef public object wrap_dynamic_struct_reader(C_DynamicStruct.Reader & reader):
return _DynamicStructReader()._init(reader, None)
cdef public void wrap_remote_call(PyObject * func, Response & r):
cdef public void wrap_remote_call(PyObject * func, Response & r) except *:
response = _Response()._init_childptr(new Response(moveResponse(r)), None)
func_obj = <object>func
# TODO: decref func?
func_obj(response)
cdef public VoidPromise * call_server_method(PyObject * _server, char * _method_name, CallContext & _context):
cdef public VoidPromise * call_server_method(PyObject * _server, char * _method_name, CallContext & _context) except *:
server = <object>_server
method_name = <object>_method_name
context = _CallContext()._init(_context)
ret = getattr(server, method_name)(context)
func = getattr(server, method_name)
ret = func(context)
if ret is not None:
if type(ret) is _VoidPromise:
return new VoidPromise(moveVoidPromise(deref((<_VoidPromise>ret).thisptr)))
else:
raise ValueError('Server function returned a value that was not a VoidPromise: ' + str(ret))
try:
warning_msg = 'Server function (%s) returned a value that was not a VoidPromise: return = %s' % (method_name, str(ret))
except:
warning_msg = 'Server function (%s) returned a value that was not a VoidPromise' % (method_name)
_warnings.warn_explicit(warning_msg, UserWarning, _inspect.getsourcefile(func), _inspect.getsourcelines(func)[1])
return NULL