Fix for Promise.done_callback (#2544)

* Don't call done_cb on exceptions

Signed-off-by: starry69 <starry369126@outlook.com>

* improve docs

Co-authored-by: Bibo-Joshi <hinrich.mahler@freenet.de>

* revert black

Co-authored-by: Bibo-Joshi <hinrich.mahler@freenet.de>
This commit is contained in:
Stɑrry Shivɑm 2021-06-05 20:38:45 +05:30 committed by GitHub
parent 653691fafb
commit 46cdeb495a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 1 deletions

View file

@ -100,7 +100,7 @@ class Promise:
finally:
self.done.set()
if self._done_callback:
if self._exception is None and self._done_callback:
try:
self._done_callback(self.result())
except Exception as exc:
@ -136,6 +136,10 @@ class Promise:
"""
Callback to be run when :class:`telegram.ext.utils.promise.Promise` becomes done.
Note:
Callback won't be called if :attr:`pooled_function`
raises an exception.
Args:
callback (:obj:`callable`): The callable that will be called when promise is done.
callback will be called by passing ``Promise.result()`` as only positional argument.

View file

@ -136,3 +136,17 @@ class TestPromise:
)
assert caplog.records[1].message.startswith("Full traceback:")
assert promise.result() == "done!"
def test_done_cb_not_run_on_excp(self):
def callback():
raise TelegramError('Error')
def done_callback(_):
self.test_flag = True
promise = Promise(callback, [], {})
promise.add_done_callback(done_callback)
promise.run()
assert isinstance(promise.exception, TelegramError)
assert promise.done
assert self.test_flag is False