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
Next Next commit
Add Python buffer api support
  • Loading branch information
thesn10 authored and filmor committed May 17, 2020
commit 3c9f7b3edf801bd6ff0024196cb6a8126dd84f47
1 change: 1 addition & 0 deletions AUTHORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
- ([@OneBlue](https://github.com/OneBlue))
- ([@rico-chet](https://github.com/rico-chet))
- ([@rmadsen-ks](https://github.com/rmadsen-ks))
- ([@SnGmng](https://github.com/SnGmng))
- ([@stonebig](https://github.com/stonebig))
- ([@testrunner123](https://github.com/testrunner123))

1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ This document follows the conventions laid out in [Keep a CHANGELOG][].
- Added Runtime.None to be able to pass None as parameter into Python from .NET
- Added 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])

### Changed

Expand Down
1 change: 1 addition & 0 deletions src/embed_tests/Python.EmbeddingTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@
<Compile Include="TestFinalizer.cs" />
<Compile Include="TestInstanceWrapping.cs" />
<Compile Include="TestPyAnsiString.cs" />
<Compile Include="TestPyBuffer.cs" />
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT: a bit off

Copy link
Contributor Author
@thesn10 thesn10 Nov 2, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm ive used TAB instead of spaces. I didnt know TAB has different length than 4 spaces on github.

<Compile Include="TestPyFloat.cs" />
<Compile Include="TestPyInt.cs" />
<Compile Include="TestPyList.cs" />
Expand Down
72 changes: 72 additions & 0 deletions src/embed_tests/TestPyBuffer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using System.Text;
using NUnit.Framework;
using Python.Runtime;

namespace Python.EmbeddingTest {
class TestPyBuffer
{
[OneTimeSetUp]
public void SetUp()
{
PythonEngine.Initialize();
}

[OneTimeTearDown]
public void Dispose()
{
PythonEngine.Shutdown();
}

[Test]
public void TestBufferWrite()
{
if (Runtime.Runtime.pyversionnumber < 35) return;

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

using (Py.GIL())
{
using (var scope = Py.CreateScope())
{
scope.Exec($"arr = bytearray({bufferTestString.Length})");
PyObject pythonArray = scope.Get("arr");
byte[] managedArray = new UTF8Encoding().GetBytes(bufferTestString);

using (PyBuffer buf = pythonArray.GetBuffer())
{
buf.Write(managedArray, 0, managedArray.Length);
}

string result = scope.Eval("arr.decode('utf-8')").ToString();
Assert.IsTrue(result == bufferTestString);
}
}
}

[Test]
public void TestBufferRead()
{
if (Runtime.Runtime.pyversionnumber < 35) return;

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

using (Py.GIL())
{
using (var scope = Py.CreateScope())
{
scope.Exec($"arr = b'{bufferTestString}'");
PyObject pythonArray = scope.Get("arr");
byte[] managedArray = new byte[bufferTestString.Length];

using (PyBuffer buf = pythonArray.GetBuffer())
{
buf.Read(managedArray, 0, managedArray.Length);
}

string result = new UTF8Encoding().GetString(managedArray);
Assert.IsTrue(result == bufferTestString);
}
}
}
}
}
4 changes: 3 additions & 1 deletion src/runtime/Python.Runtime.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
<Compile Include="arrayobject.cs" />
<Compile Include="assemblymanager.cs" />
<Compile Include="BorrowedReference.cs" />
<Compile Include="bufferinterface.cs" />
<Compile Include="classderived.cs" />
<Compile Include="classbase.cs" />
<Compile Include="classmanager.cs" />
Expand Down Expand Up @@ -130,6 +131,7 @@
<Compile Include="overload.cs" />
<Compile Include="propertyobject.cs" />
<Compile Include="pyansistring.cs" />
<Compile Include="pybuffer.cs" />
<Compile Include="pydict.cs" />
<Compile Include="PyExportAttribute.cs" />
<Compile Include="pyfloat.cs" />
Expand Down Expand Up @@ -183,4 +185,4 @@
<Copy SourceFiles="$(TargetAssembly)" DestinationFolder="$(PythonBuildDir)" />
<!--Copy SourceFiles="$(TargetAssemblyPdb)" Condition="Exists('$(TargetAssemblyPdb)')" DestinationFolder="$(PythonBuildDir)" /-->
</Target>
</Project>
</Project>
106 changes: 106 additions & 0 deletions src/runtime/bufferinterface.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
using System;
using System.Runtime.InteropServices;

namespace Python.Runtime
{
/* buffer interface */
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
internal struct Py_buffer {
public IntPtr buf;
public IntPtr obj; /* owned reference */
[MarshalAs(UnmanagedType.SysInt)]
public IntPtr len;
[MarshalAs(UnmanagedType.SysInt)]
public IntPtr itemsize; /* This is Py_ssize_t so it can be
pointed to by strides in simple case.*/
[MarshalAs(UnmanagedType.Bool)]
public bool _readonly;
public int ndim;
[MarshalAs(UnmanagedType.LPStr)]
public string format;
public IntPtr shape;
public IntPtr strides;
public IntPtr suboffsets;
public IntPtr _internal;
}

public enum BufferOrderStyle
{
C,
Fortran,
EitherOne,
}

/* Flags for getting buffers */
public enum PyBUF
{
/// <summary>
/// Simple buffer without shape strides and suboffsets
/// </summary>
SIMPLE = 0,
/// <summary>
/// Controls the <see cref="PyBuffer.ReadOnly"/> field. If set, the exporter MUST provide a writable buffer or else report failure. Otherwise, the exporter MAY provide either a read-only or writable buffer, but the choice MUST be consistent for all consumers.
/// </summary>
WRITABLE = 0x0001,
/// <summary>
/// Controls the <see cref="PyBuffer.Format"/> field. If set, this field MUST be filled in correctly. Otherwise, this field MUST be NULL.
/// </summary>
FORMATS = 0x0004,
Copy link
Contributor Author
@thesn10 thesn10 Nov 2, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is originally named FORMAT and not FORMATS but for some reason i get a "name not cls compilant" warning if i use FORMAT

/// <summary>
/// N-Dimensional buffer with shape
/// </summary>
ND = 0x0008,
/// <summary>
/// Buffer with strides and shape
/// </summary>
STRIDES = (0x0010 | ND),
/// <summary>
/// C-Contigous buffer with strides and shape
/// </summary>
C_CONTIGUOUS = (0x0020 | STRIDES),
/// <summary>
/// F-Contigous buffer with strides and shape
/// </summary>
F_CONTIGUOUS = (0x0040 | STRIDES),
/// <summary>
/// C or Fortran contigous buffer with strides and shape
/// </summary>
ANY_CONTIGUOUS = (0x0080 | STRIDES),
/// <summary>
/// Buffer with suboffsets (if needed)
/// </summary>
INDIRECT = (0x0100 | STRIDES),
/// <summary>
/// Writable C-Contigous buffer with shape
/// </summary>
CONTIG = (ND | WRITABLE),
/// <summary>
/// Readonly C-Contigous buffer with shape
/// </summary>
CONTIG_RO = (ND),
/// <summary>
/// Writable buffer with shape and strides
/// </summary>
STRIDED = (STRIDES | WRITABLE),
/// <summary>
/// Readonly buffer with shape and strides
/// </summary>
STRIDED_RO = (STRIDES),
/// <summary>
/// Writable buffer with shape, strides and format
/// </summary>
RECORDS = (STRIDES | WRITABLE | FORMATS),
/// <summary>
/// Readonly buffer with shape, strides and format
/// </summary>
RECORDS_RO = (STRIDES | FORMATS),
/// <summary>
/// Writable indirect buffer with shape, strides, format and suboffsets (if needed)
/// </summary>
FULL = (INDIRECT | WRITABLE | FORMATS),
/// <summary>
/// Readonly indirect buffer with shape, strides, format and suboffsets (if needed)
/// </summary>
FULL_RO = (INDIRECT | FORMATS),
}
}
Loading
0