-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
buffer.go
62 lines (51 loc) · 1.58 KB
/
buffer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package bridge
import (
"errors"
"github.com/DataDog/go-python3"
"github.com/apache/arrow/go/arrow/memory"
)
func PyBuffersToBuffers(pyBuffers *python3.PyObject) ([]*memory.Buffer, error) {
// First buffer is the null mask buffer, second is the values.
// [<pyarrow.lib.Buffer object at 0x113d46a08>, <pyarrow.lib.Buffer object at 0x114761998>]
if !python3.PyList_Check(pyBuffers) {
return nil, errors.New("pyBuffers is not a list")
}
length := python3.PyList_Size(pyBuffers)
buffers := make([]*memory.Buffer, 0, length)
for i := 0; i < length; i++ {
buffer, err := PyBuffersGetBuffer(pyBuffers, i)
if err != nil {
return nil, err
}
// buffers[i] = buffer
buffers = append(buffers, buffer)
}
return buffers, nil
}
func PyBuffersGetBuffer(pyBuffers *python3.PyObject, i int) (*memory.Buffer, error) {
// Get the buffer at index i
pyBuffer := python3.PyList_GetItem(pyBuffers, i)
if pyBuffer == nil {
return nil, errors.New("could not get pyBuffer")
}
defer pyBuffer.DecRef()
goBytes, err := PyBufferToBytes(pyBuffer)
if err != nil {
return nil, err
}
buffer := memory.NewBufferBytes(goBytes)
return buffer, nil
}
func PyBufferToBytes(pyBuffer *python3.PyObject) ([]byte, error) {
// <pyarrow.lib.Buffer object at 0x113d46a08>
// Convert the buffer to our Py_buffer struct type
pyBuf, err := python3.PyObject_GetBuffer(pyBuffer, python3.PyBUF_SIMPLE)
if err {
return nil, errors.New("could not get pyBuf")
}
goBytes := python3.PyObject_GetBufferBytes(pyBuf)
if goBytes == nil {
return nil, errors.New("could not get goBytes")
}
return goBytes, nil
}