classification
Title: Non-existant directory in sys.path prevents further imports
Type: behavior Stage:
Components: Interpreter Core Versions: Python 3.0, Python 3.1, Python 2.6, Python 2.5
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: cemasoniv, ggenellina
Priority: normal Keywords:

Created on 2009-08-03 20:36 by cemasoniv, last changed 2010-08-03 21:32 by terry.reedy. This issue is now closed.

Files
File name Uploaded Description Edit
test.py cemasoniv, 2009-08-03 20:36 Test case
Messages (2)
msg91241 - (view) Author: Charles Mason (cemasoniv) Date: 2009-08-03 20:36
Steps to reproduce:

1) Add to sys.path a path that does not exist
2) Import a module, any module.  This invokes get_path_importer over
every element of sys.path.  The NullImporter __init__ method is called
and an instance created for each non-existing path element in sys.path.
3) Create the path and put a valid module in said path
4) Try to import that module.

Behavior is that the interpreter fails to import.  This behavior seems
local to import.c only.

Attached a test case that shows "failed" for 2.5, 2.6, 3.0. 3.1rc1+.  I
have yet to test on the trunk but I can/will do that if necessary.

I believe I can fix this myself but I want to verify this is incorrect
behavior (a bug) and not an "Undocumented Feature" (or hell, maybe it
*is* documented somewhere).  I've skimmed over PEP 302 and didn't see
any relevant information.

If someone gives me the go ahead (or at least doesn't give me reason not
to), I'll get a patch put together, perhaps for several different "fixes".
msg91287 - (view) Author: Gabriel Genellina (ggenellina) Date: 2009-08-05 00:05
Looks like an optimization for the common case when directories aren't 
created "on the fly". Your proposal would slow down all imports (a 
typical sys.path actually contains a few nonexisting directories).

Quoting PEP302:

    The results of path hook checks are cached in
    sys.path_importer_cache, which is a dictionary mapping path entries
    to importer objects.  The cache is checked before sys.path_hooks is
    scanned.  If it is necessary to force a rescan of sys.path_hooks, it
    is possible to manually clear all or part of
    sys.path_importer_cache.

So the problem is that some cached data is outdated. In this case, 
after creating a directory that might be listed in sys.path, you should 
clear the cached entries using either of these lines:

sys.path_importer_cache.pop("/tmp/foobar", None)
sys.path_importer_cache.clear()

and then your test code succeeds.

(I'd close the issue as 'invalid')
History
Date User Action Args
2010-08-03 21:32:55terry.reedysetstatus: open -> closed
resolution: not a bug
2009-08-05 00:05:56ggenellinasetnosy: + ggenellina
messages: + msg91287
2009-08-03 20:36:02cemasonivcreate