Skip to content

Commit 2c84cd3

Browse files
committed
feat(pyromod): add bound methods and refactor listener logic
1 parent 664f9cd commit 2c84cd3

18 files changed

+137
-70
lines changed

pyrogram/client.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -444,8 +444,6 @@ def loop(self, value: asyncio.AbstractEventLoop):
444444

445445
self.listeners = {listener_type: [] for listener_type in pyrogram.enums.ListenerTypes}
446446

447-
self.listeners = {listener_type: [] for listener_type in pyrogram.enums.ListenerTypes}
448-
449447
def __enter__(self):
450448
return self.start()
451449

pyrogram/enums/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
from .suggested_post_state import SuggestedPostState
5858
from .upgraded_gift_origin import UpgradedGiftOrigin
5959
from .user_status import UserStatus
60-
from .listerner_types import ListenerTypes
60+
from .listener_types import ListenerTypes
6161

6262
__all__ = [
6363
'BlockList',

pyrogram/handlers/callback_query_handler.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -140,18 +140,17 @@ async def check(self, client: "pyrogram.Client", query: CallbackQuery):
140140
else:
141141
handler_does_match = True
142142

143-
if PyromodConfig.unallowed_click_alert and listener:
143+
if PyromodConfig.unallowed_click_alert and listener and not listener_does_match:
144144
data = self.compose_data_identifier(query)
145145
permissive_identifier = Identifier(
146-
chat_id=data.chat_id,
147-
message_id=data.message_id,
148-
inline_message_id=data.inline_message_id,
146+
chat_id=listener.identifier.chat_id,
147+
message_id=listener.identifier.message_id,
148+
inline_message_id=listener.identifier.inline_message_id,
149149
from_user_id=None,
150150
)
151151

152152
if (
153153
permissive_identifier.matches(data)
154-
and not listener_does_match
155154
and listener.unallowed_click_alert
156155
):
157156
alert = (
@@ -188,18 +187,17 @@ async def resolve_future_or_callback(
188187
try:
189188
listener.future.set_result(query)
190189
except asyncio.CancelledError:
191-
# Future cancelled during shutdown; stop propagation silently
192190
raise pyrogram.StopPropagation
193-
194191
raise pyrogram.StopPropagation
192+
elif listener.future and listener.future.done():
193+
await self.original_callback(client, query, *args)
195194
elif listener.callback:
196195
try:
197196
await utils.invoke_callable(listener.callback, client, query, *args)
198197
except asyncio.CancelledError:
199198
raise pyrogram.StopPropagation
200-
201199
raise pyrogram.StopPropagation
202200
else:
203-
raise ValueError("Listener must have either a future or a callback")
201+
await self.original_callback(client, query, *args)
204202
else:
205203
await self.original_callback(client, query, *args)

pyrogram/handlers/message_handler.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,13 +139,13 @@ async def resolve_future_or_callback(self, client: "pyrogram.Client", message: M
139139
listener.future.set_result(message)
140140
except asyncio.CancelledError:
141141
raise pyrogram.StopPropagation
142-
143142
raise pyrogram.StopPropagation
143+
elif listener.future and listener.future.done():
144+
await self.original_callback(client, message, *args)
144145
elif listener.callback:
145146
await utils.invoke_callable(listener.callback, client, message, *args)
146-
147147
raise pyrogram.StopPropagation
148148
else:
149-
raise ValueError("Listener must have either a future or a callback")
149+
await self.original_callback(client, message, *args)
150150
else:
151151
await self.original_callback(client, message, *args)

pyrogram/methods/pyromod/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
from .get_many_listeners_matching_with_identifier_pattern import GetManyListenersMatchingWithIdentifierPattern
2424
from .listen import Listen
2525
from .register_next_step_handler import RegisterNextStepHandler
26-
from .remove_listerner import RemoveListener
26+
from .remove_listener import RemoveListener
2727
from .stop_listener import StopListener
2828
from .stop_listening import StopListening
2929
from .wait_for_callback_query import WaitForCallbackQuery

pyrogram/methods/pyromod/ask.py

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
1818

1919
import asyncio
20-
import inspect
2120
from typing import TYPE_CHECKING
2221
import pyrogram
2322

23+
from pyrogram import utils
2424
from pyrogram.filters import Filter
2525
from pyrogram.errors import ListenerTimeout
2626
from pyrogram.types import Identifier, Listener
@@ -137,30 +137,19 @@ async def ask(
137137

138138
try:
139139
response = await asyncio.wait_for(future, timeout)
140-
except asyncio.exceptions.TimeoutError:
140+
except asyncio.TimeoutError:
141141
if callable(PyromodConfig.timeout_handler):
142-
handler = PyromodConfig.timeout_handler
143-
144-
if (
145-
inspect.iscoroutinefunction(handler)
146-
or inspect.iscoroutinefunction(getattr(handler, "__call__", None))
147-
):
148-
result = handler(pattern, listener, timeout)
149-
if inspect.isawaitable(result):
150-
await result
151-
else:
152-
await self.loop.run_in_executor(
153-
None, handler, pattern, listener, timeout
154-
)
142+
await utils.invoke_callable(
143+
PyromodConfig.timeout_handler, pattern, listener, timeout,
144+
executor=self.executor, loop=self.loop
145+
)
155146
elif PyromodConfig.throw_exceptions:
156-
# Remove listener before raising
157147
try:
158148
self.remove_listener(listener)
159149
except Exception:
160150
pass
161151
raise ListenerTimeout(timeout)
162152

163-
# Cleanup listener on timeout to avoid zombie listeners
164153
try:
165154
self.remove_listener(listener)
166155
except Exception:

pyrogram/methods/pyromod/get_listener_matching_with_data.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def get_listener_matching_with_data(
3333
3434
Parameters:
3535
data (:obj:`~pyrogram.types.Identifier`):
36-
The Identifier to match agains.
36+
The Identifier to match against.
3737
3838
listener_type (:obj:`~pyrogram.enums.ListenerTypes`):
3939
The type of listener to get.

pyrogram/methods/pyromod/get_listener_matching_with_identifier_pattern.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def get_listener_matching_with_identifier_pattern(
3737
3838
Parameters:
3939
pattern (:obj:`~pyrogram.types.Identifier`):
40-
The Identifier to match agains.
40+
The Identifier to match against.
4141
4242
listener_type (:obj:`~pyrogram.enums.ListenerTypes`):
4343
The type of listener to get.

pyrogram/methods/pyromod/get_many_listeners_matching_with_data.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ def get_many_listeners_matching_with_data(
2727
data: Identifier,
2828
listener_type: "pyrogram.enums.ListenerTypes",
2929
) -> List[Listener]:
30-
"""Gets multiple listener that matches the given data.
30+
"""Gets multiple listeners that match the given data.
3131
3232
.. include:: /_includes/usable-by/users-bots.rst
3333
3434
Parameters:
3535
data (:obj:`~pyrogram.types.Identifier`):
36-
The Identifier to match agains.
36+
The Identifier to match against.
3737
3838
listener_type (:obj:`~pyrogram.enums.ListenerTypes`):
3939
The type of listener to get.

0 commit comments

Comments
 (0)