Issue 4375: inspect.getsource doesn't work with PYTHONPATH and source compiled from a different dir (original) (raw)
I'm running into a similar problem as Jean-Paul in:
http://bugs.python.org/issue4223
But the patch in it doesn't work for me. My issue is also with files compiled already with python3.0, but then being accessed from a different location using PYTHONPATH. Here's a full example of the problem:
mkdir dir1 mkdir dir1/foo mkdir dir2 echo 'def f(): pass' > dir1/foo/init.py cd dir1 python3.0 -c "import foo" cd ../dir2 python3.0 -c "import inspect, sys; sys.path.append('../dir1'); import foo; print(inspect.getsource(foo.f))"
Which errors out with:
Traceback (most recent call last): File "", line 1, in File "/opt/local/Library/Frameworks/Python.framework/Versions/3.0/lib/python3 .0/inspect.py", line 691, in getsource lines, lnum = getsourcelines(object) File "/opt/local/Library/Frameworks/Python.framework/Versions/3.0/lib/python3 .0/inspect.py", line 680, in getsourcelines lines, lnum = findsource(object) File "/opt/local/Library/Frameworks/Python.framework/Versions/3.0/lib/python3 .0/inspect.py", line 528, in findsource raise IOError('could not get source code') IOError: could not get source code
If I instrument inspect, it looks like what's happening is that linecache is being passed f.code.co_filename. However with the sys.path manipulation, that filename is baked into the .pyc file as "foo/init.py". This confuses linecache which can't find the file. Here's one suggestion of a fix.
--- /tmp/inspect.py 2008-11-21 01:34:56.000000000 -0800 +++ /opt/local/lib/python3.0/inspect.py 2008-11-21 01:35:28.000000000 - 0800 @@ -518,6 +518,7 @@ is raised if the source code cannot be retrieved.""" file = getsourcefile(object) or getfile(object) module = getmodule(object, file)
- file = getsourcefile(module) or getfile(file) if module: lines = linecache.getlines(file, module.dict) else:
It looks like in most situations the module has an accurate file that's updated from PYTHONPATH. I'm not sure if this works in every situation, but this, along with the other patch, work together to get the source printing working.