FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
cel-python/cel_expr_python/py_descriptor_database.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_descriptor_database.cc
Copy path
More file actions
More file actions
Latest commit
History
History
History
219 lines (194 loc) · 6.79 KB
Breadcrumbs
cel-python
/
cel_expr_python
/
py_descriptor_database.cc
Copy path
File metadata and controls
219 lines (194 loc) · 6.79 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*
* 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
*
* http://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.
*/
#
define
PY_SSIZE_T_CLEAN
#
include
"
cel_expr_python/py_descriptor_database.h
"
#
include
<
Python.h
>
//
IWYU pragma: keep - Needed for PyObject
#
include
<
cstdint
>
#
include
"
absl/log/absl_check.h
"
#
include
"
common/minimal_descriptor_pool.h
"
#
include
"
cel_expr_python/py_error_status.h
"
#
include
"
google/protobuf/descriptor.h
"
#
include
<
pybind11/pybind11.h
>
namespace
cel_python
{
namespace
py
=
pybind11;
PyDescriptorDatabase::PyDescriptorDatabase
(PyObject* py_descriptor_pool)
: py_descriptor_pool_(py_descriptor_pool),
standard_pool_
(cel::GetMinimalDescriptorPool()) {
ABSL_CHECK
(
PyGILState_Check
());
Py_XINCREF
(py_descriptor_pool_);
}
PyDescriptorDatabase::~PyDescriptorDatabase
() {
if
(py_descriptor_pool_ ==
nullptr
) {
return
;
}
if
(!
PyGILState_Check
()) {
py::gil_scoped_acquire acquire;
Py_XDECREF
(py_descriptor_pool_);
}
else
{
Py_XDECREF
(py_descriptor_pool_);
}
}
//
Find a file by file name. Fills in in *output and returns true if found.
//
Otherwise, returns false, leaving the contents of *output undefined.
bool
PyDescriptorDatabase::FindFileByName
(StringViewArg filename,
google::protobuf::FileDescriptorProto* output) {
const
google::protobuf::FileDescriptor* file = standard_pool_.
FindFileByName
(filename);
if
(file !=
nullptr
) {
file->
CopyTo
(output);
return
true
;
}
if
(py_descriptor_pool_ ==
nullptr
) {
return
false
;
}
py::gil_scoped_acquire acquire;
PyObject* pyfile =
PyObject_CallMethod
(
py_descriptor_pool_,
"
FindFileByName
"
,
"
s#
"
, filename.
data
(),
static_cast
<Py_ssize_t>(filename.
size
()));
if
(pyfile ==
nullptr
) {
//
Ignore KeyError raised by the method, if any
if
(
PyErr_Occurred
() == PyExc_KeyError) {
PyErr_Clear
();
}
else
{
PyErr_noteAndClear
();
}
return
false
;
}
PyObject* pyfile_serialized =
PyObject_GetAttrString
(pyfile,
"
serialized_pb
"
);
Py_DECREF
(pyfile);
if
(pyfile_serialized ==
nullptr
) {
PyErr_Format
(PyExc_TypeError,
"
Python file has no attribute 'serialized_pb'
"
);
return
false
;
}
bool
ok = output->
ParseFromArray
(
reinterpret_cast
<
uint8_t
*>(
PyBytes_AS_STRING
(pyfile_serialized)),
PyBytes_GET_SIZE
(pyfile_serialized));
if
(!ok) {
PyErr_Format
(PyExc_RuntimeError,
"
Failed to parse descriptor for %s
"
,
filename.
data
());
PyErr_noteAndClear
();
}
Py_DECREF
(pyfile_serialized);
return
ok;
}
//
Find the file that declares the given fully-qualified symbol name.
//
If found, fills in *output and returns true, otherwise returns false
//
and leaves *output undefined.
bool
PyDescriptorDatabase::FindFileContainingSymbol
(
StringViewArg symbol_name, google::protobuf::FileDescriptorProto* output) {
const
google::protobuf::FileDescriptor* file =
standard_pool_.
FindFileContainingSymbol
(symbol_name);
if
(file !=
nullptr
) {
file->
CopyTo
(output);
return
true
;
}
if
(py_descriptor_pool_ ==
nullptr
) {
return
false
;
}
py::gil_scoped_acquire acquire;
PyObject* pyfile =
PyObject_CallMethod
(
py_descriptor_pool_,
"
FindFileContainingSymbol
"
,
"
s#
"
, symbol_name.
data
(),
static_cast
<Py_ssize_t>(symbol_name.
size
()));
if
(pyfile ==
nullptr
) {
//
Ignore KeyError raised by the method, if any
if
(
PyErr_Occurred
() == PyExc_KeyError) {
PyErr_Clear
();
}
else
{
PyErr_noteAndClear
();
}
return
false
;
}
PyObject* pyfile_serialized =
PyObject_GetAttrString
(pyfile,
"
serialized_pb
"
);
Py_DECREF
(pyfile);
if
(pyfile_serialized ==
nullptr
) {
PyErr_Format
(PyExc_TypeError,
"
Python file has no attribute 'serialized_pb'
"
);
return
false
;
}
bool
ok = output->
ParseFromArray
(
reinterpret_cast
<
uint8_t
*>(
PyBytes_AS_STRING
(pyfile_serialized)),
PyBytes_GET_SIZE
(pyfile_serialized));
if
(!ok) {
PyErr_Format
(PyExc_RuntimeError,
"
Failed to parse descriptor for %s
"
,
symbol_name.
data
());
}
Py_DECREF
(pyfile_serialized);
return
ok;
}
//
Find the file which defines an extension extending the given message type
//
with the given field number. If found, fills in *output and returns true,
//
otherwise returns false and leaves *output undefined. containing_type
//
must be a fully-qualified type name.
bool
PyDescriptorDatabase::FindFileContainingExtension
(
StringViewArg containing_type,
int
field_number,
google::protobuf::FileDescriptorProto* output) {
if
(py_descriptor_pool_ ==
nullptr
) {
return
false
;
}
py::gil_scoped_acquire acquire;
PyObject* py_containing_type =
PyObject_CallMethod
(
py_descriptor_pool_,
"
FindMessageTypeByName
"
,
"
s#
"
,
containing_type.
data
(),
static_cast
<Py_ssize_t>(containing_type.
size
()));
if
(py_containing_type ==
nullptr
) {
//
Ignore KeyError raised by the method, if any
if
(
PyErr_Occurred
() == PyExc_KeyError) {
PyErr_Clear
();
}
else
{
PyErr_noteAndClear
();
}
return
false
;
}
PyObject* ext_desc =
PyObject_CallMethod
(py_descriptor_pool_,
"
FindExtensionByNumber
"
,
"
Oi
"
,
py_containing_type, field_number);
if
(ext_desc ==
nullptr
) {
//
Ignore KeyError raised by the method, if any
if
(
PyErr_Occurred
() == PyExc_KeyError) {
PyErr_Clear
();
}
else
{
PyErr_noteAndClear
();
}
return
false
;
}
PyObject* pyfile =
PyObject_GetAttrString
(ext_desc,
"
file
"
);
Py_DECREF
(ext_desc);
if
(pyfile ==
nullptr
) {
PyErr_Format
(PyExc_TypeError,
"
Extension descriptor has no attribute 'file'
"
);
PyErr_noteAndClear
();
return
false
;
}
PyObject* pyfile_serialized =
PyObject_GetAttrString
(pyfile,
"
serialized_pb
"
);
Py_DECREF
(pyfile);
if
(pyfile_serialized ==
nullptr
) {
PyErr_Format
(PyExc_TypeError,
"
Python file has no attribute 'serialized_pb'
"
);
PyErr_noteAndClear
();
return
false
;
}
bool
ok = output->
ParseFromArray
(
reinterpret_cast
<
uint8_t
*>(
PyBytes_AS_STRING
(pyfile_serialized)),
PyBytes_GET_SIZE
(pyfile_serialized));
if
(!ok) {
PyErr_Format
(PyExc_RuntimeError,
"
Failed to parse descriptor for %s, extension %d
"
,
containing_type.
data
(), field_number);
PyErr_noteAndClear
();
}
Py_DECREF
(pyfile_serialized);
return
ok;
}
}
//
namespace cel_python
Back
|
FazBrowse Home
|
New Git URL