[Python-Dev] why doesn't print pass unicode strings on to the file object?

Martin von Loewis loewis@informatik.hu-berlin.de
Tue, 18 Sep 2001 11:56:42 +0200 (MEST)


> That won't be possible without breaking user code since
> it is well documented that codecs.lookup() returns a tuple.

Suppose codecs.lookup would return an instance of

_fields = {'encode':0,'decode':1,'reader':2,'writer':3}
class CodecInfo(tuple):
    __dynamic__ = 0
    def __getattr__(self, name):
        try:
            return self[_fields[name]]
        except KeyError:
            raise AttributeError, name

What user code exactly would break? Would that be a serious problem?

> BTW, I don't think that adding a class CodecInfo which takes
> the encoding name as constructor argument would introduce
> much inflation of functions here. You will have to provide
> such a class anyway to achieve what you are looking for, so
> I guess this is the way to go.

I guess not. If the codec.lookup return value is changed, then I can
write

encoder = codecs.lookup("latin-1").encode

Without that, I have to write

encoder = codecs.CodecInfo(codecs.lookup("latin-1")).encode

This is overly complicated.

> > It may be that an inherited tuple class might achieve the same
> > effect. Can you identify the places where codecs.lookup is assumed to
> > return tuples?
> 
> I'd rather not make the interface more complicated. The C side
> certainly cannot be changed for the reasons given above and 
> Python uses could choose your new CodecInfo class to get access
> to a nicer interface.

What code exactly would have to change if I wanted lookup to return a
CodecInfo object?

Regards,
Martin