diff --git a/python/pyarrow/__init__.py b/python/pyarrow/__init__.py index c6f0be04e8d0..dce438910151 100644 --- a/python/pyarrow/__init__.py +++ b/python/pyarrow/__init__.py @@ -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 diff --git a/python/pyarrow/io.pyx b/python/pyarrow/io.pyx index d528bdc49520..d64427aa36ef 100644 --- a/python/pyarrow/io.pyx +++ b/python/pyarrow/io.pyx @@ -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 = 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)) @@ -505,7 +509,7 @@ 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 @@ -513,7 +517,7 @@ cdef class BufferReader(NativeFile): self.is_open = True -def build_arrow_buffer(object obj): +def frombuffer(object obj): """ Construct an Arrow buffer from a Python bytes object """ diff --git a/python/pyarrow/tests/test_io.py b/python/pyarrow/tests/test_io.py index 9cd15c4a76ce..15c5e6b92438 100644 --- a/python/pyarrow/tests/test_io.py +++ b/python/pyarrow/tests/test_io.py @@ -23,6 +23,7 @@ import numpy as np from pyarrow.compat import u, guid +import pyarrow as pa import pyarrow.io as io # ---------------------------------------------------------------------- @@ -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) @@ -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) @@ -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