8000 Add python buffer api support by thesn10 · Pull Request #980 · pythonnet/pythonnet · GitHub
[go: up one dir, main page]

Skip to content

Add python buffer api support #980

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

Closed
wants to merge 4 commits into from
Closed
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
Update finalizer 2
  • Loading branch information
thesn10 committed May 29, 2020
commit 530e8c719e26a1b08280a877e7e49ae7bac27182
49 changes: 13 additions & 36 deletions src/runtime/pybuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace Python.Runtime
{
public sealed class PyBuffer : IDisposable
public sealed class PyBuffer : IPyDisposable
{
private PyObject _exporter;
private Py_buffer _view;
Expand Down Expand Up @@ -217,43 +217,11 @@ private void Dispose(bool disposing)
{
if (!disposedValue)
{
Debug.Assert(_view.obj != IntPtr.Zero, "Buffer object is invalid (no exporter object ref)");
//if (_view.obj == IntPtr.Zero)
//{
// return;
//}

if (Runtime.Py_IsInitialized() == 0)
throw new InvalidOperationException("Python runtime must be initialized");

if (!Runtime.IsFinalizing)
{
long refcount = Runtime.Refcount(_view.obj);
Debug.Assert(refcount > 0, "Object refcount is 0 or less");

if (refcount == 1)
{
Runtime.PyErr_Fetch(out var errType, out var errVal, out var traceback);

try
{
// this also decrements ref count for _view->obj
Runtime.PyBuffer_Release(ref _view);
Runtime.CheckExceptionOccurred();
}
finally
{
// Python requires finalizers to preserve exception:
// https://docs.python.org/3/extending/newtypes.html#finalization-and-de-allocation
Runtime.PyErr_Restore(errType, errVal, traceback);
}
}
else
{
// this also decrements ref count for _view->obj
Runtime.PyBuffer_Release(ref _view);
}
}
// this also decrements ref count for _view->obj
Runtime.PyBuffer_Release(ref _view);

_exporter = null;
Shape = null;
Expand All @@ -266,7 +234,11 @@ private void Dispose(bool disposing)

~PyBuffer()
{
Dispose(false);
if (disposedValue)
{
return;
}
Finalizer.Instance.AddFinalizedObject(this);
}

/// <summary>
Expand All @@ -278,5 +250,10 @@ public void Dispose()
Dispose(true);
GC.SuppressFinalize(this);
}

public IntPtr[] GetTrackedHandles()
{
return new IntPtr[] { _view.obj };
}
}
}
0