Fixed catching unnececcary exceptions
authorAndrey Vlasovskikh <andrey.vlasovskikh@gmail.com>
Sat, 11 Sep 2010 15:16:24 +0400
changeset 119 f9c027029540
parent 118 51f4b89d6c8d
child 120 bf6908d12aae
Fixed catching unnececcary exceptions Exception Abort can't be thrown, so it is useless. Catching SystemExit and KeyboardInterrupt makes little sense as the thread can be terminated by KeyboardInterrupt before or after entering the wrapped command processing function. So one have to handle these exceptions in his main function anyway. Catching any exception in order to print a message and re-raise it changes the standard behaviour and might seem confusing.
opster.py
--- a/opster.py	Sun Sep 19 19:25:02 2010 +0300
+++ b/opster.py	Sat Sep 11 15:16:24 2010 +0400
@@ -74,18 +74,12 @@
                 # no catcher here because this is call from Python
                 return call_cmd_regular(func, options_)(*args, **opts)
 
-            try:
-                opts, args = catcher(lambda: parse(argv, options_), help_func)
-            except Abort:
-                return -1
+            opts, args = catcher(lambda: parse(argv, options_), help_func)
 
-            try:
-                if opts.pop('help', False):
-                    return help_func()
-                return catcher(lambda: call_cmd(name_, func)(*args, **opts),
-                               help_func)
-            except Abort:
-                return -1
+            if opts.pop('help', False):
+                return help_func()
+            return catcher(lambda: call_cmd(name_, func)(*args, **opts),
+                           help_func)
 
         return inner
     return wrapper
@@ -125,20 +119,17 @@
 
     autocomplete(cmdtable, args, middleware)
 
-    try:
-        name, func, args, kwargs = catcher(
-            lambda: _dispatch(args, cmdtable, globaloptions),
+    name, func, args, kwargs = catcher(
+        lambda: _dispatch(args, cmdtable, globaloptions),
+        help_func)
+    if name == '_completion':       # skip middleware
+        return catcher(
+            lambda: call_cmd(name, func)(*args, **kwargs),
             help_func)
-        if name == '_completion':       # skip middleware
-            return catcher(
-                lambda: call_cmd(name, func)(*args, **kwargs),
-                help_func)
-        else:
-            return catcher(
-                lambda: call_cmd(name, middleware(func))(*args, **kwargs),
-                help_func)
-    except Abort:
-        return -1
+    else:
+        return catcher(
+            lambda: call_cmd(name, middleware(func))(*args, **kwargs),
+            help_func)
 
 # --------
 # Help
@@ -460,15 +451,6 @@
         help_func()
     except FOError, e:
         err('%s\n' % e)
-    except KeyboardInterrupt:
-        err('interrupted!\n')
-    except SystemExit:
-        raise
-    except:
-        err('unknown exception encountered')
-        raise
-
-    raise Abort
 
 def call_cmd(name, func):
     def inner(*args, **kwargs):
@@ -622,8 +604,5 @@
 class ParseError(CommandException):
     'Raised on error in command line parsing'
 
-class Abort(CommandException):
-    'Abort execution'
-
 class FOError(CommandException):
     'Raised on trouble with opster configuration'