8000 Allow access to python bytearrays as []byte by tcriess · Pull Request #26 · sbinet/go-python · GitHub
[go: up one dir, main page]

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

Allow access to python bytearrays as []byte #26

Merged
merged 3 commits into from
Aug 7, 2015
Merged

Conversation

tcriess
Copy link
@tcriess tcriess commented Aug 7, 2015

With the existing functions I could not find a way to handle python bytearrays that contain zeros - PyByteArray_AsString always cuts the string off at the first zero byte (I guess C.GoString is responsible). Hence I've added the two functions to create []bytes from bytearrays. I'm not sure if that creates a memory leak though.

8000
// Return the contents of bytearray as []bytes, size length
func PyByteArray_AsBytesN(self *PyObject, length int) []byte {
c_str := C.PyByteArray_AsString(topy(self))
return C.GoBytes(unsafe.Pointer(c_str),C.int(length))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

perhaps check that the passed length is within the allowed range?
ie: call _gopy_PyByteArray_GET_SIZE and compare (+panic with out of range)

@sbinet
Copy link
Owner
sbinet commented Aug 7, 2015

LGTM modulo the minor comments above.

thanks!

@tcriess
Copy link
Author
tcriess commented Aug 7, 2015

Ok, I've changed the docstring and the length type to uint64 + check the given length argument vs. the bytearray length (panic if it is out of range).

@sbinet
Copy link
Owner
sbinet commented Aug 7, 2015

you should keep length as an int. that's what Go uses to index slices.

@tcriess
Copy link
Author
tcriess commented Aug 7, 2015

Ah, ok. length is back to int (+ check if >= 0)

sbinet added a commit that referenced this pull request Aug 7, 2015
Allow access to python bytearrays as []byte
@sbinet sbinet merged commit db7434b into sbinet:master Aug 7, 2015
@sbinet
Copy link
Owner
sbinet commented Aug 7, 2015

thanks.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
0