- Notifications
You must be signed in to change notification settings - Fork749
Pybuffer#1195
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Pybuffer#1195
Changes fromall commits
Commits
Show all changes
8 commits Select commitHold shift + click to select a range
ac997a9
Add Python buffer api support
thesn10def2cc2
Update finalizer
thesn108237e87
Update finalizer 2
thesn10edd849f
bring back pyversionnumber but leave it internal
koubaa7b30cfa
remove 3.5 checks
koubaa1e82ee8
use sys version info with C API
koubaad201197
Use System.Version
koubaa0ed417c
compare full version
koubaaFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
1 change: 1 addition & 0 deletionsAUTHORS.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletionsCHANGELOG.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletionssrc/embed_tests/Python.EmbeddingTest.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletionssrc/embed_tests/TestPyBuffer.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
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() | ||
{ | ||
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() | ||
{ | ||
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); | ||
} | ||
} | ||
} | ||
} | ||
} |
2 changes: 2 additions & 0 deletionssrc/runtime/Python.Runtime.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletionssrc/runtime/bufferinterface.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
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, | ||
/// <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), | ||
} | ||
} |
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.