Skip to content

Commit

Permalink
handle errors
Browse files Browse the repository at this point in the history
  • Loading branch information
wjones127 committed Jan 11, 2024
1 parent 17ba2bc commit c539894
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
12 changes: 9 additions & 3 deletions python/pyarrow/src/arrow/python/common.cc
Original file line number Diff line number Diff line change
Expand Up @@ -153,14 +153,20 @@ class PythonErrorDetail : public StatusDetail {
formatted.reset(PyObject_CallFunctionObjArgs(fmt_exception.obj(), exc_type_.obj(),
exc_value_.obj(), exc_traceback_.obj(),
NULL));
RETURN_IF_PYERROR();

std::stringstream ss;
ss << "Python exception: ";
Py_ssize_t num_lines = PyList_GET_SIZE(formatted.obj());
Py_ssize_t num_lines = PySequence_Length(formatted.obj());
for (Py_ssize_t i = 0; i < num_lines; ++i) {
Py_ssize_t line_size;
const char* data =
PyUnicode_AsUTF8AndSize(PyList_GET_ITEM(formatted.obj(), i), &line_size);

PyObject* line = PyList_GET_ITEM(formatted.obj(), i);
RETURN_IF_PYERROR();

const char* data = PyUnicode_AsUTF8AndSize(line, &line_size);
RETURN_IF_PYERROR();

ss << std::string_view(data, line_size);
}
return ss.str();
Expand Down
3 changes: 3 additions & 0 deletions python/pyarrow/tests/test_cffi.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,7 @@ def test_export_import_batch_reader(reader_factory):

@needs_cffi
def test_export_import_exception_reader():
# See: https://github.com/apache/arrow/issues/37164
c_stream = ffi.new("struct ArrowArrayStream*")
ptr_stream = int(ffi.cast("uintptr_t", c_stream))

Expand All @@ -441,6 +442,8 @@ def gen():
# inner *and* outer exception should be present
assert 'ValueError: foo' in str(exc_info.value)
assert 'NotImplementedError: bar' in str(exc_info.value)
# Stacktrace containing line of the raise statement
assert 'raise ValueError(\'foo\')' in str(exc_info.value)

assert pa.total_allocated_bytes() == old_allocated

Expand Down

0 comments on commit c539894

Please sign in to comment.