[issue17878] There is no way to get a list of available codecs
Walter Dörwald
report at bugs.python.org
Thu May 2 16:45:58 CEST 2013
Walter Dörwald added the comment:
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.
----------
nosy: +doerwalter
_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue17878>
_______________________________________
More information about the Python-bugs-list
mailing list