Add arguments checking to then callback
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user