How to Debug OPS (Other Peoples’ Scripts): Part 1

This is the Python edition. I’m working with a script called StellaR.py and when I run it for myself with the Stella model I’m working with, I get this…

Traceback (most recent call last):
  File “StellaR.py”, line 717, in <module>
    main()
  File “StellaR.py”, line 541, in main
    convT=convWrite(conv)
  File “StellaR.py”, line 263, in convWrite
    iftemp2 = if_extract(iftemp[2])
  File “StellaR.py”, line 196, in if_extract
    ifsplit3 = re.split(‘(else\s+|ELSE\s+|Else\s+|else|ELSE|Else)’,ifsplit2[2],1
)
IndexError: list index out of range

What does it mean? Here’s how to read this error message. (In this case, ignore the first warning, it’s a result of me tinkering with the script to debug it and doesn’t mean much except that the script didn’t run and I messed with the original structure.)

The important part: when the script reached line 541 (the content of the line is under that) it encountered a problem. As you can see, that line is calling a custom function convWrite(), so the error message is then giving you more information: we can next trace the problem to line 263 of the function convWrite(). Then, that line calls a function, if_extract(). The problem ultimately seems to be here: line 196 in the custom function if_extract() is having a problem with whatever is being passed to it. When it gets to that line, it throws the IndexError.

Now, it’s time to run the debugger to find out what happened (pdb).

Personal note: There are many options for Python versions, packaging, editors, etc. This isn’t perfect but I’ve been using Python(x,y). I might be changing it soon, because it uses Python 2.x and eventually I want to upgrade this script to v. 3. Spyder, the editor packaged with the download, is accordingly outdated (especially in the context of Windows 10). I’m used to it, though, and here are the nice (not exclusive) features I look for in a Python editor…

  • easy access to ipdb
  • multiple windows to look simultaneously at the script in different panes
  • variable explorer

So, I’ll be talking about debugging in the context of Spyder 2. First off, I can run the script within Spyder 2 just to see “where it’s at” when it crashes. I can see in the variable explorer what the values are of all the variables at the time of the crash, to give a hint as to what’s going wrong. (In this case, the script writes out another script, so I also looked at where it stopped writing as a big clue to the problem.)

So, looking back at the error message, I found the value of “conv” that was causing the problem, and in this case, the value was still set there since the script crashed on it. So, I manually ran the lines from the 1st function (convWrite) “outside of” the function until I reached the error, with the value passed to the next function if_extract(). I examined this value and found the problem: this time, the value being passed to if_extract() was wrapped in parentheses, whereas the prior values were not. In my case…

'(if (sim_time<=2338) then new_tminF4170 else new_tminF7100)'
…is passed to the if_extract() function.

So, to sum up…

  1. If the script crashes, run it in an editor with a variable explorer, so you can easily see the values of the variables when the script crashes.
  2. Run the script line by line through where the error message identifies problems, to see what happens step-by-step with the values being assigned and passed on to find the issue.

Head onto part 2 for doing something within the actual debugger.

Leave a Reply

Your email address will not be published. Required fields are marked *