Andrew Kuchling wrote:
Are there any codecs that can take parameters at initialization time? If not, how about supporting them?
Even though this is not documented in the Unicode PEP, the extensibility of the constructor using extra (keyword) arguments is intended.
Here's a motivating example:
import codecs f = codecs.open('output', 'wb', encoding='DES', # arguments to open() key = '...', mode = DES.ECB) f.write('This will be encrypted\n') f.close()
For this to work, you'd need a few changes to codecs.py. Off the top of my head, I think StreamReader, StreamWriter, and StreamReaderWriter all need to grow **kwargs arguments for any additional arguments, as do codecs.open() and codecs.lookup().
The codec design allows extending the constructors using keyword arguments, however I'd rather not add a generic **kws argument to the base classes since this would hide errors (unknown or unsupported keywords, mispellings, etc.).
The codec.open() API is different though: it could grow a **kws argument which is then apply()ed to the codec constructor. The constructor will then raise any exceptions related to malformed keyword parameters.
There's no need to add **kws to codec.lookup().
Then someone could write a DES StreamReader/Streamwriter that took a key and feedback mode at creation time. (codecs.Codec instances are supposed to be stateless, right?)
The encoder and decoder functions must work stateless. StreamReader/Writer instances should be used for everything that has to do with state since these are instantiatied per use whereas encoder and decoder functions fetched using codec.lookup() can be used multiple times.