Add arguments checking to then callback

This commit is contained in:
Jason Paryani
2014-04-13 18:26:01 -07:00
parent c2b9d14268
commit cfb60cddf0
2 changed files with 21 additions and 0 deletions

View File

@@ -1463,6 +1463,17 @@ 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')
argspec = None
try:
argspec = _inspect.getargspec(func)
except:
pass
if argspec:
args_length = len(argspec.args) if argspec.args else 0
defaults_length = len(argspec.defaults) if argspec.defaults else 0
if args_length - defaults_length != 1:
raise ValueError('Function passed to `then` call must take exactly one argument')
self.is_consumed = True
return Promise()._init(helpers.then(deref(self.thisptr), <PyObject *>func, <PyObject *>error_func).attach(capnp.makePyRefCounter(<PyObject *>func), capnp.makePyRefCounter(<PyObject *>error_func)), self)

View File

@@ -289,3 +289,13 @@ def test_timer():
joined = capnp.join_promises([promise, canceller])
with pytest.raises(Exception):
joined.wait()
def test_then_args():
capnp.Promise(0).then(lambda x: 1)
with pytest.raises(ValueError):
capnp.Promise(0).then(lambda: 1)
with pytest.raises(ValueError):
capnp.Promise(0).then(lambda x, y: 1)