Issue1138

classification
Title: __file__ reflects original source location, not the pathname of the $py.class file
Type: Severity: normal
Components: Core Versions: 2.5alpha3
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: pjenvey Nosy List: kam, pjenvey
Priority: Keywords:

Created on 2008-09-23.15:05:27 by kam, last changed 2008-11-22.23:36:06 by pjenvey.

Messages
msg3614 (view) Author: Kevin A. Mitchell (kam) Date: 2008-09-23.15:05:26
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.
msg3760 (view) Author: Philip Jenvey (pjenvey) Date: 2008-11-08.00:39:43
I tried fixing this almost a year ago but it ended up breaking a module or 
two. I'll try to revisit it before the 2.5 release

I have a couple tests that ensure this in (IIRC) test_import_jy and 
test_chdir -- they're currently special cased for Jython's bad behavior
msg3808 (view) Author: Philip Jenvey (pjenvey) Date: 2008-11-22.23:36:06
fixed in r5601, thanks
History
Date User Action Args
2008-11-22 23:36:06pjenveysetstatus: open -> closed
resolution: fixed
messages: + msg3808
2008-11-08 00:39:43pjenveysetassignee: pjenvey
messages: + msg3760
nosy: + pjenvey
2008-09-23 15:05:27kamcreate