Ironpython - If name == main error

Hi,

When running an ironpython script, the if __name__ == '__main__': seems to be causing an error. I’ve checked the script in Rhino 7 and works fine.

Is this a Rhino 8 bug, or do we need to do something differently here?
Test file: GridLayout.py (2.1 KB)

The error the console is returning:

System.NullReferenceException: Object reference not set to an instance of an object.
at Eto.Forms.Window.set_Title(String value) in D:\BuildAgent\work\dujour\src4\DotNetSDK\Eto\src\Eto\Forms\Window.cs:line 244
at InvokeStub_Window.set_Title(Object, Object, IntPtr*)
at System.Reflection.MethodInvoker.Invoke(Object obj, IntPtr* args, BindingFlags invokeAttr)
— End of stack trace from previous location —
at Python.Runtime.PythonException.ThrowLastAsClrException()
at Python.Runtime.PythonException.ThrowIfIsNull(NewReference& ob)
at Python.Runtime.PyModule.Execute(PyObject script, PyDict locals)
at Python.Runtime.RhinoCodePythonEngine.ExecuteScope(PyModule pyscope, PyObject pycode, String pythonFile, IDictionary2 inputs, IDictionary2 outputs, String beforeScript, String afterScript)
at Python.Runtime.RhinoCodePythonEngine.RunCode(String scopeName, Object code, String pythonFile, IDictionary2 inputs, IDictionary2 outputs, String beforeScript, String afterScript)
at Rhino.Runtime.Code.Languages.PythonNet.CPythonCode.ExecutePython(ExecuteContext context)
at Rhino.Runtime.Code.Languages.PythonNet.CPythonCode.ExecuteCode(ExecuteContext context)
at Rhino.Runtime.Code.Code.Run(ExecuteContext context)

Thanks!
Dan

IronPython did a few sketchy things that wasn’t really complying with python specs
With Python3 you’d need to write valid Python code.
So:

You need to call the constructor of base class in your __init__ method. You can choose which base constructor, and when to call using super().__init__() pattern. Put the required variables for the base constructor inside .__init__() call


class GridLayoutTemplate(forms.Dialog[bool]):
    def __init__(self):
        super().__init__()

IronPython allowed setting property values in the constructor, even when constructors don’t take those values as inputs e.g. forms.Label(Text="Label 2"). This is invalid in Python 3

self.m_label1 = forms.Label()
self.m_label1.Text = "Label 1"

self.m_label2 = forms.Label()
self.m_label2.Text = "Label 2"

self.DefaultButton = forms.Button()
self.DefaultButton.Text = "OK"

# ...

Here is the edited script

GridLayout_edited.py (2.3 KB)

2 Likes