[Python-Dev] Cannot build new documentation

Amaury Forgeot d'Arc amauryfa at gmail.com
Mon Aug 20 23:07:52 CEST 2007


Hello,

The new documentation system is really a major improvement: congratulations!

However, I run into a problem while trying to build the new python docs.
I initially used python2.5 on windows, but Linux seems to have the same problem.
Am I really the only one who gets this error?

The offending code is in Doc\tools\sphinx\builder.py, and looks like this:

>>> from __future__ import with_statement
>>> import codecs
>>> with codecs.open('foo.txt', 'w', 'utf-8') as fp:
...     print type(fp), fp
...     fp.write(u"\xb6")
...
<type 'file'> <open file 'foo.txt', mode 'wb' at 0x00C649E8>
Traceback (most recent call last):
  File "<stdin>", line 3, in <module>
UnicodeEncodeError: 'ascii' codec can't encode character u'\xb6' in
position 0: ordinal not in range(128)

Where does the 'ascii' codec come from? I propose the following explanation:

- codecs.open returns a wrapped stream (a codec.StreamReaderWriter).
This object implements some methods (read, write...) and delegates the
others to the underlying file object.
- 'with .. as ..' calls the __enter__ method, and assigns the result to fp.
- but StreamReaderWriter does not define __enter__, so file.__enter__
is called instead
and fp actually references the underlying file!

An obvious workaround is to add the __enter__ method to StreamReaderWriter:
def __enter__(self):
    return self
This is not perfect though, because one may want to wrap say a
socket.makefile instead of a file.

It seems like the delegation pattern does not mix well with context managers...
Is there another solution?
Or did I miss something obvious?

-- 
Amaury Forgeot d'Arc


More information about the Python-Dev mailing list