Issue1421812

classification
Title: Jython 2.2a and 2.1 imports directories as modules
Type: Severity: normal
Components: None Versions:
Milestone:
process
Status: closed Resolution:
Dependencies: Superseder:
Assigned To: cgroves Nosy List: cgroves, pedronis, pekka.klarck
Priority: immediate Keywords:

Created on 2006-02-01.20:06:51 by anonymous, last changed 2006-04-16.13:46:06 by pedronis.

Messages
msg1093 (view) Author: Nobody/Anonymous (nobody) Date: 2006-02-01.20:06:51
Jython 2.2a and Jython 2.1 will import a directory even
if it does not contain a __init__.py file in it.

Moreover - these directories will override builtins and
modules from JYTHON_HOME/Lib such as sys.

example:
mkdir sys
./jython
import sys

will import the wrong module.
This is quite severe.
msg1094 (view) Author: Samuele Pedroni (pedronis) Date: 2006-02-01.20:52:22
Logged In: YES 
user_id=61408

notice that importing dir without __init__ is part of the java
package importing support. 

OTOH builtin modules should take precedence. I cannot
reproduce the problem with 2.1.

Is sadly there in 2.2a, in general the re-worked importing
mechanism needs much more testing.
msg1095 (view) Author: Pekka Klärck (pekka.klarck) Date: 2006-02-02.22:36:35
Logged In: YES 
user_id=1379331

It seems that in 2.1 builtin modules do take precedence and
thus this is not so big a problem there. Anyway, since you
are not working only with builtins this behaviour can pretty
easily bite you even in that case.

Consider for example that you are building a tool called
'robot' (we are) and in your code do things like 'import
robot' (we do that too). Now if your user creates a
directory 'robot' where she places something related to the
framework (our user did) and runs the framework so that this
directory is in PYTHONPATH before your framework module
(yes, she did that too) you are going to get some pretty
weird and hard to debug error messages (as we did). 
msg1096 (view) Author: Samuele Pedroni (pedronis) Date: 2006-02-02.22:59:03
Logged In: YES 
user_id=61408

This feature is there to enable importing java packages from
sys.path and is there since a long time. Bug in 2.2 not with
standing is going to stay in some form.

Notice that overall python module/packages in 2.1 take
precedence over java ones, so even with the setup:

x/ (empty dir) x.py

import x

would import the Python module x. The 2.1 behavior should be 
 reinstantiated in 2.2. I'm sorry so far I didn't manage to
look to the import refactorings that happened in 2.2, it
seems some things got broken.
msg1097 (view) Author: Pekka Klärck (pekka.klarck) Date: 2006-02-02.23:29:24
Logged In: YES 
user_id=1379331

I see. Will that work also with directories with __init__.py
(e.g. x/x/__init__.py)? 
msg1098 (view) Author: Samuele Pedroni (pedronis) Date: 2006-04-16.13:46:06
Logged In: YES 
user_id=61408

the PEP302 implementation promoted java importing to a
meta_path importer. This was the right move forward but
these are tried before normal imports.

The total result is that now java imports take precedence
over Python imports. This is too much of a backward
incompatible change, also I fear it may breaks subtle
assumptions that are still in place when one mixes java
classes on the sys.path.

One possible solution is to promote normal sys.path python
importing as a meta_path elements too, and put it before the
java importer. This is the long term plan for CPython too,
but because is not implemented there (and may not be for a
while) it may create subtle incompatibilities too, depending
on how people use the meta_path.

The other solution (which was discussed around the PEP302
impl time) is to have a different entry in the importer
interface to load java classes and do a first pass over
meta_path and normal imports to load python modules and a
second one (using the different entry point) to load java
classes. Java importing is not supported anyway by CPython.
This would also re-establish the correct and backward
compatible precedences.

I fear I will not have time for a while to dig more into
this myself, although importing is an area I know very well.
It's also a pity that we didn't manage to simplify our
import mechanism or really improve on it.
History
Date User Action Args
2006-02-01 20:06:51anonymouscreate