Skip to content

ARROW-866: [Python] Be robust to PyErr_Fetch returning a null exc value - #606

Closed
wesm wants to merge 3 commits into
apache:masterfrom
wesm:ARROW-866
Closed

wesm wants to merge 3 commits into
apache:masterfrom
wesm:ARROW-866

Conversation

@wesm

@wesm wesm commented Apr 27, 2017

Copy link
Copy Markdown
Member

cc @BryanCutler. This was a tricky one. I am not sure how to reproduce with our current code -- I reverted the patch from ARROW-822 to get a reproduction so I could fix this. Now, the error raised is:

/home/wesm/code/arrow/python/pyarrow/_error.pyx in pyarrow._error.check_status (/home/wesm/code/arrow/python/build/temp.linux-x86_64-2.7/_error.cxx:1324)()
     58             raise ArrowInvalid(message)
     59         elif status.IsIOError():
---> 60             raise ArrowIOError(message)
     61         elif status.IsOutOfMemory():
     62             raise ArrowMemoryError(message)

ArrowIOError: IOError: Error message was null

I'm not sure why calling tell on the socket object results in a bad exception state, but in any case it seems that the result of PyErr_Fetch cannot be relied upon to be non-null even when PyErr_Occurred() returns non-null

wesm added 3 commits April 26, 2017 22:59
Change-Id: I33d667ffe4ea65f1cc2031a158afcb27446ded0d
Change-Id: Ibfbdeb0f0aaf2ddfce809d67952e4b30a4ecc2c5
Change-Id: I940f50bc5632d6c2c06f1abbe130908149c68c61

@xhochy xhochy left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1, LGTM

@asfgit asfgit closed this in 81be9c6 Apr 27, 2017
@wesm
wesm deleted the ARROW-866 branch April 28, 2017 15:23
@BryanCutler

Copy link
Copy Markdown
Member

Hi @wesm , sorry for the delay but I was out of town. I looked into this and the problem isn't that exc returns null, but it's a tuple with (error code, message) and then PyBytes_AsString in PyObjectStringify fails for this, making it seem as that stringified.bytes was null. For the error in the repro code, PyErr_Fetch returns:

exc_type = <type 'exceptions.IOError'>
exc_value = (29, 'Illegal seek')

I tried the below patch and it seems to work because PyErr_NormalizeException will make the tuple into an instance of IOError and then convert that object into a str. I think it should handle all cases and PyString_AsString will handle unicode. What do you think?

diff --git a/cpp/src/arrow/python/io.cc b/cpp/src/arrow/python/io.cc
index ba82a45..114c3df 100644
--- a/cpp/src/arrow/python/io.cc
+++ b/cpp/src/arrow/python/io.cc
@@ -45,10 +45,13 @@ 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);
+    PyErr_NormalizeException(&exc_type, &exc_value, &traceback);
+    PyObject *exc_value_str = PyObject_Str(exc_value);
+    char *c_str = PyString_AsString(exc_value_str);
+    std::string message(c_str);
     Py_XDECREF(exc_type);
     Py_XDECREF(exc_value);
+    Py_XDECREF(exc_value_str);
     Py_XDECREF(traceback);
     PyErr_Clear();
     return Status::IOError(message);

@wesm

wesm commented May 2, 2017

Copy link
Copy Markdown
Member Author

Cool, thanks for digging into this. Would you submit this as a patch? Thanks!

jeffknupp pushed a commit to jeffknupp/arrow that referenced this pull request Jun 3, 2017
cc @BryanCutler. This was a tricky one. I am not sure how to reproduce with our current code -- I reverted the patch from ARROW-822 to get a reproduction so I could fix this. Now, the error raised is:

```
/home/wesm/code/arrow/python/pyarrow/_error.pyx in pyarrow._error.check_status (/home/wesm/code/arrow/python/build/temp.linux-x86_64-2.7/_error.cxx:1324)()
     58             raise ArrowInvalid(message)
     59         elif status.IsIOError():
---> 60             raise ArrowIOError(message)
     61         elif status.IsOutOfMemory():
     62             raise ArrowMemoryError(message)

ArrowIOError: IOError: Error message was null
```

I'm not sure why calling `tell` on the socket object results in a bad exception state, but in any case it seems that the result of `PyErr_Fetch` cannot be relied upon to be non-null even when `PyErr_Occurred()` returns non-null

Author: Wes McKinney <wes.mckinney@twosigma.com>

Closes apache#606 from wesm/ARROW-866 and squashes the following commits:

fa395cd [Wes McKinney] Enable other kinds of Status errors to be returned
0bd11c2 [Wes McKinney] Consolidate error handling code a bit
9d59dd2 [Wes McKinney] Be robust to PyErr_Fetch returning a null exc value
pribor pushed a commit to GlobalWebIndex/arrow that referenced this pull request Oct 24, 2025
…/release.sh` (apache#606)

Fixes apacheGH-603.

Message from Git:

hint: You have created a nested tag. The object referred to by your new
tag is
hint: already a tag. If you meant to tag the object that it points to,
use:
    hint:
    hint: 	git tag -f v18.2.0 v18.2.0-rc5^{}
    hint: Disable this message with "git config advice.nestedTag false"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants