From 80021a294a7fb786e751e946a6b05f9b2a2b8ec0 Mon Sep 17 00:00:00 2001 From: xtreak Date: Sun, 23 Sep 2018 19:40:27 +0530 Subject: [PATCH 1/3] Encode default value before replacement in option --- Lib/optparse.py | 8 +++++++- Lib/test/test_optparse.py | 9 +++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/Lib/optparse.py b/Lib/optparse.py index bec492d8cdbf1f2..13777cfe2fbc4fc 100644 --- a/Lib/optparse.py +++ b/Lib/optparse.py @@ -285,7 +285,13 @@ 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)) + encoding = sys.getfilesystemencoding() or sys.getdefaultencoding() + if isinstance(default_value, unicode): + default_value = default_value.encode(encoding) + else: + default_value = str(default_value) + + return option.help.replace(self.default_tag, 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..abb7e1fd6f8212c 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 + \ + " -p PROB, --prob=PROB blow up with probability PROB [default: ol\xc3\xa9!]\n" + self.assertHelp(self.parser, expected_help) + def test_alt_expand(self): self.parser.add_option("-f", "--file", default="foo.txt", From ae41d972b268cba2803b2537718da3886daff382 Mon Sep 17 00:00:00 2001 From: xtreak Date: Sun, 23 Sep 2018 19:45:04 +0530 Subject: [PATCH 2/3] Add NEWS --- .../next/Library/2018-09-23-19-44-54.bpo-24307.EAcncD.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2018-09-23-19-44-54.bpo-24307.EAcncD.rst 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. From fc3c87da356741f15780ef6e08fbc293260ff6f0 Mon Sep 17 00:00:00 2001 From: xtreak Date: Tue, 16 Oct 2018 18:48:11 +0530 Subject: [PATCH 3/3] Use unicode to fix double encoding during conversion --- Lib/optparse.py | 8 +------- Lib/test/test_optparse.py | 2 +- 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/Lib/optparse.py b/Lib/optparse.py index 13777cfe2fbc4fc..733cdc230f9ed96 100644 --- a/Lib/optparse.py +++ b/Lib/optparse.py @@ -285,13 +285,7 @@ def expand_default(self, option): if default_value is NO_DEFAULT or default_value is None: default_value = self.NO_DEFAULT_VALUE - encoding = sys.getfilesystemencoding() or sys.getdefaultencoding() - if isinstance(default_value, unicode): - default_value = default_value.encode(encoding) - else: - default_value = str(default_value) - - return option.help.replace(self.default_tag, 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 abb7e1fd6f8212c..6551e2839e75f17 100644 --- a/Lib/test/test_optparse.py +++ b/Lib/test/test_optparse.py @@ -619,7 +619,7 @@ def test_unicode_default(self): help="blow up with probability PROB [default: %default]") self.parser.set_defaults(prob=u"ol\u00E9!") expected_help = self.help_prefix + \ - " -p PROB, --prob=PROB blow up with probability PROB [default: ol\xc3\xa9!]\n" + 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):