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.

1 Like