inspect.getblock()

Aaron Brady acb at uchicago.edu
Sun Apr 22 22:27:47 EDT 2007


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
-------------- next part --------------
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()

-------------- next part --------------
import inspect



r = open('the bogus file.txt').read()

code = compile( r, 'the bogus file.txt', 'exec' )

exec code



print '***'

print inspect.getsource(code)



More information about the Python-list mailing list