• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc.  All rights reserved.
3 //
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file or at
6 // https://developers.google.com/open-source/licenses/bsd
7 
8 #define PY_SSIZE_T_CLEAN
9 #include <Python.h>
10 
11 #include "google/protobuf/message_lite.h"
12 #include "google/protobuf/pyext/descriptor.h"
13 #include "google/protobuf/pyext/descriptor_pool.h"
14 #include "google/protobuf/pyext/message.h"
15 #include "google/protobuf/pyext/message_factory.h"
16 #include "google/protobuf/proto_api.h"
17 
18 namespace {
19 
20 // C++ API.  Clients get at this via proto_api.h
21 struct ApiImplementation : google::protobuf::python::PyProto_API {
GetMessagePointer__anon19fcf8680111::ApiImplementation22   const google::protobuf::Message* GetMessagePointer(PyObject* msg) const override {
23     return google::protobuf::python::PyMessage_GetMessagePointer(msg);
24   }
GetMutableMessagePointer__anon19fcf8680111::ApiImplementation25   google::protobuf::Message* GetMutableMessagePointer(PyObject* msg) const override {
26     return google::protobuf::python::PyMessage_GetMutableMessagePointer(msg);
27   }
MessageDescriptor_AsDescriptor__anon19fcf8680111::ApiImplementation28   const google::protobuf::Descriptor* MessageDescriptor_AsDescriptor(
29       PyObject* desc) const override {
30     return google::protobuf::python::PyMessageDescriptor_AsDescriptor(desc);
31   }
EnumDescriptor_AsDescriptor__anon19fcf8680111::ApiImplementation32   const google::protobuf::EnumDescriptor* EnumDescriptor_AsDescriptor(
33       PyObject* enum_desc) const override {
34     return google::protobuf::python::PyEnumDescriptor_AsDescriptor(enum_desc);
35   }
36 
GetDefaultDescriptorPool__anon19fcf8680111::ApiImplementation37   const google::protobuf::DescriptorPool* GetDefaultDescriptorPool() const override {
38     return google::protobuf::python::GetDefaultDescriptorPool()->pool;
39   }
40 
GetDefaultMessageFactory__anon19fcf8680111::ApiImplementation41   google::protobuf::MessageFactory* GetDefaultMessageFactory() const override {
42     return google::protobuf::python::GetDefaultDescriptorPool()
43         ->py_message_factory->message_factory;
44   }
NewMessage__anon19fcf8680111::ApiImplementation45   PyObject* NewMessage(const google::protobuf::Descriptor* descriptor,
46                        PyObject* py_message_factory) const override {
47     return google::protobuf::python::PyMessage_New(descriptor, py_message_factory);
48   }
NewMessageOwnedExternally__anon19fcf8680111::ApiImplementation49   PyObject* NewMessageOwnedExternally(
50       google::protobuf::Message* msg, PyObject* py_message_factory) const override {
51     return google::protobuf::python::PyMessage_NewMessageOwnedExternally(
52         msg, py_message_factory);
53   }
DescriptorPool_FromPool__anon19fcf8680111::ApiImplementation54   PyObject* DescriptorPool_FromPool(
55       const google::protobuf::DescriptorPool* pool) const override {
56     return google::protobuf::python::PyDescriptorPool_FromPool(pool);
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     {nullptr, nullptr}};
75 
76 static struct PyModuleDef _module = {PyModuleDef_HEAD_INIT,
77                                      "_message",
78                                      module_docstring,
79                                      -1,
80                                      ModuleMethods, /* m_methods */
81                                      nullptr,
82                                      nullptr,
83                                      nullptr,
84                                      nullptr};
85 
PyInit__message()86 PyMODINIT_FUNC PyInit__message() {
87   PyObject* m;
88   m = PyModule_Create(&_module);
89   if (m == nullptr) {
90     return nullptr;
91   }
92 
93   if (!google::protobuf::python::InitProto2MessageModule(m)) {
94     Py_DECREF(m);
95     return nullptr;
96   }
97 
98   // Adds the C++ API
99   if (PyObject* api = PyCapsule_New(
100           new ApiImplementation(), google::protobuf::python::PyProtoAPICapsuleName(),
101           [](PyObject* o) {
102             delete (ApiImplementation*)PyCapsule_GetPointer(
103                 o, google::protobuf::python::PyProtoAPICapsuleName());
104           })) {
105     PyModule_AddObject(m, "proto_API", api);
106   } else {
107     return nullptr;
108   }
109 
110   return m;
111 }
112