-
Notifications
You must be signed in to change notification settings - Fork 4k
ARROW-4036: [C++] Pluggable Status message, by exposing an abstract delegate class. #4484
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
dec4585
not-quite pluggable error codes
emkornfield cd22df6
format
emkornfield 14e3467
dont rely on rtti
emkornfield 484b3a2
style fix
emkornfield 9f59160
don't make_shared inline
emkornfield 317ea9c
fix complie
emkornfield 8f011b3
fix status propagation
emkornfield 4586fd1
fix typo
emkornfield a4e6a1f
add logging for debug
emkornfield 3626a90
try removing message
emkornfield 85786ef
change messages
emkornfield 74d563c
fixes
emkornfield 9c905b0
fix lint
emkornfield 21e1b95
fix compilation
emkornfield ea56d1e
Fix PythonErrorDetail to store Python error state
pitrou 729bba1
Fix Py2 issues (hopefully)
pitrou 040216d
fixes for comments outside python
emkornfield a66f999
make format
emkornfield 4d1ab8d
don't import plasma errors directly into top level pyarrow module
emkornfield File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,11 +23,15 @@ | |
|
|
||
| #include "arrow/memory_pool.h" | ||
| #include "arrow/status.h" | ||
| #include "arrow/util/checked_cast.h" | ||
| #include "arrow/util/logging.h" | ||
|
|
||
| #include "arrow/python/helpers.h" | ||
|
|
||
| namespace arrow { | ||
|
|
||
| using internal::checked_cast; | ||
|
|
||
| namespace py { | ||
|
|
||
| static std::mutex memory_pool_mutex; | ||
|
|
@@ -47,6 +51,129 @@ MemoryPool* get_memory_pool() { | |
| } | ||
| } | ||
|
|
||
| // ---------------------------------------------------------------------- | ||
| // PythonErrorDetail | ||
|
|
||
| namespace { | ||
|
|
||
| const char kErrorDetailTypeId[] = "arrow::py::PythonErrorDetail"; | ||
|
|
||
| // Try to match the Python exception type with an appropriate Status code | ||
| StatusCode MapPyError(PyObject* exc_type) { | ||
| StatusCode code; | ||
|
|
||
| if (PyErr_GivenExceptionMatches(exc_type, PyExc_MemoryError)) { | ||
| code = StatusCode::OutOfMemory; | ||
| } else if (PyErr_GivenExceptionMatches(exc_type, PyExc_IndexError)) { | ||
| code = StatusCode::IndexError; | ||
| } else if (PyErr_GivenExceptionMatches(exc_type, PyExc_KeyError)) { | ||
| code = StatusCode::KeyError; | ||
| } else if (PyErr_GivenExceptionMatches(exc_type, PyExc_TypeError)) { | ||
| code = StatusCode::TypeError; | ||
| } else if (PyErr_GivenExceptionMatches(exc_type, PyExc_ValueError) || | ||
| PyErr_GivenExceptionMatches(exc_type, PyExc_OverflowError)) { | ||
| code = StatusCode::Invalid; | ||
| } else if (PyErr_GivenExceptionMatches(exc_type, PyExc_EnvironmentError)) { | ||
| code = StatusCode::IOError; | ||
| } else if (PyErr_GivenExceptionMatches(exc_type, PyExc_NotImplementedError)) { | ||
| code = StatusCode::NotImplemented; | ||
| } else { | ||
| code = StatusCode::UnknownError; | ||
| } | ||
| return code; | ||
| } | ||
|
|
||
| // PythonErrorDetail indicates a Python exception was raised. | ||
| class PythonErrorDetail : public StatusDetail { | ||
| public: | ||
| const char* type_id() const override { return kErrorDetailTypeId; } | ||
|
|
||
| std::string ToString() const override { | ||
| // This is simple enough not to need the GIL | ||
| const auto ty = reinterpret_cast<const PyTypeObject*>(exc_type_.obj()); | ||
| // XXX Should we also print traceback? | ||
| return std::string("Python exception: ") + ty->tp_name; | ||
| } | ||
|
|
||
| void RestorePyError() const { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thanks for doing this, I started down this path and decided to keep it simpler because there were semantics of the GIL I was hazy on. |
||
| Py_INCREF(exc_type_.obj()); | ||
| Py_INCREF(exc_value_.obj()); | ||
| Py_INCREF(exc_traceback_.obj()); | ||
| PyErr_Restore(exc_type_.obj(), exc_value_.obj(), exc_traceback_.obj()); | ||
| } | ||
|
|
||
| PyObject* exc_type() const { return exc_type_.obj(); } | ||
|
|
||
| PyObject* exc_value() const { return exc_value_.obj(); } | ||
|
|
||
| static std::shared_ptr<PythonErrorDetail> FromPyError() { | ||
| PyObject* exc_type = nullptr; | ||
| PyObject* exc_value = nullptr; | ||
| PyObject* exc_traceback = nullptr; | ||
|
|
||
| PyErr_Fetch(&exc_type, &exc_value, &exc_traceback); | ||
| PyErr_NormalizeException(&exc_type, &exc_value, &exc_traceback); | ||
| ARROW_CHECK(exc_type) | ||
| << "PythonErrorDetail::FromPyError called without a Python error set"; | ||
| DCHECK(PyType_Check(exc_type)); | ||
| DCHECK(exc_value); // Ensured by PyErr_NormalizeException, double-check | ||
| if (exc_traceback == nullptr) { | ||
| // Needed by PyErr_Restore() | ||
| Py_INCREF(Py_None); | ||
| exc_traceback = Py_None; | ||
| } | ||
|
|
||
| std::shared_ptr<PythonErrorDetail> detail(new PythonErrorDetail); | ||
| detail->exc_type_.reset(exc_type); | ||
| detail->exc_value_.reset(exc_value); | ||
| detail->exc_traceback_.reset(exc_traceback); | ||
| return detail; | ||
| } | ||
|
|
||
| protected: | ||
| PythonErrorDetail() = default; | ||
|
|
||
| OwnedRefNoGIL exc_type_, exc_value_, exc_traceback_; | ||
| }; | ||
|
|
||
| } // namespace | ||
|
|
||
| // ---------------------------------------------------------------------- | ||
| // Python exception <-> Status | ||
|
|
||
| Status ConvertPyError(StatusCode code) { | ||
| auto detail = PythonErrorDetail::FromPyError(); | ||
| if (code == StatusCode::UnknownError) { | ||
| code = MapPyError(detail->exc_type()); | ||
| } | ||
|
|
||
| std::string message; | ||
| RETURN_NOT_OK(internal::PyObject_StdStringStr(detail->exc_value(), &message)); | ||
| return Status(code, message, detail); | ||
| } | ||
|
|
||
| Status PassPyError() { | ||
| if (PyErr_Occurred()) { | ||
| return ConvertPyError(); | ||
| } | ||
| return Status::OK(); | ||
| } | ||
|
|
||
| bool IsPyError(const Status& status) { | ||
| if (status.ok()) { | ||
| return false; | ||
| } | ||
| auto detail = status.detail(); | ||
| bool result = detail != nullptr && detail->type_id() == kErrorDetailTypeId; | ||
| return result; | ||
| } | ||
|
|
||
| void RestorePyError(const Status& status) { | ||
| ARROW_CHECK(IsPyError(status)); | ||
| const auto& detail = checked_cast<const PythonErrorDetail&>(*status.detail()); | ||
| detail.RestorePyError(); | ||
| } | ||
|
|
||
| // ---------------------------------------------------------------------- | ||
| // PyBuffer | ||
|
|
||
|
|
@@ -64,7 +191,7 @@ Status PyBuffer::Init(PyObject* obj) { | |
| } | ||
| return Status::OK(); | ||
| } else { | ||
| return Status(StatusCode::PythonError, ""); | ||
| return ConvertPyError(StatusCode::Invalid); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -83,56 +210,5 @@ PyBuffer::~PyBuffer() { | |
| } | ||
| } | ||
|
|
||
| // ---------------------------------------------------------------------- | ||
| // Python exception -> Status | ||
|
|
||
| Status ConvertPyError(StatusCode code) { | ||
| PyObject* exc_type = nullptr; | ||
| PyObject* exc_value = nullptr; | ||
| PyObject* traceback = nullptr; | ||
|
|
||
| PyErr_Fetch(&exc_type, &exc_value, &traceback); | ||
| PyErr_NormalizeException(&exc_type, &exc_value, &traceback); | ||
|
|
||
| DCHECK_NE(exc_type, nullptr) << "ConvertPyError called without an exception set"; | ||
|
|
||
| OwnedRef exc_type_ref(exc_type); | ||
| OwnedRef exc_value_ref(exc_value); | ||
| OwnedRef traceback_ref(traceback); | ||
|
|
||
| std::string message; | ||
| RETURN_NOT_OK(internal::PyObject_StdStringStr(exc_value, &message)); | ||
|
|
||
| if (code == StatusCode::UnknownError) { | ||
| // Try to match the Python exception type with an appropriate Status code | ||
| if (PyErr_GivenExceptionMatches(exc_type, PyExc_MemoryError)) { | ||
| code = StatusCode::OutOfMemory; | ||
| } else if (PyErr_GivenExceptionMatches(exc_type, PyExc_IndexError)) { | ||
| code = StatusCode::IndexError; | ||
| } else if (PyErr_GivenExceptionMatches(exc_type, PyExc_KeyError)) { | ||
| code = StatusCode::KeyError; | ||
| } else if (PyErr_GivenExceptionMatches(exc_type, PyExc_TypeError)) { | ||
| code = StatusCode::TypeError; | ||
| } else if (PyErr_GivenExceptionMatches(exc_type, PyExc_ValueError) || | ||
| PyErr_GivenExceptionMatches(exc_type, PyExc_OverflowError)) { | ||
| code = StatusCode::Invalid; | ||
| } else if (PyErr_GivenExceptionMatches(exc_type, PyExc_EnvironmentError)) { | ||
| code = StatusCode::IOError; | ||
| } else if (PyErr_GivenExceptionMatches(exc_type, PyExc_NotImplementedError)) { | ||
| code = StatusCode::NotImplemented; | ||
| } | ||
| } | ||
| return Status(code, message); | ||
| } | ||
|
|
||
| Status PassPyError() { | ||
| if (PyErr_Occurred()) { | ||
| // Do not call PyErr_Clear, the assumption is that someone further | ||
| // up the call stack will want to deal with the Python error. | ||
| return Status(StatusCode::PythonError, ""); | ||
| } | ||
| return Status::OK(); | ||
| } | ||
|
|
||
| } // namespace py | ||
| } // namespace arrow | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.