5

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?

0

2 Answers 2

1

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.

5
  • Sure it is. Just replace it with an object with a custom __str__() method. Commented Jun 16, 2011 at 14:29
  • I'm just trying this, but I don't know what to put inside this __str__() methode. I mean, putting some encoding conversion here, for example for accent removing, seems the same as writing my name without accent from the very beginning, as any print will call it. Or, I should see how to be able to change the output depending on the calling function... not sure this is legal.
    – Joël
    Commented Jun 16, 2011 at 14:58
  • @Ignacio It expects a string of bytes regardless. And sys.getdefaultencoding unhelpfully says 'ascii'.
    – Josh Lee
    Commented Jun 16, 2011 at 15:02
  • Well with my little knowledge, I see no solution. Could I consider this as a bug in help() function?
    – Joël
    Commented Jun 17, 2011 at 7:59
  • I found out how to solve this (at least on my machine...), see my answer below. Any comments will be appriciated.
    – Joël
    Commented Jun 29, 2011 at 14:56
1

You need to use a simple string and not a Unicode string. Therefore:

__author__ = 'Joël'

The built-in help method then displays:

AUTHOR
    Joël

Edit: If this doesn't work, then you can force returning a 8-bit string version of your name by doing this:

 __author__ = u'Joël'.encode('utf-8')
2
  • 6
    I worry that this will not be cross platform, as it depends on the platform’s default encoding.
    – Josh Lee
    Commented Jun 16, 2011 at 14:29
  • Good idea, I did not try this before. Alas, it does not work as good as I would like it to do: on my own computer in IDLE, configured in UTF-8, I get Joël. Weird. And if I come back to explicit unicode string, no problem anywhere but with help().
    – Joël
    Commented Jun 16, 2011 at 14:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.