19

I want to run pdb, step through the code, and at some point change the value pointed at by some name. So I might want to change the value pointed at by the name 'stationLat'. But it seems I can't. Here's the example:

>>> import extractPercentiles
>>> import pdb
>>> pdb.run( "extractPercentiles.extractOneStation()" )
> <string>(1)<module>()->None
(Pdb) s
--Call--
> /scratch/extractPercentiles.py(96)extractOneStation()
-> def extractOneStation() :
(Pdb) tbreak 132
Breakpoint 3 at /scratch/extractPercentiles.py:132
(Pdb) c

Deleted breakpoint 3
> /scratch/extractPercentiles.py(132)extractOneStation()
-> stationLon = float(stationLoc[3])

So now I'm at a place where I would like to change the value of stationlat. Pdb appears to allow me to set stationLat to a new value, but when I inspect the value, it is unchanged:

(Pdb) stationLat
-34.171100000000003
(Pdb) stationLat = -40
(Pdb) stationLat   
-34.171100000000003
(Pdb) !stationLat = -40
(Pdb) stationLat
-34.171100000000003
(Pdb) 

You can see I tried using ! as well, without success.

The pdb manual says I should be able to change variables:

Commands that the debugger doesn’t recognize are assumed to be Python statements and are executed in the context of the program being debugged. Python statements can also be prefixed with an exclamation point (!). This is a powerful way to inspect the program being debugged; it is even possible to change a variable or call a function

Is this a question of scope? Is it to do with the way I have started pdb? I tried the embedded "pdb.set_trace" idiom and I got the same result.

Thanks for reading.

2 Answers 2

10

This appears to be a bug in Python 2.6. You should be able to do this in Python 2.7.

Sign up to request clarification or add additional context in comments.

6 Comments

Ah! Ok, thanks. I thought maybe I had to demangle names or use some special namespace. I had tried going up and down the frame stack too.
Oh, i'll record that for this project I am forced to work back in python 2.5.2, which still has the bug.
also, there's certain variables you can't assign. like "a" or "b"
You can assign them if you use ! before the assignment statement, as explained in the question.
@Michael Hoffman, the exclamation mark works. Why didn't you modify your answer but added a comment instead?
|
0

Actually, the value of the variable does get changed when you assign a new value in pdb. But if you try to read the variable in pdb again without running your code, it may reset to the original value.

If you step back into your code, you should see that it will use your new value (-40).

Try this:

stationLat = -40
s # step back into code
stationLat # should display -40

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.