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
Show file tree
Hide file tree
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
Merge branch 'master' into pybuffer2
  • Loading branch information
lostmsu authored Oct 30, 2020
commit abf957f1c94d8ece5d28aa94eb5d877236034d2e
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ This document follows the conventions laid out in [Keep a CHANGELOG][].
## [Unreleased][]

### Added
- Added Python 3 buffer api support and PyBuffer interface for fast byte and numpy array read/write ([#980][p980])

### Changed
- Drop support for Python 2, 3.4, and 3.5
Expand Down Expand Up @@ -49,7 +50,8 @@ This version improves performance on benchmarks significantly compared to 2.3.
- `Runtime.None` to be able to pass `None` as parameter into Python from .NET
- `PyObject.IsNone()` to check if a Python object is None in .NET.
- Support for Python 3.8
- Added Python 3 buffer api support and PyBuffer interface for fast byte and numpy array read/write ([#980][p980])
- Codecs as the designated way to handle automatic conversions between
.NET and Python types

### Changed

Expand Down
4 changes: 0 additions & 4 deletions src/embed_tests/TestPyBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ public void Dispose()
[Test]
public void TestBufferWrite()
{
if (Runtime.Runtime.pyversionnumber < 35) return;

string bufferTestString = "hello world! !$%&/()=?";

using (Py.GIL())
Expand All @@ -46,8 +44,6 @@ public void TestBufferWrite()
[Test]
public void TestBufferRead()
{
if (Runtime.Runtime.pyversionnumber < 35) return;

string bufferTestString = "hello world! !$%&/()=?";

using (Py.GIL())
Expand Down
8 changes: 3 additions & 5 deletions src/runtime/pybuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ private static char OrderStyleToChar(BufferOrderStyle order, bool eitherOneValid
/// </summary>
public static long SizeFromFormat(string format)
{
if (Runtime.pyversionnumber < 39)
if (Runtime.PyVersion < new Version(3,9))
throw new NotSupportedException("SizeFromFormat requires at least Python 3.9");
return (long)Runtime.PyBuffer_SizeFromFormat(format);
}
Expand All @@ -111,7 +111,7 @@ public IntPtr GetPointer(long[] indices)
{
if (disposedValue)
throw new ObjectDisposedException(nameof(PyBuffer));
if (Runtime.pyversionnumber < 37)
if (Runtime.PyVersion < new Version(3, 7))
throw new NotSupportedException("GetPointer requires at least Python 3.7");
return Runtime.PyBuffer_GetPointer(ref _view, indices.Select(x => (IntPtr)x).ToArray());
}
Expand All @@ -123,7 +123,7 @@ public void FromContiguous(IntPtr buf, long len, BufferOrderStyle fort)
{
if (disposedValue)
throw new ObjectDisposedException(nameof(PyBuffer));
if (Runtime.pyversionnumber < 37)
if (Runtime.PyVersion < new Version(3, 7))
throw new NotSupportedException("FromContiguous requires at least Python 3.7");

if (Runtime.PyBuffer_FromContiguous(ref _view, buf, (IntPtr)len, OrderStyleToChar(fort, false)) < 0)
Expand All @@ -139,8 +139,6 @@ public void ToContiguous(IntPtr buf, BufferOrderStyle order)
{
if (disposedValue)
throw new ObjectDisposedException(nameof(PyBuffer));
if (Runtime.pyversionnumber < 36)
throw new NotSupportedException("ToContiguous requires at least Python 3.6");

if (Runtime.PyBuffer_ToContiguous(buf, ref _view, _view.len, OrderStyleToChar(order, true)) < 0)
throw new PythonException();
Expand Down
1 change: 0 additions & 1 deletion src/runtime/pyobject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1092,7 +1092,6 @@ public override int GetHashCode()
/// </remarks>
public PyBuffer GetBuffer(PyBUF flags = PyBUF.SIMPLE)
{
if (Runtime.pyversionnumber < 35) throw new NotSupportedException("GetBuffer requires at least Python 3.5");
return new PyBuffer(this, flags);
}

Expand Down
6 changes: 5 additions & 1 deletion src/runtime/runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1146,10 +1146,14 @@ internal static long PyObject_Size(IntPtr pointer)
[DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)]
internal static extern IntPtr PyObject_Dir(IntPtr pointer);

#if PYTHON_WITH_PYDEBUG
[DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)]
internal static extern void _Py_NewReference(IntPtr ob);
#endif

//====================================================================
// Python buffer API
//====================================================================

[DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)]
internal static extern int PyObject_CheckBuffer(IntPtr obj);

Expand Down
You are viewing a condensed version of this merge commit. You can view the full changes here.
0