Skip to content

Commit bc409f6

Browse files
dmitriplotnikovcopybara-github
authored andcommitted
Rename "py_cel.Cel" class to "py_cel.Env" for consistency
Documentation of CEL ubiquitously refers to this object as "enviroment". The API should reflect this convention. PiperOrigin-RevId: 853942563
1 parent a95b351 commit bc409f6

8 files changed

Lines changed: 89 additions & 90 deletions

File tree

BUILD

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ exports_files(["LICENSE"])
1212
pybind_extension(
1313
name = "py_cel",
1414
srcs = [
15-
"py_cel.cc",
16-
"py_cel.h",
1715
"py_cel_activation.cc",
1816
"py_cel_activation.h",
1917
"py_cel_arena.cc",
2018
"py_cel_arena.h",
19+
"py_cel_env.cc",
20+
"py_cel_env.h",
2121
"py_cel_env_internal.cc",
2222
"py_cel_env_internal.h",
2323
"py_cel_expression.cc",

conformance/conformance_test.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,14 +158,14 @@ def _run_conformance_test(self, simple_test: simple_pb.SimpleTest):
158158
break
159159

160160
self.descriptor_pool = descriptor_pool.Default()
161-
self.cel = cel.Cel(
161+
self.env = cel.NewEnv(
162162
self.descriptor_pool,
163163
variables=decls,
164164
extensions=extensions,
165165
container=simple_test.container,
166166
)
167167
try:
168-
compiled_expr = self.cel.compile(
168+
compiled_expr = self.env.compile(
169169
simple_test.expr, disable_check=simple_test.disable_check
170170
)
171171
except Exception as e: # pylint: disable=broad-except
@@ -188,7 +188,7 @@ def _run_conformance_test(self, simple_test: simple_pb.SimpleTest):
188188
for key, value in simple_test.bindings.items():
189189
values[key] = self._convert_value(value.value)
190190

191-
act = self.cel.Activation(values)
191+
act = self.env.Activation(values)
192192
try:
193193
res = compiled_expr.eval(act)
194194
except Exception as e: # pylint: disable=broad-except

custom_ext/custom_ext_test.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,16 @@ def _compile_expr(
4040
) -> cel.Expression:
4141
"""Creates a CEL expression for the given extension and compiles the expression."""
4242
self.descriptor_pool = descriptor_pool.Default()
43-
self.cel = cel.Cel(
43+
self.env = cel.NewEnv(
4444
self.descriptor_pool,
4545
variables={},
4646
extensions=[ext()],
4747
)
48-
return self.cel.compile(expression)
48+
return self.env.compile(expression)
4949

5050
def _create_activation(self, impl) -> cel.Activation:
5151
"""Creates a CEL Activation with a late-bound translate function."""
52-
return self.cel.Activation(
52+
return self.env.Activation(
5353
{},
5454
functions=[
5555
cel.Function(
@@ -66,7 +66,7 @@ def test_basic_function(self, ext):
6666
compiled_expr = self._compile_expr(
6767
ext, "'Hello, world!'.translate('en', 'es')"
6868
)
69-
act = self.cel.Activation({})
69+
act = self.env.Activation({})
7070
res = compiled_expr.eval(act)
7171

7272
self.assertEqual(res.value(), "¡Hola Mundo!")
@@ -85,7 +85,7 @@ def test_late_bound_function(self, ext):
8585
@parameterized.named_parameters(EXT_IMPLEMENTATIONS)
8686
def test_error_no_matching_overload(self, ext):
8787
compiled_expr = self._compile_expr(ext, "translate_late('Hello, world!')")
88-
act = self.cel.Activation(
88+
act = self.env.Activation(
8989
{},
9090
functions=[
9191
cel.Function(

py_cel.cc renamed to py_cel_env.cc

Lines changed: 46 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
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

3939
namespace 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
}

py_cel.h renamed to py_cel_env.h

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
#ifndef THIRD_PARTY_CEL_PYTHON_PY_CEL_H_
17-
#define THIRD_PARTY_CEL_PYTHON_PY_CEL_H_
16+
#ifndef THIRD_PARTY_CEL_PYTHON_PY_CEL_ENV_H_
17+
#define THIRD_PARTY_CEL_PYTHON_PY_CEL_ENV_H_
1818

1919
#include <Python.h> // IWYU pragma: keep - Needed for PyObject
2020

@@ -41,21 +41,16 @@ class RuntimeOptions;
4141
// All classes and functions in this namespace are pybind11-wrapped.
4242
namespace cel_python {
4343

44-
class PyCel;
45-
class PyCelEnv;
44+
class PyCelEnvInternal;
4645
class PyCelFunction;
4746
class PyMessageFactory;
4847

4948
// CEL environment. Provides access to the CEL compiler.
50-
class PyCel {
49+
class PyCelEnv {
5150
public:
5251
static void DefinePythonBindings(pybind11::module& m);
5352

54-
explicit PyCel(PyObject* descriptor_pool = nullptr,
55-
std::unordered_map<std::string, PyCelType> variable_types = {},
56-
const std::vector<PyObject*>& extensions = {},
57-
std::string container = "");
58-
~PyCel();
53+
~PyCelEnv();
5954

6055
absl::StatusOr<PyCelExpression> Compile(const std::string& cel_expr,
6156
bool disable_check = false);
@@ -69,9 +64,13 @@ class PyCel {
6964
std::shared_ptr<PyCelEnvInternal> GetEnv() { return env_; }
7065

7166
private:
67+
// Private constructor. Use `py_cel.NewEnv()` in python to obtain an instance.
68+
PyCelEnv(PyObject* descriptor_pool,
69+
std::unordered_map<std::string, PyCelType> variable_types,
70+
const std::vector<PyObject*>& extensions, std::string container);
7271
std::shared_ptr<PyCelEnvInternal> env_;
7372
};
7473

7574
} // namespace cel_python
7675

77-
#endif // THIRD_PARTY_CEL_PYTHON_PY_CEL_H_
76+
#endif // THIRD_PARTY_CEL_PYTHON_PY_CEL_ENV_H_

py_cel_module.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
#include "py_cel.h"
1615
#include "py_cel_activation.h"
1716
#include "py_cel_arena.h"
17+
#include "py_cel_env.h"
1818
#include "py_cel_expression.h"
1919
#include "py_cel_function.h"
2020
#include "py_cel_function_decl.h"
@@ -40,7 +40,7 @@ PYBIND11_MODULE(py_cel, m) {
4040
PyCelFunctionDecl::DefinePythonBindings(m);
4141
PyCelPythonExtension::DefinePythonBindings(m);
4242
PyCelFunction::DefinePythonBindings(m);
43-
PyCel::DefinePythonBindings(m);
43+
PyCelEnv::DefinePythonBindings(m);
4444
}
4545

4646
} // namespace cel_python

0 commit comments

Comments
 (0)