Inspect.py stopped working

Hello

inspect.py used to work in the past, but it appears it stopped working. Basic examples now give me errors:

from inspect import getframeinfo, stack

def debuginfo(message):
    caller = getframeinfo(stack()[1][0])
    print "%s:%d - %s" % (caller.filename, caller.lineno, message)

def grr(arg):
    debuginfo(arg)

grr("aargh")

Message: main

Traceback:
line 503, in getmodule, “C:\Program Files\Rhino 6\Plug-ins\IronPython\Lib\inspect.py”
line 531, in findsource, “C:\Program Files\Rhino 6\Plug-ins\IronPython\Lib\inspect.py”
line 1015, in getframeinfo, “C:\Program Files\Rhino 6\Plug-ins\IronPython\Lib\inspect.py”
line 1040, in getouterframes, “C:\Program Files\Rhino 6\Plug-ins\IronPython\Lib\inspect.py”
line 1062, in stack, “C:\Program Files\Rhino 6\Plug-ins\IronPython\Lib\inspect.py”
line 4, in debuginfo, “C:\Users\klemmtch\AppData\Local\Temp\TempScript.py”
line 8, in grr, “C:\Users\klemmtch\AppData\Local\Temp\TempScript.py”
line 10, in , “C:\Users\klemmtch\AppData\Local\Temp\TempScript.py”

I’m using Version 6 SR27 (6.27.20176.5001, 6/24/2020). This seems to be the same problem:

Am I doing something wrong, or could this be reinstated please? Thank you very much!

Chris

Hi Chris,

I’m not sure what caused this to stop working, and I’m sorry it took a while to reply to your message, but happy to say this should be fixed in an upcoming Beta release of Rhino 7. The issue was due to the __main__ module not being defined and injected into sys.modules in Rhino Python, while getmodule (line 503 of “C:\Program Files\Rhino 6\Plug-ins\IronPython\Lib\inspect.py”) expects it to be there. The relevant lines of inspect.py are:

def inspect.getmodule(object, _filename=None):
   [ ...]
    # Check the main module
    main = sys.modules['__main__']  # Line 503
    if not hasattr(object, '__name__'):
        return None
    if hasattr(main, object.__name__):
        mainobject = getattr(main, object.__name__)
        if mainobject is object:
            return main

This issue is tracked as RH-61060 Rhino Python __main__ namespace is not defined.

2 Likes

Hi,

I’m running into this very same thing trying to attach a ptvsd debugger to some ghpython code from vscode. I see from the bugtrack this seems to be resolved in 7.1? Is there any way to get this solved or work around it in that latest rhino 6 version?

thanks,
lukas

@pierrec

@fertig At that point in the lifecycle of Rhino 6 I don’t think it would make sense to backport the Rhino 7 changes to Rhino 6.

The guide “Debugging GHPython components Visual Studio” seems to have a ptvsd debugger connected to Rhino, in a version of Rhino 7 that predates the fix. Is that what you are doing too? I’m not sure why it would work in that guide and not in your code. Do you have a way to run your code without using the inspect.getmodule method?

Since there is no __main__ module defined, a possible workaround could be to comment out the lines 503 and 506-509 in C:\Program Files\Rhino 6\Plug-ins\IronPython\Lib\inspect.py. That would get you past the error at the top of this thread, but I don’t know if it will be enough to make your debugger work.