473,326 Members | 2,114 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Trace dynamically compiled code?

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?

-- Ed Leafe
-- http://leafe.com
-- http://dabodev.com

Mar 14 '06 #1
2 1519
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:
Just some notes about your code:
nm = "myMethod"
code = """def myMethod(self):
print "Line 1"
print "My Value is %s" % self.Value
return
"""
compCode = compile(code, "", "exec")
exec compCode
Try not using bare exec statements, since they pollute the local scope.
In your example you could use:

compCode = compile(code, "", "exec")
d = {}
exec compCode in d
func = d[nm]

See http://docs.python.org/ref/exec.html for details.
exec "self.%s = %s.__get__(self)" % (nm, nm)
You don't need dynamic execution here; you can simply use
setattr and the new module:

import new
method = new.instancemethod(func, self)
setattr(self, nm, method)

and yes, I remember that I was the one who suggested you
the __get__ hack.
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 should check py lib: http://codespeak.net/py/current/doc/ ,
specifically the py.code "module". Then you can modify the
function from above:

import inspect
f = inspect.currentframe()
lineno = f.f_lineno - 5 # or some other constant
filename = f.f_code.co_filename

import py
co = py.code.Code(func)
new_code = co.new(co_lineno=lineno, co_filename=filename)
new_func = new.function(new_code, func.func_globals, nm,
func.func_defaults, func.func_closure)

-- Ed Leafe
-- http://leafe.com
-- http://dabodev.com


Ziga Seilnacht

Mar 14 '06 #2
On Mar 14, 2006, at 12:45 PM, Ziga Seilnacht wrote:
Try not using bare exec statements, since they pollute the local
scope.
In your example you could use:

compCode = compile(code, "", "exec")
d = {}
exec compCode in d
func = d[nm]


OK, noted and changed in my source.
exec "self.%s = %s.__get__(self)" % (nm, nm)


You don't need dynamic execution here; you can simply use
setattr and the new module:

import new
method = new.instancemethod(func, self)
setattr(self, nm, method)

and yes, I remember that I was the one who suggested you
the __get__ hack.


I've made the change you suggested. May I ask why one is better than
the other? Namespace pollution doesn't seem to be an issue here.
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 should check py lib: http://codespeak.net/py/current/doc/ ,
specifically the py.code "module". Then you can modify the
function from above:

import inspect
f = inspect.currentframe()
lineno = f.f_lineno - 5 # or some other constant
filename = f.f_code.co_filename

import py
co = py.code.Code(func)
new_code = co.new(co_lineno=lineno, co_filename=filename)
new_func = new.function(new_code, func.func_globals, nm,
func.func_defaults, func.func_closure)


OK, thanks for the lead. I will take a look at this as soon as I
have the time to sit down and attempt to digest it. But before I do,
will I be able to add code like this to the same part of the
framework where the dynamic code is created, and have it work? I ask
this because an even bigger problem is the matter of creating dynamic
classes on the fly from saved XML files. I have a class that converts
the XML to the corresponding Python code for the class, and it's
working great, except for the tracing through the debugger issue.
Would I be able to apply the py lib stuff to this, too?

-- Ed Leafe
-- http://leafe.com
-- http://dabodev.com

Mar 15 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Joshua Coady | last post by:
Do Trace calls have any impact on performance if Trace is disabled in the config file? Josh
4
by: Jazz | last post by:
Hello, I have a question about trace as MSDN seems confuses me.(http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemDiagnosticsTraceClassTopic.asp). My answer...
12
by: Wardeaux | last post by:
All, Wanting to find a way to create web pages to add to my website without having to recompile the codebehind everytime I want to add a new one... Here's the deal: I have a web app that takes...
5
by: Tascien | last post by:
I was able to build an asp page dynamically depending on the question that the user answered through the online wizard... I think i can dynamically also create '.vb' or '.aspx' dynamically...
2
by: Jason L James | last post by:
Hi all, can anyone help me with some tracing I am trying to do in my application. I have a configuration file containing the XML as detailed below: <?xml version="1.0" encoding="utf-8" ?>...
3
by: Benny Raymond | last post by:
Does anyone know off hand if dynamically compiled C# is debuggable in ..net 2.0?
4
by: kplkumar | last post by:
This is what my supervisor is looking for. We want runtime debugging/trace logging. In other words, we want to log every method that was executed, perhaps the variables in it as well - everytime...
0
by: Aryan | last post by:
Hi All, My application was working fine and giving me proper trace messages, but now all of sudden it started showing expcetion as "Object reference not set to an instance of an object....
0
by: =?Utf-8?B?QmVuIERpbHRz?= | last post by:
I'm using C# as a sort of scripting language by having text entered by my user compiled using the CSharpCodeProvider and run, with references to an assembly that has utility functions in it. ...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.