pitrou commented on code in PR #39380:
URL: https://github.com/apache/arrow/pull/39380#discussion_r1446427840
##########
python/pyarrow/src/arrow/python/common.cc:
##########
@@ -131,6 +138,34 @@ class PythonErrorDetail : public StatusDetail {
}
protected:
+ Result<std::string> FormatImpl() const {
+ PyAcquireGIL lock;
+
+ // Use traceback.format_exception()
+ OwnedRef traceback_module;
+ RETURN_NOT_OK(internal::ImportModule("traceback", &traceback_module));
+
+ OwnedRef fmt_exception;
+ RETURN_NOT_OK(internal::ImportFromModule(traceback_module.obj(),
"format_exception",
+ &fmt_exception));
+
+ OwnedRef formatted;
+ formatted.reset(PyObject_CallFunctionObjArgs(fmt_exception.obj(),
exc_type_.obj(),
Review Comment:
You need to check for errors here.
##########
python/pyarrow/src/arrow/python/common.cc:
##########
@@ -131,6 +138,34 @@ class PythonErrorDetail : public StatusDetail {
}
protected:
+ Result<std::string> FormatImpl() const {
+ PyAcquireGIL lock;
+
+ // Use traceback.format_exception()
+ OwnedRef traceback_module;
+ RETURN_NOT_OK(internal::ImportModule("traceback", &traceback_module));
+
+ OwnedRef fmt_exception;
+ RETURN_NOT_OK(internal::ImportFromModule(traceback_module.obj(),
"format_exception",
+ &fmt_exception));
+
+ OwnedRef formatted;
+ formatted.reset(PyObject_CallFunctionObjArgs(fmt_exception.obj(),
exc_type_.obj(),
+ exc_value_.obj(),
exc_traceback_.obj(),
+ NULL));
+
+ std::stringstream ss;
+ ss << "Python exception: ";
+ Py_ssize_t num_lines = PyList_GET_SIZE(formatted.obj());
Review Comment:
You should probably check that we do have a list here. Or you can simply use
`PySequence_Length` and `PySequence_GetItem` (this code is not
performance-critical).
##########
python/pyarrow/tests/test_cffi.py:
##########
@@ -414,6 +414,37 @@ def test_export_import_batch_reader(reader_factory):
pa.RecordBatchReader._import_from_c(ptr_stream)
+@needs_cffi
+def test_export_import_exception_reader():
+ c_stream = ffi.new("struct ArrowArrayStream*")
Review Comment:
Can you add a reference to the GH issue?
##########
python/pyarrow/src/arrow/python/common.cc:
##########
@@ -131,6 +138,34 @@ class PythonErrorDetail : public StatusDetail {
}
protected:
+ Result<std::string> FormatImpl() const {
+ PyAcquireGIL lock;
+
+ // Use traceback.format_exception()
+ OwnedRef traceback_module;
+ RETURN_NOT_OK(internal::ImportModule("traceback", &traceback_module));
+
+ OwnedRef fmt_exception;
+ RETURN_NOT_OK(internal::ImportFromModule(traceback_module.obj(),
"format_exception",
+ &fmt_exception));
+
+ OwnedRef formatted;
+ formatted.reset(PyObject_CallFunctionObjArgs(fmt_exception.obj(),
exc_type_.obj(),
+ exc_value_.obj(),
exc_traceback_.obj(),
+ NULL));
+
+ std::stringstream ss;
+ ss << "Python exception: ";
+ Py_ssize_t num_lines = PyList_GET_SIZE(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);
Review Comment:
Again, should either type-check and check for errors, or call the more
generic `PyObject_StdStringStr`.
##########
python/pyarrow/tests/test_cffi.py:
##########
@@ -414,6 +414,37 @@ def test_export_import_batch_reader(reader_factory):
pa.RecordBatchReader._import_from_c(ptr_stream)
+@needs_cffi
+def test_export_import_exception_reader():
+ c_stream = ffi.new("struct ArrowArrayStream*")
+ ptr_stream = int(ffi.cast("uintptr_t", c_stream))
+
+ gc.collect() # Make sure no Arrow data dangles in a ref cycle
+ old_allocated = pa.total_allocated_bytes()
+
+ def gen():
+ if True:
+ try:
+ raise ValueError('foo')
+ except ValueError as e:
+ raise NotImplementedError('bar') from e
+ else:
+ yield from make_batches()
+
+ original = pa.RecordBatchReader.from_batches(make_schema(), gen())
+ original._export_to_c(ptr_stream)
+
+ reader = pa.RecordBatchReader._import_from_c(ptr_stream)
+ with pytest.raises(OSError) as exc_info:
+ reader.read_next_batch()
+
+ # inner *and* outer exception should be present
+ assert 'ValueError: foo' in str(exc_info.value)
+ assert 'NotImplementedError: bar' in str(exc_info.value)
Review Comment:
Can you also test that the traceback is present? For example:
```suggestion
assert "raise ValueError('foo')" in str(exc_info.value)
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]