Skip to content

Fix check in PyScope so that we don't leak a reference. #500

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 20, 2017
Merged
Changes from all commits
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
24 changes: 11 additions & 13 deletions src/runtime/pyscope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,16 @@ public class PyScope : DynamicObject, IDisposable
/// </remarks>
internal PyScope(IntPtr ptr, PyScopeManager manager)
{
if (Runtime.PyObject_Type(ptr) != Runtime.PyModuleType)
if (!Runtime.PyType_IsSubtype(Runtime.PyObject_TYPE(ptr), Runtime.PyModuleType))
{
throw new PyScopeException("object is not a module");
}
Manager = manager ?? PyScopeManager.Global;
obj = ptr;
//Refcount of the variables not increase
variables = Runtime.PyModule_GetDict(obj);
if (variables == IntPtr.Zero)
{
throw new PythonException();
}
Runtime.CheckExceptionOccurred();

Runtime.PyDict_SetItemString(
variables, "__builtins__",
Runtime.PyEval_GetBuiltins()
Expand Down Expand Up @@ -123,7 +121,7 @@ public dynamic Import(string name, string asname = null)
PyObject module = PythonEngine.ImportModule(name);
Import(module, asname);
return module;
}
}
}

/// <summary>
Expand Down Expand Up @@ -290,7 +288,7 @@ public PyObject Eval(string code, PyDict locals = null)
/// Evaluate a Python expression
/// </summary>
/// <remarks>
/// Evaluate a Python expression
/// Evaluate a Python expression
/// and convert the result to a Managed Object of given type.
/// </remarks>
public T Eval<T>(string code, PyDict locals = null)
Expand Down Expand Up @@ -400,7 +398,7 @@ public PyObject Get(string name)
{
PyObject scope;
var state = TryGet(name, out scope);
if(!state)
if (!state)
{
throw new PyScopeException($"The scope of name '{Name}' has no attribute '{name}'");
}
Expand Down Expand Up @@ -447,7 +445,7 @@ public bool TryGet(string name, out PyObject value)
/// Get Method
/// </summary>
/// <remarks>
/// Obtain the value of the variable of given name,
/// Obtain the value of the variable of given name,
/// and convert the result to a Managed Object of given type.
/// If the variable does not exist, throw an Exception.
/// </remarks>
Expand All @@ -466,7 +464,7 @@ public T Get<T>(string name)
/// TryGet Method
/// </summary>
/// <remarks>
/// Obtain the value of the variable of given name,
/// Obtain the value of the variable of given name,
/// and convert the result to a Managed Object of given type.
/// If the variable does not exist, return false.
/// </remarks>
Expand All @@ -479,18 +477,18 @@ public bool TryGet<T>(string name, out T value)
{
value = default(T);
return false;
}
}
if (pyObj == null)
{
if(typeof(T).IsValueType)
if (typeof(T).IsValueType)
{
throw new PyScopeException($"The value of the attribute '{name}' is None which cannot be convert to '{typeof(T).ToString()}'");
}
else
{
value = default(T);
return true;
}
}
}
value = pyObj.As<T>();
return true;
Expand Down