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
3 changes: 2 additions & 1 deletion python/pyarrow/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@

from pyarrow.filesystem import Filesystem, HdfsClient, LocalFilesystem
from pyarrow.io import (HdfsFile, NativeFile, PythonFileInterface,
Buffer, InMemoryOutputStream, BufferReader)
Buffer, InMemoryOutputStream, BufferReader,
frombuffer)

from pyarrow.ipc import FileReader, FileWriter, StreamReader, StreamWriter

Expand Down
16 changes: 10 additions & 6 deletions python/pyarrow/io.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,18 @@ cdef class NativeFile:

def write(self, data):
"""
Write bytes-like (unicode, encoded to UTF-8) to file
Write byte from any object implementing buffer protocol (bytes,
bytearray, ndarray, pyarrow.Buffer)
"""
self._assert_writeable()

data = tobytes(data)
if isinstance(data, six.string_types):
data = tobytes(data)

cdef const uint8_t* buf = <const uint8_t*> cp.PyBytes_AS_STRING(data)
cdef int64_t bufsize = len(data)
cdef Buffer arrow_buffer = frombuffer(data)

cdef const uint8_t* buf = arrow_buffer.buffer.get().data()
cdef int64_t bufsize = len(arrow_buffer)
with nogil:
check_status(self.wr_file.get().Write(buf, bufsize))

Expand Down Expand Up @@ -505,15 +509,15 @@ cdef class BufferReader(NativeFile):
if isinstance(obj, Buffer):
self.buffer = obj
else:
self.buffer = build_arrow_buffer(obj)
self.buffer = frombuffer(obj)

self.rd_file.reset(new CBufferReader(self.buffer.buffer))
self.is_readable = 1
self.is_writeable = 0
self.is_open = True


def build_arrow_buffer(object obj):
def frombuffer(object obj):
"""
Construct an Arrow buffer from a Python bytes object
"""
Expand Down
29 changes: 23 additions & 6 deletions python/pyarrow/tests/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import numpy as np

from pyarrow.compat import u, guid
import pyarrow as pa
import pyarrow.io as io

# ----------------------------------------------------------------------
Expand Down Expand Up @@ -127,28 +128,29 @@ def get_buffer():
def test_buffer_bytes():
val = b'some data'

buf = io.build_arrow_buffer(val)
buf = pa.frombuffer(val)
assert isinstance(buf, io.Buffer)

result = buf.to_pybytes()

assert result == val


def test_buffer_memoryview():
val = b'some data'

buf = io.build_arrow_buffer(val)
buf = pa.frombuffer(val)
assert isinstance(buf, io.Buffer)

result = memoryview(buf)

assert result == val


def test_buffer_bytearray():
val = bytearray(b'some data')


buf = io.build_arrow_buffer(val)
buf = pa.frombuffer(val)
assert isinstance(buf, io.Buffer)

result = bytearray(buf)
Expand All @@ -159,7 +161,7 @@ def test_buffer_bytearray():
def test_buffer_memoryview_is_immutable():
val = b'some data'

buf = io.build_arrow_buffer(val)
buf = pa.frombuffer(val)
assert isinstance(buf, io.Buffer)

result = memoryview(buf)
Expand Down Expand Up @@ -198,21 +200,36 @@ def test_inmemory_write_after_closed():
with pytest.raises(IOError):
f.write(b'not ok')


def test_buffer_protocol_ref_counting():
import gc

def make_buffer(bytes_obj):
return bytearray(io.build_arrow_buffer(bytes_obj))
return bytearray(pa.frombuffer(bytes_obj))

buf = make_buffer(b'foo')
gc.collect()
assert buf == b'foo'


def test_nativefile_write_memoryview():
f = io.InMemoryOutputStream()
data = b'ok'

arr = np.frombuffer(data, dtype='S1')

f.write(arr)
f.write(bytearray(data))

buf = f.get_result()

assert buf.to_pybytes() == data * 2


# ----------------------------------------------------------------------
# OS files and memory maps


@pytest.fixture
def sample_disk_data(request):
SIZE = 4096
Expand Down