Fixed a long-standing bug with exceptions not being able to handle unicode.

This was fixed by storing the exception string manually inside each
exception class. The error message is now stored in exception.msg instead
of relying on __str__(). It seems the normal behavior (by printing exceptions
directly) is broken in Python 2. It *does* work in Python 3, but this is
because it always handles everything as unicode.
This commit is contained in:
Adam Waldenberg 2013-07-14 03:15:14 +02:00
parent 5c9335088a
commit b28d86d1f2
3 changed files with 12 additions and 4 deletions

View File

@ -32,7 +32,11 @@ DEFAULT_FORMAT = __available_formats__[2]
__selected_format__ = DEFAULT_FORMAT
class InvalidFormatError(Exception):
pass
def __init__(self, msg):
self.msg = msg
def msg():
return self.msg
def select(format):
global __selected_format__

View File

@ -178,8 +178,8 @@ def main():
elif o in("-x", "--exclude"):
filtering.add(a)
except (format.InvalidFormatError, optval.InvalidOptionArgument, getopt.error) as msg:
print(sys.argv[0], "\b:", msg)
except (format.InvalidFormatError, optval.InvalidOptionArgument, getopt.error) as exception:
print(sys.argv[0], "\b:", exception.msg)
print(_("Try `{0} --help' for more information.").format(sys.argv[0]))
sys.exit(2)

View File

@ -20,7 +20,11 @@ from __future__ import unicode_literals
import getopt
class InvalidOptionArgument(Exception):
pass
def __init__(self, msg):
self.msg = msg
def msg():
return self.msg
def __find_arg_in_options__(arg, options):
for opt in options: