Skip to content

Commit 46cdeb4

Browse files
Fix for Promise.done_callback (python-telegram-bot#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>
1 parent 653691f commit 46cdeb4

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

telegram/ext/utils/promise.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ def run(self) -> None:
100100

101101
finally:
102102
self.done.set()
103-
if self._done_callback:
103+
if self._exception is None and self._done_callback:
104104
try:
105105
self._done_callback(self.result())
106106
except Exception as exc:
@@ -136,6 +136,10 @@ def add_done_callback(self, callback: Callable) -> None:
136136
"""
137137
Callback to be run when :class:`telegram.ext.utils.promise.Promise` becomes done.
138138
139+
Note:
140+
Callback won't be called if :attr:`pooled_function`
141+
raises an exception.
142+
139143
Args:
140144
callback (:obj:`callable`): The callable that will be called when promise is done.
141145
callback will be called by passing ``Promise.result()`` as only positional argument.

tests/test_promise.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,3 +136,17 @@ def done_callback(_):
136136
)
137137
assert caplog.records[1].message.startswith("Full traceback:")
138138
assert promise.result() == "done!"
139+
140+
def test_done_cb_not_run_on_excp(self):
141+
def callback():
142+
raise TelegramError('Error')
143+
144+
def done_callback(_):
145+
self.test_flag = True
146+
147+
promise = Promise(callback, [], {})
148+
promise.add_done_callback(done_callback)
149+
promise.run()
150+
assert isinstance(promise.exception, TelegramError)
151+
assert promise.done
152+
assert self.test_flag is False

0 commit comments

Comments
 (0)