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

Skip to content

Correctly dispose the result of PyRun_String #1071

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
Mar 2, 2020
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions src/embed_tests/Python.EmbeddingTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
<Compile Include="pyimport.cs" />
<Compile Include="pyinitialize.cs" />
<Compile Include="pyrunstring.cs" />
<Compile Include="References.cs" />
<Compile Include="TestConverter.cs" />
<Compile Include="TestCustomMarshal.cs" />
<Compile Include="TestDomainReload.cs" />
Expand Down
40 changes: 40 additions & 0 deletions src/embed_tests/References.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
namespace Python.EmbeddingTest
{
using NUnit.Framework;
using Python.Runtime;

public class References
{
private Py.GILState _gs;

[SetUp]
public void SetUp()
{
_gs = Py.GIL();
}

[TearDown]
public void Dispose()
{
_gs.Dispose();
}

[Test]
public void MoveToPyObject_SetsNull()
{
var dict = new PyDict();
NewReference reference = Runtime.PyDict_Items(dict.Handle);
try
{
Assert.IsFalse(reference.IsNull());

using (reference.MoveToPyObject())
Assert.IsTrue(reference.IsNull());
}
finally
{
reference.Dispose();
}
}
}
}
34 changes: 27 additions & 7 deletions src/runtime/NewReference.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,23 @@
namespace Python.Runtime
{
using System;
using System.Diagnostics.Contracts;

/// <summary>
/// Represents a reference to a Python object, that is tracked by Python's reference counting.
/// </summary>
[NonCopyable]
ref struct NewReference
{
IntPtr pointer;
public bool IsNull => this.pointer == IntPtr.Zero;

/// <summary>Gets a raw pointer to the Python object</summary>
public IntPtr DangerousGetAddress()
=> this.IsNull ? throw new NullReferenceException() : this.pointer;

/// <summary>
/// Returns <see cref="PyObject"/> wrapper around this reference, which now owns
/// the pointer. Sets the original reference to <c>null</c>, as it no longer owns it.
/// </summary>
public PyObject MoveToPyObject()
{
if (this.IsNull) throw new NullReferenceException();
if (this.IsNull()) throw new NullReferenceException();

var result = new PyObject(this.pointer);
this.pointer = IntPtr.Zero;
Expand All @@ -31,9 +28,32 @@ public PyObject MoveToPyObject()
/// </summary>
public void Dispose()
{
if (!this.IsNull)
if (!this.IsNull())
Runtime.XDecref(this.pointer);
this.pointer = IntPtr.Zero;
}

[Pure]
internal static IntPtr DangerousGetAddress(in NewReference reference)
=> IsNull(reference) ? throw new NullReferenceException() : reference.pointer;
[Pure]
internal static bool IsNull(in NewReference reference)
=> reference.pointer == IntPtr.Zero;
}

/// <summary>
/// These members can not be directly in <see cref="NewReference"/> type,
/// because <c>this</c> is always passed by value, which we need to avoid.
/// (note <code>this in NewReference</code> vs the usual <code>this NewReference</code>)
/// </summary>
static class NewReferenceExtensions
{
/// <summary>Gets a raw pointer to the Python object</summary>
[Pure]
public static IntPtr DangerousGetAddress(this in NewReference reference)
=> NewReference.DangerousGetAddress(reference);
[Pure]
public static bool IsNull(this in NewReference reference)
=> NewReference.IsNull(reference);
}
}
1 change: 1 addition & 0 deletions src/runtime/Python.Runtime.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@
<Compile Include="pythonengine.cs" />
<Compile Include="pythonexception.cs" />
<Compile Include="pytuple.cs" />
<Compile Include="ReferenceExtensions.cs" />
<Compile Include="runtime.cs" />
<Compile Include="typemanager.cs" />
<Compile Include="typemethod.cs" />
Expand Down
20 changes: 20 additions & 0 deletions src/runtime/ReferenceExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace Python.Runtime
{
using System.Diagnostics.Contracts;

static class ReferenceExtensions
{
/// <summary>
/// Checks if the reference points to Python object <c>None</c>.
/// </summary>
[Pure]
public static bool IsNone(this in NewReference reference)
=> reference.DangerousGetAddress() == Runtime.PyNone;
/// <summary>
/// Checks if the reference points to Python object <c>None</c>.
/// </summary>
[Pure]
public static bool IsNone(this BorrowedReference reference)
=> reference.DangerousGetAddress() == Runtime.PyNone;
}
}
2 changes: 1 addition & 1 deletion src/runtime/pydict.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public PyObject Items()
var items = Runtime.PyDict_Items(this.obj);
try
{
if (items.IsNull)
if (items.IsNull())
{
throw new PythonException();
}
Expand Down
31 changes: 23 additions & 8 deletions src/runtime/pyscope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -278,11 +278,19 @@ public PyObject Eval(string code, PyDict locals = null)
Check();
IntPtr _locals = locals == null ? variables : locals.obj;
var flag = (IntPtr)Runtime.Py_eval_input;
IntPtr ptr = Runtime.PyRun_String(

NewReference reference = Runtime.PyRun_String(
code, flag, variables, _locals
);
Runtime.CheckExceptionOccurred();
return new PyObject(ptr);
try
{
Runtime.CheckExceptionOccurred();
return reference.MoveToPyObject();
}
finally
{
reference.Dispose();
}
}

/// <summary>
Expand Down Expand Up @@ -316,15 +324,22 @@ public void Exec(string code, PyDict locals = null)
private void Exec(string code, IntPtr _globals, IntPtr _locals)
{
var flag = (IntPtr)Runtime.Py_file_input;
IntPtr ptr = Runtime.PyRun_String(
NewReference reference = Runtime.PyRun_String(
code, flag, _globals, _locals
);
Runtime.CheckExceptionOccurred();
if (ptr != Runtime.PyNone)

try
{
throw new PythonException();
Runtime.CheckExceptionOccurred();
if (!reference.IsNone())
{
throw new PythonException();
}
}
finally
{
reference.Dispose();
}
Runtime.XDecref(ptr);
}

/// <summary>
Expand Down
22 changes: 15 additions & 7 deletions src/runtime/pythonengine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -543,12 +543,13 @@ public static PyObject Eval(string code, IntPtr? globals = null, IntPtr? locals
/// </remarks>
public static void Exec(string code, IntPtr? globals = null, IntPtr? locals = null)
{
PyObject result = RunString(code, globals, locals, RunFlagType.File);
if (result.obj != Runtime.PyNone)
using (PyObject result = RunString(code, globals, locals, RunFlagType.File))
{
throw new PythonException();
if (result.obj != Runtime.PyNone)
{
throw new PythonException();
}
}
result.Dispose();
}


Expand Down Expand Up @@ -594,13 +595,20 @@ internal static PyObject RunString(string code, IntPtr? globals, IntPtr? locals,

try
{
IntPtr result = Runtime.PyRun_String(
NewReference result = Runtime.PyRun_String(
code, (IntPtr)flag, globals.Value, locals.Value
);

Runtime.CheckExceptionOccurred();
try
{
Runtime.CheckExceptionOccurred();

return new PyObject(result);
return result.MoveToPyObject();
}
finally
{
result.Dispose();
}
}
finally
{
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -802,7 +802,7 @@ public static extern int Py_Main(
internal static extern int PyRun_SimpleString(string code);

[DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)]
internal static extern IntPtr PyRun_String(string code, IntPtr st, IntPtr globals, IntPtr locals);
internal static extern NewReference PyRun_String(string code, IntPtr st, IntPtr globals, IntPtr locals);

[DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)]
internal static extern IntPtr PyEval_EvalCode(IntPtr co, IntPtr globals, IntPtr locals);
Expand Down