GitHub Viewer
// Copyright 2025 Google LLC
//
// 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
//
// https://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.
#include "cel_expr_python/py_cel_function.h"
#include
#include
#include
#include
#include
#include "absl/log/absl_check.h"
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/strings/str_format.h"
#include "absl/types/span.h"
#include "common/value.h"
#include "runtime/embedder_context.h"
#include "runtime/function.h"
#include "cel_expr_python/py_cel_arena.h"
#include "cel_expr_python/py_cel_env_internal.h"
#include "cel_expr_python/py_cel_type.h"
#include "cel_expr_python/py_cel_value.h"
#include "cel_expr_python/py_error_status.h"
#include "cel_expr_python/status_macros.h"
#include
#include
namespace cel_python {
namespace py = ::pybind11;
namespace {
static std::shared_ptr GetEnvFromContext(
const cel::Function::InvokeContext& context) {
ABSL_CHECK(context.embedder_context()); // Crash OK: all call sites are local
// to the library.
return *context.embedder_context()->Get();
}
} // namespace
void PyCelFunction::DefinePythonBindings(pybind11::module& m) {
py::class_(m, "Function")
.def(py::init(),
py::arg("function_name"), py::arg("parameters"),
py::arg("is_member"), py::arg("impl"),
py::arg("return_type") = PyCelType::Dyn());
}
PyCelFunction::PyCelFunction(std::string function_name,
std::vector parameters, bool is_member,
py::object impl, PyCelType return_type)
: function_name_(std::move(function_name)),
return_type_(std::move(return_type)),
parameters_(std::move(parameters)),
is_member_(is_member),
impl_(std::move(impl)) {
ABSL_CHECK(PyGILState_Check());
}
PyCelFunctionAdapter::PyCelFunctionAdapter(std::string function_name,
PyCelType return_type,
py::object py_function)
: function_name_(std::move(function_name)),
return_type_(std::move(return_type)),
py_function_(std::move(py_function)) {}
absl::StatusOr PyCelFunctionAdapter::Invoke(
absl::Span args,
const cel::Function::InvokeContext& context) const {
ABSL_CHECK(PyGILState_Check());
std::shared_ptr env = GetEnvFromContext(context);
CEL_PYTHON_ASSIGN_OR_RETURN(auto py_arena,
PyCelArena::FromProtoArena(context.arena()));
PyObject* py_args = PyTuple_New(args.size());
for (int i = 0; i < args.size(); ++i) {
PyTuple_SetItem(py_args, i,
CelValueToPyObject(args[i], env, py_arena,
/*plain_value=*/true));
}
PyObject* result = PyObject_CallObject(py_function_.ptr(), py_args);
Py_DECREF(py_args);
absl::Status status = PyErr_toStatus();
if (!status.ok()) {
Py_XDECREF(result);
return cel::ErrorValue(status);
}
absl::StatusOr cel_result = PyObjectToCelValue(
result, return_type_,
[this]() {
return absl::StrFormat(
"Python function '%s'",
PyUnicode_AsUTF8(PyObject_Repr(py_function_.ptr())));
},
env, context.arena());
Py_XDECREF(result);
return cel_result;
}
} // namespace cel_python