2727#include " absl/strings/str_format.h"
2828#include " absl/types/span.h"
2929#include " common/value.h"
30+ #include " runtime/embedder_context.h"
3031#include " runtime/function.h"
3132#include " py_cel_env.h"
3233#include " py_cel_type.h"
@@ -65,12 +66,9 @@ PyCelFunction::~PyCelFunction() {
6566 PyGILState_Release (gil_state);
6667};
6768
68- PyCelFunctionAdapter::PyCelFunctionAdapter (const std::shared_ptr<PyCelEnv>& env,
69- std::string function_name,
69+ PyCelFunctionAdapter::PyCelFunctionAdapter (std::string function_name,
7070 PyObject* py_function)
71- : env_(env),
72- function_name_ (std::move(function_name)),
73- py_function_(py_function) {
71+ : function_name_(std::move(function_name)), py_function_(py_function) {
7472 Py_XINCREF (py_function_);
7573}
7674
@@ -84,13 +82,18 @@ absl::StatusOr<cel::Value> PyCelFunctionAdapter::Invoke(
8482 absl::Span<const cel::Value> args,
8583 const cel::Function::InvokeContext& context) const {
8684 ABSL_CHECK (PyGILState_Check ());
85+ ABSL_CHECK (context.embedder_context ());
86+
87+ std::shared_ptr<PyCelEnv> env =
88+ *context.embedder_context ()->Get <std::shared_ptr<PyCelEnv>*>();
89+ ABSL_CHECK (env);
8790
8891 PY_CEL_ASSIGN_OR_RETURN (auto py_arena,
8992 PyCelArena::FromProtoArena (context.arena ()));
9093 PyObject* py_args = PyTuple_New (args.size ());
9194 for (int i = 0 ; i < args.size (); ++i) {
9295 PyTuple_SetItem (py_args, i,
93- CelValueToPyObject (args[i], env_ , py_arena,
96+ CelValueToPyObject (args[i], env , py_arena,
9497 /* plain_value=*/ true ));
9598 }
9699 PyObject* result = PyObject_CallObject (py_function_, py_args);
@@ -105,7 +108,7 @@ absl::StatusOr<cel::Value> PyCelFunctionAdapter::Invoke(
105108 return absl::StrFormat (" Python function '%s'" ,
106109 PyUnicode_AsUTF8 (PyObject_Repr (py_function_)));
107110 },
108- env_ , context.arena ());
111+ env , context.arena ());
109112};
110113
111114} // namespace cel_python
0 commit comments