mirror of
https://github.com/python-telegram-bot/python-telegram-bot.git
synced 2025-03-27 08:50:38 +01:00
Call Application.post_stop
Only if Application.stop
was called (#4211)
This commit is contained in:
parent
637b8e260b
commit
b496fabf62
3 changed files with 12 additions and 5 deletions
|
@ -1086,6 +1086,7 @@ class Application(Generic[BT, CCT, UD, CD, BD, JQ], AsyncContextManager["Applica
|
||||||
loop.run_until_complete(self.updater.stop()) # type: ignore[union-attr]
|
loop.run_until_complete(self.updater.stop()) # type: ignore[union-attr]
|
||||||
if self.running:
|
if self.running:
|
||||||
loop.run_until_complete(self.stop())
|
loop.run_until_complete(self.stop())
|
||||||
|
# post_stop should be called only if stop was called!
|
||||||
if self.post_stop:
|
if self.post_stop:
|
||||||
loop.run_until_complete(self.post_stop(self))
|
loop.run_until_complete(self.post_stop(self))
|
||||||
loop.run_until_complete(self.shutdown())
|
loop.run_until_complete(self.shutdown())
|
||||||
|
|
|
@ -1334,7 +1334,13 @@ class ApplicationBuilder(Generic[BT, CCT, UD, CD, BD, JQ]):
|
||||||
|
|
||||||
Tip:
|
Tip:
|
||||||
This can be used for custom stop logic that requires to await coroutines, e.g.
|
This can be used for custom stop logic that requires to await coroutines, e.g.
|
||||||
sending message to a chat before shutting down the bot
|
sending message to a chat before shutting down the bot.
|
||||||
|
|
||||||
|
Hint:
|
||||||
|
The callback will be called only, if :meth:`Application.stop` was indeed called
|
||||||
|
successfully. For example, if the application is stopped early by calling
|
||||||
|
:meth:`Application.stop_running` within :meth:`post_init`, then the set callback will
|
||||||
|
*not* be called.
|
||||||
|
|
||||||
Example:
|
Example:
|
||||||
.. code::
|
.. code::
|
||||||
|
|
|
@ -2134,7 +2134,6 @@ class TestApplication:
|
||||||
"app_initialize",
|
"app_initialize",
|
||||||
"updater_initialize",
|
"updater_initialize",
|
||||||
"app_shutdown",
|
"app_shutdown",
|
||||||
"post_stop",
|
|
||||||
"post_shutdown",
|
"post_shutdown",
|
||||||
"updater_shutdown",
|
"updater_shutdown",
|
||||||
}
|
}
|
||||||
|
@ -2441,7 +2440,8 @@ class TestApplication:
|
||||||
app.run_polling(close_loop=False)
|
app.run_polling(close_loop=False)
|
||||||
|
|
||||||
# The important part here is that start(_polling) are *not* called!
|
# The important part here is that start(_polling) are *not* called!
|
||||||
assert called_callbacks == ["post_stop", "post_shutdown"]
|
# post_stop must not be called either, since we never called stop()
|
||||||
|
assert called_callbacks == ["post_shutdown"]
|
||||||
|
|
||||||
assert len(caplog.records) == 1
|
assert len(caplog.records) == 1
|
||||||
assert caplog.records[-1].name == "telegram.ext.Application"
|
assert caplog.records[-1].name == "telegram.ext.Application"
|
||||||
|
|
Loading…
Add table
Reference in a new issue