Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
format cleanup
  • Loading branch information
vmuriart authored and yagweb committed Apr 7, 2017
commit c15555cafd070c1dcf16f8a5eeeaa54905234d30
34 changes: 17 additions & 17 deletions src/embed_tests/pyscope.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
using System;
using NUnit.Framework;
using Python.Runtime;

namespace Python.EmbeddingTest
{
[TestFixture]
public class PyScopeTest
{
PyScope ps;
private PyScope ps;

[SetUp]
public void SetUp()
{
ps = Py.Session("test");
}

[TearDown]
public void TearDown()
public void Dispose()
{
ps.Dispose();
}
Expand All @@ -27,7 +27,7 @@ public void TearDown()
public void TestEval()
{
ps.SetVariable("a", 1);
int result = ps.Eval<int>("a+2");
var result = ps.Eval<int>("a + 2");
Assert.AreEqual(result, 3);
}

Expand All @@ -39,8 +39,8 @@ public void TestExec()
{
ps.SetVariable("bb", 100); //declare a global variable
ps.SetVariable("cc", 10); //declare a local variable
ps.Exec("aa=bb+cc+3");
int result = ps.GetVariable<System.Int32>("aa");
ps.Exec("aa = bb + cc + 3");
var result = ps.GetVariable<int>("aa");
Assert.AreEqual(result, 113);
}

Expand All @@ -53,7 +53,7 @@ public void TestCompileExpression()
{
ps.SetVariable("bb", 100); //declare a global variable
ps.SetVariable("cc", 10); //declare a local variable
var script = ps.Compile("bb+cc+3", "", RunFlagType.Eval);
PyObject script = ps.Compile("bb + cc + 3", "", RunFlagType.Eval);
var result = ps.Execute<int>(script);
Assert.AreEqual(result, 113);
}
Expand All @@ -68,14 +68,14 @@ public void TestCompileStatements()
{
ps.SetVariable("bb", 100); //declare a global variable
ps.SetVariable("cc", 10); //declare a local variable
var script = ps.Compile("aa=bb+cc+3", "", RunFlagType.File);
PyObject script = ps.Compile("aa = bb + cc + 3", "", RunFlagType.File);
ps.Execute(script);
int result = ps.GetVariable<int>("aa");
var result = ps.GetVariable<int>("aa");
Assert.AreEqual(result, 113);
}

/// <summary>
/// Exec Python statements in a subscope of the session then discard it.
/// Exec Python statements in a SubScope of the session then discard it.
/// </summary>
[Test]
public void TestSubScope()
Expand All @@ -84,8 +84,8 @@ public void TestSubScope()
ps.SetVariable("cc", 10); //declare a local variable

PyScope scope = ps.SubScope();
scope.Exec("aa=bb+cc+3");
int result = scope.GetVariable<System.Int32>("aa");
scope.Exec("aa = bb + cc + 3");
var result = scope.GetVariable<int>("aa");
Assert.AreEqual(result, 113); //
scope.Dispose();

Expand All @@ -103,8 +103,8 @@ public void TestImport()
Assert.IsTrue(ps.ContainsVariable("sys"));

ps.Exec("sys.attr1 = 2");
int value1 = ps.Eval<int>("sys.attr1");
int value2 = (int)sys.attr1.AsManagedObject(typeof(int));
var value1 = ps.Eval<int>("sys.attr1");
var value2 = (int)sys.attr1.AsManagedObject(typeof(int));
Assert.AreEqual(value1, 2);
Assert.AreEqual(value2, 2);
}
Expand Down Expand Up @@ -135,8 +135,8 @@ public void TestSuspend()
PythonEngine.RunSimpleString("import sys;");
}

ps.Exec("aa=bb+cc+3");
int result = ps.GetVariable<System.Int32>("aa");
ps.Exec("aa = bb + cc + 3");
var result = ps.GetVariable<int>("aa");
Assert.AreEqual(result, 113);
}
}
Expand Down
77 changes: 34 additions & 43 deletions src/runtime/pythonengine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -527,9 +527,8 @@ public enum RunFlagType
Eval = 258
}

public class PySessionDisposedException: Exception
public class PySessionDisposedException : Exception
{

}

public class PyScope : IDisposable
Expand Down Expand Up @@ -564,7 +563,7 @@ public void ReleaseLock()

public void Dispose()
{
this.ReleaseLock();
ReleaseLock();
GC.SuppressFinalize(this);
}

Expand All @@ -584,7 +583,7 @@ public void Dispose()

private PyScope(GILState state)
{
this.isDisposed = false;
isDisposed = false;
this.state = state;
globals = Runtime.PyDict_New();
if (globals == IntPtr.Zero)
Expand All @@ -599,14 +598,14 @@ private PyScope(GILState state)
}

internal PyScope(string name, GILState state)
:this(state)
: this(state)
{
this.state = state;
this.name = name;
Runtime.PyDict_SetItemString(
globals, "__builtins__",
Runtime.PyEval_GetBuiltins()
);
globals, "__builtins__",
Runtime.PyEval_GetBuiltins()
);
}

internal PyScope(PyScope parent, GILState state)
Expand Down Expand Up @@ -639,29 +638,21 @@ internal PyScope(PyScope parent, GILState state)
/// <summary>
/// the dict for global variables
/// </summary>
public IntPtr globals
{
get;
private set;
}
public IntPtr globals { get; private set; }

/// <summary>
/// the dict for local variables
/// </summary>
public IntPtr locals
{
get;
private set;
}
public IntPtr locals { get; private set; }

public PyScope SubScope()
{
return new PyScope(this, this.state);
return new PyScope(this, state);
}

public void Suspend()
{
this.state.ReleaseLock();
state.ReleaseLock();
}

