Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

help() with unicode __author__ string

In the beginning of my scripts in Python 2.6, I would like to write my name as it is spelled, i.e. "Joël" (with trema on e). So I write __author__ = u'Joël', and I can retrieve it by a simple print __author__.

Problem appears with the built-in help() function, as I get an error message:

UnicodeEncodeError: 'ascii' codec can't encode character u'\xeb' in position 2: ordinal not in range(128)

I cannot upgrade to Python 3.x, and I find this function very helpful (and it will surely be for those who will get my scripts). I also did not forget to encode the files in UTF-8, and to specify it in the scripts by adding this:

# -*- coding: utf-8 -*-

Any idea on where this comes from?

Thanks in advance for your answers.


EDIT Looking to the "Dive Into Python" book again, I found out how to have a correct render on my machine, see http://www.diveintopython.org/xml_processing/unicode.html.

The idea is that, my default encoding for Python was ASCII, and this did prevent help() to make a correct output. What I did is to add a script named like sitecustomize.py in {pythondir}\Lib\site-packages, setting the default encoding:

import sys
sys.setdefaultencoding('iso-8859-1')

And now, with an input string written like u'Joël', I get a correct output through call of help().

Problem is, I'm quite sure that this will break on other's computers. Any idea how I could handle this?

like image 592
Joël Avatar asked Nov 05 '22 20:11

Joël


1 Answers

Pydoc explicitly wants to convert the author name to ascii:

  File "/usr/local/Cellar/python/2.7.1/lib/python2.7/pydoc.py", line 1111, in docmodule
    result = result + self.section('AUTHOR', str(object.__author__))
UnicodeEncodeError: 'ascii' codec can't encode character u'\xeb' in position 2: ordinal not in range(128)

It’s unlikely that you can work around this.

like image 141
Josh Lee Avatar answered Nov 14 '22 00:11

Josh Lee