From 6948421e7e17fbd9c2c6b0d3fa7efbb6a3c33588 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Calixte?= Date: Thu, 6 Dec 2018 12:26:11 -0500 Subject: [PATCH] Byte Types --- byte_array.go | 55 +++++++++++++++++++++++++++++++++ byte_array_test.go | 66 +++++++++++++++++++++++++++++++++++++++ bytes.go | 59 +++++++++++++++++++++++++++++++++++ bytes_test.go | 77 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 257 insertions(+) create mode 100644 byte_array.go create mode 100644 byte_array_test.go create mode 100644 bytes.go create mode 100644 bytes_test.go diff --git a/byte_array.go b/byte_array.go new file mode 100644 index 0000000..aa6144f --- /dev/null +++ b/byte_array.go @@ -0,0 +1,55 @@ +package python3 + +/* +#include "Python.h" +#include "macro.h" +#include "type.h" +*/ +import "C" +import "unsafe" + +//ByteArray : https://docs.python.org/3/c-api/bytearray.html#c.PyByteArray_Type +var ByteArray = togo(C._go_PyByteArray_Type) + +//PyByteArray_Check : https://docs.python.org/3/c-api/bytearray.html#c.PyByteArray_Check +func PyByteArray_Check(o *PyObject) bool { + return C._go_PyByteArray_Check(toc(o)) != 0 +} + +//PyByteArray_CheckExact : https://docs.python.org/3/c-api/bytearray.html#c.PyByteArray_CheckExact +func PyByteArray_CheckExact(o *PyObject) bool { + return C._go_PyByteArray_CheckExact(toc(o)) != 0 +} + +//PyByteArray_FromObject : https://docs.python.org/3/c-api/bytearray.html#c.PyByteArray_FromObject +func PyByteArray_FromObject(o *PyObject) *PyObject { + return togo(C.PyByteArray_FromObject(toc(o))) +} + +//PyByteArray_FromStringAndSize : https://docs.python.org/3/c-api/bytearray.html#c.PyByteArray_FromStringAndSize +func PyByteArray_FromStringAndSize(str string) *PyObject { + cstr := C.CString(str) + defer C.free(unsafe.Pointer(cstr)) + + return togo(C.PyByteArray_FromStringAndSize(cstr, C.Py_ssize_t(len(str)))) +} + +//PyByteArray_Concat : https://docs.python.org/3/c-api/bytearray.html#c.PyByteArray_Concat +func PyByteArray_Concat(a, b *PyObject) *PyObject { + return togo(C.PyByteArray_Concat(toc(a), toc(b))) +} + +//PyByteArray_Size : https://docs.python.org/3/c-api/bytearray.html#c.PyByteArray_Size +func PyByteArray_Size(o *PyObject) int { + return int(C.PyByteArray_Size(toc(o))) +} + +//PyByteArray_AsString : https://docs.python.org/3/c-api/bytearray.html#c.PyByteArray_AsString +func PyByteArray_AsString(o *PyObject) string { + return C.GoString(C.PyByteArray_AsString(toc(o))) +} + +//PyByteArray_Resize : https://docs.python.org/3/c-api/bytearray.html#c.PyByteArray_Resize +func PyByteArray_Resize(bytearray *PyObject, len int) { + C.PyByteArray_Resize(toc(bytearray), C.Py_ssize_t(len)) +} diff --git a/byte_array_test.go b/byte_array_test.go new file mode 100644 index 0000000..c4d688e --- /dev/null +++ b/byte_array_test.go @@ -0,0 +1,66 @@ +package python3 + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestByteArrayCheck(t *testing.T) { + Py_Initialize() + + s1 := "aaaaaaaa" + + array1 := PyByteArray_FromStringAndSize(s1) + assert.True(t, PyByteArray_Check(array1)) + assert.True(t, PyByteArray_CheckExact(array1)) + defer array1.DecRef() +} + +func TestByteArrayFromAsString(t *testing.T) { + Py_Initialize() + + s1 := "aaaaaaaa" + + array1 := PyByteArray_FromStringAndSize(s1) + defer array1.DecRef() + + assert.Equal(t, s1, PyByteArray_AsString(array1)) +} + +func TestByteArrayConcat(t *testing.T) { + Py_Initialize() + + s1 := "aaaaaaaa" + s2 := "bbbbbbbb" + + array1 := PyByteArray_FromStringAndSize(s1) + defer array1.DecRef() + + bytes := PyBytes_FromString(s2) + assert.NotNil(t, bytes) + defer bytes.DecRef() + + array2 := PyByteArray_FromObject(bytes) + assert.NotNil(t, array2) + defer array2.DecRef() + + newArray := PyByteArray_Concat(array1, array2) + defer newArray.DecRef() + + assert.Equal(t, s1+s2, PyByteArray_AsString(newArray)) +} + +func TestByteArrayResize(t *testing.T) { + Py_Initialize() + + s1 := "aaaaaaaa" + + array1 := PyByteArray_FromStringAndSize(s1) + defer array1.DecRef() + + length := 20 + PyByteArray_Resize(array1, 20) + + assert.Equal(t, length, PyByteArray_Size(array1)) +} diff --git a/bytes.go b/bytes.go new file mode 100644 index 0000000..3886a57 --- /dev/null +++ b/bytes.go @@ -0,0 +1,59 @@ +package python3 + +/* +#include "Python.h" +#include "macro.h" +#include "type.h" +*/ +import "C" +import "unsafe" + +//Bytes : https://docs.python.org/3/c-api/bytes.html#c.PyBytes_Type +var Bytes = togo(C._go_PyBytes_Type) + +//PyBytes_Check : https://docs.python.org/3/c-api/bytes.html#c.PyBytes_Check +func PyBytes_Check(o *PyObject) bool { + return C._go_PyBytes_Check(toc(o)) != 0 +} + +//PyBytes_CheckExact : https://docs.python.org/3/c-api/bytes.html#c.PyBytes_CheckExact +func PyBytes_CheckExact(o *PyObject) bool { + return C._go_PyBytes_CheckExact(toc(o)) != 0 +} + +//PyBytes_FromString : https://docs.python.org/3/c-api/bytes.html#c.PyBytes_FromString +func PyBytes_FromString(str string) *PyObject { + cstr := C.CString(str) + defer C.free(unsafe.Pointer(cstr)) + + return togo(C.PyBytes_FromString(cstr)) +} + +//PyBytes_FromObject : https://docs.python.org/3/c-api/bytes.html#c.PyBytes_FromObject +func PyBytes_FromObject(o *PyObject) *PyObject { + return togo(C.PyBytes_FromObject(toc(o))) +} + +//PyBytes_Size : https://docs.python.org/3/c-api/bytes.html#c.PyBytes_Size +func PyBytes_Size(o *PyObject) int { + return int(C.PyBytes_Size(toc(o))) +} + +//PyBytes_AsString : https://docs.python.org/3/c-api/bytes.html#c.PyBytes_AsString +func PyBytes_AsString(o *PyObject) string { + return C.GoString(C.PyBytes_AsString(toc(o))) +} + +//PyBytes_Concat : https://docs.python.org/3/c-api/bytes.html#c.PyBytes_Concat +func PyBytes_Concat(bytes, newpart *PyObject) *PyObject { + cbytes := toc(bytes) + C.PyBytes_Concat(&cbytes, toc(newpart)) + return togo(cbytes) +} + +//PyBytes_ConcatAndDel : https://docs.python.org/3/c-api/bytes.html#c.PyBytes_ConcatAndDel +func PyBytes_ConcatAndDel(bytes, newpart *PyObject) *PyObject { + cbytes := toc(bytes) + C.PyBytes_ConcatAndDel(&cbytes, toc(newpart)) + return togo(cbytes) +} diff --git a/bytes_test.go b/bytes_test.go new file mode 100644 index 0000000..6d62838 --- /dev/null +++ b/bytes_test.go @@ -0,0 +1,77 @@ +package python3 + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestBytesCheck(t *testing.T) { + Py_Initialize() + + s1 := "aaaaaaaa" + + bytes1 := PyBytes_FromString(s1) + assert.True(t, PyBytes_Check(bytes1)) + assert.True(t, PyBytes_CheckExact(bytes1)) + defer bytes1.DecRef() +} + +func TestBytesFromAsString(t *testing.T) { + Py_Initialize() + + s1 := "aaaaaaaa" + + bytes1 := PyBytes_FromString(s1) + defer bytes1.DecRef() + + assert.Equal(t, s1, PyBytes_AsString(bytes1)) +} + +func TestBytesSize(t *testing.T) { + Py_Initialize() + + s1 := "aaaaaaaa" + + bytes1 := PyBytes_FromString(s1) + defer bytes1.DecRef() + + assert.Equal(t, 8, PyBytes_Size(bytes1)) +} + +func TestBytesConcat(t *testing.T) { + Py_Initialize() + + s1 := "aaaaaaaa" + s2 := "bbbbbbbb" + + bytes1 := PyBytes_FromString(s1) + + bytes2 := PyBytes_FromString(s2) + assert.NotNil(t, bytes2) + defer bytes2.DecRef() + + bytes1 = PyBytes_Concat(bytes1, bytes2) + assert.NotNil(t, bytes1) + defer bytes1.DecRef() + + assert.Equal(t, s1+s2, PyBytes_AsString(bytes1)) +} + +func TestBytesConcatAndDel(t *testing.T) { + Py_Initialize() + + s1 := "aaaaaaaa" + s2 := "bbbbbbbb" + + bytes1 := PyBytes_FromString(s1) + + bytes2 := PyBytes_FromString(s2) + assert.NotNil(t, bytes2) + + bytes1 = PyBytes_ConcatAndDel(bytes1, bytes2) + assert.NotNil(t, bytes1) + defer bytes1.DecRef() + + assert.Equal(t, s1+s2, PyBytes_AsString(bytes1)) +}