[Python-Dev] regrtest.py mystery

Martin v. Loewis martin@v.loewis.de
Sat, 15 Dec 2001 23:52:17 +0100


> What's up with that?  So far, they all seem to involve the encodings
> directory ...

Python/codecs.c holds search functions in _PyCodec_SearchPath, among
them encodings.__init__.search_function. It also imports "encodings"
the first time somebody invokes .encode/.decode on some kind of
string, but doesn't hold onto the module.

When the last reference to encodings.__init__ goes away, Python will
clear all globals - but codecs still holds a reference to
search_function, so that won't go away. Invoking search_function later
will cause the problem you see.

One solution would be to bind all globals used inside
encodings.__init__.search_function to the search_function, e.g. by
means of a class:

class GlobalSearchFunction:
  _cache =
  _unknown = 
  class CodecRegistryError(...):
  def search_function(self,encoding):

codecs.register(GlobalSearchFunction().search_function)

Of course, if this gets cleaned from sys.modules, nobody will be able
to catch CodecRegistryError anymore (since re-importing the module
will produce a different class).

Regards,
Martin