Written by

Senior Startups and Community Programs Manager at InterSystems Corporation
Question Evgeny Shvarov · 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

Comments

Robert Cemper · Oct 8, 2022

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>

0
Alex Woodhead · Oct 9, 2022

Hi Evgeny,

You could consider using the Python "traceback" module.

For example:

Class TEST.TraceBack [ Abstract ]{ClassMethod GetErrorTrace() As %String [ Language = python ]{import tracebackerrStr=""try:
  7/0except:
  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

0
Guillaume Rongier · Oct 9, 2022

For debugging embedded python, i do not use ObjectScript wrapper.

Use "pure" python, with the VsCode and the irispython interpreter, after use the debugger like any normal python program/script :

image

0
Raj Singh  Oct 14, 2022 to Guillaume Rongier

@Guillaume Rongier I've had trouble getting this setup to work. Can you share your configuration (settings.json file?) to get VS Code to use irispython?

0