3737#include " status_macros.h"
3838#include < pybind11/pybind11.h>
3939#include < pybind11/stl.h>
40- #include " pybind11_abseil/status_casters .h"
40+ #include " pybind11_abseil/absl_casters .h"
4141
4242namespace cel_python {
4343
@@ -56,7 +56,7 @@ static std::shared_ptr<PyCelEnvInternal> GetEnvFromContext(
5656
5757void PyCelFunction::DefinePythonBindings (pybind11::module & m) {
5858 py::class_<PyCelFunction, std::shared_ptr<PyCelFunction>>(m, " Function" )
59- .def (py::init<std::string, std::vector<PyCelType>, bool , PyObject* ,
59+ .def (py::init<std::string, std::vector<PyCelType>, bool , py::object ,
6060 PyCelType>(),
6161 py::arg (" function_name" ), py::arg (" parameters" ),
6262 py::arg (" is_member" ), py::arg (" impl" ),
@@ -65,36 +65,21 @@ void PyCelFunction::DefinePythonBindings(pybind11::module& m) {
6565
6666PyCelFunction::PyCelFunction (std::string function_name,
6767 std::vector<PyCelType> parameters, bool is_member,
68- PyObject* impl, PyCelType return_type)
68+ py::object impl, PyCelType return_type)
6969 : function_name_(std::move(function_name)),
7070 return_type_ (std::move(return_type)),
7171 parameters_(std::move(parameters)),
7272 is_member_(is_member),
73- impl_(impl) {
73+ impl_(std::move( impl) ) {
7474 ABSL_CHECK (PyGILState_Check ());
75- Py_XINCREF (impl_);
7675}
7776
78- PyCelFunction::~PyCelFunction () {
79- auto gil_state = PyGILState_Ensure ();
80- Py_XDECREF (impl_);
81- PyGILState_Release (gil_state);
82- };
83-
8477PyCelFunctionAdapter::PyCelFunctionAdapter (std::string function_name,
8578 PyCelType return_type,
86- PyObject* py_function)
79+ py::object py_function)
8780 : function_name_(std::move(function_name)),
8881 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- }
82+ py_function_(std::move(py_function)) {}
9883
9984absl::StatusOr<cel::Value> PyCelFunctionAdapter::Invoke (
10085 absl::Span<const cel::Value> args,
@@ -110,7 +95,7 @@ absl::StatusOr<cel::Value> PyCelFunctionAdapter::Invoke(
11095 CelValueToPyObject (args[i], env, py_arena,
11196 /* plain_value=*/ true ));
11297 }
113- PyObject* result = PyObject_CallObject (py_function_, py_args);
98+ PyObject* result = PyObject_CallObject (py_function_. ptr () , py_args);
11499 absl::Status status = PyErr_toStatus ();
115100 if (!status.ok ()) {
116101 return cel::ErrorValue (status);
@@ -119,8 +104,9 @@ absl::StatusOr<cel::Value> PyCelFunctionAdapter::Invoke(
119104 return PyObjectToCelValue (
120105 result, return_type_,
121106 [this ]() {
122- return absl::StrFormat (" Python function '%s'" ,
123- PyUnicode_AsUTF8 (PyObject_Repr (py_function_)));
107+ return absl::StrFormat (
108+ " Python function '%s'" ,
109+ PyUnicode_AsUTF8 (PyObject_Repr (py_function_.ptr ())));
124110 },
125111 env, context.arena ());
126112};
0 commit comments