Message3614

Author kam
Recipients kam
Date 2008-09-23.15:05:26
SpamBayes Score 7.589549e-07
Marked as misclassified No
Message-id <1222182327.79.0.120258235115.issue1138@psf.upfronthosting.co.za>
In-reply-to
Content
It seems that the __file__ module attribute reflects the original
source, not the location the module was loaded from as would be expected.

A test case can be simply made from two files: imported.py and testimport.py

imported.py:
import os

base =  os.path.dirname(__file__)

top =  os.path.abspath(base)

up = os.path.abspath(os.path.join(base, '..'))

print __file__, base, top, up

testimport.py:
from imported import *

Running gives the same results in Jython and CPython:

$ jython testimport.py 
/Users/kam/src/importtest/imported.py /Users/kam/src/importtest
/Users/kam/src/importtest /Users/kam/src
$ python testimport.py 
/Users/kam/src/importtest/imported.py /Users/kam/src/importtest
/Users/kam/src/importtest /Users/kam/src

...but if the directory tree is copied or renamed:

$ cd ..
$ rsync -avP importtest/ it/
$ cd it
$ python testimport.py 
/Users/kam/src/it/imported.pyc /Users/kam/src/it /Users/kam/src/it
/Users/kam/src
$ jython testimport.py 
/Users/kam/src/importtest/imported.py /Users/kam/src/importtest
/Users/kam/src/importtest /Users/kam/src

But if the class file is removed:

$ rm imported\$py.class 
$ jython testimport.py 
/Users/kam/src/it /Users/kam/src/it /Users/kam/src

...the correct value is now calculated.

http://docs.python.org/ref/types.html says "__file__ is the pathname of
the file from which the module was loaded, if it was loaded from a
file". Apparently, Jython is storing the name of the original Python
file and using that. It would be expected that __file__ would contain
the name of the $py.class file it was loaded from.
History
Date User Action Args
2008-09-23 15:05:27kamsetrecipients: + kam
2008-09-23 15:05:27kamsetmessageid: <1222182327.79.0.120258235115.issue1138@psf.upfronthosting.co.za>
2008-09-23 15:05:27kamlinkissue1138 messages
2008-09-23 15:05:27kamcreate