473,545 Members | 2,820 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Can you help me with the below code? Urgent!

I want to trace a function while it executes and keep its local
variables as states. In trace function all the things work well but
after all when I print states, they are all the same. I couldn't solve
the problem. I guess it's because of frame, please can you help me?

import sys

states = []

def traceit(frame, event, arg):
location = frame.f_code.co _name

if location == "afunc":
states.append(f rame.f_locals)
print frame.f_locals

return traceit

def afunc(a, b, c):
x = a + b
y = b + c
z = b

rslt = x + y - z

return rslt

print "This is what I should see!"
sys.settrace(tr aceit)
afunc(1, 2, 3)
sys.settrace(No ne)

print "\nWhy does this happen and how can I solve it?"
for s in states:
print s

Jun 25 '06 #1
4 1372
This is because in "states" you store a reference to frame.f_locals,
not the value it takes. When you print states, all the items are the
same reference to the same object and have the same value

If you want to store the values at each cycle you should store a copy
of frame.f_locals, which will give you a different object

After import sys add the line :
import copy

and instead of
states.append(f rame.f_locals)
write
states.append(c opy.copy(frame. f_locals))
Another example of this side-effect of storing references and not
values :

Python 2.4.2 (#67, Sep 28 2005, 12:41:11) [MSC v.1310 32 bit (Intel)]
on win32
Type "help", "copyright" , "credits" or "license" for more information.
states = []
x = [0]
for i in range(10): .... x[0] = i
.... states.append(x )
.... print states [[9], [9], [9], [9], [9], [9], [9], [9], [9], [9]]


Pierre

Jun 25 '06 #2
Hello,

You're right about it but this is a simple code which tells my problem.
I need actually the frame itself for states and unfortunately
copy.copy(frame ) throws an exception. Pickling also doesn't work. Do
you have any other idea?

Thanks,

Gokce.
Pierre Quentel schrieb:
This is because in "states" you store a reference to frame.f_locals,
not the value it takes. When you print states, all the items are the
same reference to the same object and have the same value

If you want to store the values at each cycle you should store a copy
of frame.f_locals, which will give you a different object

After import sys add the line :
import copy

and instead of
states.append(f rame.f_locals)
write
states.append(c opy.copy(frame. f_locals))
Another example of this side-effect of storing references and not
values :

Python 2.4.2 (#67, Sep 28 2005, 12:41:11) [MSC v.1310 32 bit (Intel)]
on win32
Type "help", "copyright" , "credits" or "license" for more information.
states = []
x = [0]
for i in range(10): ... x[0] = i
... states.append(x )
... print states [[9], [9], [9], [9], [9], [9], [9], [9], [9], [9]]


Pierre


Jun 25 '06 #3
go********@gmai l.com a écrit :
Hello,

You're right about it but this is a simple code which tells my problem.
I need actually the frame itself for states and unfortunately
copy.copy(frame ) throws an exception. Pickling also doesn't work. Do
you have any other idea?

Thanks,

Gokce.

In your original post you said you wanted to store local variables, but
it seems that you need to store more information than this : could you
specify which ones ? Since you can't copy the stack frame, maybe you
can copy only the attributes you need

Pierre

Jun 25 '06 #4
Hello,

Thanks for your help. I just copy things that I want to keep using
copy.copy function. Trying to copy frames won't lead me to anywhere :)

Gokce.

Pierre Quentel schrieb:
go********@gmai l.com a écrit :
Hello,

You're right about it but this is a simple code which tells my problem.
I need actually the frame itself for states and unfortunately
copy.copy(frame ) throws an exception. Pickling also doesn't work. Do
you have any other idea?

Thanks,

Gokce.

In your original post you said you wanted to store local variables, but
it seems that you need to store more information than this : could you
specify which ones ? Since you can't copy the stack frame, maybe you
can copy only the attributes you need

Pierre


Jun 25 '06 #5

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

Similar topics

4
1602
by: pargat singh | last post by:
Hi I format my M/C and install fresh copy of Win2000 and ISS.First thing after that i install VS.net and every thing goes well without any error.When i try to open asp.net application i am getting web access failed The default web access mode for this project is set to file share, but the project folder at "http://..." cannot be opened...
8
1811
by: Tim::.. | last post by:
Can someone please tell me why I keep getting the following error for some of my web application users but not others??? Even though the application runs from a central webserver??? Thanks for any help... Error: -----
6
2453
by: varkey.mathew | last post by:
Dear all, Bear with me, a poor newbie(atleast in AD).. I have to authenticate a user ID and password for a user as a valid Active Directory user or not. I have created the IsAuthenticated function exactly as outlined in the below link. http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnnetsec/html/SecNetHT02.asp
5
1657
by: Vikas Kumar | last post by:
Hi, I'm not able to convert the following dll import statement in C# to VB.NET. Can any one please help me in this respect?
2
2115
by: blazerguns | last post by:
Hi all, I have used WMI method to set ipaddress in my system. But the problem is that if i have multiple iface cards this program is setting all the nic to same ip. I dont know how to stop this from happening. I have my code below. Please help to identify single iface to set that all. public void setIP(string IPAddress, string...
6
16161
by: phnimx | last post by:
I'm attempting to migrate a predominately MFC application that I've just inherited from Visual Studio.NET 2003 to Visual Studio 2005. I've managed to clean up a myriad of compile and link errors but I'm stuck on one final problem. Please note that this application compiled/linked and ran just fine out of Visual Studio.NET 2003. Details:...
5
3535
by: koonda | last post by:
Hi all, I am a student and I have a project due 20th of this month, I mean May 20, 2007 after 8 days. The project is about creating a Connect Four Game. I have found some code examples on the internet which helped me little bit. But there are lot of problems I am facing developing the whole game. I have drawn the Board and the two players can...
1
2074
by: javabeginner123 | last post by:
i have a java prob, and i have to solve it fast, but i'm just getting to know it, so plz help me solve it with full code completed, thanks so much. the prob is to create a monter fight and there is the description: The monsters are of a very strange kind, called "Bigmon". They have some basic characteristics, like attack and defense power, life...
6
3784
by: alag20 | last post by:
Hi Guys, I need some urgent help with this as I am becoming clueless now. I have 2 DllImport as below from Kernel32 private static extern int GetPrivateProfileString(string section, int key, string defaultValue, byte result, int size, string fileName); private static extern int GetPrivateProfileString(string section, string key,...
0
7692
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. ...
0
7946
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...
0
7791
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5360
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...
0
5078
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...
0
3491
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1921
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
1
1045
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
744
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.