[Python-Dev] import succeeds on second try?

Guido van Rossum guido@digicool.com
Sat, 10 Feb 2001 22:00:31 -0500


> On Sat, 10 Feb 2001, Guido van Rossum wrote:
> > 
> > That's standard stuff; happens all the time.
> 
> Hrmm... it makes me feel icky.

Maybe, but so does the alternative (to me, anyway).

> > 1. The module gets compiled to bytecode, and the compiled bytecode
> >    gets written to the .pyc file, before any attempt to execute is.
> > 
> > 2. The spam module gets entered into sys.modules at the *start* of its
> >    execution, for a number of reasons having to do with mutually
> >    recursive modules.
> > 
> > 3. The execution fails on the "import eggs" but that doesn't undo the
> >    sys.modules assignment.
> > 
> > 4. The second import of spam finds an incomplete module in
> >    sys.modyles, but doesn't know that, so returns it.
> 
> Is there a reason not to insert step 3.5?
> 
> 3.5.  If the import fails, remove the incomplete module from sys.modules.

It's hard to prove that there are no other references to it, e.g. spam
could have imported bacon which imports fine and imports spam (for a
later recursive call).  Then a second try to import spam would import
bacon again but that bacon would have a reference to the first,
incomplete copy of spam.  In general, if I can help it, I want to be
careful that I don't have multiple module objects claiming to be the
same module around, because that multiplicity will come back to bite
you when it matters that they are the same.

Also, deleting the evidence makes it harder to inspect the smoking
remains in a debugger.

--Guido van Rossum (home page: http://www.python.org/~guido/)