/// <summary>
Expand All @@ -685,7 +676,7 @@ public PyObject Import(string name)
/// </remarks>
public PyObject ImportAs(string name, string asname)
{
this.AcquireLock();
AcquireLock();
PyObject module = PythonEngine.ImportModule(name);
if (asname == null)
{
Expand All @@ -699,7 +690,7 @@ public PyObject Execute(PyObject script)
{
IntPtr ptr = Runtime.PyEval_EvalCode(script.Handle, globals, locals);
Runtime.CheckExceptionOccurred();
if(ptr == Runtime.PyNone)
if (ptr == Runtime.PyNone)
{
Runtime.XDecref(ptr);
return null;
Expand All @@ -709,12 +700,12 @@ public PyObject Execute(PyObject script)

public T Execute<T>(PyObject script)
{
var pyObj = this.Execute(script);
if(pyObj == null)
PyObject pyObj = Execute(script);
if (pyObj == null)
{
return default(T);
}
T obj = (T)pyObj.AsManagedObject(typeof(T));
var obj = (T)pyObj.AsManagedObject(typeof(T));
return obj;
}

Expand All @@ -726,7 +717,7 @@ public T Execute<T>(PyObject script)
/// </remarks>
public PyObject Compile(string code, string filename = "", RunFlagType mode = RunFlagType.File)
{
IntPtr flag = (IntPtr)mode;
var flag = (IntPtr)mode;
IntPtr ptr = Runtime.Py_CompileString(code, filename, flag);
Runtime.CheckExceptionOccurred();
return new PyObject(ptr);
Expand All @@ -741,7 +732,7 @@ public PyObject Compile(string code, string filename = "", RunFlagType mode = Ru
/// </remarks>
public PyObject Eval(string code)
{
this.AcquireLock();
AcquireLock();
var flag = (IntPtr)Runtime.Py_eval_input;
IntPtr ptr = Runtime.PyRun_String(
code, flag, globals, locals
Expand All @@ -759,7 +750,7 @@ public PyObject Eval(string code)
public T Eval<T>(string code)
{
PyObject pyObj = Eval(code);
T obj = (T)pyObj.AsManagedObject(typeof(T));
var obj = (T)pyObj.AsManagedObject(typeof(T));
return obj;
}

Expand All @@ -771,8 +762,8 @@ public T Eval<T>(string code)
/// </remarks>
public void Exec(string code)
{
this.AcquireLock();
Exec(code, this.globals, this.locals);
AcquireLock();
Exec(code, globals, locals);
}

private void Exec(string code, IntPtr _globals, IntPtr _locals)
Expand All @@ -798,7 +789,7 @@ private void Exec(string code, IntPtr _globals, IntPtr _locals)
/// </remarks>
internal void SetGlobalVariable(string name, object value)
{
this.AcquireLock();
AcquireLock();
using (var pyKey = new PyString(name))
{
IntPtr _value = GetInstHandle(value);
Expand All @@ -819,7 +810,7 @@ internal void SetGlobalVariable(string name, object value)
/// </remarks>
internal void RemoveGlobalVariable(string name)
{
this.AcquireLock();
AcquireLock();
using (var pyKey = new PyString(name))
{
int r = Runtime.PyObject_DelItem(globals, pyKey.obj);
Expand All @@ -839,7 +830,7 @@ internal void RemoveGlobalVariable(string name)
/// </remarks>
public void SetVariable(string name, object value)
{
this.AcquireLock();
AcquireLock();
using (var pyKey = new PyString(name))
{
IntPtr _value = GetInstHandle(value);
Expand All @@ -860,7 +851,7 @@ public void SetVariable(string name, object value)
/// </remarks>
public void RemoveVariable(string name)
{
this.AcquireLock();
AcquireLock();
using (var pyKey = new PyString(name))
{
int r = Runtime.PyObject_DelItem(locals, pyKey.obj);
Expand All @@ -879,7 +870,7 @@ public void RemoveVariable(string name)
/// </remarks>
public bool ContainsVariable(string name)
{
this.AcquireLock();
AcquireLock();
using (var pyKey = new PyString(name))
{
if (Runtime.PyMapping_HasKey(locals, pyKey.obj) != 0)
Expand All @@ -899,7 +890,7 @@ public bool ContainsVariable(string name)
/// </remarks>
public PyObject GetVariable(string name)
{
this.AcquireLock();
AcquireLock();
using (var pyKey = new PyString(name))
{
IntPtr op;
Expand All @@ -925,7 +916,7 @@ public PyObject GetVariable(string name)

public T GetVariable<T>(string name)
{
PyObject obj = this.GetVariable(name);
PyObject obj = GetVariable(name);
return (T)obj.AsManagedObject(typeof(T));
}

Expand All @@ -938,29 +929,29 @@ private static IntPtr GetInstHandle(object value)
}
else
{
var ptr = Converter.ToPython(value, value.GetType());
IntPtr ptr = Converter.ToPython(value, value.GetType());
return ptr;
}
}

private void AcquireLock()
{
if(isDisposed)
if (isDisposed)
{
throw new PySessionDisposedException();
}
this.state.AcquireLock();
state.AcquireLock();
}

public virtual void Dispose()
{
if(isDisposed)
if (isDisposed)
{
return;
}
Runtime.XDecref(globals);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gc issue is most likely here. You'll need to check that the engine is initialize and that it isn't finalizing before running these.

Runtime.XDecref(locals);
if (this.parent == null)
if (parent == null)
{
Py.RemoveSession(name);
}
Expand Down Expand Up @@ -999,7 +990,7 @@ public static PyScope Session(string name)
{
PythonEngine.Initialize();
}
if(Sessions.ContainsKey(name))
if (Sessions.ContainsKey(name))
{
return Sessions[name];
}
Expand Down