Hacked optparse (yet again) to force it to play nice with unicode flags.

Previously, whenever a flag had unicode characters in it; it would all
completely blow up.
This commit is contained in:
Adam Waldenberg 2013-07-12 03:36:03 +02:00
parent f4b10ce15f
commit c073e32dbe
3 changed files with 33 additions and 0 deletions

View file

@ -25,3 +25,10 @@ try:
except NameError:
def unicode(string):
return str(string)
def convert_command_line():
try:
for num, arg in enumerate(sys.argv):
sys.argv[num] = unicode(arg.decode("utf-8", "replace"))
except AttributeError:
pass

View file

@ -31,6 +31,7 @@ except:
import blame
import changes
import compatibility
import config
import extensions
import filtering
@ -124,6 +125,7 @@ def __handle_version__(__option__, __opt_str__, __value__, __parser__):
sys.exit(0)
def main():
compatibility.convert_command_line()
parser = optval.OptionParser(add_help_option=False)
try:

View file

@ -18,6 +18,11 @@
from __future__ import unicode_literals
try:
from compatibility import unicode
except:
pass
import optparse
import sys
@ -77,3 +82,22 @@ class OptionParser(optparse.OptionParser):
raise OptionParsingError(_("invalid option -- '{0}'").format(variable[1:]))
raise OptionParsingError(_("invalid command-line options"))
#Originaly taken from the optparse module (and modified).
def parse_args(self, args=None, values=None):
rargs = self._get_args(args)
if values is None:
values = self.get_default_values()
#Pylint screams about these. However, this was how it was done in the original code.
self.rargs = rargs
self.largs = largs = []
self.values = values
try:
self._process_args(largs, rargs, values)
except (optparse.BadOptionError, optparse.OptionValueError) as msg:
self.error(unicode(msg))
args = largs + rargs
return self.check_values(values, args)