-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathpy_cel.cc
More file actions
134 lines (119 loc) · 4.86 KB
/
Copy pathpy_cel.cc
File metadata and controls
134 lines (119 loc) · 4.86 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
// 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 "py_cel.h"
#include <Python.h> // IWYU pragma: keep - Needed for PyObject
#include <memory>
#include <optional>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
#include "absl/log/absl_check.h"
#include "absl/status/statusor.h"
#include "py_cel_activation.h"
#include "py_cel_arena.h"
#include "py_cel_env.h"
#include "py_cel_expression.h"
#include "py_cel_type.h"
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include "pybind11_abseil/status_casters.h"
namespace cel_python {
namespace py = pybind11;
void PyCel::DefinePythonBindings(pybind11::module& m) {
py::class_<PyCel, std::shared_ptr<PyCel>> cel_class(m, "Cel");
cel_class
.def(py::init([](py::object descriptor_pool,
std::optional<std::unordered_map<std::string, PyCelType>>
variables,
std::optional<std::vector<py::object>> extensions,
const std::optional<std::string>& container) {
PyObject* pool_ptr = nullptr;
if (descriptor_pool.is_none()) {
// Replicates python's `descriptor_pool.Default()`
pool_ptr = py::module::import("google.protobuf.descriptor_pool")
.attr("Default")()
.ptr();
} else {
pool_ptr = descriptor_pool.ptr();
}
std::vector<PyObject*> ext_ptrs;
if (extensions) {
ext_ptrs.reserve(extensions->size());
for (const auto& ext : *extensions) {
ext_ptrs.push_back(ext.ptr());
}
}
return std::make_shared<PyCel>(
pool_ptr,
std::move(variables).value_or(
std::unordered_map<std::string, PyCelType>{}),
ext_ptrs, container.value_or(""));
}),
py::arg("descriptor_pool") = py::none(),
py::arg("variables") = py::none(),
py::arg("extensions") = py::none(),
py::arg("container") = py::none())
.def("compile", &PyCel::Compile, py::arg("expression"),
py::arg("disable_check") = false)
.def("deserialize", &PyCel::Deserialize, py::arg("serialized"))
.def(
"Activation",
[](PyCel& self,
std::optional<std::unordered_map<std::string, py::object>> data,
const std::optional<std::vector<std::shared_ptr<PyCelFunction>>>&
functions,
std::shared_ptr<PyCelArena> arena = nullptr) {
if (!arena) {
arena = cel_python::NewArena();
}
std::unordered_map<std::string, PyObject*> data_ptrs;
if (data) {
for (auto const& [key, val] : *data) {
data_ptrs[key] = val.ptr();
}
}
return self.NewActivation(
data_ptrs,
functions.value_or(
std::vector<std::shared_ptr<PyCelFunction>>{}),
arena);
},
py::arg("data") = py::none(), py::arg("functions") = py::none(),
py::arg("arena") = nullptr);
}
PyCel::PyCel(PyObject* descriptor_pool,
std::unordered_map<std::string, PyCelType> variable_types,
const std::vector<PyObject*>& extensions, std::string container)
: env_(std::make_unique<PyCelEnv>(descriptor_pool,
std::move(variable_types), extensions,
std::move(container))) {
ABSL_CHECK(PyGILState_Check());
}
PyCel::~PyCel() = default;
std::shared_ptr<PyCelActivation> PyCel::NewActivation(
const std::unordered_map<std::string, PyObject*>& data,
const std::vector<std::shared_ptr<PyCelFunction>>& functions,
const std::shared_ptr<PyCelArena>& arena) {
return std::make_shared<PyCelActivation>(env_, data, functions, arena);
}
absl::StatusOr<std::unique_ptr<PyCelExpression>> PyCel::Compile(
const std::string& cel_expr, bool disable_check) {
return PyCelExpression::Compile(env_, cel_expr, disable_check);
}
absl::StatusOr<std::unique_ptr<PyCelExpression>> PyCel::Deserialize(
const std::string& serialized_expr) {
return PyCelExpression::Deserialize(env_, serialized_expr);
}
} // namespace cel_python