[Python-Dev] Getting a list of registered codecs

2 views
Skip to first unread message

Paul Moore

unread,
Apr 30, 2013, 5:15:58 AM4/30/13
to Python Dev
Before I raise a bug for this, can someone confirm if I've simply missed something? I don't see any way, either in the docs or in the helpstrings from the codecs, of listing the codecs that have been registered.

FWIW, I picked this up when I was looking at writing a simple encoding converter, and I wanted to add a flag to list what conversions were supported.
Paul.

Steven D'Aprano

unread,
Apr 30, 2013, 5:36:31 AM4/30/13
to pytho...@python.org

M.-A. Lemburg

unread,
Apr 30, 2013, 5:42:49 AM4/30/13
to Paul Moore, Python Dev
It would be possible to get a list of registered codec search functions,
but there's no API to ask the search functions for a list of supported
codecs.

If you're just looking for a list of codecs supported by the stdlib
encodings module, you can use the helper that Steven mentioned, or
you can scan the encoding aliases dictionary for codecs (but this will
not necessarily return all available codecs).

--
Marc-Andre Lemburg
eGenix.com

Professional Python Services directly from the Source (#1, Apr 30 2013)
>>> Python Projects, Consulting and Support ... http://www.egenix.com/
>>> mxODBC.Zope/Plone.Database.Adapter ... http://zope.egenix.com/
>>> mxODBC, mxDateTime, mxTextTools ... http://python.egenix.com/
________________________________________________________________________
2013-04-30: Released eGenix PyRun 1.2.0 ... http://egenix.com/go44
2013-04-17: Released eGenix mx Base 3.2.6 ... http://egenix.com/go43

::::: Try our mxODBC.Connect Python Database Interface for free ! ::::::

eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48
D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
Registered at Amtsgericht Duesseldorf: HRB 46611
http://www.egenix.com/company/contact/

M.-A. Lemburg

unread,
Apr 30, 2013, 6:18:27 AM4/30/13
to Paul Moore, Python Dev
On 30.04.2013 11:52, Paul Moore wrote:
> On 30 April 2013 10:42, M.-A. Lemburg <m...@egenix.com> wrote:
>
>> It would be possible to get a list of registered codec search functions,
>> but there's no API to ask the search functions for a list of supported
>> codecs.
>>
>
> OK, so there's no way to determine in advance what values of enc will work
> in bytestr.decode(enc) or str.encode(enc)?
>
> Is there a reason why not?

Nothing in particular, except maybe that it can be expensive to generate
such a list (e.g. you'd have to verify that the codec modules import
correctly and provide the needed getregentry() API).

> As I say, a tool that offers to re-encode a file
> could reasonably be expected to list the encodings it supported (if only to
> help the user work out which way to spell utf-16le or utf16le or utf16-le
> or utf-16-le or... :-))
>
> I've raised http://bugs.python.org/issue17878 for this. Further discussion
> may be more appropriate there than on python-dev.

Paul Moore

unread,
Apr 30, 2013, 5:52:49 AM4/30/13
to M.-A. Lemburg, Python Dev
On 30 April 2013 10:42, M.-A. Lemburg <m...@egenix.com> wrote:
It would be possible to get a list of registered codec search functions,
but there's no API to ask the search functions for a list of supported
codecs.

OK, so there's no way to determine in advance what values of enc will work in bytestr.decode(enc) or str.encode(enc)?

Is there a reason why not? As I say, a tool that offers to re-encode a file could reasonably be expected to list the encodings it supported (if only to help the user work out which way to spell utf-16le or utf16le or utf16-le or utf-16-le or... :-))

I've raised http://bugs.python.org/issue17878 for this. Further discussion may be more appropriate there than on python-dev.

Paul
Reply all
Reply to author
Forward
0 new messages