[Python-Dev] PEP: Post import hooks

Christian Heimes lists at cheimes.de
Thu Jan 10 23:45:41 CET 2008


Phillip J. Eby wrote:
 >> I'm not setting the hooks to Py_None before the hook are called.

Err, make that NOW, not NOT ... stupid typo.

I'm NOW setting the hooks to Py_None before the hooks are called.

> That's fine, but here's a different catch: are you iterating over the
> hooks by taking the length before you start?  If so, then hooks that are
> added *while* the hooks are being called back, will never get called,
> because they'll be added to the end of the list (and you'll never reach
> the end).  Make sure there's a test for that case.

it = iter(self.post_import_registry[name])
self.post_import_registry[name] = None
for hook in it:
    hook(module)

> peak.util.imports sets to None after callbacks, but it uses regular list
> iteration, so hooks can be added to the end of the list while the hooks
> are still being called.

In my version a hook is immediately called when the the registry value
is set to None. When a hook is registered for a module during the
execution of the callback then the hook is fired directly and not after
the existing hooks are called. Is this a problem for you?

module "foo" is loaded:
hook1
hook2 -> registers hookX for "foo"
    hookX is called directly
hook3
hook4

> An error while running the hooks should also set the hook list to None
> and discard all the hooks.  There isn't any sane way to recover from an
> error in a post-import hook.

The hooks are set to None even when an exception is raised by a hook.

Christian


More information about the Python-Dev mailing list