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?
Not sure - but it works fine for me.
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>
That is surprising. Are you sure codecs.open is what it should be on your system? I get <type 'instance'> <open file 'foo.txt', mode 'wb' at 0xb7d89e78>
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 refrences the underlying file!
Good explanation. I'm using Python 2.5.1, and this may be relevant: ------------------------------------------------------------------------ r52518 | georg.brandl | 2006-10-29 09:39:27 +0100 (So, 29 Okt 2006) | 4 lines Fix codecs.EncodedFile which did not use file_encoding in 2.5.0, and fix all codecs file wrappers to work correctly with the "with" statement (bug #1586513). (backport from rev. 52517)
It seems like the delegation pattern does not mix well with context managers... Is there another solution? Or did I miss something obvious?
I think the obvious thing you missed is that the problem got fixed already. Whether the documentation system should be more defensive and work with 2.5.0 also is a different question. Regards, Martin