|
18 | 18 |
|
19 | 19 | import asyncio |
20 | 20 | from collections import OrderedDict |
21 | | -import inspect |
22 | 21 | import logging |
23 | 22 | from typing import Dict |
24 | 23 |
|
@@ -262,12 +261,7 @@ async def deleted_business_messages_parser(update, users, chats): |
262 | 261 | async def start(self): |
263 | 262 | if callable(self.client.start_handler): |
264 | 263 | try: |
265 | | - if inspect.iscoroutinefunction(self.client.start_handler): |
266 | | - await self.client.start_handler(self.client) |
267 | | - else: |
268 | | - result = self.client.start_handler(self.client) |
269 | | - if inspect.isawaitable(result): |
270 | | - await result |
| 264 | + await utils.invoke_callable(self.client.start_handler, self.client) |
271 | 265 | except Exception as e: |
272 | 266 | log.exception("start_handler raised: %s", e) |
273 | 267 |
|
@@ -325,12 +319,7 @@ async def stop(self, clear_handlers: bool = True): |
325 | 319 |
|
326 | 320 | if callable(self.client.stop_handler): |
327 | 321 | try: |
328 | | - if inspect.iscoroutinefunction(self.client.stop_handler): |
329 | | - await self.client.stop_handler(self.client) |
330 | | - else: |
331 | | - result = self.client.stop_handler(self.client) |
332 | | - if inspect.isawaitable(result): |
333 | | - await result |
| 322 | + await utils.invoke_callable(self.client.stop_handler, self.client) |
334 | 323 | except Exception as e: |
335 | 324 | log.exception("stop_handler raised: %s", e) |
336 | 325 |
|
@@ -437,15 +426,10 @@ async def handler_worker(self, lock): |
437 | 426 | continue |
438 | 427 |
|
439 | 428 | try: |
440 | | - if inspect.iscoroutinefunction(handler.callback): |
441 | | - await handler.callback(self.client, *args) |
442 | | - else: |
443 | | - await self.client.loop.run_in_executor( |
444 | | - self.client.executor, |
445 | | - handler.callback, |
446 | | - self.client, |
447 | | - *args |
448 | | - ) |
| 429 | + await utils.invoke_callable( |
| 430 | + handler.callback, self.client, *args, |
| 431 | + executor=self.client.executor, loop=self.client.loop |
| 432 | + ) |
449 | 433 | except asyncio.CancelledError: |
450 | 434 | raise |
451 | 435 | except pyrogram.StopPropagation: |
@@ -482,15 +466,11 @@ async def handle_update_handler_exception( |
482 | 466 | continue |
483 | 467 |
|
484 | 468 | try: |
485 | | - if inspect.iscoroutinefunction(handler.callback): |
486 | | - await handler.callback( |
487 | | - self.client, exc, update_handler, update, users, chats |
488 | | - ) |
489 | | - else: |
490 | | - await self.client.loop.run_in_executor( |
491 | | - self.client.executor, handler.callback, |
492 | | - self.client, exc, update_handler, update, users, chats |
493 | | - ) |
| 469 | + await utils.invoke_callable( |
| 470 | + handler.callback, |
| 471 | + self.client, exc, update_handler, update, users, chats, |
| 472 | + executor=self.client.executor, loop=self.client.loop |
| 473 | + ) |
494 | 474 | except pyrogram.StopPropagation: |
495 | 475 | handled = True |
496 | 476 | raise |
|
0 commit comments