Skip to content

Commit

Permalink
Deprecate pyarrow::Status in favor of just arrow::Status. Conform pya…
Browse files Browse the repository at this point in the history
…rrow use of ArrayBuilder::Finish

Change-Id: I4f5992b52eeb7421f68f199bcdd549b61b8e4f70
  • Loading branch information
wesm committed Oct 17, 2016
1 parent 9a1f77e commit 1e65b01
Show file tree
Hide file tree
Showing 19 changed files with 157 additions and 418 deletions.
6 changes: 6 additions & 0 deletions cpp/src/arrow/util/status.cc
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,18 @@ std::string Status::CodeAsString() const {
case StatusCode::KeyError:
type = "Key error";
break;
case StatusCode::TypeError:
type = "Type error";
break;
case StatusCode::Invalid:
type = "Invalid";
break;
case StatusCode::IOError:
type = "IOError";
break;
case StatusCode::UnknownError:
type = "Unknown error";
break;
case StatusCode::NotImplemented:
type = "NotImplemented";
break;
Expand Down
17 changes: 14 additions & 3 deletions cpp/src/arrow/util/status.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,10 @@ enum class StatusCode : char {
OK = 0,
OutOfMemory = 1,
KeyError = 2,
Invalid = 3,
IOError = 4,

TypeError = 3,
Invalid = 4,
IOError = 5,
UnknownError = 9,
NotImplemented = 10,
};

Expand All @@ -106,6 +107,14 @@ class ARROW_EXPORT Status {
return Status(StatusCode::KeyError, msg, -1);
}

static Status TypeError(const std::string& msg) {
return Status(StatusCode::TypeError, msg, -1);
}

static Status UnknownError(const std::string& msg) {
return Status(StatusCode::UnknownError, msg, -1);
}

static Status NotImplemented(const std::string& msg) {
return Status(StatusCode::NotImplemented, msg, -1);
}
Expand All @@ -125,6 +134,8 @@ class ARROW_EXPORT Status {
bool IsKeyError() const { return code() == StatusCode::KeyError; }
bool IsInvalid() const { return code() == StatusCode::Invalid; }
bool IsIOError() const { return code() == StatusCode::IOError; }

bool IsUnknownError() const { return code() == StatusCode::UnknownError; }
bool IsNotImplemented() const { return code() == StatusCode::NotImplemented; }

// Return a string representation of this status suitable for printing.
Expand Down
2 changes: 0 additions & 2 deletions python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -438,8 +438,6 @@ set(PYARROW_SRCS
src/pyarrow/config.cc
src/pyarrow/helpers.cc
src/pyarrow/io.cc
src/pyarrow/status.cc

src/pyarrow/adapters/builtin.cc
src/pyarrow/adapters/pandas.cc
)
Expand Down
4 changes: 1 addition & 3 deletions python/pyarrow/error.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,5 @@
# under the License.

from pyarrow.includes.libarrow cimport CStatus
from pyarrow.includes.pyarrow cimport PyStatus

cdef int check_cstatus(const CStatus& status) nogil except -1
cdef int check_status(const PyStatus& status) nogil except -1
cdef int check_status(const CStatus& status) nogil except -1
10 changes: 1 addition & 9 deletions python/pyarrow/error.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,7 @@ from pyarrow.compat import frombytes
class ArrowException(Exception):
pass

cdef int check_cstatus(const CStatus& status) nogil except -1:
if status.ok():
return 0

cdef c_string c_message = status.ToString()
with gil:
raise ArrowException(frombytes(c_message))

cdef int check_status(const PyStatus& status) nogil except -1:
cdef int check_status(const CStatus& status) nogil except -1:
if status.ok():
return 0

Expand Down
35 changes: 9 additions & 26 deletions python/pyarrow/includes/pyarrow.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -25,36 +25,19 @@ cimport pyarrow.includes.libarrow_io as arrow_io


cdef extern from "pyarrow/api.h" namespace "pyarrow" nogil:
# We can later add more of the common status factory methods as needed
cdef PyStatus PyStatus_OK "Status::OK"()

cdef cppclass PyStatus "pyarrow::Status":
PyStatus()

c_string ToString()

c_bool ok()
c_bool IsOutOfMemory()
c_bool IsKeyError()
c_bool IsTypeError()
c_bool IsIOError()
c_bool IsValueError()
c_bool IsNotImplemented()
c_bool IsArrowError()

shared_ptr[CDataType] GetPrimitiveType(Type type)
PyStatus ConvertPySequence(object obj, shared_ptr[CArray]* out)
CStatus ConvertPySequence(object obj, shared_ptr[CArray]* out)

PyStatus PandasToArrow(MemoryPool* pool, object ao,
shared_ptr[CArray]* out)
PyStatus PandasMaskedToArrow(MemoryPool* pool, object ao, object mo,
shared_ptr[CArray]* out)
CStatus PandasToArrow(MemoryPool* pool, object ao,
shared_ptr[CArray]* out)
CStatus PandasMaskedToArrow(MemoryPool* pool, object ao, object mo,
shared_ptr[CArray]* out)

PyStatus ConvertArrayToPandas(const shared_ptr[CArray]& arr,
object py_ref, PyObject** out)
CStatus ConvertArrayToPandas(const shared_ptr[CArray]& arr,
object py_ref, PyObject** out)

PyStatus ConvertColumnToPandas(const shared_ptr[CColumn]& arr,
object py_ref, PyObject** out)
CStatus ConvertColumnToPandas(const shared_ptr[CColumn]& arr,
object py_ref, PyObject** out)

MemoryPool* get_memory_pool()

Expand Down
56 changes: 27 additions & 29 deletions python/pyarrow/io.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ cimport pyarrow.includes.pyarrow as pyarrow
from pyarrow.includes.libarrow_io cimport *

from pyarrow.compat import frombytes, tobytes
from pyarrow.error cimport check_cstatus
from pyarrow.error cimport check_status

cimport cpython as cp

Expand Down Expand Up @@ -57,9 +57,9 @@ cdef class NativeFile:
if self.is_open:
with nogil:
if self.is_readonly:
check_cstatus(self.rd_file.get().Close())
check_status(self.rd_file.get().Close())
else:
check_cstatus(self.wr_file.get().Close())
check_status(self.wr_file.get().Close())
self.is_open = False

cdef read_handle(self, shared_ptr[ReadableFileInterface]* file):
Expand Down Expand Up @@ -88,22 +88,22 @@ cdef class NativeFile:
cdef int64_t size
self._assert_readable()
with nogil:
check_cstatus(self.rd_file.get().GetSize(&size))
check_status(self.rd_file.get().GetSize(&size))
return size

def tell(self):
cdef int64_t position
with nogil:
if self.is_readonly:
check_cstatus(self.rd_file.get().Tell(&position))
check_status(self.rd_file.get().Tell(&position))
else:
check_cstatus(self.wr_file.get().Tell(&position))
check_status(self.wr_file.get().Tell(&position))
return position

def seek(self, int64_t position):
self._assert_readable()
with nogil:
check_cstatus(self.rd_file.get().Seek(position))
check_status(self.rd_file.get().Seek(position))

def write(self, data):
"""
Expand All @@ -116,7 +116,7 @@ cdef class NativeFile:
cdef const uint8_t* buf = <const uint8_t*> cp.PyBytes_AS_STRING(data)
cdef int64_t bufsize = len(data)
with nogil:
check_cstatus(self.wr_file.get().Write(buf, bufsize))
check_status(self.wr_file.get().Write(buf, bufsize))

def read(self, int nbytes):
cdef:
Expand All @@ -127,8 +127,7 @@ cdef class NativeFile:
self._assert_readable()

with nogil:
check_cstatus(self.rd_file.get()
.ReadB(nbytes, &out))
check_status(self.rd_file.get().ReadB(nbytes, &out))

result = cp.PyBytes_FromStringAndSize(
<const char*>out.get().data(), out.get().size())
Expand Down Expand Up @@ -223,7 +222,7 @@ cdef class InMemoryOutputStream(NativeFile):
def get_result(self):
cdef Buffer result = Buffer()

check_cstatus(self.wr_file.get().Close())
check_status(self.wr_file.get().Close())
result.init(<shared_ptr[CBuffer]> self.buffer)

self.is_open = False
Expand Down Expand Up @@ -270,7 +269,7 @@ except ImportError:

def have_libhdfs():
try:
check_cstatus(ConnectLibHdfs())
check_status(ConnectLibHdfs())
return True
except:
return False
Expand Down Expand Up @@ -304,7 +303,7 @@ cdef class HdfsClient:
def close(self):
self._ensure_client()
with nogil:
check_cstatus(self.client.get().Disconnect())
check_status(self.client.get().Disconnect())
self.is_open = False

cdef _ensure_client(self):
Expand Down Expand Up @@ -341,8 +340,7 @@ cdef class HdfsClient:
conf.user = tobytes(user)

with nogil:
check_cstatus(
CHdfsClient.Connect(&conf, &out.client))
check_status(CHdfsClient.Connect(&conf, &out.client))
out.is_open = True

return out
Expand Down Expand Up @@ -383,8 +381,8 @@ cdef class HdfsClient:
self._ensure_client()

with nogil:
check_cstatus(self.client.get()
.ListDirectory(c_path, &listing))
check_status(self.client.get()
.ListDirectory(c_path, &listing))

cdef const HdfsPathInfo* info
for i in range(<int> listing.size()):
Expand Down Expand Up @@ -422,8 +420,8 @@ cdef class HdfsClient:

cdef c_string c_path = tobytes(path)
with nogil:
check_cstatus(self.client.get()
.CreateDirectory(c_path))
check_status(self.client.get()
.CreateDirectory(c_path))

def delete(self, path, bint recursive=False):
"""
Expand All @@ -439,8 +437,8 @@ cdef class HdfsClient:

cdef c_string c_path = tobytes(path)
with nogil:
check_cstatus(self.client.get()
.Delete(c_path, recursive))
check_status(self.client.get()
.Delete(c_path, recursive))

def open(self, path, mode='rb', buffer_size=None, replication=None,
default_block_size=None):
Expand Down Expand Up @@ -473,7 +471,7 @@ cdef class HdfsClient:
append = True

with nogil:
check_cstatus(
check_status(
self.client.get()
.OpenWriteable(c_path, append, c_buffer_size,
c_replication, c_default_block_size,
Expand All @@ -484,8 +482,8 @@ cdef class HdfsClient:
out.is_readonly = False
else:
with nogil:
check_cstatus(self.client.get()
.OpenReadable(c_path, &rd_handle))
check_status(self.client.get()
.OpenReadable(c_path, &rd_handle))

out.rd_file = <shared_ptr[ReadableFileInterface]> rd_handle
out.is_readonly = True
Expand Down Expand Up @@ -579,9 +577,9 @@ cdef class HdfsFile(NativeFile):
try:
with nogil:
while total_bytes < nbytes:
check_cstatus(self.rd_file.get()
.Read(rpc_chunksize, &bytes_read,
buf + total_bytes))
check_status(self.rd_file.get()
.Read(rpc_chunksize, &bytes_read,
buf + total_bytes))

total_bytes += bytes_read

Expand Down Expand Up @@ -647,8 +645,8 @@ cdef class HdfsFile(NativeFile):
try:
while True:
with nogil:
check_cstatus(self.rd_file.get()
.Read(self.buffer_size, &bytes_read, buf))
check_status(self.rd_file.get()
.Read(self.buffer_size, &bytes_read, buf))

total_bytes += bytes_read

Expand Down
18 changes: 9 additions & 9 deletions python/pyarrow/ipc.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ from pyarrow.includes.libarrow_io cimport *
from pyarrow.includes.libarrow_ipc cimport *
cimport pyarrow.includes.pyarrow as pyarrow

from pyarrow.error cimport check_cstatus
from pyarrow.error cimport check_status
from pyarrow.io cimport NativeFile
from pyarrow.schema cimport Schema
from pyarrow.table cimport RecordBatch
Expand Down Expand Up @@ -89,8 +89,8 @@ cdef class ArrowFileWriter:
get_writer(sink, &self.sink)

with nogil:
check_cstatus(CFileWriter.Open(self.sink.get(), schema.sp_schema,
&self.writer))
check_status(CFileWriter.Open(self.sink.get(), schema.sp_schema,
&self.writer))

self.closed = False

Expand All @@ -101,12 +101,12 @@ cdef class ArrowFileWriter:
def write_record_batch(self, RecordBatch batch):
cdef CRecordBatch* bptr = batch.batch
with nogil:
check_cstatus(self.writer.get()
.WriteRecordBatch(bptr.columns(), bptr.num_rows()))
check_status(self.writer.get()
.WriteRecordBatch(bptr.columns(), bptr.num_rows()))

def close(self):
with nogil:
check_cstatus(self.writer.get().Close())
check_status(self.writer.get().Close())
self.closed = True


Expand All @@ -124,9 +124,9 @@ cdef class ArrowFileReader:

with nogil:
if offset != 0:
check_cstatus(CFileReader.Open2(reader, offset, &self.reader))
check_status(CFileReader.Open2(reader, offset, &self.reader))
else:
check_cstatus(CFileReader.Open(reader, &self.reader))
check_status(CFileReader.Open(reader, &self.reader))

property num_dictionaries:

Expand All @@ -147,7 +147,7 @@ cdef class ArrowFileReader:
raise ValueError('Batch number {0} out of range'.format(i))

with nogil:
check_cstatus(self.reader.get().GetRecordBatch(i, &batch))
check_status(self.reader.get().GetRecordBatch(i, &batch))

result = RecordBatch()
result.init(batch)
Expand Down
Loading

0 comments on commit 1e65b01

Please sign in to comment.