Message3614
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. |
|
Date |
User |
Action |
Args |
2008-09-23 15:05:27 | kam | set | recipients:
+ kam |
2008-09-23 15:05:27 | kam | set | messageid: <1222182327.79.0.120258235115.issue1138@psf.upfronthosting.co.za> |
2008-09-23 15:05:27 | kam | link | issue1138 messages |
2008-09-23 15:05:27 | kam | create | |
|