Skip to content
This repository was archived by the owner on Jul 24, 2023. It is now read-only.

Commit db7434b

Browse files
committed
Merge pull request #26 from tcriess/master
Allow access to python bytearrays as []byte
2 parents c9a2cf7 + 33b0f5e commit db7434b

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

sequence.go

+17
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,23 @@ func PyByteArray_AsString(self *PyObject) string {
8989
return C.GoString(c_str)
9090
}
9191

92+
// PyByteArray_AsBytes returns 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+
// PyByteArray_AsBytesN returns the contents of bytearray as []bytes, size length
100+
func PyByteArray_AsBytesN(self *PyObject, length int) []byte {
101+
blength := int(C._gopy_PyByteArray_GET_SIZE(topy(self)))
102+
if (blength < length) || (length < 0) {
103+
panic("bytearray length out of range")
104+
}
105+
c_str := C.PyByteArray_AsString(topy(self))
106+
return C.GoBytes(unsafe.Pointer(c_str),C.int(length))
107+
}
108+
92109
// int PyByteArray_Resize(PyObject *bytearray, Py_ssize_t len)
93110
// Resize the internal buffer of bytearray to len.
94111
func PyByteArray_Resize(self *PyObject, sz int) error {

0 commit comments

Comments
 (0)