[Python-ideas] Query available codecs and error handlers

Steven D'Aprano steve at pearwood.info
Fri Aug 29 09:50:51 CEST 2014


On Fri, Aug 29, 2014 at 05:27:19PM +1000, Chris Angelico wrote:
> On Fri, Aug 29, 2014 at 3:45 PM, Steven D'Aprano <steve at pearwood.info> wrote:
> > Unless I am badly misinformed, there is no way to programmatically check
> > what codecs and error handlers are available. I propose two functions in
> > the codecs module:
> >
> > get_codecs()
> > get_error_handlers()
> >
> > which each return a set of the available codec, or error handler, names.
> 
> A quick look at the codecs module suggests that this may not be as
> simple as returning a list/set; when _codecs.lookup() is called, it
> does a search. 

Sure, but codecs have to be registered before they can be used. The 
register function can cache the names. Perhaps any builtin codecs might 
need to be explicitly added to the cache in order to support this, I 
don't know the details of _codecs. If the codec registry were a dict, we 
might even return a view of the dict.keys().

I'm sure there is some solution which is not quite as difficult as 
enumerating all importable modules. There's a lot fewer codecs, they 
don't have platform-dependent suffixes, and unlike modules in the 
PYTHONPATH, once Python starts up the available codecs won't change 
unless register() is called.


-- 
Steven


More information about the Python-ideas mailing list