-1

How can I retrieve a value from a function after it crashes? E.g. how would I find the value of a after the function completes/crashes.

>>> def a_is_3():
...     a=3
...     print 'a='+str(a)
... 
>>> a_is_3()
a=3
>>> a
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'a' is not defined

My real case includes massive numpy arrays created and passed around through dozens of modules. The arrays are still taking up memory after the code errors, but I can't access them.

Is there a way to retrieve what the variables were at the moment when the function crashed? I want to avoid having to add debug code and re-run the lengthy execution to capture the variables. It seems like a particularly useful trick for troubleshooting.

Trayder
  • 1
  • 2
  • 3
    http://stackoverflow.com/questions/4228637/getting-started-with-the-python-debugger-pdb – Dyno Fu Apr 30 '15 at 06:00
  • you mean when a *function* crashes, not a *script*. – abcd Apr 30 '15 at 06:02
  • 1
    Are you asking how to make `a` a global variable, instead of local to the function? Or how to extract locals from the frame of a function call that already ended? Or… what? – abarnert Apr 30 '15 at 06:03
  • I suppose what I ideally want is to recover locals from a function that has already ended, as I can still see them in memory. – Trayder Apr 30 '15 at 08:52

1 Answers1

-1

try to use global

>>> def a_is_3():
...     global a
...     a = 3
...     print('a=' + str(a))
...
>>> a_is_3()
a=3
>>> a
3
>>>

This will work quite well in an interactive session - but if you move this to a script later make sure you are aware of the implications.

Obviously, the variable a gets overridden in global scope, so be aware of that too.

Shadow
  • 8,749
  • 4
  • 47
  • 57
Sinux
  • 1,728
  • 3
  • 15
  • 28
  • just have to make sure there isn't an `a` already defined outside the function that is used for a different purpose. – abcd Apr 30 '15 at 06:07
  • Okay, that works fine for the simple case. But it doesn't work when I apply it to my real case, using a unique variable name. e.g. I insert into the code: `global ASDF; ASDF = X` and afterward get `NameError: name 'ASDF' is not defined`. X being a numpy array – Trayder Apr 30 '15 at 08:43
  • @Trayder i don't think that error has anything to do with your case being more complicated. i think it's because you haven't initialized `ASDF` outside the function (so there is an `ASDF` already defined to which the `global` call can refer). – abcd Apr 30 '15 at 17:25
  • @shadow "aware of the implications," which are . . . ? – abcd Apr 30 '15 at 17:26
  • `import __builtin__; __builtin__.ASDF = X` did it. global was insufficient because I needed to pass the variable across modules. – Trayder May 01 '15 at 02:43
  • @dbliss if you were to copy multiple fragments of code together into a script that had global variables, they could clobber each other etc... – Shadow May 05 '15 at 04:07
  • @shadow if you were to copy multiple fragments of code into an interactive session, wouldn't there be just as much clobbering? – abcd May 05 '15 at 07:16
  • That is one the implications of using global - yes... – Shadow May 06 '15 at 00:30