[Python-Dev] A codecs nit (was Re: bytes.from_hex())

Barry Warsaw barry at python.org
Wed Feb 15 22:57:41 CET 2006


On Wed, 2006-02-15 at 22:07 +0100, M.-A. Lemburg wrote:

> Those are not pseudo-encodings, they are regular codecs.
> 
> It's a common misunderstanding that codecs are only seen as serving
> the purpose of converting between Unicode and strings.
> 
> The codec system is deliberately designed to be general enough
> to also work with many other types, e.g. it is easily possible to
> write a codec that convert between the hex literal sequence you
> have above to a list of ordinals:

Slightly off-topic, but one thing that's always bothered me about the
current codecs implementation is that str.encode() (and friends)
implicitly treats its argument as module, and imports it, even if the
module doesn't live in the encodings package.  That seems like a mistake
to me (and a potential security problem if the import has side-effects).
I don't know whether at the very least restricting the imports to the
encodings package would make sense or would break things.

>>> import sys
>>> sys.modules['smtplib']
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
KeyError: 'smtplib'
>>> ''.encode('smtplib')
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
LookupError: unknown encoding: smtplib
>>> sys.modules['smtplib']
<module 'smtplib' from '/usr/lib/python2.4/smtplib.pyc'>

I can't see any reason for allowing any randomly importable module to
act like an encoding.
-Barry

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 307 bytes
Desc: This is a digitally signed message part
Url : http://mail.python.org/pipermail/python-dev/attachments/20060215/b3306099/attachment-0001.pgp 


More information about the Python-Dev mailing list