-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Remove DispatcherHandlerContinue + more unitests for dispatcher #792
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -55,25 +55,8 @@ def async_func(*args, **kwargs): | |
| return async_func | ||
|
|
||
|
|
||
| class DispatcherHandlerFlow(Exception): | ||
| """ | ||
| Dispatcher update processing manipulation exceptions are base on this class. | ||
| """ | ||
| pass | ||
|
|
||
|
|
||
| class DispatcherHandlerContinue(DispatcherHandlerFlow): | ||
| """ | ||
| If check Handler's check_updated returned true, but execution of handler raised this, | ||
| then handlers checking will continue. | ||
| """ | ||
| pass | ||
|
|
||
|
|
||
| class DispatcherHandlerStop(DispatcherHandlerFlow): | ||
| """ | ||
| Raise this in handler to prevent execution any other handlers (even in different group). | ||
| """ | ||
| class DispatcherHandlerStop(Exception): | ||
| """Raise this in handler to prevent execution any other handler (even in different group).""" | ||
| pass | ||
|
|
||
|
|
||
|
|
@@ -178,9 +161,10 @@ def _pooled(self): | |
| break | ||
|
|
||
| promise.run() | ||
| if isinstance(promise.exception, DispatcherHandlerFlow): | ||
| self.logger.warning('DispatcherHandlerFlow is not supported with async ' | ||
| 'functions; func: %s', promise.pooled_function.__name__) | ||
| if isinstance(promise.exception, DispatcherHandlerStop): | ||
| self.logger.warning( | ||
| 'DispatcherHandlerStop is not supported with async functions; func: %s', | ||
| promise.pooled_function.__name__) | ||
|
|
||
| def run_async(self, func, *args, **kwargs): | ||
| """ | ||
|
|
@@ -284,63 +268,54 @@ def process_update(self, update): | |
| return | ||
| for group in self.groups: | ||
| try: | ||
| for handler in self.handlers[group]: | ||
| try: | ||
| if handler.check_update(update): | ||
| try: | ||
| handler.handle_update(update, self) | ||
| except DispatcherHandlerContinue: | ||
| continue | ||
| break | ||
| except DispatcherHandlerFlow: | ||
| raise | ||
| except TelegramError as te: | ||
| self.logger.warning('A TelegramError was raised while processing the ' | ||
| 'Update.') | ||
|
|
||
| try: | ||
| self.dispatch_error(update, te) | ||
| except Exception: | ||
| self.logger.exception('An uncaught error was raised while ' | ||
| 'handling the error') | ||
| finally: | ||
| break | ||
|
|
||
| # Errors should not stop the thread | ||
| except Exception: | ||
| self.logger.exception('An uncaught error was raised while ' | ||
| 'processing the update') | ||
| break | ||
| for handler in (x for x in self.handlers[group] if x.check_update(update)): | ||
| handler.handle_update(update, self) | ||
| break | ||
|
|
||
| # Stop processing with any other handler. | ||
| except DispatcherHandlerStop: | ||
| self.logger.debug('Stopping further handlers due to DispatcherHandlerStop') | ||
| break | ||
|
|
||
| # Dispatch any error. | ||
| except TelegramError as te: | ||
| self.logger.warning('A TelegramError was raised while processing the Update') | ||
|
|
||
| try: | ||
| self.dispatch_error(update, te) | ||
| except DispatcherHandlerStop: | ||
| self.logger.debug('Error handler stopped further handlers') | ||
| break | ||
| except Exception: | ||
| self.logger.exception('An uncaught error was raised while handling the error') | ||
|
|
||
| # Errors should not stop the thread. | ||
| except Exception: | ||
| self.logger.exception('An uncaught error was raised while processing the update') | ||
|
|
||
| def add_handler(self, handler, group=DEFAULT_GROUP): | ||
| """ | ||
| Register a handler. | ||
| """Register a handler. | ||
|
|
||
| TL;DR: Order and priority counts. 0 or 1 handlers per group will be | ||
| used. | ||
| TL;DR: Order and priority counts. 0 or 1 handlers per group will be used. | ||
|
|
||
| A handler must be an instance of a subclass of :class:`telegram.ext.Handler`. All handlers | ||
| are organized in groups with a numeric value. The default group is 0. All groups will be | ||
| evaluated for handling an update, but only 0 or 1 handler per group will be used, | ||
| except situations when :class:`telegram.DispatcherHandlerContinue` or | ||
| :class:`telegram.DispatcherHandlerStop` were raised. | ||
| evaluated for handling an update, but only 0 or 1 handler per group will be used. If | ||
| :class:`telegram.DispatcherHandlerStop` is raised from one of the handlers, no further | ||
| handlers (regardless of the group) will be called. | ||
|
|
||
| The priority/order of handlers is determined as follows: | ||
|
|
||
| * Priority of the group (lower group number == higher priority) | ||
| * The first handler in a group which should handle an update will be | ||
| used. Other handlers from the group will not be used. The order in | ||
| which handlers were added to the group defines the priority. | ||
| * If :class:`telegram.DispatcherHandlerContinue` was raised, then next handler in the | ||
| same group will be called. | ||
| * If :class:`telegram.DispatcherHandlerStop` was raised, then zero handlers (even | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the bullet for DispatcherHandlerStop is still valid in the docstring?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it's kind of duplicate of the paragraph above, don't you think?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe yes, but now it looks weird as a bullet list for order with just one bullet
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. two bullets. see below
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. two bullets. the code below this patch has a second bullet... |
||
| from other groups) will called. | ||
| * The first handler in a group which should handle an update (see | ||
| :method:`telegram.ext.Handler.check_update`) will be used. Other handlers from the | ||
| group will not be used. The order in which handlers were added to the group defines the | ||
| priority. | ||
|
|
||
| Args: | ||
| handler (:class:`telegram.ext.Handler`): A Handler instance. | ||
| group (:obj:`int`, optional): The group identifier. Default is 0. | ||
|
|
||
| """ | ||
|
|
||
| if not isinstance(handler, Handler): | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I moved all docstrings to a newline after """ in all files. Can you please keep that here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
as discussed, i followed pep8