[Python-bugs-list] [ python-Bugs-661184 ] inspect.getsource bug

SourceForge.net noreply@sourceforge.net
Fri, 03 Jan 2003 17:09:06 -0800


Bugs item #661184, was opened at 2003-01-02 12:08
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=661184&group_id=5470

Category: Python Library
Group: Python 2.2.2
Status: Open
Resolution: None
Priority: 5
Submitted By: Michele Simionato (michele_s)
>Assigned to: Raymond Hettinger (rhettinger)
Summary: inspect.getsource bug

Initial Comment:
I have found a bug in inspect.getsource.
I am using Python 2.2.2 on Red-Hat 7.3. 
Here is the problem.

--begin bug.py

import inspect
def f(x): return x
print inspect.getsource(f)

--end bug.py

% python bug.py
Traceback (most recent call last):
  File "bug.py", line 3, in ?
    print inspect.getsource(f)
  File "/usr/local/lib/python2.2/inspect.py", line 520,
in getsource
    return string.join(lines, '')
  File "/usr/local/lib/python2.2/string.py", line 131,
in join
    return sep.join(words)
TypeError: sequence expected, NoneType found

Notice that
 
--begin noproblem.py

import inspect
def f(x): 
    return x
print inspect.getsource(f)

--end noproblem.py

works:

% python noproblem.py
def f(x):
    return x

I discovered this bug in trying to retrieve the source
code for lambda expressions: 

--begin lambda.py

import inspect
f=lambda x: x
print inspect.getsource(f) 

--begin lambda.py

(same error message).


--
Michele Simionato - Dept. of Physics and Astronomy
210 Allen Hall Pittsburgh PA 15260 U.S.A.
Phone: 001-412-624-9041 Fax: 001-412-624-9163
Home-page: http://www.phyast.pitt.edu/~micheles/




----------------------------------------------------------------------

Comment By: Just van Rossum (jvr)
Date: 2003-01-03 06:58

Message:
Logged In: YES 
user_id=92689

It seems inspect.py indeed has problems finding one-line
functions. I've attached a patch that addresses this, but to
also make it work for lambda's I had to remove a few lines
that I don't quite understand the purpose of, as it seems to
work well without it. I've commented them out. Their purpose
seems to be to adjust co.co_firstlineno in case it's off,
it's just that I don't know how/when it can be off.

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=661184&group_id=5470