Repository navigation
Expand file tree
/
Copy pathpybind11_status.h
More file actions
142 lines (117 loc) · 4.5 KB
/
Copy pathpybind11_status.h
File metadata and controls
142 lines (117 loc) · 4.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/* Copyright 2019 The TensorFlow Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/
#ifndef TENSORFLOW_PYTHON_LIB_CORE_PYBIND11_STATUS_H_
#define TENSORFLOW_PYTHON_LIB_CORE_PYBIND11_STATUS_H_
#include <Python.h>
#include "pybind11/pybind11.h"
#include "tensorflow/c/tf_status.h"
#include "tensorflow/core/lib/core/status.h"
#include "tensorflow/core/protobuf/error_codes.pb.h"
#include "tensorflow/python/lib/core/py_exception_registry.h"
namespace tensorflow {
namespace internal {
inline PyObject* CodeToPyExc(const int code) {
switch (code) {
case error::Code::INVALID_ARGUMENT:
return PyExc_ValueError;
case error::Code::OUT_OF_RANGE:
return PyExc_IndexError;
case error::Code::UNIMPLEMENTED:
return PyExc_NotImplementedError;
default:
return PyExc_RuntimeError;
}
}
inline PyObject* StatusToPyExc(const Status& status) {
return CodeToPyExc(status.code());
}
inline PyObject* TFStatusToPyExc(const TF_Status* status) {
return CodeToPyExc(TF_GetCode(status));
}
} // namespace internal
inline void MaybeRaiseFromStatus(const Status& status) {
if (!status.ok()) {
PyErr_SetString(internal::StatusToPyExc(status),
status.error_message().c_str());
throw pybind11::error_already_set();
}
}
inline void MaybeRaiseRegisteredFromStatus(const tensorflow::Status& status) {
if (!status.ok()) {
PyErr_SetObject(PyExceptionRegistry::Lookup(status.code()),
pybind11::make_tuple(pybind11::none(), pybind11::none(),
status.error_message())
.ptr());
throw pybind11::error_already_set();
}
}
inline void MaybeRaiseRegisteredFromStatusWithGIL(
const tensorflow::Status& status) {
if (!status.ok()) {
// Acquire GIL for throwing exception.
pybind11::gil_scoped_acquire acquire;
PyErr_SetObject(PyExceptionRegistry::Lookup(status.code()),
pybind11::make_tuple(pybind11::none(), pybind11::none(),
status.error_message())
.ptr());
throw pybind11::error_already_set();
}
}
inline void MaybeRaiseFromTFStatus(TF_Status* status) {
TF_Code code = TF_GetCode(status);
if (code != TF_OK) {
PyErr_SetString(internal::TFStatusToPyExc(status), TF_Message(status));
throw pybind11::error_already_set();
}
}
inline void MaybeRaiseRegisteredFromTFStatus(TF_Status* status) {
TF_Code code = TF_GetCode(status);
if (code != TF_OK) {
PyErr_SetObject(PyExceptionRegistry::Lookup(code),
pybind11::make_tuple(pybind11::none(), pybind11::none(),
TF_Message(status))
.ptr());
throw pybind11::error_already_set();
}
}
inline void MaybeRaiseRegisteredFromTFStatusWithGIL(TF_Status* status) {
TF_Code code = TF_GetCode(status);
if (code != TF_OK) {
// Acquire GIL for throwing exception.
pybind11::gil_scoped_acquire acquire;
PyErr_SetObject(PyExceptionRegistry::Lookup(code),
pybind11::make_tuple(pybind11::none(), pybind11::none(),
TF_Message(status))
.ptr());
throw pybind11::error_already_set();
}
}
} // namespace tensorflow
namespace pybind11 {
namespace detail {
// Raise an exception if a given status is not OK, otherwise return None.
//
// The correspondence between status codes and exception classes is given
// by PyExceptionRegistry. Note that the registry should be initialized
// in order to be used, see PyExceptionRegistry::Init.
template <>
struct type_caster<tensorflow::Status> {
public:
PYBIND11_TYPE_CASTER(tensorflow::Status, _("Status"));
static handle cast(tensorflow::Status status, return_value_policy, handle) {
tensorflow::MaybeRaiseFromStatus(status);
return none().inc_ref();
}
};
} // namespace detail
} // namespace pybind11
#endif // TENSORFLOW_PYTHON_LIB_CORE_PYBIND11_STATUS_H_