inspect.getblock() seems to halt prematurely. This code only prints 6
lines of the 12 line input file.
assume it's by design, but the docs don't mention getblock. docstring is
"Extract the block of code at the top of the given list of lines," which
should be "code *from* the top."
###whatiscompile2.py
import inspect
r = open('thebogusfile.txt').read()
code = compile( r, 'thebogusfile.txt', 'exec' )
exec code
print '***'
print inspect.getsource(code)
###thebogusfile.txt
def f(a, b):
print a,
print b,
c = a + b
print c,
return a + b
def g():
print 'g'
print f( 3, 4 ),
g()
###stdout
3 4 7 7 g
***
def f(a, b):
print a,
print b,
c = a + b
print c,
return a + b
def f(a, b):
print a,
print b,
c = a + b
print c,
return a + b
def g():
print 'g'
print f( 3, 4 ),
g()
import inspect
r = open('the bogus file.txt').read()
code = compile( r, 'the bogus file.txt', 'exec' )
exec code
print '***'
print inspect.getsource(code)
--
http://mail.python.org/mailman/listinfo/python-list