Can you help me with the below code? Urgent!

G

gokcemutlu

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(frame.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(traceit)
afunc(1, 2, 3)
sys.settrace(None)

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

Pierre Quentel

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(frame.f_locals)
write
states.append(copy.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
 
G

gokcemutlu

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 said:
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(frame.f_locals)
write
states.append(copy.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
 
P

Pierre Quentel

(e-mail address removed) 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
 
G

gokcemutlu

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top