Skip to content

Commit 0f12455

Browse files
author
Thorsten Riess
committed
Allow access to python bytearrays as []byte
1 parent c9a2cf7 commit 0f12455

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

sequence.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,19 @@ func PyByteArray_AsString(self *PyObject) string {
8989
return C.GoString(c_str)
9090
}
9191

92+
// Return the contents of bytearray as []bytes
93+
func PyByteArray_AsBytes(self *PyObject) []byte {
94+
length := C._gopy_PyByteArray_GET_SIZE(topy(self))
95+
c_str := C.PyByteArray_AsString(topy(self))
96+
return C.GoBytes(unsafe.Pointer(c_str),C.int(length))
97+
}
98+
99+
// Return the contents of bytearray as []bytes, size length
100+
func PyByteArray_AsBytesN(self *PyObject, length int) []byte {
101+
c_str := C.PyByteArray_AsString(topy(self))
102+
return C.GoBytes(unsafe.Pointer(c_str),C.int(length))
103+
}
104+
92105
// int PyByteArray_Resize(PyObject *bytearray, Py_ssize_t len)
93106
// Resize the internal buffer of bytearray to len.
94107
func PyByteArray_Resize(self *PyObject, sz int) error {

0 commit comments

Comments
 (0)