Title: inspect.getsource doesn't work with PYTHONPATH and source compiled from a different dir
Type: behavior Stage:
Components: Library (Lib) Versions: Python 3.0
Status: closed Resolution: out of date
Dependencies: Superseder:
Assigned To: Nosy List: erickt, georg.brandl
Priority: normal Keywords:

Created on 2008-11-21 09:53 by erickt, last changed 2009-04-04 14:48 by georg.brandl. This issue is now closed.

Messages (3)
msg76170 - (view) Author: Erick Tryzelaar (erickt) Date: 2008-11-21 09:53
I'm running into a similar problem as Jean-Paul in:

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 

mkdir dir1
mkdir dir1/foo
mkdir dir2
echo 'def f(): pass' >  dir1/foo/
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 "<string>", line 1, in <module>
.0/", line 691, in getsource
    lines, lnum = getsourcelines(object)
.0/", line 680, in getsourcelines
    lines, lnum = findsource(object)
.0/", 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/". This confuses linecache which can't find the file. 
Here's one suggestion of a fix.

--- /tmp/	2008-11-21 01:34:56.000000000 -0800
+++ /opt/local/lib/python3.0/	2008-11-21 01:35:28.000000000 -
@@ -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__)

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.
msg80837 - (view) Author: Erick Tryzelaar (erickt) Date: 2009-01-30 20:38
I just checked, and the latest svn version of python has fixed this. Could 
someone close this bug for me?
msg85398 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2009-04-04 14:48
Easily done. :)
Date User Action Args
2009-04-04 14:48:30georg.brandlsetstatus: open -> closed

nosy: + georg.brandl
messages: + msg85398

resolution: out of date
2009-01-30 20:38:35ericktsetmessages: + msg80837
2008-11-21 09:53:23ericktcreate