[Python-Dev] Add transform() and untranform() methods

Victor Stinner victor.stinner at gmail.com
Thu Nov 14 23:40:41 CET 2013

Oh, I forgot to mention that I sent this email in reaction to this issue:


Modifying the critical PyFrameObject because the codecs API raises
surprising errors doesn't sound correct. I prefer to fix how codecs
are used, than modifying the PyFrameObject.

For more information, see the issue #7475 which a long history (4
years) and many messages. Martin von Loewis wrote "I would still be
opposed to such a change, and I think it needs a PEP." and I still
agree with him on this point. Because they are different opinions and
no consensus, a PEP is required to explain why we took this decision
and list rejected alternatives.



2013/11/14 Victor Stinner <victor.stinner at gmail.com>:
> Hi,
> I saw that Nick Coghlan documented codecs.encode() and
> codecs.decode(), and changed the exception raised when codecs like
> rot_13 are used on bytes.decode() and str.encode().
> I don't like the functions codecs.encode() and codecs.decode() because
> the type of the result depends on the encoding (second parameter). We
> try to avoid this in Python.
> I would prefer to split the registry of codecs to have 3 registries:
> - "encoding" (a better name can found): encode str=>bytes, decode bytes=>str
> - bytes: encode bytes=>bytes, decode bytes=>bytes
> - str:  encode str=>str, decode str=>str
> And add transform() and untransform() methods to bytes and str types.
> In practice, it might be same codecs registry for all codecs just with
> a new attribute.
> Examples:
> - utf8: encoding
> - zlib: bytes
> - rot13: str
> The result type of bytes.transform/untransform would be bytes, and the
> result type of str.transform/untransform would be str.
> I don't know which exception should be raised when a codec is used in
> the wrong method. LookupError? TypeError "codec xxx cannot be used
> with method xxx.xx"? Something else?
> codecs.encode/decode() documentation should be removed. The functions
> should be kept, just in case if someone uses them.
> Victor

More information about the Python-Dev mailing list