GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — cel-python/cel_expr_python/py_descriptor_database.cc at main · mmulqueen/cel-python Expand file tree/ Copy pathpy_descriptor_database.ccMore file actions 210 lines (186 loc) · 6.63 KB / Copy pathpy_descriptor_database.ccFile metadata and controls210 lines (186 loc) · 6.63 KB 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 /* * 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" namespace cel_python { 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() { auto gil_state = PyGILState_Ensure(); Py_XDECREF(py_descriptor_pool_); PyGILState_Release(gil_state); } // 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) { ABSL_CHECK(PyGILState_Check()); const google::protobuf::FileDescriptor* file = standard_pool_.FindFileByName(filename); if (file != nullptr) { file->CopyTo(output); return true; } if (py_descriptor_pool_ == nullptr) { return false; } 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) { ABSL_CHECK(PyGILState_Check()); 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; } 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; } ABSL_CHECK(PyGILState_Check()); 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 |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |