1212// See the License for the specific language governing permissions and
1313// limitations under the License.
1414
15- #include " py_cel .h"
15+ #include " py_cel_env .h"
1616
1717#include < Python.h> // IWYU pragma: keep - Needed for PyObject
1818
@@ -38,48 +38,47 @@ namespace cel_python {
3838
3939namespace py = ::pybind11;
4040
41- void PyCel ::DefinePythonBindings (pybind11::module & m) {
42- py::class_<PyCel , std::shared_ptr<PyCel >> cel_class (m, " Cel " );
43- cel_class
44- . def ( py::init ([](py::object descriptor_pool ,
45- std::optional<std::unordered_map<std::string, PyCelType>>
46- variables,
47- std::optional<std::vector<py::object>> extensions,
48- const std::optional<std::string>& container) {
49- PyObject* pool_ptr = nullptr ;
50- if (descriptor_pool.is_none ()) {
51- // Replicates python's `descriptor_pool.Default()`
52- pool_ptr = py::module::import (" google.protobuf.descriptor_pool" )
53- .attr (" Default" )()
54- .ptr ();
55- } else {
56- pool_ptr = descriptor_pool.ptr ();
57- }
41+ void PyCelEnv ::DefinePythonBindings (pybind11::module & m) {
42+ py::class_<PyCelEnv , std::shared_ptr<PyCelEnv >> cel_class (m, " Env " );
43+ m. def (
44+ " NewEnv " ,
45+ [](py::object descriptor_pool,
46+ std::optional<std::unordered_map<std::string, PyCelType>> variables,
47+ std::optional<std::vector<py::object>> extensions,
48+ const std::optional<std::string>& container) {
49+ PyObject* pool_ptr = nullptr ;
50+ if (descriptor_pool.is_none ()) {
51+ // Replicates python's `descriptor_pool.Default()`
52+ pool_ptr = py::module::import (" google.protobuf.descriptor_pool" )
53+ .attr (" Default" )()
54+ .ptr ();
55+ } else {
56+ pool_ptr = descriptor_pool.ptr ();
57+ }
5858
59- std::vector<PyObject*> ext_ptrs;
60- if (extensions) {
61- ext_ptrs.reserve (extensions->size ());
62- for (const auto & ext : *extensions) {
63- ext_ptrs.push_back (ext.ptr ());
64- }
65- }
59+ std::vector<PyObject*> ext_ptrs;
60+ if (extensions) {
61+ ext_ptrs.reserve (extensions->size ());
62+ for (const auto & ext : *extensions) {
63+ ext_ptrs.push_back (ext.ptr ());
64+ }
65+ }
6666
67- return std::make_shared<PyCel>(
68- pool_ptr,
69- std::move (variables).value_or (
70- std::unordered_map<std::string, PyCelType>{}),
71- ext_ptrs, container.value_or (" " ));
72- }),
73- py::arg (" descriptor_pool" ) = py::none (),
74- py::arg (" variables" ) = py::none (),
75- py::arg (" extensions" ) = py::none (),
76- py::arg (" container" ) = py::none ())
77- .def (" compile" , &PyCel::Compile, py::arg (" expression" ),
67+ return PyCelEnv (pool_ptr,
68+ std::move (variables).value_or (
69+ std::unordered_map<std::string, PyCelType>{}),
70+ ext_ptrs, container.value_or (" " ));
71+ },
72+ py::arg (" descriptor_pool" ) = py::none (),
73+ py::arg (" variables" ) = py::none (), py::arg (" extensions" ) = py::none (),
74+ py::arg (" container" ) = py::none ());
75+ cel_class
76+ .def (" compile" , &PyCelEnv::Compile, py::arg (" expression" ),
7877 py::arg (" disable_check" ) = false )
79- .def (" deserialize" , &PyCel ::Deserialize, py::arg (" serialized" ))
78+ .def (" deserialize" , &PyCelEnv ::Deserialize, py::arg (" serialized" ))
8079 .def (
8180 " Activation" ,
82- [](PyCel & self,
81+ [](PyCelEnv & self,
8382 std::optional<std::unordered_map<std::string, py::object>> data,
8483 const std::optional<std::vector<std::shared_ptr<PyCelFunction>>>&
8584 functions,
@@ -103,30 +102,31 @@ void PyCel::DefinePythonBindings(pybind11::module& m) {
103102 py::arg (" arena" ) = nullptr );
104103}
105104
106- PyCel::PyCel (PyObject* descriptor_pool,
107- std::unordered_map<std::string, PyCelType> variable_types,
108- const std::vector<PyObject*>& extensions, std::string container)
105+ PyCelEnv::PyCelEnv (PyObject* descriptor_pool,
106+ std::unordered_map<std::string, PyCelType> variable_types,
107+ const std::vector<PyObject*>& extensions,
108+ std::string container)
109109 : env_(std::make_unique<PyCelEnvInternal>(
110110 descriptor_pool, std::move(variable_types), extensions,
111111 std::move (container))) {
112112 ABSL_CHECK (PyGILState_Check ());
113113}
114114
115- PyCel ::~PyCel () = default ;
115+ PyCelEnv ::~PyCelEnv () = default ;
116116
117- std::shared_ptr<PyCelActivation> PyCel ::NewActivation (
117+ std::shared_ptr<PyCelActivation> PyCelEnv ::NewActivation (
118118 const std::unordered_map<std::string, PyObject*>& data,
119119 const std::vector<std::shared_ptr<PyCelFunction>>& functions,
120120 const std::shared_ptr<PyCelArena>& arena) {
121121 return std::make_shared<PyCelActivation>(env_, data, functions, arena);
122122}
123123
124- absl::StatusOr<PyCelExpression> PyCel ::Compile (const std::string& cel_expr,
125- bool disable_check) {
124+ absl::StatusOr<PyCelExpression> PyCelEnv ::Compile (const std::string& cel_expr,
125+ bool disable_check) {
126126 return PyCelExpression::Compile (env_, cel_expr, disable_check);
127127}
128128
129- absl::StatusOr<PyCelExpression> PyCel ::Deserialize (
129+ absl::StatusOr<PyCelExpression> PyCelEnv ::Deserialize (
130130 const std::string& serialized_expr) {
131131 return PyCelExpression::Deserialize (env_, serialized_expr);
132132}
0 commit comments