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
22 changes: 22 additions & 0 deletions cpp/src/arrow/python/common.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
29 changes: 14 additions & 15 deletions cpp/src/arrow/python/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
29 changes: 7 additions & 22 deletions cpp/src/arrow/python/io.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -71,46 +56,46 @@ 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();
}

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();
}

Status PythonFile::Write(const uint8_t* data, int64_t nbytes) {
PyObject* py_data =
PyBytes_FromStringAndSize(reinterpret_cast<const char*>(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();
}
Expand Down
3 changes: 3 additions & 0 deletions cpp/src/arrow/status.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down