[Python-Dev] Stream codecs and _multibytecodec

Thomas Wouters thomas at python.org
Fri Apr 21 01:06:30 CEST 2006

While merging the trunk changes into the p3yk branch, I discovered what I
think is a bug in the stream codecs. It's easily reproduced in the trunk: in
Lib/codecs.py, make the 'Codec' class new-style. Then, suddenly, test_codecs
will crash with an exception like this:

ERROR: test_basics (test.test_codecs.BasicUnicodeTest)
Traceback (most recent call last):
  File "/home/thomas/python/python/trunk/Lib/test/test_codecs.py", line
1089, in test_basics
    writer = codecs.getwriter(encoding)(q)
  File "/home/thomas/python/python/trunk/Lib/codecs.py", line 296, in
    self.stream = stream
TypeError: readonly attribute

Looking at the class, it's hard to tell how it's suddenly a read-only
attribute, until you figure out which codec it breaks with: big5. Which is
defined in encodings.big5, as a subclass of codecs.Codec,
_multibytecodec.MultibyteStreamWriter and codecs.StreamWriter. And
_multibytecodec.MultibyteStreamWriter is a new-style class (as it's defined
in C) with a read-only 'stream' attribute. The conflicting attribute is
silently masked by Python, since classic classes ignore two-thirds of the
descriptor protocol (setting and deleting), but it jumps right out when all
classes become new-style.

I'm not sure whether this attribute conflict is on purpose or not, but since
it will break in the future, I suggest it gets fixed. It *looks* like
renaming the _MultibyteStreamWriter attribute is the easiest solution, but I
don't know which API has precedence. (One of the two was added fairly
recently, at least, since it didn't break in the p3yk branch until I merged
in the last few months' worth of changes :)

Thomas Wouters <thomas at python.org>

Hi! I'm a .signature virus! copy me into your .signature file to help me
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/python-dev/attachments/20060421/cf9e1aa1/attachment.htm 

More information about the Python-Dev mailing list