Skip to content

Commit 6948421

Browse files
committed
Byte Types
1 parent 17e3314 commit 6948421

File tree

4 files changed

+257
-0
lines changed

4 files changed

+257
-0
lines changed

byte_array.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package python3
2+
3+
/*
4+
#include "Python.h"
5+
#include "macro.h"
6+
#include "type.h"
7+
*/
8+
import "C"
9+
import "unsafe"
10+
11+
//ByteArray : https://docs.python.org/3/c-api/bytearray.html#c.PyByteArray_Type
12+
var ByteArray = togo(C._go_PyByteArray_Type)
13+
14+
//PyByteArray_Check : https://docs.python.org/3/c-api/bytearray.html#c.PyByteArray_Check
15+
func PyByteArray_Check(o *PyObject) bool {
16+
return C._go_PyByteArray_Check(toc(o)) != 0
17+
}
18+
19+
//PyByteArray_CheckExact : https://docs.python.org/3/c-api/bytearray.html#c.PyByteArray_CheckExact
20+
func PyByteArray_CheckExact(o *PyObject) bool {
21+
return C._go_PyByteArray_CheckExact(toc(o)) != 0
22+
}
23+
24+
//PyByteArray_FromObject : https://docs.python.org/3/c-api/bytearray.html#c.PyByteArray_FromObject
25+
func PyByteArray_FromObject(o *PyObject) *PyObject {
26+
return togo(C.PyByteArray_FromObject(toc(o)))
27+
}
28+
29+
//PyByteArray_FromStringAndSize : https://docs.python.org/3/c-api/bytearray.html#c.PyByteArray_FromStringAndSize
30+
func PyByteArray_FromStringAndSize(str string) *PyObject {
31+
cstr := C.CString(str)
32+
defer C.free(unsafe.Pointer(cstr))
33+
34+
return togo(C.PyByteArray_FromStringAndSize(cstr, C.Py_ssize_t(len(str))))
35+
}
36+
37+
//PyByteArray_Concat : https://docs.python.org/3/c-api/bytearray.html#c.PyByteArray_Concat
38+
func PyByteArray_Concat(a, b *PyObject) *PyObject {
39+
return togo(C.PyByteArray_Concat(toc(a), toc(b)))
40+
}
41+
42+
//PyByteArray_Size : https://docs.python.org/3/c-api/bytearray.html#c.PyByteArray_Size
43+
func PyByteArray_Size(o *PyObject) int {
44+
return int(C.PyByteArray_Size(toc(o)))
45+
}
46+
47+
//PyByteArray_AsString : https://docs.python.org/3/c-api/bytearray.html#c.PyByteArray_AsString
48+
func PyByteArray_AsString(o *PyObject) string {
49+
return C.GoString(C.PyByteArray_AsString(toc(o)))
50+
}
51+
52+
//PyByteArray_Resize : https://docs.python.org/3/c-api/bytearray.html#c.PyByteArray_Resize
53+
func PyByteArray_Resize(bytearray *PyObject, len int) {
54+
C.PyByteArray_Resize(toc(bytearray), C.Py_ssize_t(len))
55+
}

byte_array_test.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package python3
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestByteArrayCheck(t *testing.T) {
10+
Py_Initialize()
11+
12+
s1 := "aaaaaaaa"
13+
14+
array1 := PyByteArray_FromStringAndSize(s1)
15+
assert.True(t, PyByteArray_Check(array1))
16+
assert.True(t, PyByteArray_CheckExact(array1))
17+
defer array1.DecRef()
18+
}
19+
20+
func TestByteArrayFromAsString(t *testing.T) {
21+
Py_Initialize()
22+
23+
s1 := "aaaaaaaa"
24+
25+
array1 := PyByteArray_FromStringAndSize(s1)
26+
defer array1.DecRef()
27+
28+
assert.Equal(t, s1, PyByteArray_AsString(array1))
29+
}
30+
31+
func TestByteArrayConcat(t *testing.T) {
32+
Py_Initialize()
33+
34+
s1 := "aaaaaaaa"
35+
s2 := "bbbbbbbb"
36+
37+
array1 := PyByteArray_FromStringAndSize(s1)
38+
defer array1.DecRef()
39+
40+
bytes := PyBytes_FromString(s2)
41+
assert.NotNil(t, bytes)
42+
defer bytes.DecRef()
43+
44+
array2 := PyByteArray_FromObject(bytes)
45+
assert.NotNil(t, array2)
46+
defer array2.DecRef()
47+
48+
newArray := PyByteArray_Concat(array1, array2)
49+
defer newArray.DecRef()
50+
51+
assert.Equal(t, s1+s2, PyByteArray_AsString(newArray))
52+
}
53+
54+
func TestByteArrayResize(t *testing.T) {
55+
Py_Initialize()
56+
57+
s1 := "aaaaaaaa"
58+
59+
array1 := PyByteArray_FromStringAndSize(s1)
60+
defer array1.DecRef()
61+
62+
length := 20
63+
PyByteArray_Resize(array1, 20)
64+
65+
assert.Equal(t, length, PyByteArray_Size(array1))
66+
}

