Skip to content

Commit 3390182

Browse files
dmitriplotnikovcopybara-github
authored andcommitted
Remove dependency on pybind11_abseil:status_casters
PiperOrigin-RevId: 860604887
1 parent ef38183 commit 3390182

17 files changed

Lines changed: 117 additions & 110 deletions

BUILD

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,6 @@ pybind_extension(
9090
"@com_google_cel_spec//proto/cel/expr:syntax_cc_proto",
9191
"@com_google_protobuf//:protobuf",
9292
"@pybind11_abseil//pybind11_abseil:absl_casters",
93-
"@pybind11_abseil//pybind11_abseil:import_status_module",
94-
"@pybind11_abseil//pybind11_abseil:status_casters",
9593
],
9694
)
9795

py_cel_env.cc

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,14 @@
2424
#include <vector>
2525

2626
#include "absl/log/absl_check.h"
27-
#include "absl/status/statusor.h"
2827
#include "py_cel_activation.h"
2928
#include "py_cel_arena.h"
3029
#include "py_cel_env_internal.h"
3130
#include "py_cel_expression.h"
3231
#include "py_cel_type.h"
32+
#include "py_error_status.h"
3333
#include <pybind11/pybind11.h>
3434
#include <pybind11/stl.h>
35-
#include "pybind11_abseil/status_casters.h"
3635

