classification
Title: inspect.findsource fails after directory change
Type: behavior Stage:
Components: Library (Lib) Versions: Python 2.7, Python 2.6
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: kgabor79, r.david.murray, yselivanov
Priority: normal Keywords:

Created on 2012-09-12 15:32 by kgabor79, last changed 2014-01-30 02:09 by yselivanov. This issue is now closed.

Files
File name Uploaded Description Edit
instest.py kgabor79, 2012-09-12 15:32 Test script
Messages (6)
msg170389 - (view) Author: Gabor Kovacs (kgabor79) Date: 2012-09-12 15:32
The attached script works in 2.6(.4) but not in 2.7(.3); the script cannot locate its own source code if invoked by relative path and work directory has changed. If line 8 uncommented, everything is fine due to caching. 

I think this is related to issue #4050 changes in inspect.py. 

~ kgabor$ python ./instest.py

None ./instest.py
Traceback (most recent call last):
  File "./instest.py", line 10, in <module>
    print inspect.getsourcefile(A), inspect.getfile(A),inspect.findsource(A)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/inspect.py", line 529, in findsource
    raise IOError('source code not available')
IOError: source code not available

~ kgabor$ pwd
/Users/kgabor

~ kgabor$ python /Users/kgabor/instest.py

/Users/kgabor/instest.py /Users/kgabor/instest.py (['import inspect\n', 'import os\n', '\n', 'class A:\n', ' def __init__(self):\n', '  self.a=1\n', '\n', '#print inspect.getsourcefile(A),inspect.getfile(A),inspect.findsource(A)\n', "os.chdir('/')\n", 'print inspect.getsourcefile(A), inspect.getfile(A),inspect.findsource(A)\n'], 3)
msg209678 - (view) Author: Yury Selivanov (yselivanov) * (Python committer) Date: 2014-01-29 20:52
Sadly, there is nothing we can do about it, as we simply don't have absolute paths in __file__ attributes in your case.
msg209693 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2014-01-29 23:37
Note that this is fixed in 3.4.  That is, in 3.4 __file__ paths are made absolute when the import happens.
msg209694 - (view) Author: Yury Selivanov (yselivanov) * (Python committer) Date: 2014-01-29 23:42
Hm, maybe just for the imports? This simple script, run it directly:

test.py:

   def foo(): pass
   print(foo.__code__.co_filename)

"$ python3.4 test.py" will still print relative path "test.py".

Hence, if you do 'os.chdir' in the above script, the introspection code will break.  There are many similar issues on the tracker, btw.
msg209697 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2014-01-30 01:59
__file__ is absolute for imports, yes.  I didn't check co_filename, maybe that needs to be fixed too.
msg209698 - (view) Author: Yury Selivanov (yselivanov) * (Python committer) Date: 2014-01-30 02:09
Yes. I'll create an issue for that.
History
Date User Action Args
2014-01-30 02:09:18yselivanovsetmessages: + msg209698
2014-01-30 01:59:30r.david.murraysetmessages: + msg209697
2014-01-29 23:42:59yselivanovsetmessages: + msg209694
2014-01-29 23:37:36r.david.murraysetmessages: + msg209693
2014-01-29 20:52:50yselivanovsetstatus: open -> closed

nosy: + yselivanov
messages: + msg209678

resolution: rejected
2012-09-12 15:47:58r.david.murraysetnosy: + r.david.murray
2012-09-12 15:32:22kgabor79create