diff --git a/Lib/optparse.py b/Lib/optparse.py index bec492d8cdbf1f2..733cdc230f9ed96 100644 --- a/Lib/optparse.py +++ b/Lib/optparse.py @@ -285,7 +285,7 @@ def expand_default(self, option): if default_value is NO_DEFAULT or default_value is None: default_value = self.NO_DEFAULT_VALUE - return option.help.replace(self.default_tag, str(default_value)) + return option.help.replace(self.default_tag, unicode(default_value)) def format_option(self, option): # The help for each option consists of two parts: diff --git a/Lib/test/test_optparse.py b/Lib/test/test_optparse.py index dc2ef0b78675169..6551e2839e75f17 100644 --- a/Lib/test/test_optparse.py +++ b/Lib/test/test_optparse.py @@ -613,6 +613,15 @@ def test_float_default(self): " -p PROB, --prob=PROB blow up with probability PROB [default: 0.43]\n" self.assertHelp(self.parser, expected_help) + def test_unicode_default(self): + self.parser.add_option( + "-p", "--prob", + help="blow up with probability PROB [default: %default]") + self.parser.set_defaults(prob=u"ol\u00E9!") + expected_help = self.help_prefix + \ + u" -p PROB, --prob=PROB blow up with probability PROB [default: ol\u00E9!]\n" + self.assertHelp(self.parser, expected_help) + def test_alt_expand(self): self.parser.add_option("-f", "--file", default="foo.txt", diff --git a/Misc/NEWS.d/next/Library/2018-09-23-19-44-54.bpo-24307.EAcncD.rst b/Misc/NEWS.d/next/Library/2018-09-23-19-44-54.bpo-24307.EAcncD.rst new file mode 100644 index 000000000000000..32e3d3c23c633ac --- /dev/null +++ b/Misc/NEWS.d/next/Library/2018-09-23-19-44-54.bpo-24307.EAcncD.rst @@ -0,0 +1,2 @@ +Encode default value before replacing it in the option. Based on patch by +tanbro-liu.