Skip to content

Commit c8ae358

Browse files
authored
Fix check so that we don't leak a reference. (#500)
`PyObject_TYPE` does not `Incref` the type object returned, in contrast to `PyObject_Type`, which was used here before.
1 parent 12af794 commit c8ae358

File tree

1 file changed

+11
-13
lines changed

1 file changed

+11
-13
lines changed

src/runtime/pyscope.cs

+11-13
Original file line numberDiff line numberDiff line change
@@ -57,18 +57,16 @@ public class PyScope : DynamicObject, IDisposable
5757
/// </remarks>
5858
internal PyScope(IntPtr ptr, PyScopeManager manager)
5959
{
60-
if (Runtime.PyObject_Type(ptr) != Runtime.PyModuleType)
60+
if (!Runtime.PyType_IsSubtype(Runtime.PyObject_TYPE(ptr), Runtime.PyModuleType))
6161
{
6262
throw new PyScopeException("object is not a module");
6363
}
6464
Manager = manager ?? PyScopeManager.Global;
6565
obj = ptr;
6666
//Refcount of the variables not increase
6767
variables = Runtime.PyModule_GetDict(obj);
68-
if (variables == IntPtr.Zero)
69-
{
70-
throw new PythonException();
71-
}
68+
Runtime.CheckExceptionOccurred();
69+
7270
Runtime.PyDict_SetItemString(
7371
variables, "__builtins__",
7472
Runtime.PyEval_GetBuiltins()
@@ -123,7 +121,7 @@ public dynamic Import(string name, string asname = null)
123121
PyObject module = PythonEngine.ImportModule(name);
124122
Import(module, asname);
125123
return module;
126-
}
124+
}
127125
}
128126

129127
/// <summary>
@@ -290,7 +288,7 @@ public PyObject Eval(string code, PyDict locals = null)
290288
/// Evaluate a Python expression
291289
/// </summary>
292290
/// <remarks>
293-
/// Evaluate a Python expression
291+
/// Evaluate a Python expression
294292
/// and convert the result to a Managed Object of given type.
295293
/// </remarks>
296294
public T Eval<T>(string code, PyDict locals = null)
@@ -400,7 +398,7 @@ public PyObject Get(string name)
400398
{
401399
PyObject scope;
402400
var state = TryGet(name, out scope);
403-
if(!state)
401+
if (!state)
404402
{
405403
throw new PyScopeException($"The scope of name '{Name}' has no attribute '{name}'");
406404
}
@@ -447,7 +445,7 @@ public bool TryGet(string name, out PyObject value)
447445
/// Get Method
448446
/// </summary>
449447
/// <remarks>
450-
/// Obtain the value of the variable of given name,
448+
/// Obtain the value of the variable of given name,
451449
/// and convert the result to a Managed Object of given type.
452450
/// If the variable does not exist, throw an Exception.
453451
/// </remarks>
@@ -466,7 +464,7 @@ public T Get<T>(string name)
466464
/// TryGet Method
467465
/// </summary>
468466
/// <remarks>
469-
/// Obtain the value of the variable of given name,
467+
/// Obtain the value of the variable of given name,
470468
/// and convert the result to a Managed Object of given type.
471469
/// If the variable does not exist, return false.
472470
/// </remarks>
@@ -479,18 +477,18 @@ public bool TryGet<T>(string name, out T value)
479477
{
480478
value = default(T);
481479
return false;
482-
}
480+
}
483481
if (pyObj == null)
484482
{
485-
if(typeof(T).IsValueType)
483+
if (typeof(T).IsValueType)
486484
{
487485
throw new PyScopeException($"The value of the attribute '{name}' is None which cannot be convert to '{typeof(T).ToString()}'");
488486
}
489487
else
490488
{
491489
value = default(T);
492490
return true;
493-
}
491+
}
494492
}
495493
value = pyObj.As<T>();
496494
return true;

0 commit comments

Comments
 (0)