[Python-Dev] Easy codec access

M.-A. Lemburg mal@lemburg.com
Wed, 16 May 2001 13:06:14 +0200


Michael Hudson wrote:
> 
> Guido van Rossum <guido@digicool.com> writes:
> 
> > > I've just checked in a set of patches which implement the new
> > > .decode() method along with a couple of useful codecs.
> >
> > Cool!
> 
> Indeed.  Good idea, Marc!

Thanks :-)
 
> This is a bit unfriendly though:
> 
> >>> "bobbins".encode("gzip")
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
>   File "/usr/local/src/python/dist/build/Lib/encodings/__init__.py", line 59, in search_function
>     raise SystemError,\
> SystemError: module "encodings.gzip" failed to register
> 
> I thought SystemErrors shouldn't ever happen (isn't it what gets
> raised for an illegal opcode, for example?).

This is due to the zlib module not being installed. The reason
for the search function in encodings/__init__.py raising a
SystemError is that it did find a module named gzip, but this
module does not export the needed registration API getregentry().

Perhaps it should just raise a LookupError instead, though...
 
> > > To see just how easy it is to write codecs, please have
> > > a look at the string codecs I added in this patch (e.g.
> > > zlib_codec.py or hex_codec.py). I am pretty sure that there
> > > are a lot more useful things in the standard lib which could
> > > benefit from these easy-to-use interfaces.
> >
> > As an excercise, I added a quoted-printable codec.  It was easy
> > indeed!
> 
> urlencode would be nice.  Maybe re.escape, too.  html entities?
> That's probably a bigger can of worms, but
> 
> print "<p>%s</p>"%text.encode("html")
> 
> seems delightfully simpleminded.

Right. That's the idea... volunteers are welcome :-) 

There are lots of those little "escape this, encode that" tasks 
which could benefit from the codec machinery. The ones you
mention would certainly be good candidates. pickle and marshal
would also be a good to have wrapped as codecs.

-- 
Marc-Andre Lemburg
______________________________________________________________________
Company & Consulting:                           http://www.egenix.com/
Python Software:                        http://www.lemburg.com/python/