bytes.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package python3
2+
3+
/*
4+
#include "Python.h"
5+
#include "macro.h"
6+
#include "type.h"
7+
*/
8+
import "C"
9+
import "unsafe"
10+
11+
//Bytes : https://docs.python.org/3/c-api/bytes.html#c.PyBytes_Type
12+
var Bytes = togo(C._go_PyBytes_Type)
13+
14+
//PyBytes_Check : https://docs.python.org/3/c-api/bytes.html#c.PyBytes_Check
15+
func PyBytes_Check(o *PyObject) bool {
16+
return C._go_PyBytes_Check(toc(o)) != 0
17+
}
18+
19+
//PyBytes_CheckExact : https://docs.python.org/3/c-api/bytes.html#c.PyBytes_CheckExact
20+
func PyBytes_CheckExact(o *PyObject) bool {
21+
return C._go_PyBytes_CheckExact(toc(o)) != 0
22+
}
23+
24+
//PyBytes_FromString : https://docs.python.org/3/c-api/bytes.html#c.PyBytes_FromString
25+
func PyBytes_FromString(str string) *PyObject {
26+
cstr := C.CString(str)
27+
defer C.free(unsafe.Pointer(cstr))
28+
29+
return togo(C.PyBytes_FromString(cstr))
30+
}
31+
32+
//PyBytes_FromObject : https://docs.python.org/3/c-api/bytes.html#c.PyBytes_FromObject
33+
func PyBytes_FromObject(o *PyObject) *PyObject {
34+
return togo(C.PyBytes_FromObject(toc(o)))
35+
}
36+
37+
//PyBytes_Size : https://docs.python.org/3/c-api/bytes.html#c.PyBytes_Size
38+
func PyBytes_Size(o *PyObject) int {
39+
return int(C.PyBytes_Size(toc(o)))
40+
}
41+
42+
//PyBytes_AsString : https://docs.python.org/3/c-api/bytes.html#c.PyBytes_AsString
43+
func PyBytes_AsString(o *PyObject) string {
44+
return C.GoString(C.PyBytes_AsString(toc(o)))
45+
}
46+
47+
//PyBytes_Concat : https://docs.python.org/3/c-api/bytes.html#c.PyBytes_Concat
48+
func PyBytes_Concat(bytes, newpart *PyObject) *PyObject {
49+
cbytes := toc(bytes)
50+
C.PyBytes_Concat(&cbytes, toc(newpart))
51+
return togo(cbytes)
52+
}
53+
54+
//PyBytes_ConcatAndDel : https://docs.python.org/3/c-api/bytes.html#c.PyBytes_ConcatAndDel
55+
func PyBytes_ConcatAndDel(bytes, newpart *PyObject) *PyObject {
56+
cbytes := toc(bytes)
57+
C.PyBytes_ConcatAndDel(&cbytes, toc(newpart))
58+
return togo(cbytes)
59+
}

bytes_test.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package python3
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestBytesCheck(t *testing.T) {
10+
Py_Initialize()
11+
12+
s1 := "aaaaaaaa"
13+
14+
bytes1 := PyBytes_FromString(s1)
15+
assert.True(t, PyBytes_Check(bytes1))
16+
assert.True(t, PyBytes_CheckExact(bytes1))
17+
defer bytes1.DecRef()
18+
}
19+
20+
func TestBytesFromAsString(t *testing.T) {
21+
Py_Initialize()
22+
23+
s1 := "aaaaaaaa"
24+
25+
bytes1 := PyBytes_FromString(s1)
26+
defer bytes1.DecRef()
27+
28+
assert.Equal(t, s1, PyBytes_AsString(bytes1))
29+
}
30+
31+
func TestBytesSize(t *testing.T) {
32+
Py_Initialize()
33+
34+
s1 := "aaaaaaaa"
35+
36+
bytes1 := PyBytes_FromString(s1)
37+
defer bytes1.DecRef()
38+
39+
assert.Equal(t, 8, PyBytes_Size(bytes1))
40+
}
41+
42+
func TestBytesConcat(t *testing.T) {
43+
Py_Initialize()
44+
45+
s1 := "aaaaaaaa"
46+
s2 := "bbbbbbbb"
47+
48+
bytes1 := PyBytes_FromString(s1)
49+
50+
bytes2 := PyBytes_FromString(s2)
51+
assert.NotNil(t, bytes2)
52+
defer bytes2.DecRef()
53+
54+
bytes1 = PyBytes_Concat(bytes1, bytes2)
55+
assert.NotNil(t, bytes1)
56+
defer bytes1.DecRef()
57+
58+
assert.Equal(t, s1+s2, PyBytes_AsString(bytes1))
59+
}
60+
61+
func TestBytesConcatAndDel(t *testing.T) {
62+
Py_Initialize()
63+
64+
s1 := "aaaaaaaa"
65+
s2 := "bbbbbbbb"
66+
67+
bytes1 := PyBytes_FromString(s1)
68+
69+
bytes2 := PyBytes_FromString(s2)
70+
assert.NotNil(t, bytes2)
71+
72+
bytes1 = PyBytes_ConcatAndDel(bytes1, bytes2)
73+
assert.NotNil(t, bytes1)
74+
defer bytes1.DecRef()
75+
76+
assert.Equal(t, s1+s2, PyBytes_AsString(bytes1))
77+
}

0 commit comments

Comments
 (0)