[Python.NET] Python.Net crash on assembly reload in Unity

Viktoria Kovecses viktoria.kovecses at imaginary-spaces.com
Mon Jul 30 13:02:45 EDT 2018


Hello,

We are experiencing crashes when running Python.Net in the Unity 2018.2
game engine. Specifically, after modifying a C# script in Unity, an
automatic reload is triggered which results in a crash in Py_Finalize.

Here are the repro steps:
0. Make sure to call PythonEngine.Shutdown on domain unload (otherwise it
crashes earlier).
1. Create a Unity project and add the compiled Python.Net files (clr.pyd
and Python.Runtime.dll) to the Assets folder
2. In Unity Player Settings (Edit > Project Settings > Player), under
"Other Settings", make sure "Api Compatibility Level" is set to ".NET 4.x",
and "Scripting Runtime Version" is set to ".NET 4.x Equivalent"
3. In Assets, create the following C# script (HelloWorld.cs):

```
using UnityEditor;
using UnityEngine;
using Python.Runtime;

class HelloWorld
{
    [MenuItem("Python/Hello")]
    public static void CallHello()
    {
        using (Py.GIL())
        {
            string script = "import clr\n" +
                "clr.AddReference('UnityEngine.dll')\n" +
                "import UnityEngine\n" +
                "UnityEngine.Debug.Log('hello world!')\n";

            PythonEngine.Exec(script);
        }
    }
}
```

4. Run the script by selecting Python > Hello from the top menu
--> This will log "hello world!" to the Unity console

5. Modify HelloWorld.cs script: change Hello to Hello 1 and trigger hot
reloading of assemblies
6. run Python > Hello 1
--> Logs in the console

7. Modify HelloWorld.cs script: change Hello 1 to Hello 2:
--> crash in dict_traverse (garbage collection during Py_Finalize).

Does anyone know what could be causing the crash and/or how to fix it?

Any help is appreciated.

Thank you,
Viktoria
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/pythondotnet/attachments/20180730/4503cdd9/attachment.html>


More information about the PythonDotNet mailing list