Question
· Oct 8, 2022

How to Debug Embedded Python?

Folks!

Could you please share any best practices on how to debug Embedded Python code?

Given I have the following class method:

ClassMethod HideShip() As %Status [ Language = python ]
{
    import iris
    shipsgl=iris.gref(iris.cls(__name__)._GetParameter("BoardStorage"))
    shipsgl.kill()
    from random import Random
    board=iris.cls(__name__)._GetParameter("BoardRange")
    x=random.randint(0,board+1)
    y=random.randint(0,board+1)
    shipsgl.set([x,y],1)
}

And when I run it shows the following error:

USER>d ##class(eshvarov.sample.SeaBattle.GamePython).HideShip()

D ##CLASS(eshvarov.sample.SeaBattle.GamePython).HideShip()
^
<THROW> *%Exception.PythonException <THROW> 230 ^^0^D ##CLASS(eshvarov.sample.SeaBattle.GamePython).HideShip() <class 'KeyError'>: Invalid args -

Any idea how to guess in what line could be the problem?

The one-by-line debugger in VSCode doesn't help (I guess it's not implemented yet).

Product version: IRIS 2022.1
$ZV: IRIS for UNIX (Ubuntu Server LTS for x86-64 Containers) 2022.2 (Build 311U) Wed Aug 10 2022 00:49:03 EDT
Discussion (5)1
Log in or sign up to continue

in my various examples I took these strategies:

  • try to run the Py code in PY shell in a REAL terminal or docker command line console
  • error messages directly from the shell are much more useful  
  • insert temporary print(...) statements at suspicious points
  • what I see by COS <THROW>  is often more obfuscation than a hint  

to my experience, your 'KeyError' indicates that you try to get a value
from a non-existing global or subscript. in COS it would be <UNDEF>

Hi Evgeny,

You could consider using the Python "traceback" module.

For example:

 Class TEST.TraceBack [ Abstract ]
{ ClassMethod GetErrorTrace() As %String [ Language = python ]
{
import traceback
errStr=""
try:
  7/0
except:
  errStr=traceback.format_exc()
return errStr
} }

Demonstration:

USER>w ##class(TEST.TraceBack).GetErrorTrace()
Traceback (most recent call last):
                                    File "TraceBack", line 6, in GetErrorTrace
                                                                              ZeroDivisionError: division by zero

Kind regards,

Alex