message_module.cc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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/pyext/descriptor_pool.h>
  32. #include <google/protobuf/pyext/message.h>
  33. #include <google/protobuf/pyext/message_factory.h>
  34. #include <google/protobuf/proto_api.h>
  35. #include <google/protobuf/message_lite.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. };
  53. } // namespace
  54. static const char module_docstring[] =
  55. "python-proto2 is a module that can be used to enhance proto2 Python API\n"
  56. "performance.\n"
  57. "\n"
  58. "It provides access to the protocol buffers C++ reflection API that\n"
  59. "implements the basic protocol buffer functions.";
  60. static PyMethodDef ModuleMethods[] = {
  61. {"SetAllowOversizeProtos",
  62. (PyCFunction)google::protobuf::python::cmessage::SetAllowOversizeProtos, METH_O,
  63. "Enable/disable oversize proto parsing."},
  64. // DO NOT USE: For migration and testing only.
  65. {NULL, NULL}};
  66. #if PY_MAJOR_VERSION >= 3
  67. static struct PyModuleDef _module = {PyModuleDef_HEAD_INIT,
  68. "_message",
  69. module_docstring,
  70. -1,
  71. ModuleMethods, /* m_methods */
  72. NULL,
  73. NULL,
  74. NULL,
  75. NULL};
  76. #define INITFUNC PyInit__message
  77. #define INITFUNC_ERRORVAL NULL
  78. #else // Python 2
  79. #define INITFUNC init_message
  80. #define INITFUNC_ERRORVAL
  81. #endif
  82. PyMODINIT_FUNC INITFUNC() {
  83. PyObject* m;
  84. #if PY_MAJOR_VERSION >= 3
  85. m = PyModule_Create(&_module);
  86. #else
  87. m = Py_InitModule3("_message", ModuleMethods, module_docstring);
  88. #endif
  89. if (m == NULL) {
  90. return INITFUNC_ERRORVAL;
  91. }
  92. if (!google::protobuf::python::InitProto2MessageModule(m)) {
  93. Py_DECREF(m);
  94. return INITFUNC_ERRORVAL;
  95. }
  96. // Adds the C++ API
  97. if (PyObject* api =
  98. PyCapsule_New(new ApiImplementation(),
  99. google::protobuf::python::PyProtoAPICapsuleName(), NULL)) {
  100. PyModule_AddObject(m, "proto_API", api);
  101. } else {
  102. return INITFUNC_ERRORVAL;
  103. }
  104. #if PY_MAJOR_VERSION >= 3
  105. return m;
  106. #endif
  107. }