Can you help me with the below code? Urgent!

Discussion in 'Python' started by gokcemutlu, Jun 25, 2006.

  1. gokcemutlu

    gokcemutlu Guest

    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
     
    gokcemutlu, Jun 25, 2006
    #1
    1. Advertisements

  2. 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.
    .... x[0] = i
    .... states.append(x)
    ....
    Pierre
     
    Pierre Quentel, Jun 25, 2006
    #2
    1. Advertisements

  3. gokcemutlu

    gokcemutlu Guest

    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.


     
    gokcemutlu, Jun 25, 2006
    #3
  4. a écrit :
    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
     
    Pierre Quentel, Jun 25, 2006
    #4
  5. gokcemutlu

    gokcemutlu Guest

    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.
     
    gokcemutlu, Jun 25, 2006
    #5
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.