blob: 514722b4c383b7ce455d1334eadd40e34ee91519 [file] [log] [blame]
Feng Xiaoe841bac2015-12-11 17:09:20 -08001// 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
34#include <google/protobuf/pyext/descriptor_database.h>
35
36#include <google/protobuf/stubs/logging.h>
37#include <google/protobuf/stubs/common.h>
38#include <google/protobuf/descriptor.pb.h>
39#include <google/protobuf/pyext/message.h>
40#include <google/protobuf/pyext/scoped_pyobject_ptr.h>
41
42namespace google {
43namespace protobuf {
44namespace python {
45
46PyDescriptorDatabase::PyDescriptorDatabase(PyObject* py_database)
47 : py_database_(py_database) {
48 Py_INCREF(py_database_);
49}
50
51PyDescriptorDatabase::~PyDescriptorDatabase() { Py_DECREF(py_database_); }
52
53// Convert a Python object to a FileDescriptorProto pointer.
54// Handles all kinds of Python errors, which are simply logged.
55static bool GetFileDescriptorProto(PyObject* py_descriptor,
56 FileDescriptorProto* output) {
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 const Descriptor* filedescriptor_descriptor =
68 FileDescriptorProto::default_instance().GetDescriptor();
69 CMessage* message = reinterpret_cast<CMessage*>(py_descriptor);
70 if (PyObject_TypeCheck(py_descriptor, &CMessage_Type) &&
71 message->message->GetDescriptor() == filedescriptor_descriptor) {
72 // Fast path: Just use the pointer.
73 FileDescriptorProto* file_proto =
74 static_cast<FileDescriptorProto*>(message->message);
75 *output = *file_proto;
76 return true;
77 } else {
78 // Slow path: serialize the message. This allows to use databases which
79 // use a different implementation of FileDescriptorProto.
80 ScopedPyObjectPtr serialized_pb(
81 PyObject_CallMethod(py_descriptor, "SerializeToString", NULL));
82 if (serialized_pb == NULL) {
83 GOOGLE_LOG(ERROR)
84 << "DescriptorDatabase method did not return a FileDescriptorProto";
85 PyErr_Print();
86 return false;
87 }
88 char* str;
89 Py_ssize_t len;
90 if (PyBytes_AsStringAndSize(serialized_pb.get(), &str, &len) < 0) {
91 GOOGLE_LOG(ERROR)
92 << "DescriptorDatabase method did not return a FileDescriptorProto";
93 PyErr_Print();
94 return false;
95 }
96 FileDescriptorProto file_proto;
97 if (!file_proto.ParseFromArray(str, len)) {
98 GOOGLE_LOG(ERROR)
99 << "DescriptorDatabase method did not return a FileDescriptorProto";
100 return false;
101 }
102 *output = file_proto;
103 return true;
104 }
105}
106
107// Find a file by file name.
108bool PyDescriptorDatabase::FindFileByName(const string& filename,
109 FileDescriptorProto* output) {
110 ScopedPyObjectPtr py_descriptor(PyObject_CallMethod(
111 py_database_, "FindFileByName", "s#", filename.c_str(), filename.size()));
112 return GetFileDescriptorProto(py_descriptor.get(), output);
113}
114
115// Find the file that declares the given fully-qualified symbol name.
116bool PyDescriptorDatabase::FindFileContainingSymbol(
117 const string& symbol_name, FileDescriptorProto* output) {
118 ScopedPyObjectPtr py_descriptor(
119 PyObject_CallMethod(py_database_, "FindFileContainingSymbol", "s#",
120 symbol_name.c_str(), symbol_name.size()));
121 return GetFileDescriptorProto(py_descriptor.get(), output);
122}
123
124// Find the file which defines an extension extending the given message type
125// with the given field number.
126// Python DescriptorDatabases are not required to implement this method.
127bool PyDescriptorDatabase::FindFileContainingExtension(
128 const string& containing_type, int field_number,
129 FileDescriptorProto* output) {
130 ScopedPyObjectPtr py_method(
131 PyObject_GetAttrString(py_database_, "FindFileContainingExtension"));
132 if (py_method == NULL) {
133 // This method is not implemented, returns without error.
134 PyErr_Clear();
135 return false;
136 }
137 ScopedPyObjectPtr py_descriptor(
138 PyObject_CallFunction(py_method.get(), "s#i", containing_type.c_str(),
139 containing_type.size(), field_number));
140 return GetFileDescriptorProto(py_descriptor.get(), output);
141}
142
143} // namespace python
144} // namespace protobuf
145} // namespace google