Karthikeyan Singaravelan <tir.kar...@gmail.com> added the comment: Thanks for the patch. Would you like to make a GitHub PR. I think it's a problem with optparse in general while trying to have a default value with unicode character and %default in the help string. The same code is present in Python 3 but strings are unicode by default. An example code will be below :
# -*- coding: utf-8 -*- from optparse import OptionParser parser = OptionParser() parser.add_option("-f", "--file", dest="filename", help="write to FILE. Default value %default", metavar="FILE", default="早上好") (options, args) = parser.parse_args() $ python3.6 ../backups/bpo24307.py --help Usage: bpo24307.py [options] Options: -h, --help show this help message and exit -f FILE, --file=FILE write to FILE. Default value 早上好 $ python2.7 ../backups/bpo24307.py --help Traceback (most recent call last): File "../backups/bpo24307.py", line 9, in <module> (options, args) = parser.parse_args() File "/usr/local/Cellar/python@2/2.7.14_3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/optparse.py", line 1400, in parse_args stop = self._process_args(largs, rargs, values) File "/usr/local/Cellar/python@2/2.7.14_3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/optparse.py", line 1440, in _process_args self._process_long_opt(rargs, values) File "/usr/local/Cellar/python@2/2.7.14_3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/optparse.py", line 1515, in _process_long_opt option.process(opt, value, values, self) File "/usr/local/Cellar/python@2/2.7.14_3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/optparse.py", line 789, in process self.action, self.dest, opt, value, values, parser) File "/usr/local/Cellar/python@2/2.7.14_3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/optparse.py", line 811, in take_action parser.print_help() File "/usr/local/Cellar/python@2/2.7.14_3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/optparse.py", line 1670, in print_help file.write(self.format_help().encode(encoding, "replace")) UnicodeDecodeError: 'ascii' codec can't decode byte 0xe6 in position 148: ordinal not in range(128) Thanks ---------- _______________________________________ Python tracker <rep...@bugs.python.org> <https://bugs.python.org/issue24307> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com