Hello PythonNet Developers,
I have a problem with running scripts in sequence in my C# Unit Tests.
When I run my test alone it works perfectly, but when I run all my tests, one specific test fails with Memory Access Violation Error.
This specific test is a test with 2 script imports or 2 scripts compilations.
string Script1 = "plain_tests.py";
string Script2 = "fitting_tests.py";
PythonEngine.Initialize();
var script1 = new Script(Script1);
script1.Execute();
var script2 = new Script(Script2);
script2.Execute();
Where:
public class Script
{
public string Filename { get; private set; }
private PyObject Compiled;
public PyScope Scope;
public Script(string filename)
{
Filename = filename;
var content = File.ReadAllText(filename);
var py_lock = PythonEngine.AcquireLock();
Compiled = PythonEngine.Compile(content, filename, RunFlagType.File);
PythonEngine.ReleaseLock(py_lock);
Reset();
}
public void Execute()
{
var py_lock = PythonEngine.AcquireLock();
Scope.Execute(Compiled);
PythonEngine.ReleaseLock(py_lock);
}
public void Reset()
{
var py_lock = PythonEngine.AcquireLock();
Scope = Py.CreateScope();
PythonEngine.ReleaseLock(py_lock);
}
}
The place it fails is on script2.Execute();
Scope.Execute(Compiled) fails on the seconds script. But this only happens when running all 10 unit tests. When running only this one specific test alone - there is no problem.
The issue occurs both from direct python function calling from Imported script and from this way I presented - compiling and executing script.
Do you have any idea how to solve this issue with sequential testing? I really need your help.. I have been trying to solve this for 3 weeks now. I am really really stuck. Thanks in advance!
Best regards,
Daniel Bodurov