Trace dynamically compiled code?

Thomas Heller theller at
Tue Mar 14 15:17:29 CET 2006

Ed Leafe wrote:
> Hi,
> 	Thanks to the help of many on this list, I've been able to take code  
> that is created by the user in my app and add it to an object as an  
> instance method. The technique used is roughly:
> nm = "myMethod"
> code = """def myMethod(self):
>     print "Line 1"
>     print "My Value is %s" % self.Value
>     return
> """
> compCode = compile(code, "", "exec")
> exec compCode
> exec "self.%s = %s.__get__(self)" % (nm, nm)
> 	This is working great, but now I'm wondering if there is a way to  
> enable pdb tracing of the code as it executes? When tracing "normal"  
> code, pdb will show you the name of the script being executed, the  
> line number and the source code for the line about to be executed.  
> But when stepping through code compiled dynamically as above, the  
> current line's source code is not available to pdb, and thus does not  
> display.
> 	Does anyone know a way to compile the dynamic code so that pdb can  
> 'see' the source? I suppose I could write it all out to a bunch of  
> temp files, but that would be terribly messy. Are there any neater  
> solutions?

You coud monkey-patch the getline function in the linecache module, so
that it is able to find your dynamically generated code.  IMO that is
what pdb uses.c


More information about the Python-list mailing list