FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
cel-python/cel_expr_python/py_cel_function.cc at main · cel-expr/cel-python · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
cel-expr
/
cel-python
Public
Notifications
You must be signed in to change notification settings
Fork
6
Star
80
Code
Issues
3
Pull requests
10
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
cel-python
/
cel_expr_python
/
py_cel_function.cc
Copy path
More file actions
More file actions
Latest commit
History
History
History
117 lines (101 loc) · 4.03 KB
Breadcrumbs
cel-python
/
cel_expr_python
/
py_cel_function.cc
Copy path
File metadata and controls
117 lines (101 loc) · 4.03 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
//
Copyright 2025 Google LLC
//
//
Licensed under the Apache License, Version 2.0 (the "License");
//
you may not use this file except in compliance with the License.
//
You may obtain a copy of the License at
//
//
https://www.apache.org/licenses/LICENSE-2.0
//
//
Unless required by applicable law or agreed to in writing, software
//
distributed under the License is distributed on an "AS IS" BASIS,
//
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//
See the License for the specific language governing permissions and
//
limitations under the License.
#
include
"
cel_expr_python/py_cel_function.h
"
#
include
<
Python.h
>
#
include
<
memory
>
#
include
<
string
>
#
include
<
utility
>
#
include
<
vector
>
#
include
"
absl/log/absl_check.h
"
#
include
"
absl/status/status.h
"
#
include
"
absl/status/statusor.h
"
#
include
"
absl/strings/str_format.h
"
#
include
"
absl/types/span.h
"
#
include
"
common/value.h
"
#
include
"
runtime/embedder_context.h
"
#
include
"
runtime/function.h
"
#
include
"
cel_expr_python/py_cel_arena.h
"
#
include
"
cel_expr_python/py_cel_env_internal.h
"
#
include
"
cel_expr_python/py_cel_type.h
"
#
include
"
cel_expr_python/py_cel_value.h
"
#
include
"
cel_expr_python/py_error_status.h
"
#
include
"
cel_expr_python/status_macros.h
"
#
include
<
pybind11/pybind11.h
>
#
include
<
pybind11/stl.h
>
namespace
cel_python
{
namespace
py
=
::pybind11;
namespace
{
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<PyCelEnvInternal>*>();
}
}
//
namespace
void
PyCelFunction::DefinePythonBindings
(pybind11::
module
& m) {
py::class_<PyCelFunction, std::shared_ptr<PyCelFunction>>(m,
"
Function
"
)
.
def
(py::init<std::string, std::vector<PyCelType>,
bool
, py::object,
PyCelType>(),
py::arg
(
"
function_name
"
),
py::arg
(
"
parameters
"
),
py::arg
(
"
is_member
"
),
py::arg
(
"
impl
"
),
py::arg
(
"
return_type
"
) =
PyCelType::Dyn
());
}
PyCelFunction::PyCelFunction
(std::string function_name,
std::vector<PyCelType> parameters,
bool
is_member,
py::object impl, PyCelType return_type)
: function_name_(std::move(function_name)),
return_type_
(std::move(return_type)),
parameters_(std::move(parameters)),
is_member_(is_member),
impl_(std::move(impl)) {
ABSL_CHECK
(
PyGILState_Check
());
}
PyCelFunctionAdapter::PyCelFunctionAdapter
(std::string function_name,
PyCelType return_type,
py::object py_function)
: function_name_(std::move(function_name)),
return_type_(std::move(return_type)),
py_function_(std::move(py_function)) {}
absl::StatusOr<cel::Value>
PyCelFunctionAdapter::Invoke
(
absl::Span<
const
cel::Value> args,
const
cel::Function::InvokeContext& context)
const
{
ABSL_CHECK
(
PyGILState_Check
());
std::shared_ptr<PyCelEnvInternal> env =
GetEnvFromContext
(context);
CEL_PYTHON_ASSIGN_OR_RETURN
(
auto
py_arena,
PyCelArena::FromProtoArena
(context.
arena
()));
PyObject* py_args =
PyTuple_New
(args.
size
());
for
(
int
i =
0
; i < args.
size
(); ++i) {
PyTuple_SetItem
(py_args, i,
CelValueToPyObject
(args[i], env, py_arena,
/*
plain_value=
*/
true
));
}
PyObject* result =
PyObject_CallObject
(py_function_.
ptr
(), py_args);
Py_DECREF
(py_args);
absl::Status status =
PyErr_toStatus
();
if
(!status.
ok
()) {
Py_XDECREF
(result);
return
cel::ErrorValue
(status);
}
absl::StatusOr<cel::Value> cel_result =
PyObjectToCelValue
(
result, return_type_,
[
this
]() {
return
absl::StrFormat
(
"
Python function '%s'
"
,
PyUnicode_AsUTF8
(
PyObject_Repr
(py_function_.
ptr
())));
},
env, context.
arena
());
Py_XDECREF
(result);
return
cel_result;
}
}
//
namespace cel_python
Back
|
FazBrowse Home
|
New Git URL