Skip to content
Draft
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
7 changes: 7 additions & 0 deletions _examples/gobytes/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,11 @@
print("Python bytes to Go: ", go.Slice_byte.from_bytes(a))
print("Go bytes to Python: ", bytes(go.Slice_byte([3, 4, 5])))

# Regression test for issue #359: 0-length slice must not crash
empty = gobytes.CreateBytes(0)
print("Go empty slice: ", empty)
empty_bytes = bytes(empty)
assert empty_bytes == b"", f"expected b'', got {empty_bytes!r}"
print("Go empty bytes to Python: ", empty_bytes)

print("OK")
5 changes: 5 additions & 0 deletions bind/gen_slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,11 @@ otherwise parameter is a python list that we copy from
g.gofile.Printf("func Slice_byte_to_bytes(handle CGoHandle) *C.PyObject {\n")
g.gofile.Indent()
g.gofile.Printf("s := deptrFromHandle_Slice_byte(handle)\n")
g.gofile.Printf("if len(s) == 0 {\n")
g.gofile.Indent()
g.gofile.Printf("return C.PyBytes_FromStringAndSize(nil, 0)\n")
g.gofile.Outdent()
g.gofile.Printf("}\n")
g.gofile.Printf("ptr := unsafe.Pointer(&s[0])\n")
g.gofile.Printf("size := len(s)\n")
if WindowsOS {
Expand Down
2 changes: 2 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,8 @@ Go slice: go.Slice_byte len: 10 handle: 1 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
gobytes.HashBytes from Go bytes: gobytes.Array_4_byte len: 4 handle: 2 [12, 13, 81, 81]
Python bytes to Go: go.Slice_byte len: 4 handle: 3 [0, 1, 2, 3]
Go bytes to Python: b'\x03\x04\x05'
Go empty slice: go.Slice_byte len: 0 handle: 5 []
Go empty bytes to Python: b''
OK
`),
})
Expand Down
Loading