Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Bug Unity crash with PyModule.Exec after assembly reload

Discussion in 'Formats & External Tools Previews' started by guoxx_, Dec 8, 2022.

  1. guoxx_

    guoxx_

    Joined:
    Mar 16, 2021
    Posts:
    55
    Unity will crash if you do the following things with python scripting package

    In c# code, send a c# list to python

    Code (CSharp):
    1.         using (Py.GIL())
    2.         using (PyModule scope = Py.CreateScope())
    3.         {
    4.             List<float> foo = new List<float>();
    5.             foo.Add(1);
    6.             scope.Set("_foo", foo.ToPython());
    7.             scope.Exec(string.Format("exec(open('{0}').read())", pythonFileToExecute));
    8.         }

    In python code, convert the c# list to numpy array
    Code (CSharp):
    1. import numpy as np
    2.  
    3. foo = np.array(_foo).astype(np.float32)
    It works well. But if you then edit any c# scripts and assembly reloaded, afterward running those python codes again will crash unity.

    I create a sample unity project to reproduce the bug and attached it to this thread. Here are steps to reproduce the bug:
    1. Open a window from menu Test->Python Scripts, click button "Run Python Script"
    2. Edit any c# file so that unity will reload assemblies
    3. Click button "Run Python Script" again, unity will crash
     

    Attached Files: