| 186 | } |
| 187 | |
| 188 | Status TestCheckPyErrorStatus() { |
| 189 | Status st; |
| 190 | std::string expected_detail = ""; |
| 191 | |
| 192 | auto check_error = [](Status& st, const char* expected_message = "some error", |
| 193 | std::string expected_detail = "") { |
| 194 | st = CheckPyError(); |
| 195 | ASSERT_EQ(st.message(), expected_message); |
| 196 | ASSERT_FALSE(PyErr_Occurred()); |
| 197 | if (expected_detail.size() > 0) { |
| 198 | auto detail = st.detail(); |
| 199 | ASSERT_NE(detail, nullptr); |
| 200 | ASSERT_EQ(detail->ToString(), expected_detail); |
| 201 | } |
| 202 | return Status::OK(); |
| 203 | }; |
| 204 | |
| 205 | for (PyObject* exc_type : {PyExc_Exception, PyExc_SyntaxError}) { |
| 206 | PyErr_SetString(exc_type, "some error"); |
| 207 | ASSERT_OK(check_error(st)); |
| 208 | ASSERT_TRUE(st.IsUnknownError()); |
| 209 | } |
| 210 | |
| 211 | PyErr_SetString(PyExc_TypeError, "some error"); |
| 212 | ASSERT_OK( |
| 213 | check_error(st, "some error", FormatPythonException("TypeError", "some error"))); |
| 214 | ASSERT_TRUE(st.IsTypeError()); |
| 215 | |
| 216 | PyErr_SetString(PyExc_ValueError, "some error"); |
| 217 | ASSERT_OK(check_error(st)); |
| 218 | ASSERT_TRUE(st.IsInvalid()); |
| 219 | |
| 220 | PyErr_SetString(PyExc_KeyError, "some error"); |
| 221 | ASSERT_OK(check_error(st, "'some error'")); |
| 222 | ASSERT_TRUE(st.IsKeyError()); |
| 223 | |
| 224 | for (PyObject* exc_type : {PyExc_OSError, PyExc_IOError}) { |
| 225 | PyErr_SetString(exc_type, "some error"); |
| 226 | ASSERT_OK(check_error(st)); |
| 227 | ASSERT_TRUE(st.IsIOError()); |
| 228 | } |
| 229 | |
| 230 | PyErr_SetString(PyExc_NotImplementedError, "some error"); |
| 231 | ASSERT_OK(check_error(st, "some error", |
| 232 | FormatPythonException("NotImplementedError", "some error"))); |
| 233 | ASSERT_TRUE(st.IsNotImplemented()); |
| 234 | |
| 235 | // No override if a specific status code is given |
| 236 | PyErr_SetString(PyExc_TypeError, "some error"); |
| 237 | st = CheckPyError(StatusCode::SerializationError); |
| 238 | ASSERT_TRUE(st.IsSerializationError()); |
| 239 | ASSERT_EQ(st.message(), "some error"); |
| 240 | ASSERT_FALSE(PyErr_Occurred()); |
| 241 | |
| 242 | return Status::OK(); |
| 243 | } |
| 244 | |
| 245 | Status TestCheckPyErrorStatusNoGIL() { |
nothing calls this directly
no test coverage detected