blob: 7c4df47f4577882f6310afe605c760541e06756c [file] [log] [blame]
Austin Schuh40c16522018-10-28 20:27:54 -07001// 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/message.h>
34
35#include <google/protobuf/message_lite.h>
36
37static PyObject* GetPythonProto3PreserveUnknownsDefault(
38 PyObject* /*m*/, PyObject* /*args*/) {
39 if (google::protobuf::internal::GetProto3PreserveUnknownsDefault()) {
40 Py_RETURN_TRUE;
41 } else {
42 Py_RETURN_FALSE;
43 }
44}
45
46static PyObject* SetPythonProto3PreserveUnknownsDefault(
47 PyObject* /*m*/, PyObject* arg) {
48 if (!arg || !PyBool_Check(arg)) {
49 PyErr_SetString(
50 PyExc_TypeError,
51 "Argument to SetPythonProto3PreserveUnknownsDefault must be boolean");
52 return NULL;
53 }
54 google::protobuf::internal::SetProto3PreserveUnknownsDefault(PyObject_IsTrue(arg));
55 Py_RETURN_NONE;
56}
57
58static const char module_docstring[] =
59"python-proto2 is a module that can be used to enhance proto2 Python API\n"
60"performance.\n"
61"\n"
62"It provides access to the protocol buffers C++ reflection API that\n"
63"implements the basic protocol buffer functions.";
64
65static PyMethodDef ModuleMethods[] = {
66 {"SetAllowOversizeProtos",
67 (PyCFunction)google::protobuf::python::cmessage::SetAllowOversizeProtos,
68 METH_O, "Enable/disable oversize proto parsing."},
69 // DO NOT USE: For migration and testing only.
70 {"GetPythonProto3PreserveUnknownsDefault",
71 (PyCFunction)GetPythonProto3PreserveUnknownsDefault,
72 METH_NOARGS, "Get Proto3 preserve unknowns default."},
73 // DO NOT USE: For migration and testing only.
74 {"SetPythonProto3PreserveUnknownsDefault",
75 (PyCFunction)SetPythonProto3PreserveUnknownsDefault,
76 METH_O, "Enable/disable proto3 unknowns preservation."},
77 { NULL, NULL}
78};
79
80#if PY_MAJOR_VERSION >= 3
81static struct PyModuleDef _module = {
82 PyModuleDef_HEAD_INIT,
83 "_message",
84 module_docstring,
85 -1,
86 ModuleMethods, /* m_methods */
87 NULL,
88 NULL,
89 NULL,
90 NULL
91};
92#define INITFUNC PyInit__message
93#define INITFUNC_ERRORVAL NULL
94#else // Python 2
95#define INITFUNC init_message
96#define INITFUNC_ERRORVAL
97#endif
98
99extern "C" {
100 PyMODINIT_FUNC INITFUNC(void) {
101 PyObject* m;
102#if PY_MAJOR_VERSION >= 3
103 m = PyModule_Create(&_module);
104#else
105 m = Py_InitModule3("_message", ModuleMethods,
106 module_docstring);
107#endif
108 if (m == NULL) {
109 return INITFUNC_ERRORVAL;
110 }
111
112 if (!google::protobuf::python::InitProto2MessageModule(m)) {
113 Py_DECREF(m);
114 return INITFUNC_ERRORVAL;
115 }
116
117#if PY_MAJOR_VERSION >= 3
118 return m;
119#endif
120 }
121}