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

Created on 2012-09-12 15:32 by kgabor79, last changed 2012-09-12 15:47 by r.david.murray.

Files
File name Uploaded Description Edit
instest.py kgabor79, 2012-09-12 15:32 Test script
Messages (1)
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)
History
Date User Action Args
2012-09-12 15:47:58r.david.murraysetnosy: + r.david.murray
2012-09-12 15:32:22kgabor79create