mirror of
https://github.com/python-telegram-bot/python-telegram-bot.git
synced 2025-03-13 11:18:20 +01:00
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:
parent
653691fafb
commit
46cdeb495a
2 changed files with 19 additions and 1 deletions
|
@ -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.
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Add table
Reference in a new issue