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
  • Loading branch information
thesn10 committed May 23, 2020
commit 52cc24ccdbf11decd176875ccfd54cf1ef9f0bab
42 changes: 40 additions & 2 deletions src/runtime/pybuffer.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
Expand Down Expand Up @@ -214,8 +215,45 @@ public int Read(byte[] buffer, int offset, int count) {

private void Dispose(bool disposing)
{
if (!disposedValue) {
Runtime.PyBuffer_Release(ref _view);
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);
}
}

_exporter = null;
Shape = null;
Expand Down