Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
8 changes: 4 additions & 4 deletions py_cel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 <pybind11/pybind11.h>
Expand Down Expand Up @@ -106,9 +106,9 @@ void PyCel::DefinePythonBindings(pybind11::module& m) {
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))) {
: env_(std::make_unique<PyCelEnvInternal>(
descriptor_pool, std::move(variable_types), extensions,
std::move(container))) {
ABSL_CHECK(PyGILState_Check());
}

Expand Down
4 changes: 2 additions & 2 deletions py_cel.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ class PyCel {
const std::vector<std::shared_ptr<PyCelFunction>>& functions,
const std::shared_ptr<PyCelArena>& arena);

std::shared_ptr<PyCelEnv> GetEnv() { return env_; }
std::shared_ptr<PyCelEnvInternal> GetEnv() { return env_; }

private:
std::shared_ptr<PyCelEnv> env_;
std::shared_ptr<PyCelEnvInternal> env_;
};

} // namespace cel_python
Expand Down
9 changes: 6 additions & 3 deletions py_cel_activation.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -46,7 +47,7 @@ void PyCelActivation::DefinePythonBindings(py::module& m) {
}

PyCelActivation::PyCelActivation(
std::shared_ptr<PyCelEnv> env,
std::shared_ptr<PyCelEnvInternal> env,
const std::unordered_map<std::string, PyObject*>& data,
const std::vector<std::shared_ptr<PyCelFunction>>& functions,
const std::shared_ptr<PyCelArena>& arena)
Expand Down Expand Up @@ -79,6 +80,8 @@ PyCelActivation::PyCelActivation(

PyCelActivation::~PyCelActivation() = default;

std::shared_ptr<PyCelEnv> PyCelActivation::GetEnv() const { return env_; }
std::shared_ptr<PyCelEnvInternal> PyCelActivation::GetEnv() const {
return env_;
}

} // namespace cel_python
8 changes: 4 additions & 4 deletions py_cel_activation.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,26 +28,26 @@

