message_module.cc 3.7 KB

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