Byte Types
parent
17e3314c41
commit
6948421e7e
|
@ -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))
|
||||
}
|
|
@ -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))
|
||||
}
|
|
@ -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)
|
||||
}
|
|
@ -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))
|
||||
}
|
Loading…
Reference in New Issue