namespace cel_python {

class PyCelEnv;
class PyCelEnvInternal;
class PyCelFunction;

// Wraps a CelActivation. Supports evaluation of CEL expressions.
class PyCelActivation {
public:
static void DefinePythonBindings(pybind11::module& m);

PyCelActivation(std::shared_ptr<PyCelEnv> env,
PyCelActivation(std::shared_ptr<PyCelEnvInternal> env,
const std::unordered_map<std::string, PyObject*>& data,
const std::vector<std::shared_ptr<PyCelFunction>>& functions,
const std::shared_ptr<PyCelArena>& arena);
~PyCelActivation();

std::shared_ptr<PyCelEnv> GetEnv() const;
std::shared_ptr<PyCelEnvInternal> GetEnv() const;
std::shared_ptr<PyCelArena> GetArena() const { return arena_; }
const cel::Activation* GetActivation() const { return &activation_; }

private:
std::shared_ptr<PyCelEnv> env_;
std::shared_ptr<PyCelEnvInternal> env_;
std::shared_ptr<PyCelArena> arena_;
cel::Activation activation_;
};
Expand Down
25 changes: 12 additions & 13 deletions py_cel_env.cc → py_cel_env_internal.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 <memory>
#include <string>
Expand Down Expand Up @@ -47,10 +47,10 @@

namespace cel_python {

PyCelEnv::PyCelEnv(PyObject* descriptor_pool,
std::unordered_map<std::string, PyCelType> variableTypes,
const std::vector<PyObject*>& extensions,
std::string container)
PyCelEnvInternal::PyCelEnvInternal(
PyObject* descriptor_pool,
std::unordered_map<std::string, PyCelType> variableTypes,
const std::vector<PyObject*>& extensions, std::string container)
: py_descriptor_database_(descriptor_pool),
descriptor_pool_(&py_descriptor_database_),
message_factory_(&descriptor_pool_),
Expand All @@ -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<PyCelEnv>` to extensions.
absl::StatusOr<const cel::Compiler*> PyCelEnv::GetCompiler(
const std::shared_ptr<PyCelEnv>& env) {
absl::StatusOr<const cel::Compiler*> PyCelEnvInternal::GetCompiler(
const std::shared_ptr<PyCelEnvInternal>& env) {
if (env->compiler_) {
return env->compiler_.get();
}
Expand Down Expand Up @@ -100,8 +98,8 @@ absl::StatusOr<const cel::Compiler*> PyCelEnv::GetCompiler(
return env->compiler_.get();
}

absl::StatusOr<const cel::Runtime*> PyCelEnv::GetRuntime(
const std::shared_ptr<PyCelEnv>& env, RuntimeMode runtime_mode) {
absl::StatusOr<const cel::Runtime*> PyCelEnvInternal::GetRuntime(
const std::shared_ptr<PyCelEnvInternal>& env, RuntimeMode runtime_mode) {
if (auto it = env->runtimes_.find(runtime_mode); it != env->runtimes_.end()) {
return it->second.get();
}
Expand Down Expand Up @@ -136,7 +134,8 @@ absl::StatusOr<const cel::Runtime*> 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()) {
Expand All @@ -158,7 +157,7 @@ PyCelExtensionHandle::~PyCelExtensionHandle() {
}

absl::StatusOr<PyCelExtension*> PyCelExtensionHandle::GetExtension(
const std::shared_ptr<PyCelEnv>& env) {
const std::shared_ptr<PyCelEnvInternal>& env) {
if (cel_extension_) {
return cel_extension_;
}
Expand Down
32 changes: 18 additions & 14 deletions py_cel_env.h → py_cel_env_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <Python.h> // IWYU pragma: keep - Needed for PyObject

Expand All @@ -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"
Expand All @@ -38,13 +39,15 @@

namespace cel_python {

class PyCelEnvInternal;

class PyCelExtensionHandle {
public:
explicit PyCelExtensionHandle(PyObject* extension);
~PyCelExtensionHandle();

absl::StatusOr<PyCelExtension*> GetExtension(
const std::shared_ptr<PyCelEnv>& env);
const std::shared_ptr<PyCelEnvInternal>& env);

private:
// The Python object that was passed to the constructor and is retained for
Expand All @@ -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<std::string, PyCelType> variableTypes,
const std::vector<PyObject*>& extensions, std::string container);
~PyCelEnv() = default;
PyCelEnvInternal(PyObject* descriptor_pool,
std::unordered_map<std::string, PyCelType> variableTypes,
const std::vector<PyObject*>& extensions,
std::string container);
~PyCelEnvInternal() = default;

static absl::StatusOr<const cel::Compiler*> GetCompiler(
const std::shared_ptr<PyCelEnv>& env);
const std::shared_ptr<PyCelEnvInternal>& env);

enum RuntimeMode {
// Standard CEL runtime with warnings treated as errors.
Expand All @@ -77,7 +81,7 @@ class PyCelEnv {
};

static absl::StatusOr<const cel::Runtime*> GetRuntime(
const std::shared_ptr<PyCelEnv>& env, RuntimeMode runtime_mode);
const std::shared_ptr<PyCelEnvInternal>& env, RuntimeMode runtime_mode);

const google::protobuf::DescriptorPool* GetDescriptorPool() const {
return &descriptor_pool_;
Expand Down Expand Up @@ -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_
16 changes: 9 additions & 7 deletions py_cel_expression.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -71,12 +71,12 @@ void PyCelExpression::DefinePythonBindings(py::module& m) {
}

absl::StatusOr<PyCelExpression> PyCelExpression::Compile(
const std::shared_ptr<PyCelEnv>& env, const std::string& cel_expr,
const std::shared_ptr<PyCelEnvInternal>& 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, "<input>"));
Expand Down Expand Up @@ -121,21 +121,22 @@ absl::StatusOr<PyCelValue> PyCelExpression::Eval(
if (std::holds_alternative<ParsedExpr>(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<ParsedExpr>(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<CheckedExpr>(expr_)));
}
}
std::shared_ptr<PyCelArena> arena = activation.GetArena();
std::shared_ptr<PyCelEnv> env = activation.GetEnv();
std::shared_ptr<PyCelEnvInternal> env = activation.GetEnv();
cel::EmbedderContext embedder_context = cel::EmbedderContext::From(&env);
cel::EvaluateOptions options;
options.message_factory = env->GetMessageFactory();
Expand All @@ -158,7 +159,8 @@ std::string PyCelExpression::Serialize() const {
}

absl::StatusOr<PyCelExpression> PyCelExpression::Deserialize(
const std::shared_ptr<PyCelEnv>& env, const std::string& serialized_expr) {
const std::shared_ptr<PyCelEnvInternal>& env,
const std::string& serialized_expr) {
ABSL_CHECK(PyGILState_Check());
google::protobuf::Any any;
if (!any.ParseFromString(serialized_expr)) {
Expand Down
13 changes: 7 additions & 6 deletions py_cel_expression.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

namespace cel_python {

class PyCelEnv;
class PyCelEnvInternal;

// Wraps a CelExpression. Supports serialization and deserialization.
class PyCelExpression {
Expand All @@ -41,10 +41,10 @@ class PyCelExpression {
PyCelExpression(PyCelExpression&& other) = default;

PyCelExpression(const cel::expr::ParsedExpr& parsed_expr,
std::shared_ptr<PyCelEnv> env)
std::shared_ptr<PyCelEnvInternal> env)
: expr_(std::move(parsed_expr)), env_(std::move(env)) {}
PyCelExpression(const cel::expr::CheckedExpr& checked_expr,
std::shared_ptr<PyCelEnv> env)
std::shared_ptr<PyCelEnvInternal> env)
: expr_(std::move(checked_expr)), env_(std::move(env)) {}

PyCelType GetReturnType();
Expand All @@ -54,16 +54,17 @@ class PyCelExpression {
std::string Serialize() const;

static absl::StatusOr<PyCelExpression> Compile(
const std::shared_ptr<PyCelEnv>& env, const std::string& cel_expr,
const std::shared_ptr<PyCelEnvInternal>& env, const std::string& cel_expr,
bool disable_check);

static absl::StatusOr<PyCelExpression> Deserialize(
const std::shared_ptr<PyCelEnv>& env, const std::string& serialized_expr);
const std::shared_ptr<PyCelEnvInternal>& env,
const std::string& serialized_expr);

private:
std::variant<cel::expr::ParsedExpr, cel::expr::CheckedExpr>
expr_;
std::shared_ptr<PyCelEnv> env_;
std::shared_ptr<PyCelEnvInternal> env_;
std::unique_ptr<cel::Program> cel_program_;
};

Expand Down
9 changes: 5 additions & 4 deletions py_cel_function.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -44,11 +45,11 @@ namespace py = ::pybind11;

namespace {

static std::shared_ptr<PyCelEnv> GetEnvFromContext(
static std::shared_ptr<PyCelEnvInternal> 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<std::shared_ptr<PyCelEnv>*>();
return *context.embedder_context()->Get<std::shared_ptr<PyCelEnvInternal>*>();
}

} // namespace
Expand Down Expand Up @@ -94,7 +95,7 @@ absl::StatusOr<cel::Value> PyCelFunctionAdapter::Invoke(
const cel::Function::InvokeContext& context) const {
ABSL_CHECK(PyGILState_Check());

std::shared_ptr<PyCelEnv> env = GetEnvFromContext(context);
std::shared_ptr<PyCelEnvInternal> env = GetEnvFromContext(context);
PY_CEL_ASSIGN_OR_RETURN(auto py_arena,
PyCelArena::FromProtoArena(context.arena()));
PyObject* py_args = PyTuple_New(args.size());
Expand Down
2 changes: 1 addition & 1 deletion py_cel_function.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Loading