3736
namespace cel_python {
3837

@@ -121,14 +120,13 @@ std::shared_ptr<PyCelActivation> PyCelEnv::NewActivation(
121120
return std::make_shared<PyCelActivation>(env_, data, functions, arena);
122121
}
123122

124-
absl::StatusOr<PyCelExpression> PyCelEnv::Compile(const std::string& cel_expr,
125-
bool disable_check) {
126-
return PyCelExpression::Compile(env_, cel_expr, disable_check);
123+
PyCelExpression PyCelEnv::Compile(const std::string& cel_expr,
124+
bool disable_check) {
125+
return ThrowIfError(PyCelExpression::Compile(env_, cel_expr, disable_check));
127126
}
128127

129-
absl::StatusOr<PyCelExpression> PyCelEnv::Deserialize(
130-
const std::string& serialized_expr) {
131-
return PyCelExpression::Deserialize(env_, serialized_expr);
128+
PyCelExpression PyCelEnv::Deserialize(const std::string& serialized_expr) {
129+
return ThrowIfError(PyCelExpression::Deserialize(env_, serialized_expr));
132130
}
133131

134132
} // namespace cel_python

py_cel_env.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
#include <unordered_map>
2424
#include <vector>
2525

26-
#include "absl/status/statusor.h"
2726
#include "py_cel_activation.h"
2827
#include "py_cel_arena.h"
2928
#include "py_cel_expression.h"
@@ -52,10 +51,11 @@ class PyCelEnv {
5251

5352
~PyCelEnv();
5453

55-
absl::StatusOr<PyCelExpression> Compile(const std::string& cel_expr,
56-
bool disable_check = false);
57-
absl::StatusOr<PyCelExpression> Deserialize(
58-
const std::string& serialized_expr);
54+
// May throw exceptions.
55+
PyCelExpression Compile(const std::string& cel_expr,
56+
bool disable_check = false);
57+
// May throw exceptions.
58+
PyCelExpression Deserialize(const std::string& serialized_expr);
5959
std::shared_ptr<PyCelActivation> NewActivation(
6060
const std::unordered_map<std::string, PyObject*>& data,
6161
const std::vector<std::shared_ptr<PyCelFunction>>& functions,

py_cel_expression.cc

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@
5353
#include "status_macros.h"
5454
#include <pybind11/pybind11.h>
5555
#include <pybind11/stl.h>
56-
#include "pybind11_abseil/status_casters.h"
5756

5857
namespace cel_python {
5958

@@ -79,26 +78,25 @@ void PyCelExpression::DefinePythonBindings(py::module& m) {
7978
data,
8079
const std::optional<std::vector<std::shared_ptr<PyCelFunction>>>&
8180
functions,
82-
const std::shared_ptr<PyCelArena>& arena =
83-
nullptr) -> absl::StatusOr<PyCelValue> {
81+
const std::shared_ptr<PyCelArena>& arena = nullptr) -> PyCelValue {
8482
if (activation) {
8583
if (data || functions || arena) {
86-
return absl::InvalidArgumentError(
87-
"Cannot provide both activation and any other arguments.");
84+
throw StatusToException(absl::InvalidArgumentError(
85+
"Cannot provide both activation and any other arguments."));
8886
}
89-
return self.Eval(**activation);
87+
return ThrowIfError(self.Eval(**activation));
9088
}
9189
std::unordered_map<std::string, PyObject*> data_ptrs;
9290
if (data) {
9391
for (auto const& [key, val] : *data) {
9492
data_ptrs[key] = val.ptr();
9593
}
9694
}
97-
return self.Eval(PyCelActivation(
95+
return ThrowIfError(self.Eval(PyCelActivation(
9896
self.env_, data_ptrs,
9997
functions.value_or(
10098
std::vector<std::shared_ptr<PyCelFunction>>{}),
101-
arena ? arena : NewArena()));
99+
arena ? arena : NewArena())));
102100
},
103101
py::arg("activation") = py::none(), py::arg("data") = py::none(),
104102
py::arg("functions") = py::none(), py::arg("arena") = nullptr);

py_cel_function.cc

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
#include "status_macros.h"
3838
#include <pybind11/pybind11.h>
3939
#include <pybind11/stl.h>
40-
#include "pybind11_abseil/status_casters.h"
4140

4241
namespace cel_python {
4342

@@ -56,7 +55,7 @@ static std::shared_ptr<PyCelEnvInternal> GetEnvFromContext(
5655

5756
void PyCelFunction::DefinePythonBindings(pybind11::module& m) {
5857
py::class_<PyCelFunction, std::shared_ptr<PyCelFunction>>(m, "Function")
59-
.def(py::init<std::string, std::vector<PyCelType>, bool, PyObject*,
58+
.def(py::init<std::string, std::vector<PyCelType>, bool, py::object,
6059
PyCelType>(),
6160
py::arg("function_name"), py::arg("parameters"),
6261
py::arg("is_member"), py::arg("impl"),
@@ -65,36 +64,21 @@ void PyCelFunction::DefinePythonBindings(pybind11::module& m) {
6564

6665
PyCelFunction::PyCelFunction(std::string function_name,
6766
std::vector<PyCelType> parameters, bool is_member,
68-
PyObject* impl, PyCelType return_type)
67+
py::object impl, PyCelType return_type)
6968
: function_name_(std::move(function_name)),
7069
return_type_(std::move(return_type)),
7170
parameters_(std::move(parameters)),
7271
is_member_(is_member),
73-
impl_(impl) {
72+
impl_(std::move(impl)) {
7473
ABSL_CHECK(PyGILState_Check());
75-
Py_XINCREF(impl_);
7674
}
7775

78-
PyCelFunction::~PyCelFunction() {
79-
auto gil_state = PyGILState_Ensure();
80-
Py_XDECREF(impl_);
81-
PyGILState_Release(gil_state);
82-
};
83-
8476
PyCelFunctionAdapter::PyCelFunctionAdapter(std::string function_name,
8577
PyCelType return_type,
86-
PyObject* py_function)
78+
py::object py_function)
8779
: function_name_(std::move(function_name)),
8880
return_type_(std::move(return_type)),
89-
py_function_(py_function) {
90-
Py_XINCREF(py_function_);
91-
}
92-
93-
PyCelFunctionAdapter::~PyCelFunctionAdapter() {
94-
auto gil_state = PyGILState_Ensure();
95-
Py_XDECREF(py_function_);
96-
PyGILState_Release(gil_state);
97-
}
81+
py_function_(std::move(py_function)) {}
9882

9983
absl::StatusOr<cel::Value> PyCelFunctionAdapter::Invoke(
10084
absl::Span<const cel::Value> args,
@@ -110,7 +94,7 @@ absl::StatusOr<cel::Value> PyCelFunctionAdapter::Invoke(
11094
CelValueToPyObject(args[i], env, py_arena,
11195
/*plain_value=*/true));
11296
}
113-
PyObject* result = PyObject_CallObject(py_function_, py_args);
97+
PyObject* result = PyObject_CallObject(py_function_.ptr(), py_args);
11498
absl::Status status = PyErr_toStatus();
11599
if (!status.ok()) {
116100
return cel::ErrorValue(status);
@@ -119,8 +103,9 @@ absl::StatusOr<cel::Value> PyCelFunctionAdapter::Invoke(
119103
return PyObjectToCelValue(
120104
result, return_type_,
121105
[this]() {
122-
return absl::StrFormat("Python function '%s'",
123-
PyUnicode_AsUTF8(PyObject_Repr(py_function_)));
106+
return absl::StrFormat(
107+
"Python function '%s'",
108+
PyUnicode_AsUTF8(PyObject_Repr(py_function_.ptr())));
124109
},
125110
env, context.arena());
126111
};

py_cel_function.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929

3030
namespace cel_python {
3131

32+
namespace py = ::pybind11;
33+
3234
class PyCelEnvInternal;
3335

3436
// Wrapper for a Python function that implements a CEL late-bound function.
@@ -38,30 +40,28 @@ class PyCelFunction {
3840
static void DefinePythonBindings(pybind11::module& m);
3941

4042
PyCelFunction(std::string function_name, std::vector<PyCelType> parameters,
41-
bool is_member, PyObject* impl, PyCelType return_type);
42-
~PyCelFunction();
43+
bool is_member, py::object impl, PyCelType return_type);
4344

4445
std::string function_name() const { return function_name_; }
4546
const std::vector<PyCelType>& parameters() const { return parameters_; }
4647
bool is_member() const { return is_member_; }
47-
PyObject* impl() const { return impl_; }
48+
py::object impl() const { return impl_; }
4849
const PyCelType& return_type() const { return return_type_; }
4950

5051
private:
5152
std::string function_name_;
5253
PyCelType return_type_;
5354
std::vector<PyCelType> parameters_;
5455
bool is_member_;
55-
PyObject* impl_;
56+
py::object impl_;
5657
};
5758

5859
// Internal wrapper for a Python function that implements a CEL extension
5960
// function.
6061
class PyCelFunctionAdapter : public cel::Function {
6162
public:
6263
PyCelFunctionAdapter(std::string function_name, PyCelType return_type,
63-
PyObject* py_function);
64-
~PyCelFunctionAdapter() override;
64+
py::object py_function);
6565

6666
absl::StatusOr<cel::Value> Invoke(
6767
absl::Span<const cel::Value> args,
@@ -70,7 +70,7 @@ class PyCelFunctionAdapter : public cel::Function {
7070
private:
7171
std::string function_name_;
7272
PyCelType return_type_;
73-
PyObject* py_function_;
73+
py::object py_function_;
7474
};
7575

7676
} // namespace cel_python

py_cel_function_decl.cc

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121
#include "py_cel_overload.h"
2222
#include <pybind11/pybind11.h>
2323
#include <pybind11/stl.h>
24-
#include "pybind11_abseil/absl_casters.h"
25-
#include "pybind11_abseil/status_casters.h"
2624

2725
namespace cel_python {
2826

py_cel_module.cc

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,10 @@
2323
#include "py_cel_type.h"
2424
#include "py_cel_value.h"
2525
#include <pybind11/pybind11.h>
26-
#include "pybind11_abseil/import_status_module.h"
2726

2827
namespace cel_python {
2928

3029
PYBIND11_MODULE(py_cel, m) {
31-
pybind11::google::ImportStatusModule();
3230
m.doc() = "Python bindings for CEL.";
3331

3432
PyCelArena::DefinePythonBindings(m);

py_cel_overload.cc

Lines changed: 5 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
#include "py_cel_type.h"
2323
#include <pybind11/pybind11.h>
2424
#include <pybind11/stl.h>
25-
#include "pybind11_abseil/status_casters.h"
2625

2726
namespace cel_python {
2827

@@ -33,50 +32,23 @@ void PyCelOverload::DefinePythonBindings(py::module_& m) {
3332
.def(py::init([](const std::string& overload_id,
3433
const PyCelType& return_type,
3534
const std::vector<PyCelType>& parameters, bool is_member,
36-
PyObject* impl) {
35+
py::object impl) {
3736
return PyCelOverload(overload_id, return_type, parameters,
38-
is_member, impl != Py_None ? impl : nullptr);
37+
is_member, std::move(impl));
3938
}),
4039
py::arg("overload_id"), py::arg("return_type"),
4140
py::arg("parameters"), py::arg("is_member") = false,
42-
py::arg("impl") = nullptr);
41+
py::arg("impl") = py::none());
4342
}
4443

4544
PyCelOverload::PyCelOverload(std::string overload_id,
4645
const PyCelType& return_type,
4746
std::vector<PyCelType> parameters, bool is_member,
48-
PyObject* py_function)
47+
py::object py_function)
4948
: overload_id_(std::move(overload_id)),
5049
return_type_(return_type),
5150
parameters_(std::move(parameters)),
5251
is_member_(is_member),
53-
py_function_(py_function) {
54-
Py_XINCREF(py_function_);
55-
}
56-
57-
PyCelOverload::PyCelOverload(const PyCelOverload& other) {
58-
overload_id_ = other.overload_id_;
59-
return_type_ = other.return_type_;
60-
parameters_ = other.parameters_;
61-
is_member_ = other.is_member_;
62-
py_function_ = other.py_function_;
63-
Py_XINCREF(py_function_);
64-
};
65-
66-
PyCelOverload& PyCelOverload::operator=(const PyCelOverload& other) {
67-
overload_id_ = other.overload_id_;
68-
return_type_ = other.return_type_;
69-
parameters_ = other.parameters_;
70-
is_member_ = other.is_member_;
71-
py_function_ = other.py_function_;
72-
Py_XINCREF(py_function_);
73-
return *this;
74-
}
75-
76-
PyCelOverload::~PyCelOverload() {
77-
auto gil_state = PyGILState_Ensure();
78-
Py_XDECREF(py_function_);
79-
PyGILState_Release(gil_state);
80-
}
52+
py_function_(std::move(py_function)) {}
8153

8254
} // namespace cel_python

py_cel_overload.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,30 +25,31 @@
2525

2626
namespace cel_python {
2727

28+
namespace py = ::pybind11;
29+
2830
class PyCelOverload {
2931
public:
3032
static void DefinePythonBindings(pybind11::module& m);
3133

32-
PyCelOverload(const PyCelOverload&);
33-
PyCelOverload& operator=(const PyCelOverload&);
34+
PyCelOverload(const PyCelOverload&) = default;
35+
PyCelOverload& operator=(const PyCelOverload&) = default;
3436

3537
PyCelOverload(std::string overload_id, const PyCelType& return_type,
3638
std::vector<PyCelType> parameters, bool is_member = false,
37-
PyObject* py_function = nullptr);
38-
~PyCelOverload();
39+
py::object py_function = py::none());
3940

4041
std::string overload_id() const { return overload_id_; }
4142
PyCelType return_type() const { return return_type_; }
4243
const std::vector<PyCelType>& parameters() const { return parameters_; }
4344
bool is_member() const { return is_member_; }
44-
PyObject* py_function() const { return py_function_; }
45+
py::object py_function() const { return py_function_; }
4546

4647
private:
4748
std::string overload_id_;
4849
PyCelType return_type_;
4950
std::vector<PyCelType> parameters_;
5051
bool is_member_;
51-
PyObject* py_function_ = nullptr;
52+
py::object py_function_ = py::none();
5253
};
5354

5455
} // namespace cel_python

0 commit comments

Comments
 (0)