message_module.cc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. #include <Python.h>
  31. #include <google/protobuf/message_lite.h>
  32. #include <google/protobuf/pyext/descriptor_pool.h>
  33. #include <google/protobuf/pyext/message.h>
  34. #include <google/protobuf/pyext/message_factory.h>
  35. #include <google/protobuf/proto_api.h>
  36. namespace {
  37. // C++ API. Clients get at this via proto_api.h
  38. struct ApiImplementation : google::protobuf::python::PyProto_API {
  39. const google::protobuf::Message* GetMessagePointer(PyObject* msg) const override {
  40. return google::protobuf::python::PyMessage_GetMessagePointer(msg);
  41. }
  42. google::protobuf::Message* GetMutableMessagePointer(PyObject* msg) const override {
  43. return google::protobuf::python::PyMessage_GetMutableMessagePointer(msg);
  44. }
  45. const google::protobuf::DescriptorPool* GetDefaultDescriptorPool() const override {
  46. return google::protobuf::python::GetDefaultDescriptorPool()->pool;
  47. }
  48. google::protobuf::MessageFactory* GetDefaultMessageFactory() const override {
  49. return google::protobuf::python::GetDefaultDescriptorPool()
  50. ->py_message_factory->message_factory;
  51. }
  52. PyObject* NewMessage(const google::protobuf::Descriptor* descriptor,
  53. PyObject* py_message_factory) const override {
  54. return google::protobuf::python::PyMessage_New(descriptor, py_message_factory);
  55. }
  56. PyObject* NewMessageOwnedExternally(
  57. google::protobuf::Message* msg, PyObject* py_message_factory) const override {
  58. return google::protobuf::python::PyMessage_NewMessageOwnedExternally(
  59. msg, py_message_factory);
  60. }
  61. };
  62. } // namespace
  63. static const char module_docstring[] =
  64. "python-proto2 is a module that can be used to enhance proto2 Python API\n"
  65. "performance.\n"
  66. "\n"
  67. "It provides access to the protocol buffers C++ reflection API that\n"
  68. "implements the basic protocol buffer functions.";
  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. #if PY_MAJOR_VERSION >= 3
  76. static struct PyModuleDef _module = {PyModuleDef_HEAD_INIT,
  77. "_message",
  78. module_docstring,
  79. -1,
  80. ModuleMethods, /* m_methods */
  81. NULL,
  82. NULL,
  83. NULL,
  84. NULL};
  85. #define INITFUNC PyInit__message
  86. #define INITFUNC_ERRORVAL NULL
  87. #else // Python 2
  88. #define INITFUNC init_message
  89. #define INITFUNC_ERRORVAL
  90. #endif
  91. PyMODINIT_FUNC INITFUNC() {
  92. PyObject* m;
  93. #if PY_MAJOR_VERSION >= 3
  94. m = PyModule_Create(&_module);
  95. #else
  96. m = Py_InitModule3("_message", ModuleMethods, module_docstring);
  97. #endif
  98. if (m == NULL) {
  99. return INITFUNC_ERRORVAL;
  100. }
  101. if (!google::protobuf::python::InitProto2MessageModule(m)) {
  102. Py_DECREF(m);
  103. return INITFUNC_ERRORVAL;
  104. }
  105. // Adds the C++ API
  106. if (PyObject* api = PyCapsule_New(
  107. new ApiImplementation(), google::protobuf::python::PyProtoAPICapsuleName(),
  108. [](PyObject* o) {
  109. delete (ApiImplementation*)PyCapsule_GetPointer(
  110. o, google::protobuf::python::PyProtoAPICapsuleName());
  111. })) {
  112. PyModule_AddObject(m, "proto_API", api);
  113. } else {
  114. return INITFUNC_ERRORVAL;
  115. }
  116. #if PY_MAJOR_VERSION >= 3
  117. return m;
  118. #endif
  119. }