diff --git a/cpp/src/arrow/python/common.cc b/cpp/src/arrow/python/common.cc index 717cb5c5cc12..bedd458c783f 100644 --- a/cpp/src/arrow/python/common.cc +++ b/cpp/src/arrow/python/common.cc @@ -64,5 +64,27 @@ PyBuffer::~PyBuffer() { Py_XDECREF(obj_); } +Status CheckPyError(StatusCode code) { + if (PyErr_Occurred()) { + PyObject *exc_type, *exc_value, *traceback; + PyErr_Fetch(&exc_type, &exc_value, &traceback); + PyObjectStringify stringified(exc_value); + Py_XDECREF(exc_type); + Py_XDECREF(exc_value); + Py_XDECREF(traceback); + PyErr_Clear(); + + // ARROW-866: in some esoteric cases, formatting exc_value can fail. This + // was encountered when calling tell() on a socket file + if (stringified.bytes != nullptr) { + std::string message(stringified.bytes); + return Status(code, message); + } else { + return Status(code, "Error message was null"); + } + } + return Status::OK(); +} + } // namespace py } // namespace arrow diff --git a/cpp/src/arrow/python/common.h b/cpp/src/arrow/python/common.h index 882bb156224c..2e23b42e3cc5 100644 --- a/cpp/src/arrow/python/common.h +++ b/cpp/src/arrow/python/common.h @@ -80,27 +80,26 @@ struct ARROW_EXPORT PyObjectStringify { if (PyUnicode_Check(obj)) { bytes_obj = PyUnicode_AsUTF8String(obj); tmp_obj.reset(bytes_obj); + bytes = PyBytes_AsString(bytes_obj); + size = PyBytes_GET_SIZE(bytes_obj); + } else if (PyBytes_Check(obj)) { + bytes = PyBytes_AsString(obj); + size = PyBytes_GET_SIZE(obj); } else { - bytes_obj = obj; + bytes = nullptr; + size = -1; } - bytes = PyBytes_AsString(bytes_obj); - size = PyBytes_GET_SIZE(bytes_obj); } }; +Status CheckPyError(StatusCode code = StatusCode::UnknownError); + // TODO(wesm): We can just let errors pass through. To be explored later -#define RETURN_IF_PYERROR() \ - if (PyErr_Occurred()) { \ - PyObject *exc_type, *exc_value, *traceback; \ - PyErr_Fetch(&exc_type, &exc_value, &traceback); \ - PyObjectStringify stringified(exc_value); \ - std::string message(stringified.bytes); \ - Py_DECREF(exc_type); \ - Py_XDECREF(exc_value); \ - Py_XDECREF(traceback); \ - PyErr_Clear(); \ - return Status::UnknownError(message); \ - } +#define RETURN_IF_PYERROR() \ + RETURN_NOT_OK(CheckPyError()); + +#define PY_RETURN_IF_ERROR(CODE) \ + RETURN_NOT_OK(CheckPyError(CODE)); // Return the common PyArrow memory pool ARROW_EXPORT void set_default_memory_pool(MemoryPool* pool); diff --git a/cpp/src/arrow/python/io.cc b/cpp/src/arrow/python/io.cc index 327e8fe9ff78..a7193854c4d0 100644 --- a/cpp/src/arrow/python/io.cc +++ b/cpp/src/arrow/python/io.cc @@ -41,21 +41,6 @@ PythonFile::~PythonFile() { Py_DECREF(file_); } -static Status CheckPyError() { - if (PyErr_Occurred()) { - PyObject *exc_type, *exc_value, *traceback; - PyErr_Fetch(&exc_type, &exc_value, &traceback); - PyObjectStringify stringified(exc_value); - std::string message(stringified.bytes); - Py_XDECREF(exc_type); - Py_XDECREF(exc_value); - Py_XDECREF(traceback); - PyErr_Clear(); - return Status::IOError(message); - } - return Status::OK(); -} - // This is annoying: because C++11 does not allow implicit conversion of string // literals to non-const char*, we need to go through some gymnastics to use // PyObject_CallMethod without a lot of pain (its arguments are non-const @@ -71,7 +56,7 @@ Status PythonFile::Close() { // whence: 0 for relative to start of file, 2 for end of file PyObject* result = cpp_PyObject_CallMethod(file_, "close", "()"); Py_XDECREF(result); - ARROW_RETURN_NOT_OK(CheckPyError()); + PY_RETURN_IF_ERROR(StatusCode::IOError); return Status::OK(); } @@ -79,13 +64,13 @@ Status PythonFile::Seek(int64_t position, int whence) { // whence: 0 for relative to start of file, 2 for end of file PyObject* result = cpp_PyObject_CallMethod(file_, "seek", "(ii)", position, whence); Py_XDECREF(result); - ARROW_RETURN_NOT_OK(CheckPyError()); + PY_RETURN_IF_ERROR(StatusCode::IOError); return Status::OK(); } Status PythonFile::Read(int64_t nbytes, PyObject** out) { PyObject* result = cpp_PyObject_CallMethod(file_, "read", "(i)", nbytes); - ARROW_RETURN_NOT_OK(CheckPyError()); + PY_RETURN_IF_ERROR(StatusCode::IOError); *out = result; return Status::OK(); } @@ -93,24 +78,24 @@ Status PythonFile::Read(int64_t nbytes, PyObject** out) { Status PythonFile::Write(const uint8_t* data, int64_t nbytes) { PyObject* py_data = PyBytes_FromStringAndSize(reinterpret_cast(data), nbytes); - ARROW_RETURN_NOT_OK(CheckPyError()); + PY_RETURN_IF_ERROR(StatusCode::IOError); PyObject* result = cpp_PyObject_CallMethod(file_, "write", "(O)", py_data); Py_XDECREF(py_data); Py_XDECREF(result); - ARROW_RETURN_NOT_OK(CheckPyError()); + PY_RETURN_IF_ERROR(StatusCode::IOError); return Status::OK(); } Status PythonFile::Tell(int64_t* position) { PyObject* result = cpp_PyObject_CallMethod(file_, "tell", "()"); - ARROW_RETURN_NOT_OK(CheckPyError()); + PY_RETURN_IF_ERROR(StatusCode::IOError); *position = PyLong_AsLongLong(result); Py_DECREF(result); // PyLong_AsLongLong can raise OverflowError - ARROW_RETURN_NOT_OK(CheckPyError()); + PY_RETURN_IF_ERROR(StatusCode::IOError); return Status::OK(); } diff --git a/cpp/src/arrow/status.h b/cpp/src/arrow/status.h index dd65b753fef3..6a8cee27cb75 100644 --- a/cpp/src/arrow/status.h +++ b/cpp/src/arrow/status.h @@ -91,6 +91,9 @@ class ARROW_EXPORT Status { Status() : state_(NULL) {} ~Status() { delete[] state_; } + Status(StatusCode code, const std::string& msg) + : Status(code, msg, -1) {} + // Copy the specified status. Status(const Status& s); void operator=(const Status& s);