This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: optparse does not hande unicode help strings
Type: Stage:
Components: Unicode Versions: Python 2.4
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: gward Nosy List: gward, tomcato
Priority: normal Keywords:

Created on 2006-05-31 11:52 by tomcato, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (2)
msg28693 - (view) Author: Tom Cato Amundsen (tomcato) Date: 2006-05-31 11:52
Unicode strings with non-ascii chars in generate a
UnicodeEncodeError

  File "/usr/lib/python2.3/optparse.py", line 1370, in
print_help
    file.write(self.format_help())
UnicodeEncodeError: 'ascii' codec can't encode
characters in position 200-202:
+ordinal not in range(128)

I'm subclassing OptionParser and adds the following
method to get it to work. Should something like this be
done for python 2.5?

def print_help(self, file=None):
    if file is None:
        file = sys.stdout
    file.write(self.format_help().encode(file.encoding,
'replace'))


Tom Cato
msg28694 - (view) Author: Greg Ward (gward) (Python committer) Date: 2006-06-11 16:25
Logged In: YES 
user_id=14422

Fixed pretty much as suggested in upstream Optik repository:
r520.

(However, I had to use
  getattr(file, 'encoding', sys.getdefaultencoding()) 
to maintain compatibility with Python 2.0 .. 2.2).

No doc changes, since help generation isn't really
documented all that well anywhere.  ;-(

Fixed on Python trunk: r46861.
History
Date User Action Args
2022-04-11 14:56:17adminsetgithub: 43438
2006-05-31 11:52:05tomcatocreate