• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <Python.h>
32 
33 #include <google/protobuf/pyext/descriptor_pool.h>
34 #include <google/protobuf/pyext/message.h>
35 #include <google/protobuf/pyext/message_factory.h>
36 #include <google/protobuf/proto_api.h>
37 
38 #include <google/protobuf/message_lite.h>
39 
40 namespace {
41 
42 // C++ API.  Clients get at this via proto_api.h
43 struct ApiImplementation : google::protobuf::python::PyProto_API {
GetMessagePointer__anon9cbd9e670111::ApiImplementation44   const google::protobuf::Message* GetMessagePointer(PyObject* msg) const override {
45     return google::protobuf::python::PyMessage_GetMessagePointer(msg);
46   }
GetMutableMessagePointer__anon9cbd9e670111::ApiImplementation47   google::protobuf::Message* GetMutableMessagePointer(PyObject* msg) const override {
48     return google::protobuf::python::PyMessage_GetMutableMessagePointer(msg);
49   }
GetDefaultDescriptorPool__anon9cbd9e670111::ApiImplementation50   const google::protobuf::DescriptorPool* GetDefaultDescriptorPool() const override {
51     return google::protobuf::python::GetDefaultDescriptorPool()->pool;
52   }
53 
GetDefaultMessageFactory__anon9cbd9e670111::ApiImplementation54   google::protobuf::MessageFactory* GetDefaultMessageFactory() const override {
55     return google::protobuf::python::GetDefaultDescriptorPool()
56         ->py_message_factory->message_factory;
57   }
58 };
59 
60 }  // namespace
61 
62 static const char module_docstring[] =
63     "python-proto2 is a module that can be used to enhance proto2 Python API\n"
64     "performance.\n"
65     "\n"
66     "It provides access to the protocol buffers C++ reflection API that\n"
67     "implements the basic protocol buffer functions.";
68 
69 static PyMethodDef ModuleMethods[] = {
70     {"SetAllowOversizeProtos",
71      (PyCFunction)google::protobuf::python::cmessage::SetAllowOversizeProtos, METH_O,
72      "Enable/disable oversize proto parsing."},
73     // DO NOT USE: For migration and testing only.
74     {NULL, NULL}};
75 
76 #if PY_MAJOR_VERSION >= 3
77 static struct PyModuleDef _module = {PyModuleDef_HEAD_INIT,
78                                      "_message",
79                                      module_docstring,
80                                      -1,
81                                      ModuleMethods, /* m_methods */
82                                      NULL,
83                                      NULL,
84                                      NULL,
85                                      NULL};
86 #define INITFUNC PyInit__message
87 #define INITFUNC_ERRORVAL NULL
88 #else  // Python 2
89 #define INITFUNC init_message
90 #define INITFUNC_ERRORVAL
91 #endif
92 
INITFUNC()93 PyMODINIT_FUNC INITFUNC() {
94   PyObject* m;
95 #if PY_MAJOR_VERSION >= 3
96   m = PyModule_Create(&_module);
97 #else
98   m = Py_InitModule3("_message", ModuleMethods, module_docstring);
99 #endif
100   if (m == NULL) {
101     return INITFUNC_ERRORVAL;
102   }
103 
104   if (!google::protobuf::python::InitProto2MessageModule(m)) {
105     Py_DECREF(m);
106     return INITFUNC_ERRORVAL;
107   }
108 
109   // Adds the C++ API
110   if (PyObject* api = PyCapsule_New(
111           new ApiImplementation(), google::protobuf::python::PyProtoAPICapsuleName(),
112           [](PyObject* o) {
113             delete (ApiImplementation*)PyCapsule_GetPointer(
114                 o, google::protobuf::python::PyProtoAPICapsuleName());
115           })) {
116     PyModule_AddObject(m, "proto_API", api);
117   } else {
118     return INITFUNC_ERRORVAL;
119   }
120 
121 #if PY_MAJOR_VERSION >= 3
122   return m;
123 #endif
124 }
125