[2.7] bpo-24307: Fix unicode error in optparse with unicode value for %default by tirkarthi · Pull Request #9911 · python/cpython (original) (raw)

I found one more error where if the conflicting option is a unicode string then it throws UnicodeDecodeError.

from optparse import OptionParser

parser = OptionParser()
parser.add_option("-f", u"--file", dest="filename",
                  help="write to FILE. Default value %default",
                  metavar="FILE",
                  default=u"   ")

parser.add_option("-f", u"--   ", dest="filename",
                  help="write to FILE. Default value %default",
                  metavar="FILE",
                  default=u"   ")


(options, args) = parser.parse_args()
./python.exe ../backups/bpo24307.py -f
Traceback (most recent call last):
  File "../backups/bpo24307.py", line 14, in <module>
    default=u"早上好")
  File "/Users/karthikeyansingaravelan/stuff/python/cpython/Lib/optparse.py", line 1021, in add_option
    self._check_conflict(option)
  File "/Users/karthikeyansingaravelan/stuff/python/cpython/Lib/optparse.py", line 996, in _check_conflict
    option)
  File "/Users/karthikeyansingaravelan/stuff/python/cpython/Lib/optparse.py", line 113, in __init__
    self.option_id = str(option)
UnicodeEncodeError: 'ascii' codec can't encode characters in position 5-7: ordinal not in range(128)