diff --git a/Lib/test/test_optparse.py b/Lib/test/test_optparse.py --- a/Lib/test/test_optparse.py +++ b/Lib/test/test_optparse.py @@ -25,6 +25,9 @@ retype = type(re.compile('')) +# Used for testing output bugs from issue #2931 +RUSSIAN_TEXT = u'\u0420\u0443\u0441\u0441\u043a\u0438\u0439 \u0442\u0435\u043a\u0441\u0442' + class InterceptedError(Exception): def __init__(self, error_message=None, @@ -1558,6 +1561,51 @@ """ self.assertHelpEquals(expect) + def test_unicode_error_issue2931(self): + """OptionParser.error function doesn't work with unicode argument""" + # We expect error() to run cleanly and then raise SystemExit. + try: + self.parser.error(RUSSIAN_TEXT) + except InterceptedError: + pass + + def test_redirected_unicode_help_issue2931(self): + """optparse fails running 'prog.py --help > out.txt' with unicode help""" + + # the following basically emulates 'prog.py --help > out.txt' + self.parser.usage = RUSSIAN_TEXT + expected = "Usage: " + RUSSIAN_TEXT + """ + +Options: + -a APPLE throw APPLEs at basket + -b NUM, --boo=NUM shout "boo!" NUM times (in order to frighten away all the + evil spirits that cause trouble and mayhem) + --foo=FOO store FOO in the foo list for later fooing + -h, --help show this help message and exit +""" + self.assertOutput(['--help'], expected) + + def test_translated_unicode_error_message_issue2931(self): + """optparse doesn't work when its error messages are gettext-translated""" + + # to emulate gettext function + def dummy_gettext(msg): + if msg == 'no such option: %s': + return RUSSIAN_TEXT + ' %s' + return msg + + try: + import optparse + old_gettext = optparse._ + optparse._ = dummy_gettext + + try: + OptionParser().parse_args(["--unknown"]) + except SystemExit: + pass + finally: + optparse._ = old_gettext + def test_help_description_groups(self): self.parser.set_description( "This is the program description for %prog. %prog has "