Skip to content
This repository was archived by the owner on Aug 10, 2022. It is now read-only.

Add PyBytes_FromStringAndSize to allow strings with NUL characters to be processed #31

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions bytes.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ func PyBytes_FromString(str string) *PyObject {
return togo(C.PyBytes_FromString(cstr))
}

//PyBytes_FromStringAndSize : https://docs.python.org/3/c-api/bytes.html#c.PyBytes_FromStringAndSize
func PyBytes_FromStringAndSize(str string) *PyObject {
cstr := C.CString(str)
defer C.free(unsafe.Pointer(cstr))

return togo(C.PyBytes_FromStringAndSize(cstr, C.Py_ssize_t(len(str))))
}

//PyBytes_FromObject : https://docs.python.org/3/c-api/bytes.html#c.PyBytes_FromObject
func PyBytes_FromObject(o *PyObject) *PyObject {
return togo(C.PyBytes_FromObject(toc(o)))
Expand Down
11 changes: 11 additions & 0 deletions bytes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,17 @@ func TestBytesFromAsString(t *testing.T) {
assert.Equal(t, s1, PyBytes_AsString(bytes1))
}

func TestBytesFromStringAndSize(t *testing.T) {
Py_Initialize()

s1 := "aaaaaaaa\x00bbbb"

bytes1 := PyBytes_FromStringAndSize(s1)
defer bytes1.DecRef()

assert.Equal(t, s1, PyBytes_AsString(bytes1))
}

func TestBytesSize(t *testing.T) {
Py_Initialize()

Expand Down