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 can be included by other C++ libraries, typically extension modules
32 // which want to interact with the Python Messages coming from the "cpp"
33 // implementation of protocol buffers.
34 //
35 // Usage:
36 // Declare a (probably static) variable to hold the API:
37 // const PyProto_API* py_proto_api;
38 // In some initialization function, write:
39 // py_proto_api = static_cast<const PyProto_API*>(PyCapsule_Import(
40 // PyProtoAPICapsuleName(), 0));
41 // if (!py_proto_api) { ...handle ImportError... }
42 // Then use the methods of the returned class:
43 // py_proto_api->GetMessagePointer(...);
44
45 #ifndef GOOGLE_PROTOBUF_PYTHON_PROTO_API_H__
46 #define GOOGLE_PROTOBUF_PYTHON_PROTO_API_H__
47
48 #define PY_SSIZE_T_CLEAN
49 #include <Python.h>
50
51 #include <google/protobuf/descriptor_database.h>
52 #include <google/protobuf/message.h>
53
54 namespace google {
55 namespace protobuf {
56 namespace python {
57
58 // Note on the implementation:
59 // This API is designed after
60 // https://docs.python.org/3/extending/extending.html#providing-a-c-api-for-an-extension-module
61 // The class below contains no mutable state, and all methods are "const";
62 // we use a C++ class instead of a C struct with functions pointers just because
63 // the code looks more readable.
64 struct PyProto_API {
65 // The API object is created at initialization time and never freed.
66 // This destructor is never called.
~PyProto_APIPyProto_API67 virtual ~PyProto_API() {}
68
69 // Operations on Messages.
70
71 // If the passed object is a Python Message, returns its internal pointer.
72 // Otherwise, returns NULL with an exception set.
73 virtual const Message* GetMessagePointer(PyObject* msg) const = 0;
74
75 // If the passed object is a Python Message, returns a mutable pointer.
76 // Otherwise, returns NULL with an exception set.
77 // This function will succeed only if there are no other Python objects
78 // pointing to the message, like submessages or repeated containers.
79 // With the current implementation, only empty messages are in this case.
80 virtual Message* GetMutableMessagePointer(PyObject* msg) const = 0;
81
82 // If the passed object is a Python Message Descriptor, returns its internal
83 // pointer.
84 // Otherwise, returns NULL with an exception set.
85 virtual const Descriptor* MessageDescriptor_AsDescriptor(
86 PyObject* desc) const = 0;
87
88 // If the passed object is a Python Enum Descriptor, returns its internal
89 // pointer.
90 // Otherwise, returns NULL with an exception set.
91 virtual const EnumDescriptor* EnumDescriptor_AsDescriptor(
92 PyObject* enum_desc) const = 0;
93
94 // Expose the underlying DescriptorPool and MessageFactory to enable C++ code
95 // to create Python-compatible message.
96 virtual const DescriptorPool* GetDefaultDescriptorPool() const = 0;
97 virtual MessageFactory* GetDefaultMessageFactory() const = 0;
98
99 // Allocate a new protocol buffer as a python object for the provided
100 // descriptor. This function works even if no Python module has been imported
101 // for the corresponding protocol buffer class.
102 // The factory is usually null; when provided, it is the MessageFactory which
103 // owns the Python class, and will be used to find and create Extensions for
104 // this message.
105 // When null is returned, a python error has already been set.
106 virtual PyObject* NewMessage(const Descriptor* descriptor,
107 PyObject* py_message_factory) const = 0;
108
109 // Allocate a new protocol buffer where the underlying object is owned by C++.
110 // The factory must currently be null. This function works even if no Python
111 // module has been imported for the corresponding protocol buffer class.
112 // When null is returned, a python error has already been set.
113 //
114 // Since this call returns a python object owned by C++, some operations
115 // are risky, and it must be used carefully. In particular:
116 // * Avoid modifying the returned object from the C++ side while there are
117 // existing python references to it or it's subobjects.
118 // * Avoid using python references to this object or any subobjects after the
119 // C++ object has been freed.
120 // * Calling this with the same C++ pointer will result in multiple distinct
121 // python objects referencing the same C++ object.
122 virtual PyObject* NewMessageOwnedExternally(
123 Message* msg, PyObject* py_message_factory) const = 0;
124
125 // Returns a new reference for the given DescriptorPool.
126 // The returned object does not manage the C++ DescriptorPool: it is the
127 // responsibility of the caller to keep it alive.
128 // As long as the returned Python DescriptorPool object is kept alive,
129 // functions that process C++ descriptors or messages created from this pool
130 // can work and return their Python counterparts.
131 virtual PyObject* DescriptorPool_FromPool(
132 const google::protobuf::DescriptorPool* pool) const = 0;
133 };
134
PyProtoAPICapsuleName()135 inline const char* PyProtoAPICapsuleName() {
136 static const char kCapsuleName[] =
137 "google.protobuf.pyext._message.proto_API";
138 return kCapsuleName;
139 }
140
141 } // namespace python
142 } // namespace protobuf
143 } // namespace google
144
145 #endif // GOOGLE_PROTOBUF_PYTHON_PROTO_API_H__
146