This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
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
process
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 2022-04-11 14:56 by admin. 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:

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 "<string>", line 1, in <module>
  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.
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. :)
History
Date User Action Args
2022-04-11 14:56:41adminsetgithub: 48625
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