Message188269
The point of using a function is to allow the function special hanling of the encoding name, which goes beyond a simple map lookup, i.e. you could do the following:
import codecs
def search_function(encoding):
if not encoding.startswith("append-"):
return None
suffix = encoding[7:]
def encode(s, errors="strict"):
s = (s + suffix).encode("utf-8", errors)
return (s, len(s))
def decode(s, errors="strict"):
s = bytes(s).decode("utf-8", errors)
if s.endswith(suffix):
s = s[:-len(suffix)]
return (s, len(s))
return codecs.CodecInfo(encode, decode, name=encoding)
codecs.register(search_function)
$ python
Python 3.3.1 (default, Apr 29 2013, 15:35:47)
[GCC 4.2.1 Compatible Apple LLVM 4.2 (clang-425.0.24)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import appendcodec
>>> 'foo'.encode('append-bar')
b'foobar'
>>> b'foobar'.decode('append-bar')
'foo'
The search function can't return a list of codec names in this case, as the list is infinite. |
|
Date |
User |
Action |
Args |
2013-05-02 14:45:58 | doerwalter | set | recipients:
+ doerwalter, lemburg, paul.moore, ncoghlan, vstinner, ezio.melotti, dmi.baranov |
2013-05-02 14:45:58 | doerwalter | set | messageid: <1367505958.56.0.481860488119.issue17878@psf.upfronthosting.co.za> |
2013-05-02 14:45:58 | doerwalter | link | issue17878 messages |
2013-05-02 14:45:58 | doerwalter | create | |
|