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

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


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