Message188269
| Author |
doerwalter |
| Recipients |
dmi.baranov, doerwalter, ezio.melotti, lemburg, ncoghlan, paul.moore, vstinner |
| Date |
2013年05月02日.14:45:58 |
| SpamBayes Score |
-1.0 |
| Marked as misclassified |
Yes |
| Message-id |
<1367505958.56.0.481860488119.issue17878@psf.upfronthosting.co.za> |
| In-reply-to |
| Content |
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. |
|