473,804 Members | 3,021 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1541
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.instancemet hod(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.current frame()
lineno = f.f_lineno - 5 # or some other constant
filename = f.f_code.co_fil ename

import py
co = py.code.Code(fu nc)
new_code = co.new(co_linen o=lineno, co_filename=fil ename)
new_func = new.function(ne w_code, func.func_globa ls, nm,
func.func_defau lts, func.func_closu re)

-- 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.instancemet hod(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.current frame()
lineno = f.f_lineno - 5 # or some other constant
filename = f.f_code.co_fil ename

import py
co = py.code.Code(fu nc)
new_code = co.new(co_linen o=lineno, co_filename=fil ename)
new_func = new.function(ne w_code, func.func_globa ls, nm,
func.func_defau lts, func.func_closu re)


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
2602
by: Joshua Coady | last post by:
Do Trace calls have any impact on performance if Trace is disabled in the config file? Josh
4
2312
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 to my question is NO, as it's decided at compile time. Basically, from what I understand is if you define the Trace tag(by default it is defined in Release build) and you build the project( in release build), those lines containing the Trace...
12
2263
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 work orders for 7 different items, each item gets 1 page for input specs. All works well. I want to add a new item. I do not want to recompile and redistribute my app everytime just to add a new input page. Can ASPX resources be compiled into a...
5
1466
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 depending on the answers that users entered in the online wizard... the only issue here, is that, once the wizard created the asp page, the online visitors could then request the page, and execute it w/no problem, and be able to use embended dynamic...
2
2914
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" ?> <configuration> <system.diagnostics> <switches>
3
1876
by: Benny Raymond | last post by:
Does anyone know off hand if dynamically compiled C# is debuggable in ..net 2.0?
4
4681
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 the method is executed. This should be such that it can be turned on/off in a configuration file. This way it will help us locate the error exactly where it occured and when and all variables related to it.
0
1049
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. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.NullReferenceException:...
0
1128
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. When an exception is thrown by my dynamically-compiled assembly, I can get a stack trace on the exception, but it doesn't show what line the error occurred on. I need to be able to tell my user where their error occurred. It doesn't seem to matter...
0
10583
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10337
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10323
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9160
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7622
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6854
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5654
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4301
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3822
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.