Skip to content
Closed
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
4 changes: 2 additions & 2 deletions python/pyarrow/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@
from pyarrow.lib import TimestampType

# Buffers, allocation
from pyarrow.lib import (Buffer, ResizableBuffer, compress, decompress,
allocate_buffer, frombuffer)
from pyarrow.lib import (Buffer, ForeignBuffer, ResizableBuffer, compress,
decompress, allocate_buffer, frombuffer)

from pyarrow.lib import (MemoryPool, total_allocated_bytes,
set_memory_pool, default_memory_pool,
Expand Down
12 changes: 12 additions & 0 deletions python/pyarrow/io.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -720,6 +720,18 @@ cdef class Buffer:
return self.size


cdef class ForeignBuffer(Buffer):

def __init__(self, addr, size, base):
cdef:
intptr_t c_addr = addr
int64_t c_size = size
self.base = base
cdef shared_ptr[CBuffer] buffer = make_shared[CBuffer](
<uint8_t*>c_addr, c_size)
self.init(<shared_ptr[CBuffer]> buffer)


cdef class ResizableBuffer(Buffer):

cdef void init_rz(self, const shared_ptr[CResizableBuffer]& buffer):
Expand Down
5 changes: 5 additions & 0 deletions python/pyarrow/lib.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,11 @@ cdef class Buffer:
cdef int _check_nullptr(self) except -1


cdef class ForeignBuffer(Buffer):
cdef:
object base


cdef class ResizableBuffer(Buffer):

cdef void init_rz(self, const shared_ptr[CResizableBuffer]& buffer)
Expand Down
9 changes: 9 additions & 0 deletions python/pyarrow/tests/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import weakref

import numpy as np
import numpy.testing as npt

import pandas as pd

Expand Down Expand Up @@ -253,6 +254,14 @@ def test_buffer_equals():
assert buf2.equals(buf5)


def test_foreign_buffer():
n = np.array([1, 2])
addr = n.__array_interface__["data"][0]
size = n.nbytes
fb = pa.ForeignBuffer(addr, size, n)
npt.assert_array_equal(np.asarray(fb), n.view(dtype=np.int8))


def test_allocate_buffer():
buf = pa.allocate_buffer(100)
assert buf.size == 100
Expand Down