Skip to content

Commit db7434b

Browse files
committed
Merge pull request sbinet#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

Lines changed: 17 additions & 0 deletions
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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy