From a95b351cab3baffaa0b5ca84f12a732fb94b9040 Mon Sep 17 00:00:00 2001 From: Dmitri Plotnikov Date: Fri, 9 Jan 2026 15:22:54 -0800 Subject: [PATCH] Rename "PyCelEnv" class to "PyCelEnvInternal" This is done in preparation for renaming PyCel to PyCelEnv PiperOrigin-RevId: 854362711 --- BUILD | 4 ++-- py_cel.cc | 8 +++---- py_cel.h | 4 ++-- py_cel_activation.cc | 9 ++++--- py_cel_activation.h | 8 +++---- py_cel_env.cc => py_cel_env_internal.cc | 25 ++++++++++--------- py_cel_env.h => py_cel_env_internal.h | 32 ++++++++++++++----------- py_cel_expression.cc | 16 +++++++------ py_cel_expression.h | 13 +++++----- py_cel_function.cc | 9 +++---- py_cel_function.h | 2 +- py_cel_python_extension.cc | 2 +- py_cel_value.cc | 10 ++++---- py_cel_value.h | 14 +++++------ py_cel_value_provider.h | 6 ++--- 15 files changed, 86 insertions(+), 76 deletions(-) rename py_cel_env.cc => py_cel_env_internal.cc (89%) rename py_cel_env.h => py_cel_env_internal.h (80%) diff --git a/BUILD b/BUILD index 36ff332..d9e3c8b 100644 --- a/BUILD +++ b/BUILD @@ -18,8 +18,8 @@ pybind_extension( "py_cel_activation.h", "py_cel_arena.cc", "py_cel_arena.h", - "py_cel_env.cc", - "py_cel_env.h", + "py_cel_env_internal.cc", + "py_cel_env_internal.h", "py_cel_expression.cc", "py_cel_expression.h", "py_cel_function.cc", diff --git a/py_cel.cc b/py_cel.cc index 792af71..83f8807 100644 --- a/py_cel.cc +++ b/py_cel.cc @@ -27,7 +27,7 @@ #include "absl/status/statusor.h" #include "py_cel_activation.h" #include "py_cel_arena.h" -#include "py_cel_env.h" +#include "py_cel_env_internal.h" #include "py_cel_expression.h" #include "py_cel_type.h" #include @@ -106,9 +106,9 @@ void PyCel::DefinePythonBindings(pybind11::module& m) { PyCel::PyCel(PyObject* descriptor_pool, std::unordered_map variable_types, const std::vector& extensions, std::string container) - : env_(std::make_unique(descriptor_pool, - std::move(variable_types), extensions, - std::move(container))) { + : env_(std::make_unique( + descriptor_pool, std::move(variable_types), extensions, + std::move(container))) { ABSL_CHECK(PyGILState_Check()); } diff --git a/py_cel.h b/py_cel.h index 04a55a8..74d2a35 100644 --- a/py_cel.h +++ b/py_cel.h @@ -66,10 +66,10 @@ class PyCel { const std::vector>& functions, const std::shared_ptr& arena); - std::shared_ptr GetEnv() { return env_; } + std::shared_ptr GetEnv() { return env_; } private: - std::shared_ptr env_; + std::shared_ptr env_; }; } // namespace cel_python diff --git a/py_cel_activation.cc b/py_cel_activation.cc index f6e32ea..0818c15 100644 --- a/py_cel_activation.cc +++ b/py_cel_activation.cc @@ -26,7 +26,8 @@ #include "absl/strings/string_view.h" #include "common/function_descriptor.h" #include "common/kind.h" -#include "py_cel_env.h" +#include "py_cel_arena.h" +#include "py_cel_env_internal.h" #include "py_cel_function.h" #include "py_cel_value_provider.h" #include "google/protobuf/arena.h" @@ -46,7 +47,7 @@ void PyCelActivation::DefinePythonBindings(py::module& m) { } PyCelActivation::PyCelActivation( - std::shared_ptr env, + std::shared_ptr env, const std::unordered_map& data, const std::vector>& functions, const std::shared_ptr& arena) @@ -79,6 +80,8 @@ PyCelActivation::PyCelActivation( PyCelActivation::~PyCelActivation() = default; -std::shared_ptr PyCelActivation::GetEnv() const { return env_; } +std::shared_ptr PyCelActivation::GetEnv() const { + return env_; +} } // namespace cel_python diff --git a/py_cel_activation.h b/py_cel_activation.h index e99afb3..e2b6e66 100644 --- a/py_cel_activation.h +++ b/py_cel_activation.h @@ -28,7 +28,7 @@ namespace cel_python { -class PyCelEnv; +class PyCelEnvInternal; class PyCelFunction; // Wraps a CelActivation. Supports evaluation of CEL expressions. @@ -36,18 +36,18 @@ class PyCelActivation { public: static void DefinePythonBindings(pybind11::module& m); - PyCelActivation(std::shared_ptr env, + PyCelActivation(std::shared_ptr env, const std::unordered_map& data, const std::vector>& functions, const std::shared_ptr& arena); ~PyCelActivation(); - std::shared_ptr GetEnv() const; + std::shared_ptr GetEnv() const; std::shared_ptr GetArena() const { return arena_; } const cel::Activation* GetActivation() const { return &activation_; } private: - std::shared_ptr env_; + std::shared_ptr env_; std::shared_ptr arena_; cel::Activation activation_; }; diff --git a/py_cel_env.cc b/py_cel_env_internal.cc similarity index 89% rename from py_cel_env.cc rename to py_cel_env_internal.cc index b7dbdba..d487033 100644 --- a/py_cel_env.cc +++ b/py_cel_env_internal.cc @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include "py_cel_env.h" +#include "py_cel_env_internal.h" #include #include @@ -47,10 +47,10 @@ namespace cel_python { -PyCelEnv::PyCelEnv(PyObject* descriptor_pool, - std::unordered_map variableTypes, - const std::vector& extensions, - std::string container) +PyCelEnvInternal::PyCelEnvInternal( + PyObject* descriptor_pool, + std::unordered_map variableTypes, + const std::vector& extensions, std::string container) : py_descriptor_database_(descriptor_pool), descriptor_pool_(&py_descriptor_database_), message_factory_(&descriptor_pool_), @@ -62,10 +62,8 @@ PyCelEnv::PyCelEnv(PyObject* descriptor_pool, } } -// TODO(b/462745713): change the parameter to a const reference once we no -// longer need to pass in a `shared_ptr` to extensions. -absl::StatusOr PyCelEnv::GetCompiler( - const std::shared_ptr& env) { +absl::StatusOr PyCelEnvInternal::GetCompiler( + const std::shared_ptr& env) { if (env->compiler_) { return env->compiler_.get(); } @@ -100,8 +98,8 @@ absl::StatusOr PyCelEnv::GetCompiler( return env->compiler_.get(); } -absl::StatusOr PyCelEnv::GetRuntime( - const std::shared_ptr& env, RuntimeMode runtime_mode) { +absl::StatusOr PyCelEnvInternal::GetRuntime( + const std::shared_ptr& env, RuntimeMode runtime_mode) { if (auto it = env->runtimes_.find(runtime_mode); it != env->runtimes_.end()) { return it->second.get(); } @@ -136,7 +134,8 @@ absl::StatusOr PyCelEnv::GetRuntime( return runtime_ptr; } -const PyCelType& PyCelEnv::GetVariableType(const std::string& name) const { +const PyCelType& PyCelEnvInternal::GetVariableType( + const std::string& name) const { ABSL_CHECK(PyGILState_Check()); auto it = variable_types_.find(name); if (it != variable_types_.end()) { @@ -158,7 +157,7 @@ PyCelExtensionHandle::~PyCelExtensionHandle() { } absl::StatusOr PyCelExtensionHandle::GetExtension( - const std::shared_ptr& env) { + const std::shared_ptr& env) { if (cel_extension_) { return cel_extension_; } diff --git a/py_cel_env.h b/py_cel_env_internal.h similarity index 80% rename from py_cel_env.h rename to py_cel_env_internal.h index f21ad7e..c12f404 100644 --- a/py_cel_env.h +++ b/py_cel_env_internal.h @@ -12,8 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. -#ifndef THIRD_PARTY_CEL_PYTHON_PY_CEL_ENV_H_ -#define THIRD_PARTY_CEL_PYTHON_PY_CEL_ENV_H_ +#ifndef THIRD_PARTY_CEL_PYTHON_PY_CEL_ENV_INTERNAL_H_ +#define THIRD_PARTY_CEL_PYTHON_PY_CEL_ENV_INTERNAL_H_ #include // IWYU pragma: keep - Needed for PyObject @@ -27,7 +27,8 @@ #include "absl/status/statusor.h" #include "compiler/compiler.h" #include "runtime/runtime.h" -#include "py_cel.h" +#include "runtime/runtime_builder.h" +#include "runtime/runtime_options.h" #include "py_cel_extension.h" #include "py_cel_type.h" #include "py_descriptor_database.h" @@ -38,13 +39,15 @@ namespace cel_python { +class PyCelEnvInternal; + class PyCelExtensionHandle { public: explicit PyCelExtensionHandle(PyObject* extension); ~PyCelExtensionHandle(); absl::StatusOr GetExtension( - const std::shared_ptr& env); + const std::shared_ptr& env); private: // The Python object that was passed to the constructor and is retained for @@ -56,17 +59,18 @@ class PyCelExtensionHandle { PyCelExtension* cel_extension_; }; -// PyCelEnv is a container for internal CEL components not exposed to the python -// side. -class PyCelEnv { +// PyCelEnvInternal is a container for internal CEL components not exposed to +// the python side. +class PyCelEnvInternal { public: - PyCelEnv(PyObject* descriptor_pool, - std::unordered_map variableTypes, - const std::vector& extensions, std::string container); - ~PyCelEnv() = default; + PyCelEnvInternal(PyObject* descriptor_pool, + std::unordered_map variableTypes, + const std::vector& extensions, + std::string container); + ~PyCelEnvInternal() = default; static absl::StatusOr GetCompiler( - const std::shared_ptr& env); + const std::shared_ptr& env); enum RuntimeMode { // Standard CEL runtime with warnings treated as errors. @@ -77,7 +81,7 @@ class PyCelEnv { }; static absl::StatusOr GetRuntime( - const std::shared_ptr& env, RuntimeMode runtime_mode); + const std::shared_ptr& env, RuntimeMode runtime_mode); const google::protobuf::DescriptorPool* GetDescriptorPool() const { return &descriptor_pool_; @@ -116,4 +120,4 @@ class PyCelEnv { } // namespace cel_python -#endif // THIRD_PARTY_CEL_PYTHON_PY_CEL_ENV_H_ +#endif // THIRD_PARTY_CEL_PYTHON_PY_CEL_ENV_INTERNAL_H_ diff --git a/py_cel_expression.cc b/py_cel_expression.cc index 1a6169a..2e2b2e8 100644 --- a/py_cel_expression.cc +++ b/py_cel_expression.cc @@ -42,7 +42,7 @@ #include "runtime/runtime.h" #include "py_cel_activation.h" #include "py_cel_arena.h" -#include "py_cel_env.h" +#include "py_cel_env_internal.h" #include "py_cel_type.h" #include "py_cel_value.h" #include "py_error_status.h" @@ -71,12 +71,12 @@ void PyCelExpression::DefinePythonBindings(py::module& m) { } absl::StatusOr PyCelExpression::Compile( - const std::shared_ptr& env, const std::string& cel_expr, + const std::shared_ptr& env, const std::string& cel_expr, bool disable_check) { ABSL_CHECK(PyGILState_Check()); PY_CEL_ASSIGN_OR_RETURN(const cel::Compiler* compiler, - PyCelEnv::GetCompiler(env)); + PyCelEnvInternal::GetCompiler(env)); if (disable_check) { PY_CEL_ASSIGN_OR_RETURN(auto s, cel::NewSource(cel_expr, "")); @@ -121,21 +121,22 @@ absl::StatusOr PyCelExpression::Eval( if (std::holds_alternative(expr_)) { CEL_PYTHON_ASSIGN_OR_RETURN( const cel::Runtime* runtime, - PyCelEnv::GetRuntime(env_, PyCelEnv::kStandardIgnoreWarnings)); + PyCelEnvInternal::GetRuntime( + env_, PyCelEnvInternal::kStandardIgnoreWarnings)); CEL_PYTHON_ASSIGN_OR_RETURN( cel_program_, cel::extensions::ProtobufRuntimeAdapter::CreateProgram( *runtime, std::get(expr_))); } else { CEL_PYTHON_ASSIGN_OR_RETURN( const cel::Runtime* runtime, - PyCelEnv::GetRuntime(env_, PyCelEnv::kStandard)); + PyCelEnvInternal::GetRuntime(env_, PyCelEnvInternal::kStandard)); CEL_PYTHON_ASSIGN_OR_RETURN( cel_program_, cel::extensions::ProtobufRuntimeAdapter::CreateProgram( *runtime, std::get(expr_))); } } std::shared_ptr arena = activation.GetArena(); - std::shared_ptr env = activation.GetEnv(); + std::shared_ptr env = activation.GetEnv(); cel::EmbedderContext embedder_context = cel::EmbedderContext::From(&env); cel::EvaluateOptions options; options.message_factory = env->GetMessageFactory(); @@ -158,7 +159,8 @@ std::string PyCelExpression::Serialize() const { } absl::StatusOr PyCelExpression::Deserialize( - const std::shared_ptr& env, const std::string& serialized_expr) { + const std::shared_ptr& env, + const std::string& serialized_expr) { ABSL_CHECK(PyGILState_Check()); google::protobuf::Any any; if (!any.ParseFromString(serialized_expr)) { diff --git a/py_cel_expression.h b/py_cel_expression.h index b8a0e61..ba6debb 100644 --- a/py_cel_expression.h +++ b/py_cel_expression.h @@ -31,7 +31,7 @@ namespace cel_python { -class PyCelEnv; +class PyCelEnvInternal; // Wraps a CelExpression. Supports serialization and deserialization. class PyCelExpression { @@ -41,10 +41,10 @@ class PyCelExpression { PyCelExpression(PyCelExpression&& other) = default; PyCelExpression(const cel::expr::ParsedExpr& parsed_expr, - std::shared_ptr env) + std::shared_ptr env) : expr_(std::move(parsed_expr)), env_(std::move(env)) {} PyCelExpression(const cel::expr::CheckedExpr& checked_expr, - std::shared_ptr env) + std::shared_ptr env) : expr_(std::move(checked_expr)), env_(std::move(env)) {} PyCelType GetReturnType(); @@ -54,16 +54,17 @@ class PyCelExpression { std::string Serialize() const; static absl::StatusOr Compile( - const std::shared_ptr& env, const std::string& cel_expr, + const std::shared_ptr& env, const std::string& cel_expr, bool disable_check); static absl::StatusOr Deserialize( - const std::shared_ptr& env, const std::string& serialized_expr); + const std::shared_ptr& env, + const std::string& serialized_expr); private: std::variant expr_; - std::shared_ptr env_; + std::shared_ptr env_; std::unique_ptr cel_program_; }; diff --git a/py_cel_function.cc b/py_cel_function.cc index 094449f..776296e 100644 --- a/py_cel_function.cc +++ b/py_cel_function.cc @@ -29,7 +29,8 @@ #include "common/value.h" #include "runtime/embedder_context.h" #include "runtime/function.h" -#include "py_cel_env.h" +#include "py_cel_arena.h" +#include "py_cel_env_internal.h" #include "py_cel_type.h" #include "py_cel_value.h" #include "py_error_status.h" @@ -44,11 +45,11 @@ namespace py = ::pybind11; namespace { -static std::shared_ptr GetEnvFromContext( +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*>(); + return *context.embedder_context()->Get*>(); } } // namespace @@ -94,7 +95,7 @@ absl::StatusOr PyCelFunctionAdapter::Invoke( const cel::Function::InvokeContext& context) const { ABSL_CHECK(PyGILState_Check()); - std::shared_ptr env = GetEnvFromContext(context); + std::shared_ptr env = GetEnvFromContext(context); PY_CEL_ASSIGN_OR_RETURN(auto py_arena, PyCelArena::FromProtoArena(context.arena())); PyObject* py_args = PyTuple_New(args.size()); diff --git a/py_cel_function.h b/py_cel_function.h index baa0116..1aafc4a 100644 --- a/py_cel_function.h +++ b/py_cel_function.h @@ -34,7 +34,7 @@ namespace cel_python { -class PyCelEnv; +class PyCelEnvInternal; // Wrapper for a Python function that implements a CEL late-bound function. // These function implementations are supplied to Activation. diff --git a/py_cel_python_extension.cc b/py_cel_python_extension.cc index 4b9a313..aa75d3b 100644 --- a/py_cel_python_extension.cc +++ b/py_cel_python_extension.cc @@ -30,7 +30,7 @@ #include "compiler/compiler.h" #include "runtime/runtime_builder.h" #include "runtime/runtime_options.h" -#include "py_cel_env.h" +#include "py_cel_env_internal.h" #include "py_cel_extension.h" #include "py_cel_function.h" #include "py_cel_function_decl.h" diff --git a/py_cel_value.cc b/py_cel_value.cc index 2a72ddc..2861f6f 100644 --- a/py_cel_value.cc +++ b/py_cel_value.cc @@ -37,7 +37,7 @@ #include "common/value.h" #include "common/value_kind.h" #include "py_cel.h" -#include "py_cel_env.h" +#include "py_cel_env_internal.h" #include "py_cel_type.h" #include "py_cel_value_provider.h" #include "py_error_status.h" @@ -68,7 +68,7 @@ void PyCelValue::DefinePythonBindings(py::module& m) { // Should be called with the GIL held. PyCelValue::PyCelValue(cel::Value& cel_value, std::shared_ptr arena, - std::shared_ptr env) + std::shared_ptr env) : cel_value_(std::move(cel_value)), object_(nullptr), plain_object_(nullptr), @@ -117,7 +117,7 @@ PyObject* PyCelValue::PlainValue() { std::string PyCelValue::ToString() { return cel_value_.DebugString(); } PyCelValueProvider::PyCelValueProvider(std::string name, PyObject* value, - std::shared_ptr env) + std::shared_ptr env) : name_(std::move(name)), py_object_(value), env_(std::move(env)) { Py_INCREF(py_object_); } @@ -258,7 +258,7 @@ std::string PyCelMapItemAccessor::ToString() { // This should be called with the GIL held. PyObject* CelValueToPyObject(const cel::Value& cel_value, - const std::shared_ptr& env, + const std::shared_ptr& env, const std::shared_ptr& arena, bool plain_value) { switch (cel_value.kind()) { @@ -438,7 +438,7 @@ static void EnsureDateTimeModuleImported() { absl::StatusOr PyObjectToCelValue( PyObject* py_object, const PyCelType& expected_type, absl::FunctionRef context, - const std::shared_ptr& env, google::protobuf::Arena* arena, + const std::shared_ptr& env, google::protobuf::Arena* arena, bool bypass_type_check) { if (!py_object) { return cel::ErrorValue(absl::InvalidArgumentError( diff --git a/py_cel_value.h b/py_cel_value.h index 657c7e0..b32d557 100644 --- a/py_cel_value.h +++ b/py_cel_value.h @@ -31,7 +31,7 @@ namespace cel_python { class PyCelArena; -class PyCelEnv; +class PyCelEnvInternal; class PyMessageFactory; // Wraps a cel::Value. Converts the cel::Value to a Python object on demand and @@ -43,7 +43,7 @@ class PyCelValue { static void DefinePythonBindings(pybind11::module& m); PyCelValue(cel::Value& cel_value, std::shared_ptr arena, - std::shared_ptr env); + std::shared_ptr env); // Move constructor and assignment. PyCelValue(PyCelValue&& other) noexcept = default; @@ -69,7 +69,7 @@ class PyCelValue { PyObject* object_; PyObject* plain_object_; std::shared_ptr arena_; - std::shared_ptr env_; + std::shared_ptr env_; }; // Variant of PyCelValue that is used to access a specific element of a list. @@ -77,7 +77,7 @@ class PyCelListItemAccessor : public PyCelValue { public: PyCelListItemAccessor(cel::Value celValue, int index, std::shared_ptr arena, - std::shared_ptr env) + std::shared_ptr env) : PyCelValue(celValue, std::move(arena), std::move(env)), index_(index) {} // Move constructor. @@ -104,7 +104,7 @@ class PyCelMapItemAccessor : public PyCelValue { public: PyCelMapItemAccessor(cel::Value celValue, cel::Value key, std::shared_ptr arena, - std::shared_ptr env) + std::shared_ptr env) : PyCelValue(celValue, std::move(arena), std::move(env)), key_(std::move(key)) {} @@ -126,14 +126,14 @@ class PyCelMapItemAccessor : public PyCelValue { }; PyObject* CelValueToPyObject(const cel::Value& cel_value, - const std::shared_ptr& env, + const std::shared_ptr& env, const std::shared_ptr& arena, bool plain_value); absl::StatusOr PyObjectToCelValue( PyObject* py_object, const PyCelType& expected_type, absl::FunctionRef context, - const std::shared_ptr& env, google::protobuf::Arena* arena, + const std::shared_ptr& env, google::protobuf::Arena* arena, bool bypass_type_check = false); } // namespace cel_python diff --git a/py_cel_value_provider.h b/py_cel_value_provider.h index e640445..fa564e3 100644 --- a/py_cel_value_provider.h +++ b/py_cel_value_provider.h @@ -22,7 +22,7 @@ #include #include "common/value.h" -#include "py_cel_env.h" +#include "py_cel_env_internal.h" #include "google/protobuf/arena.h" #include "google/protobuf/descriptor.h" #include "google/protobuf/message.h" @@ -36,7 +36,7 @@ namespace cel_python { class PyCelValueProvider { public: PyCelValueProvider(std::string name, PyObject* value, - std::shared_ptr env); + std::shared_ptr env); ~PyCelValueProvider(); cel::Value Provide(const google::protobuf::DescriptorPool* descriptor_pool, @@ -46,7 +46,7 @@ class PyCelValueProvider { private: std::string name_; PyObject* py_object_; - std::shared_ptr env_; + std::shared_ptr env_; cel::ErrorValue InvalidTypeError() const; };