Skip to content

Commit f3fbdf0

Browse files
dmitriplotnikovcopybara-github
authored andcommitted
Rename PY_CEL_* macros to CEL_PYTHON_*
PiperOrigin-RevId: 868409116
1 parent 3a058b0 commit f3fbdf0

10 files changed

Lines changed: 87 additions & 85 deletions

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -252,15 +252,15 @@ This method adds extension function definitions to the provided
252252
absl::Status ConfigureCompiler(
253253
cel::CompilerBuilder& compiler_builder,
254254
const proto2::DescriptorPool& descriptor_pool) {
255-
PY_CEL_ASSIGN_OR_RETURN(
255+
CEL_PYTHON_ASSIGN_OR_RETURN(
256256
auto func_translate,
257257
cel::MakeFunctionDecl("translate",
258258
cel::MakeMemberOverloadDecl("translate_inst",
259259
/*return_type=*/cel::StringType(),
260260
/*target=*/cel::StringType(),
261261
/*from_lang=*/cel::StringType(),
262262
/*to_lang=*/cel::StringType())));
263-
PY_CEL_RETURN_IF_ERROR(
263+
CEL_PYTHON_RETURN_IF_ERROR(
264264
compiler_builder.GetCheckerBuilder().AddFunction(func_translate));
265265
return absl::OkStatus();
266266
}
@@ -292,7 +292,7 @@ absl::Status ConfigureRuntime(cel::RuntimeBuilder& runtime_builder,
292292
const StringValue&>;
293293
auto status = TranslateFunctionAdapter::RegisterMemberOverload(
294294
"translate", &Translate, runtime_builder.function_registry());
295-
PY_CEL_RETURN_IF_ERROR(status);
295+
CEL_PYTHON_RETURN_IF_ERROR(status);
296296
return absl::OkStatus();
297297
}
298298
```
@@ -368,7 +368,7 @@ If the extension is written in C++, use the `RegisterLazyFunction` function:
368368
using MyFunctionAdapter =
369369
cel::UnaryFunctionAdapter<absl::StatusOr<cel::IntValue>,
370370
const cel::IntValue&>;
371-
PY_CEL_RETURN_IF_ERROR(
371+
CEL_PYTHON_RETURN_IF_ERROR(
372372
runtime_builder.function_registry().RegisterLazyFunction(
373373
MyFunctionAdapter::CreateDescriptor(
374374
"my_func",

custom_ext/sample_cel_ext.cc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,24 +71,24 @@ class SampleCelExtension : public cel_python::CelExtension {
7171
absl::Status ConfigureCompiler(
7272
cel::CompilerBuilder& compiler_builder,
7373
const google::protobuf::DescriptorPool& descriptor_pool) override {
74-
PY_CEL_ASSIGN_OR_RETURN(
74+
CEL_PYTHON_ASSIGN_OR_RETURN(
7575
auto func_translate,
7676
MakeFunctionDecl("translate",
7777
MakeMemberOverloadDecl("translate_inst",
7878
/*return_type=*/StringType(),
7979
/*target=*/StringType(),
8080
/*from_lang=*/StringType(),
8181
/*to_lang=*/StringType())));
82-
PY_CEL_RETURN_IF_ERROR(
82+
CEL_PYTHON_RETURN_IF_ERROR(
8383
compiler_builder.GetCheckerBuilder().AddFunction(func_translate));
8484

85-
PY_CEL_ASSIGN_OR_RETURN(
85+
CEL_PYTHON_ASSIGN_OR_RETURN(
8686
auto func_translate_late,
8787
MakeFunctionDecl("translate_late",
8888
MakeOverloadDecl("late_bound_translation",
8989
/*return_type=*/StringType(),
9090
/*text=*/StringType())));
91-
PY_CEL_RETURN_IF_ERROR(
91+
CEL_PYTHON_RETURN_IF_ERROR(
9292
compiler_builder.GetCheckerBuilder().AddFunction(func_translate_late));
9393
return absl::OkStatus();
9494
}
@@ -101,13 +101,13 @@ class SampleCelExtension : public cel_python::CelExtension {
101101
const StringValue&>;
102102
auto status = TranslateFunctionAdapter::RegisterMemberOverload(
103103
"translate", &Translate, runtime_builder.function_registry());
104-
PY_CEL_RETURN_IF_ERROR(status);
104+
CEL_PYTHON_RETURN_IF_ERROR(status);
105105

106106
// Register a lazy function that will be bound at evaluation time.
107107
using TranslateLateFunctionAdapter =
108108
::cel::UnaryFunctionAdapter<absl::StatusOr<StringValue>,
109109
const StringValue&>;
110-
PY_CEL_RETURN_IF_ERROR(
110+
CEL_PYTHON_RETURN_IF_ERROR(
111111
runtime_builder.function_registry().RegisterLazyFunction(
112112
TranslateLateFunctionAdapter::CreateDescriptor(
113113
"translate_late",

py_cel/ext/ext_math.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class ExtMath : public CelExtension {
3636

3737
absl::Status ConfigureRuntime(cel::RuntimeBuilder& runtime_builder,
3838
const cel::RuntimeOptions& opts) override {
39-
PY_CEL_RETURN_IF_ERROR(cel::extensions::RegisterMathExtensionFunctions(
39+
CEL_PYTHON_RETURN_IF_ERROR(cel::extensions::RegisterMathExtensionFunctions(
4040
runtime_builder.function_registry(), opts));
4141
return absl::OkStatus();
4242
}

py_cel/py_cel_env_internal.cc

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -70,31 +70,31 @@ absl::StatusOr<const cel::Compiler*> PyCelEnvInternal::GetCompiler(
7070

7171
cel::CompilerOptions compiler_options;
7272
compiler_options.parser_options.enable_quoted_identifiers = true;
73-
PY_CEL_ASSIGN_OR_RETURN(
73+
CEL_PYTHON_ASSIGN_OR_RETURN(
7474
std::unique_ptr<cel::CompilerBuilder> compiler_builder,
7575
cel::NewCompilerBuilder(&env->descriptor_pool_, compiler_options));
7676
compiler_builder->GetCheckerBuilder().set_container(env->container_);
77-
PY_CEL_RETURN_IF_ERROR(
77+
CEL_PYTHON_RETURN_IF_ERROR(
7878
compiler_builder->AddLibrary(cel::StandardCompilerLibrary()));
7979
for (std::unique_ptr<CelExtensionHandle>& extension_handle :
8080
env->extensions_) {
81-
PY_CEL_ASSIGN_OR_RETURN(CelExtension * extension,
82-
extension_handle->GetExtension(env));
83-
PY_CEL_RETURN_IF_ERROR(
81+
CEL_PYTHON_ASSIGN_OR_RETURN(CelExtension * extension,
82+
extension_handle->GetExtension(env));
83+
CEL_PYTHON_RETURN_IF_ERROR(
8484
extension->ConfigureCompiler(*compiler_builder, env->descriptor_pool_));
8585
}
8686
google::protobuf::Arena* arena = compiler_builder->GetCheckerBuilder().arena();
8787
for (const auto& [name, type] : env->variable_types_) {
88-
PY_CEL_ASSIGN_OR_RETURN(
88+
CEL_PYTHON_ASSIGN_OR_RETURN(
8989
cel::Type cel_type,
9090
PyCelType::ToCelType(type, arena, env->descriptor_pool_));
9191
cel::VariableDecl var;
9292
var.set_name(name);
9393
var.set_type(cel_type);
94-
PY_CEL_RETURN_IF_ERROR(
94+
CEL_PYTHON_RETURN_IF_ERROR(
9595
compiler_builder->GetCheckerBuilder().AddVariable(var));
9696
}
97-
PY_CEL_ASSIGN_OR_RETURN(env->compiler_, compiler_builder->Build());
97+
CEL_PYTHON_ASSIGN_OR_RETURN(env->compiler_, compiler_builder->Build());
9898
return env->compiler_.get();
9999
}
100100

@@ -116,19 +116,19 @@ absl::StatusOr<const cel::Runtime*> PyCelEnvInternal::GetRuntime(
116116
opts.fail_on_warnings = false;
117117
break;
118118
}
119-
PY_CEL_ASSIGN_OR_RETURN(
119+
CEL_PYTHON_ASSIGN_OR_RETURN(
120120
cel::RuntimeBuilder builder,
121121
cel::CreateStandardRuntimeBuilder(&env->descriptor_pool_, opts));
122-
PY_CEL_RETURN_IF_ERROR(cel::EnableReferenceResolver(
122+
CEL_PYTHON_RETURN_IF_ERROR(cel::EnableReferenceResolver(
123123
builder, cel::ReferenceResolverEnabled::kAlways));
124124
for (std::unique_ptr<CelExtensionHandle>& extension_handle :
125125
env->extensions_) {
126-
PY_CEL_ASSIGN_OR_RETURN(CelExtension * extension,
127-
extension_handle->GetExtension(env));
128-
PY_CEL_RETURN_IF_ERROR(extension->ConfigureRuntime(builder, opts));
126+
CEL_PYTHON_ASSIGN_OR_RETURN(CelExtension * extension,
127+
extension_handle->GetExtension(env));
128+
CEL_PYTHON_RETURN_IF_ERROR(extension->ConfigureRuntime(builder, opts));
129129
}
130-
PY_CEL_ASSIGN_OR_RETURN(std::unique_ptr<cel::Runtime> runtime,
131-
std::move(builder).Build());
130+
CEL_PYTHON_ASSIGN_OR_RETURN(std::unique_ptr<cel::Runtime> runtime,
131+
std::move(builder).Build());
132132
const cel::Runtime* runtime_ptr = runtime.get();
133133
env->runtimes_[runtime_mode] = std::move(runtime);
134134
return runtime_ptr;

py_cel/py_cel_expression.cc

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -107,25 +107,25 @@ absl::StatusOr<PyCelExpression> PyCelExpression::Compile(
107107
bool disable_check) {
108108
ABSL_CHECK(PyGILState_Check());
109109

110-
PY_CEL_ASSIGN_OR_RETURN(const cel::Compiler* compiler,
111-
PyCelEnvInternal::GetCompiler(env));
110+
CEL_PYTHON_ASSIGN_OR_RETURN(const cel::Compiler* compiler,
111+
PyCelEnvInternal::GetCompiler(env));
112112

113113
if (disable_check) {
114-
PY_CEL_ASSIGN_OR_RETURN(auto s, cel::NewSource(cel_expr, "<input>"));
115-
CEL_PYTHON_ASSIGN_OR_RETURN(auto ast, compiler->GetParser().Parse(*s));
114+
CEL_PYTHON_ASSIGN_OR_RETURN(auto s, cel::NewSource(cel_expr, "<input>"));
115+
PY_CEL_PYTHON_ASSIGN_OR_RETURN(auto ast, compiler->GetParser().Parse(*s));
116116
ParsedExpr parsed_expr;
117-
PY_CEL_RETURN_IF_ERROR(cel::AstToParsedExpr(*ast, &parsed_expr));
117+
CEL_PYTHON_RETURN_IF_ERROR(cel::AstToParsedExpr(*ast, &parsed_expr));
118118
return PyCelExpression(parsed_expr, env);
119119
}
120120

121-
CEL_PYTHON_ASSIGN_OR_RETURN(auto validation, compiler->Compile(cel_expr));
121+
PY_CEL_PYTHON_ASSIGN_OR_RETURN(auto validation, compiler->Compile(cel_expr));
122122
if (!validation.IsValid() || validation.GetAst() == nullptr) {
123123
return absl::InvalidArgumentError(validation.FormatError());
124124
}
125-
PY_CEL_ASSIGN_OR_RETURN(std::unique_ptr<cel::Ast> ast,
126-
validation.ReleaseAst());
125+
CEL_PYTHON_ASSIGN_OR_RETURN(std::unique_ptr<cel::Ast> ast,
126+
validation.ReleaseAst());
127127
CheckedExpr checked_expr;
128-
PY_CEL_RETURN_IF_ERROR(cel::AstToCheckedExpr(*ast, &checked_expr));
128+
CEL_PYTHON_RETURN_IF_ERROR(cel::AstToCheckedExpr(*ast, &checked_expr));
129129
return PyCelExpression(checked_expr, env);
130130
}
131131

@@ -151,18 +151,18 @@ absl::StatusOr<PyCelValue> PyCelExpression::Eval(
151151
ABSL_CHECK(PyGILState_Check());
152152
if (cel_program_ == nullptr) {
153153
if (std::holds_alternative<ParsedExpr>(expr_)) {
154-
CEL_PYTHON_ASSIGN_OR_RETURN(
154+
PY_CEL_PYTHON_ASSIGN_OR_RETURN(
155155
const cel::Runtime* runtime,
156156
PyCelEnvInternal::GetRuntime(
157157
env_, PyCelEnvInternal::kStandardIgnoreWarnings));
158-
CEL_PYTHON_ASSIGN_OR_RETURN(
158+
PY_CEL_PYTHON_ASSIGN_OR_RETURN(
159159
cel_program_, cel::extensions::ProtobufRuntimeAdapter::CreateProgram(
160160
*runtime, std::get<ParsedExpr>(expr_)));
161161
} else {
162-
CEL_PYTHON_ASSIGN_OR_RETURN(
162+
PY_CEL_PYTHON_ASSIGN_OR_RETURN(
163163
const cel::Runtime* runtime,
164164
PyCelEnvInternal::GetRuntime(env_, PyCelEnvInternal::kStandard));
165-
CEL_PYTHON_ASSIGN_OR_RETURN(
165+
PY_CEL_PYTHON_ASSIGN_OR_RETURN(
166166
cel_program_, cel::extensions::ProtobufRuntimeAdapter::CreateProgram(
167167
*runtime, std::get<CheckedExpr>(expr_)));
168168
}
@@ -173,7 +173,7 @@ absl::StatusOr<PyCelValue> PyCelExpression::Eval(
173173
cel::EvaluateOptions options;
174174
options.message_factory = env->GetMessageFactory();
175175
options.embedder_context = &embedder_context;
176-
CEL_PYTHON_ASSIGN_OR_RETURN(
176+
PY_CEL_PYTHON_ASSIGN_OR_RETURN(
177177
cel::Value result,
178178
cel_program_->Evaluate(arena->GetArena(), *activation.GetActivation(),
179179
std::move(options)));

py_cel/py_cel_function.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ absl::StatusOr<cel::Value> PyCelFunctionAdapter::Invoke(
8686
ABSL_CHECK(PyGILState_Check());
8787

8888
std::shared_ptr<PyCelEnvInternal> env = GetEnvFromContext(context);
89-
PY_CEL_ASSIGN_OR_RETURN(auto py_arena,
90-
PyCelArena::FromProtoArena(context.arena()));
89+
CEL_PYTHON_ASSIGN_OR_RETURN(auto py_arena,
90+
PyCelArena::FromProtoArena(context.arena()));
9191
PyObject* py_args = PyTuple_New(args.size());
9292
for (int i = 0; i < args.size(); ++i) {
9393
PyTuple_SetItem(py_args, i,

py_cel/py_cel_python_extension.cc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,23 +71,23 @@ absl::Status PyCelPythonExtension::ConfigureCompiler(
7171
for (const PyCelOverload& overload : function.overloads()) {
7272
cel::OverloadDecl overload_decl;
7373
overload_decl.set_id(overload.overload_id());
74-
PY_CEL_ASSIGN_OR_RETURN(
74+
CEL_PYTHON_ASSIGN_OR_RETURN(
7575
cel::Type cel_type,
7676
PyCelType::ToCelType(overload.return_type(), arena, descriptor_pool));
7777
overload_decl.set_result(cel_type);
7878
overload_decl.set_member(overload.is_member());
7979
auto& mutable_args = overload_decl.mutable_args();
8080
mutable_args.reserve(overload.parameters().size());
8181
for (const auto& arg : overload.parameters()) {
82-
PY_CEL_ASSIGN_OR_RETURN(
82+
CEL_PYTHON_ASSIGN_OR_RETURN(
8383
cel::Type cel_type,
8484
PyCelType::ToCelType(arg, arena, descriptor_pool));
8585
mutable_args.push_back(cel_type);
8686
}
87-
PY_CEL_RETURN_IF_ERROR(
87+
CEL_PYTHON_RETURN_IF_ERROR(
8888
function_decl.AddOverload(std::move(overload_decl)));
8989
}
90-
PY_CEL_RETURN_IF_ERROR(
90+
CEL_PYTHON_RETURN_IF_ERROR(
9191
compiler_builder.GetCheckerBuilder().MergeFunction(function_decl));
9292
}
9393

@@ -113,12 +113,12 @@ absl::Status PyCelPythonExtension::ConfigureRuntime(
113113
cel::FunctionDescriptor descriptor(function.name(), overload.is_member(),
114114
types, kFunctionDescriptorOptions);
115115
if (!overload.py_function().is_none()) {
116-
PY_CEL_RETURN_IF_ERROR(runtime_builder.function_registry().Register(
116+
CEL_PYTHON_RETURN_IF_ERROR(runtime_builder.function_registry().Register(
117117
descriptor, std::make_unique<PyCelFunctionAdapter>(
118118
function.name(), overload.return_type(),
119119
overload.py_function())));
120120
} else {
121-
PY_CEL_RETURN_IF_ERROR(
121+
CEL_PYTHON_RETURN_IF_ERROR(
122122
runtime_builder.function_registry().RegisterLazyFunction(
123123
descriptor));
124124
}

py_cel/py_cel_value.cc

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -452,8 +452,8 @@ absl::StatusOr<cel::Value> PyObjectToCelValue(
452452

453453
switch (expected_type.GetKind()) {
454454
case cel::Kind::kDyn: {
455-
PY_CEL_ASSIGN_OR_RETURN(const PyCelType& type,
456-
PyCelType::ForPyObject(py_object, context));
455+
CEL_PYTHON_ASSIGN_OR_RETURN(const PyCelType& type,
456+
PyCelType::ForPyObject(py_object, context));
457457
return PyObjectToCelValue(py_object, type, context, env, arena,
458458
/*bypass_type_check=*/true);
459459
}
@@ -649,8 +649,8 @@ absl::StatusOr<cel::Value> PyObjectToCelValue(
649649
}
650650
}
651651
case cel::Kind::kMessage: {
652-
PY_CEL_ASSIGN_OR_RETURN(const PyCelType& type,
653-
PyCelType::ForPyObject(py_object, context));
652+
CEL_PYTHON_ASSIGN_OR_RETURN(const PyCelType& type,
653+
PyCelType::ForPyObject(py_object, context));
654654
if (!bypass_type_check && type.GetName() != expected_type.GetName()) {
655655
return InvalidTypeError(py_object, context, expected_type);
656656
}
@@ -718,15 +718,15 @@ absl::StatusOr<cel::Value> PyObjectToCelValue(
718718
PyObject* item = PyList_GetItem(py_object, i);
719719
// Note: PyList_GetItem returns a borrowed reference, so we shouldn't
720720
// DECREF it.
721-
PY_CEL_ASSIGN_OR_RETURN(cel::Value converted_value,
722-
PyObjectToCelValue(
723-
item, element_type,
724-
[context, i]() {
725-
return absl::StrFormat("%s[%d]",
726-
context(), i);
727-
},
728-
env, arena));
729-
PY_CEL_RETURN_IF_ERROR(builder->Add(converted_value));
721+
CEL_PYTHON_ASSIGN_OR_RETURN(cel::Value converted_value,
722+
PyObjectToCelValue(
723+
item, element_type,
724+
[context, i]() {
725+
return absl::StrFormat(
726+
"%s[%d]", context(), i);
727+
},
728+
env, arena));
729+
CEL_PYTHON_RETURN_IF_ERROR(builder->Add(converted_value));
730730
}
731731
return std::move(*builder).Build();
732732
}
@@ -742,7 +742,7 @@ absl::StatusOr<cel::Value> PyObjectToCelValue(
742742
while (PyDict_Next(py_object, &pos, &key, &value)) {
743743
// Note: PyDict_Next returns borrowed references, so we shouldn't
744744
// DECREF them.
745-
PY_CEL_ASSIGN_OR_RETURN(
745+
CEL_PYTHON_ASSIGN_OR_RETURN(
746746
cel::Value converted_key,
747747
PyObjectToCelValue(
748748
key, key_type,
@@ -753,7 +753,7 @@ absl::StatusOr<cel::Value> PyObjectToCelValue(
753753
},
754754
env, arena));
755755

756-
PY_CEL_ASSIGN_OR_RETURN(
756+
CEL_PYTHON_ASSIGN_OR_RETURN(
757757
cel::Value converted_value,
758758
PyObjectToCelValue(
759759
value, value_type,
@@ -764,7 +764,8 @@ absl::StatusOr<cel::Value> PyObjectToCelValue(
764764
PyUnicode_AsUTF8(PyObject_Repr(value)));
765765
},
766766
env, arena));
767-
PY_CEL_RETURN_IF_ERROR(builder->Put(converted_key, converted_value));
767+
CEL_PYTHON_RETURN_IF_ERROR(
768+
builder->Put(converted_key, converted_value));
768769
}
769770
return std::move(*builder).Build();
770771
}

py_cel/py_error_status.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@
2525
#include "py_cel/status_macros.h"
2626
#include <pybind11/pybind11.h>
2727

28-
#define CEL_PYTHON_ASSIGN_OR_RETURN(...) \
29-
PY_CEL_RETURN_IF_ERROR(PyErr_toStatus()); \
30-
PY_CEL_ASSIGN_OR_RETURN(__VA_ARGS__); \
31-
PY_CEL_RETURN_IF_ERROR(PyErr_toStatus());
28+
#define PY_CEL_PYTHON_ASSIGN_OR_RETURN(...) \
29+
CEL_PYTHON_RETURN_IF_ERROR(PyErr_toStatus()); \
30+
CEL_PYTHON_ASSIGN_OR_RETURN(__VA_ARGS__); \
31+
CEL_PYTHON_RETURN_IF_ERROR(PyErr_toStatus());
3232

3333
namespace cel_python {
3434

0 commit comments

Comments
 (0)