various re-import errors

Guido.van.Rossum@cwi.nl
Wed, 13 Apr 1994 15:20:59 +0200

I've received various reports on core dumps and errors caused by
attempts to reload or re-import built-in or dynamically loaded
modules.

Specifically:

(1)

import foo # where foo is a dynamically loaded module
reload(foo)

causes a core dump, at least on systems using SVR4 shared libraries
for dynamic module loading.

(2)
import sys
del sys.modules['sys']
import sys

dumps core.

(3)

import math
reload(math)

raises ImportError: No module named math

(4)

import stdwin
import sys
del sys.modules['stdwin']
import stdwin

does not actually seem to fail but looks very scary because stdwin
cannot be initialized twice in the same process.

In Python version 1.0.1++ (to be released as 1.0.2) I've added the
following checks:

- Calling reload() for dynamically loaded modules (case 1) is
forbidden and raises an ImportError

- Ditto for built-in modules (case 3)

- Attempts to force a reinitialization of a few privileged built-in
modules like sys, __main__ and __builtin__ by deleting them from
sys.modules (case 2) raises ImportError

- Similar attempts for non-privileged built-in modules (case 4) are
not caught. This may still dump core depending on whether the
module's init*() function can be called more than once or not.
(Forbidding this would require more work as it would require
maintaining a table of flags telling whether a particular module was
already initialized.)

- Calling reload() for frozen modules is fixed and should now work as
expected (except I haven't tested it yet :).

--Guido van Rossum, CWI, Amsterdam <Guido.van.Rossum@cwi.nl>
URL: <http://www.cwi.nl/cwi/people/Guido.van.Rossum.html>