diff -r cf70f030a744 Lib/optparse.py --- a/Lib/optparse.py Wed Jun 18 23:07:46 2014 -0400 +++ b/Lib/optparse.py Mon Jun 23 21:45:46 2014 +0300 @@ -1666,8 +1666,12 @@ """ if file is None: file = sys.stdout - encoding = self._get_encoding(file) - file.write(self.format_help().encode(encoding, "replace")) + try: + encoding = self._get_encoding(file) + except AttributeError: + file.write(self.format_help()) + else: + file.write(self.format_help().encode(encoding, "replace")) # class OptionParser diff -r cf70f030a744 Lib/test/test_optparse.py --- a/Lib/test/test_optparse.py Wed Jun 18 23:07:46 2014 -0400 +++ b/Lib/test/test_optparse.py Mon Jun 23 21:45:46 2014 +0300 @@ -1504,7 +1504,7 @@ return InterceptingOptionParser(option_list=options) def assertHelpEquals(self, expected_output): - if type(expected_output) is types.UnicodeType: + if test_support.have_unicode and type(expected_output) is types.UnicodeType: encoding = self.parser._get_encoding(sys.stdout) expected_output = expected_output.encode(encoding, "replace") @@ -1542,21 +1542,23 @@ self.parser = self.make_parser(0) self.assertHelpEquals(_expected_very_help_short_lines) + @test_support.requires_unicode def test_help_unicode(self): self.parser = InterceptingOptionParser(usage=SUPPRESS_USAGE) - self.parser.add_option("-a", action="store_true", help=u"ol\u00E9!") + self.parser.add_option("-a", action="store_true", help=u"ol\xE9!") expect = u"""\ Options: -h, --help show this help message and exit - -a ol\u00E9! + -a ol\xE9! """ self.assertHelpEquals(expect) + @test_support.requires_unicode def test_help_unicode_description(self): self.parser = InterceptingOptionParser(usage=SUPPRESS_USAGE, - description=u"ol\u00E9!") + description=u"ol\xE9!") expect = u"""\ -ol\u00E9! +ol\xE9! Options: -h, --help show this help message and exit