[Import-sig] Imports with hooks not thread safe?

Gordon McMillan gmcm@hypernet.com
Sun, 14 Jul 2002 09:58:26 -0400


On 13 Jul 2002 at 20:55, Anthony Tuininga wrote:

> Thanks. But wouldn't it make more sense to acquire the
> lock around __any__ import, regardless of whether it
> was hooked or not? 

You can't. Hooks typically *replace*
builtin.__import__. Imports from Python code
(where import has been hooked) don't necessarily touch
anything in the builtin Python import machinery.

[...]

> I agree that the exposing of the import lock would
> also solve the problem but that would mean that all
> the import hooks would have to do this in order to
> be thread safe. 

Correct. Writing a good import hook isn't easy.
Most import hooks are specializations or extensions
of behavior in some little area, so can get away with
it.

> Or could it be placed in the core
> imputils.py in the base class? Comments? 

iu is a replacement for imputils. imputils has a
number of flaws - no import lock; it does not
put None in sys.modules when the "is this a
relative import?" test fails. Can't recall what else,
but that's why I wrote iu.

-- Gordon
http://www.mcmillan-inc.com/