python/google/protobuf/pyext/descriptor_database.cc
Go to the documentation of this file.
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 // https://developers.google.com/protocol-buffers/
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
14 // distribution.
15 // * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 
31 // This file defines a C++ DescriptorDatabase, which wraps a Python Database
32 // and delegate all its operations to Python methods.
33 
35 
41 
42 namespace google {
43 namespace protobuf {
44 namespace python {
45 
47  : py_database_(py_database) {
48  Py_INCREF(py_database_);
49 }
50 
52 
53 // Convert a Python object to a FileDescriptorProto pointer.
54 // Handles all kinds of Python errors, which are simply logged.
55 static bool GetFileDescriptorProto(PyObject* py_descriptor,
57  if (py_descriptor == NULL) {
58  if (PyErr_ExceptionMatches(PyExc_KeyError)) {
59  // Expected error: item was simply not found.
60  PyErr_Clear();
61  } else {
62  GOOGLE_LOG(ERROR) << "DescriptorDatabase method raised an error";
63  PyErr_Print();
64  }
65  return false;
66  }
67  if (py_descriptor == Py_None) {
68  return false;
69  }
70  const Descriptor* filedescriptor_descriptor =
72  CMessage* message = reinterpret_cast<CMessage*>(py_descriptor);
73  if (PyObject_TypeCheck(py_descriptor, CMessage_Type) &&
74  message->message->GetDescriptor() == filedescriptor_descriptor) {
75  // Fast path: Just use the pointer.
76  FileDescriptorProto* file_proto =
77  static_cast<FileDescriptorProto*>(message->message);
78  *output = *file_proto;
79  return true;
80  } else {
81  // Slow path: serialize the message. This allows to use databases which
82  // use a different implementation of FileDescriptorProto.
83  ScopedPyObjectPtr serialized_pb(
84  PyObject_CallMethod(py_descriptor, "SerializeToString", NULL));
85  if (serialized_pb == NULL) {
87  << "DescriptorDatabase method did not return a FileDescriptorProto";
88  PyErr_Print();
89  return false;
90  }
91  char* str;
92  Py_ssize_t len;
93  if (PyBytes_AsStringAndSize(serialized_pb.get(), &str, &len) < 0) {
95  << "DescriptorDatabase method did not return a FileDescriptorProto";
96  PyErr_Print();
97  return false;
98  }
99  FileDescriptorProto file_proto;
100  if (!file_proto.ParseFromArray(str, len)) {
102  << "DescriptorDatabase method did not return a FileDescriptorProto";
103  return false;
104  }
105  *output = file_proto;
106  return true;
107  }
108 }
109 
110 // Find a file by file name.
111 bool PyDescriptorDatabase::FindFileByName(const string& filename,
113  ScopedPyObjectPtr py_descriptor(PyObject_CallMethod(
114  py_database_, "FindFileByName", "s#", filename.c_str(), filename.size()));
115  return GetFileDescriptorProto(py_descriptor.get(), output);
116 }
117 
118 // Find the file that declares the given fully-qualified symbol name.
120  const string& symbol_name, FileDescriptorProto* output) {
121  ScopedPyObjectPtr py_descriptor(
122  PyObject_CallMethod(py_database_, "FindFileContainingSymbol", "s#",
123  symbol_name.c_str(), symbol_name.size()));
124  return GetFileDescriptorProto(py_descriptor.get(), output);
125 }
126 
127 // Find the file which defines an extension extending the given message type
128 // with the given field number.
129 // Python DescriptorDatabases are not required to implement this method.
131  const string& containing_type, int field_number,
133  ScopedPyObjectPtr py_method(
134  PyObject_GetAttrString(py_database_, "FindFileContainingExtension"));
135  if (py_method == NULL) {
136  // This method is not implemented, returns without error.
137  PyErr_Clear();
138  return false;
139  }
140  ScopedPyObjectPtr py_descriptor(
141  PyObject_CallFunction(py_method.get(), "s#i", containing_type.c_str(),
142  containing_type.size(), field_number));
143  return GetFileDescriptorProto(py_descriptor.get(), output);
144 }
145 
146 // Finds the tag numbers used by all known extensions of
147 // containing_type, and appends them to output in an undefined
148 // order.
149 // Python DescriptorDatabases are not required to implement this method.
151  const string& containing_type, std::vector<int>* output) {
152  ScopedPyObjectPtr py_method(
153  PyObject_GetAttrString(py_database_, "FindAllExtensionNumbers"));
154  if (py_method == NULL) {
155  // This method is not implemented, returns without error.
156  PyErr_Clear();
157  return false;
158  }
159  ScopedPyObjectPtr py_list(
160  PyObject_CallFunction(py_method.get(), "s#", containing_type.c_str(),
161  containing_type.size()));
162  if (py_list == NULL) {
163  PyErr_Print();
164  return false;
165  }
166  Py_ssize_t size = PyList_Size(py_list.get());
167  int64 item_value;
168  for (Py_ssize_t i = 0 ; i < size; ++i) {
169  ScopedPyObjectPtr item(PySequence_GetItem(py_list.get(), i));
170  item_value = PyLong_AsLong(item.get());
171  if (item_value < 0) {
173  << "FindAllExtensionNumbers method did not return "
174  << "valid extension numbers.";
175  PyErr_Print();
176  return false;
177  }
178  output->push_back(item_value);
179  }
180  return true;
181 }
182 
183 } // namespace python
184 } // namespace protobuf
185 } // namespace google
google::protobuf::python::PyDescriptorDatabase::FindFileByName
bool FindFileByName(const string &filename, FileDescriptorProto *output)
Definition: python/google/protobuf/pyext/descriptor_database.cc:111
NULL
NULL
Definition: test_security_zap.cpp:405
google::protobuf::int64
int64_t int64
Definition: protobuf/src/google/protobuf/stubs/port.h:151
item
cJSON * item
Definition: cJSON.h:236
google::protobuf::python::CMessage
Definition: python/google/protobuf/pyext/message.h:100
google::protobuf::python::ScopedPythonPtr::get
PyObjectStruct * get() const
Definition: scoped_pyobject_ptr.h:76
google::protobuf::python::GetFileDescriptorProto
static bool GetFileDescriptorProto(PyObject *py_descriptor, FileDescriptorProto *output)
Definition: python/google/protobuf/pyext/descriptor_database.cc:55
google::protobuf::python::PyDescriptorDatabase::py_database_
PyObject * py_database_
Definition: python/google/protobuf/pyext/descriptor_database.h:75
google::protobuf::python::ScopedPythonPtr
Definition: scoped_pyobject_ptr.h:46
google::protobuf::python::PyDescriptorDatabase::~PyDescriptorDatabase
~PyDescriptorDatabase()
Definition: python/google/protobuf/pyext/descriptor_database.cc:51
google::protobuf::python::PyDescriptorDatabase::FindAllExtensionNumbers
bool FindAllExtensionNumbers(const string &containing_type, std::vector< int > *output)
Definition: python/google/protobuf/pyext/descriptor_database.cc:150
google::protobuf::python::CMessage_Type
PyTypeObject * CMessage_Type
Definition: python/google/protobuf/pyext/message.cc:2836
scoped_pyobject_ptr.h
google::protobuf::python::PyDescriptorDatabase::FindFileContainingExtension
bool FindFileContainingExtension(const string &containing_type, int field_number, FileDescriptorProto *output)
Definition: python/google/protobuf/pyext/descriptor_database.cc:130
update_failure_list.str
str
Definition: update_failure_list.py:41
google::protobuf::python::PyDescriptorDatabase::FindFileContainingSymbol
bool FindFileContainingSymbol(const string &symbol_name, FileDescriptorProto *output)
Definition: python/google/protobuf/pyext/descriptor_database.cc:119
FileDescriptorProto::GetDescriptor
static const ::PROTOBUF_NAMESPACE_ID::Descriptor * GetDescriptor()
Definition: descriptor.pb.h:542
GOOGLE_LOG
#define GOOGLE_LOG(LEVEL)
Definition: logging.h:146
size
#define size
Definition: glcorearb.h:2944
FileDescriptorProto
Definition: descriptor.pb.h:501
google::protobuf::ERROR
static const LogLevel ERROR
Definition: protobuf/src/google/protobuf/testing/googletest.h:70
i
int i
Definition: gmock-matchers_test.cc:764
len
int len
Definition: php/ext/google/protobuf/map.c:206
common.h
size
GLsizeiptr size
Definition: glcorearb.h:2943
logging.h
google::protobuf::Descriptor
Definition: src/google/protobuf/descriptor.h:231
google::protobuf::python::PyDescriptorDatabase::PyDescriptorDatabase
PyDescriptorDatabase(PyObject *py_database)
Definition: python/google/protobuf/pyext/descriptor_database.cc:46
FileDescriptorProto::default_instance
static const FileDescriptorProto & default_instance()
Definition: descriptor.pb.cc:1818
message.h
descriptor_database.h
descriptor.pb.h
output
const upb_json_parsermethod const upb_symtab upb_sink * output
Definition: ruby/ext/google/protobuf_c/upb.h:10503
google
Definition: data_proto2_to_proto3_util.h:11
message
GLenum GLuint GLenum GLsizei const GLchar * message
Definition: glcorearb.h:2695


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:50