From 9d59dd2b6f89e95ce9fad3547f6b6943a23055ca Mon Sep 17 00:00:00 2001 From: Wes McKinney Date: Wed, 26 Apr 2017 22:59:16 -0400 Subject: [PATCH 1/3] Be robust to PyErr_Fetch returning a null exc value Change-Id: I33d667ffe4ea65f1cc2031a158afcb27446ded0d --- cpp/src/arrow/python/common.h | 10 +++++++--- cpp/src/arrow/python/io.cc | 11 +++++++++-- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/cpp/src/arrow/python/common.h b/cpp/src/arrow/python/common.h index 882bb156224c..12a0dbf0c204 100644 --- a/cpp/src/arrow/python/common.h +++ b/cpp/src/arrow/python/common.h @@ -80,11 +80,15 @@ 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); } }; diff --git a/cpp/src/arrow/python/io.cc b/cpp/src/arrow/python/io.cc index 327e8fe9ff78..684a1509a2c0 100644 --- a/cpp/src/arrow/python/io.cc +++ b/cpp/src/arrow/python/io.cc @@ -46,12 +46,19 @@ static Status CheckPyError() { 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); + + // 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::IOError(message); + } else { + return Status::IOError("Error message was null"); + } } return Status::OK(); } From 0bd11c272339361fb45502e16eb0b6f191e88215 Mon Sep 17 00:00:00 2001 From: Wes McKinney Date: Wed, 26 Apr 2017 23:05:29 -0400 Subject: [PATCH 2/3] Consolidate error handling code a bit Change-Id: Ibfbdeb0f0aaf2ddfce809d67952e4b30a4ecc2c5 --- cpp/src/arrow/python/common.cc | 22 +++++++++++++++++++++ cpp/src/arrow/python/common.h | 17 +++++----------- cpp/src/arrow/python/io.cc | 36 +++++++--------------------------- 3 files changed, 34 insertions(+), 41 deletions(-) diff --git a/cpp/src/arrow/python/common.cc b/cpp/src/arrow/python/common.cc index 717cb5c5cc12..825aa8c5831d 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() { + 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::IOError(message); + } else { + return Status::IOError("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 12a0dbf0c204..9445b43e2659 100644 --- a/cpp/src/arrow/python/common.h +++ b/cpp/src/arrow/python/common.h @@ -92,19 +92,12 @@ struct ARROW_EXPORT PyObjectStringify { } }; +Status CheckPyError(); + // 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()); + // 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 684a1509a2c0..cb0642772979 100644 --- a/cpp/src/arrow/python/io.cc +++ b/cpp/src/arrow/python/io.cc @@ -41,28 +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); - 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::IOError(message); - } else { - return Status::IOError("Error message was null"); - } - } - 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 @@ -78,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()); + RETURN_IF_PYERROR(); return Status::OK(); } @@ -86,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()); + RETURN_IF_PYERROR(); 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()); + RETURN_IF_PYERROR(); *out = result; return Status::OK(); } @@ -100,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()); + RETURN_IF_PYERROR(); PyObject* result = cpp_PyObject_CallMethod(file_, "write", "(O)", py_data); Py_XDECREF(py_data); Py_XDECREF(result); - ARROW_RETURN_NOT_OK(CheckPyError()); + RETURN_IF_PYERROR(); return Status::OK(); } Status PythonFile::Tell(int64_t* position) { PyObject* result = cpp_PyObject_CallMethod(file_, "tell", "()"); - ARROW_RETURN_NOT_OK(CheckPyError()); + RETURN_IF_PYERROR(); *position = PyLong_AsLongLong(result); Py_DECREF(result); // PyLong_AsLongLong can raise OverflowError - ARROW_RETURN_NOT_OK(CheckPyError()); + RETURN_IF_PYERROR(); return Status::OK(); } From fa395cd8cb231a51a008c8ee4f67de84574dd55b Mon Sep 17 00:00:00 2001 From: Wes McKinney Date: Thu, 27 Apr 2017 09:45:36 -0400 Subject: [PATCH 3/3] Enable other kinds of Status errors to be returned Change-Id: I940f50bc5632d6c2c06f1abbe130908149c68c61 --- cpp/src/arrow/python/common.cc | 6 +++--- cpp/src/arrow/python/common.h | 4 +++- cpp/src/arrow/python/io.cc | 14 +++++++------- cpp/src/arrow/status.h | 3 +++ 4 files changed, 16 insertions(+), 11 deletions(-) diff --git a/cpp/src/arrow/python/common.cc b/cpp/src/arrow/python/common.cc index 825aa8c5831d..bedd458c783f 100644 --- a/cpp/src/arrow/python/common.cc +++ b/cpp/src/arrow/python/common.cc @@ -64,7 +64,7 @@ PyBuffer::~PyBuffer() { Py_XDECREF(obj_); } -Status CheckPyError() { +Status CheckPyError(StatusCode code) { if (PyErr_Occurred()) { PyObject *exc_type, *exc_value, *traceback; PyErr_Fetch(&exc_type, &exc_value, &traceback); @@ -78,9 +78,9 @@ Status CheckPyError() { // was encountered when calling tell() on a socket file if (stringified.bytes != nullptr) { std::string message(stringified.bytes); - return Status::IOError(message); + return Status(code, message); } else { - return Status::IOError("Error message was null"); + return Status(code, "Error message was null"); } } return Status::OK(); diff --git a/cpp/src/arrow/python/common.h b/cpp/src/arrow/python/common.h index 9445b43e2659..2e23b42e3cc5 100644 --- a/cpp/src/arrow/python/common.h +++ b/cpp/src/arrow/python/common.h @@ -92,12 +92,14 @@ struct ARROW_EXPORT PyObjectStringify { } }; -Status CheckPyError(); +Status CheckPyError(StatusCode code = StatusCode::UnknownError); // TODO(wesm): We can just let errors pass through. To be explored later #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 cb0642772979..a7193854c4d0 100644 --- a/cpp/src/arrow/python/io.cc +++ b/cpp/src/arrow/python/io.cc @@ -56,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); - RETURN_IF_PYERROR(); + PY_RETURN_IF_ERROR(StatusCode::IOError); return Status::OK(); } @@ -64,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); - RETURN_IF_PYERROR(); + 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); - RETURN_IF_PYERROR(); + PY_RETURN_IF_ERROR(StatusCode::IOError); *out = result; return Status::OK(); } @@ -78,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); - RETURN_IF_PYERROR(); + PY_RETURN_IF_ERROR(StatusCode::IOError); PyObject* result = cpp_PyObject_CallMethod(file_, "write", "(O)", py_data); Py_XDECREF(py_data); Py_XDECREF(result); - RETURN_IF_PYERROR(); + PY_RETURN_IF_ERROR(StatusCode::IOError); return Status::OK(); } Status PythonFile::Tell(int64_t* position) { PyObject* result = cpp_PyObject_CallMethod(file_, "tell", "()"); - RETURN_IF_PYERROR(); + PY_RETURN_IF_ERROR(StatusCode::IOError); *position = PyLong_AsLongLong(result); Py_DECREF(result); // PyLong_AsLongLong can raise OverflowError - RETURN_IF_PYERROR(); + 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);