From 21913da3ee709a6eebcbbe323e36f5c4ed3a619a Mon Sep 17 00:00:00 2001 From: Benedikt Reinartz Date: Mon, 19 Jun 2017 17:54:09 +0200 Subject: [PATCH] Fix check so that we don't leak a reference. `PyObject_TYPE` does not `Incref` the type object returned, in contrast to `PyObject_Type`, which was used here before. --- src/runtime/pyscope.cs | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/src/runtime/pyscope.cs b/src/runtime/pyscope.cs index 1991772dc..25282ac26 100644 --- a/src/runtime/pyscope.cs +++ b/src/runtime/pyscope.cs @@ -57,7 +57,7 @@ public class PyScope : DynamicObject, IDisposable /// 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"); } @@ -65,10 +65,8 @@ internal PyScope(IntPtr ptr, PyScopeManager manager) 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() @@ -123,7 +121,7 @@ public dynamic Import(string name, string asname = null) PyObject module = PythonEngine.ImportModule(name); Import(module, asname); return module; - } + } } /// @@ -290,7 +288,7 @@ public PyObject Eval(string code, PyDict locals = null) /// Evaluate a Python expression /// /// - /// Evaluate a Python expression + /// Evaluate a Python expression /// and convert the result to a Managed Object of given type. /// public T Eval(string code, PyDict locals = null) @@ -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}'"); } @@ -447,7 +445,7 @@ public bool TryGet(string name, out PyObject value) /// Get Method /// /// - /// 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. /// @@ -466,7 +464,7 @@ public T Get(string name) /// TryGet Method /// /// - /// 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. /// @@ -479,10 +477,10 @@ public bool TryGet(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()}'"); } @@ -490,7 +488,7 @@ public bool TryGet(string name, out T value) { value = default(T); return true; - } + } } value = pyObj.As(); return true;