1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 // https://developers.google.com/protocol-buffers/
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
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
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.
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.
38 // This is either 0 for python, 1 for CPP V1, 2 for CPP V2.
40 // 0 is default and is equivalent to
41 // PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python
43 // 1 is set with -DPYTHON_PROTO2_CPP_IMPL_V1 and is equivalent to
44 // PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=cpp
46 // PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION_VERSION=1
48 // 2 is set with -DPYTHON_PROTO2_CPP_IMPL_V2 and is equivalent to
49 // PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=cpp
51 // PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION_VERSION=2
52 #ifdef PYTHON_PROTO2_CPP_IMPL_V1
53 #if PY_MAJOR_VERSION >= 3
54 #error "PYTHON_PROTO2_CPP_IMPL_V1 is not supported under Python 3."
56 static int kImplVersion = 1;
58 #ifdef PYTHON_PROTO2_CPP_IMPL_V2
59 static int kImplVersion = 2;
61 #ifdef PYTHON_PROTO2_PYTHON_IMPL
62 static int kImplVersion = 0;
65 // The defaults are set here. Python 3 uses the fast C++ APIv2 by default.
66 // Python 2 still uses the Python version by default until some compatibility
67 // issues can be worked around.
68 #if PY_MAJOR_VERSION >= 3
69 static int kImplVersion = 2;
71 static int kImplVersion = 0;
74 #endif // PYTHON_PROTO2_PYTHON_IMPL
75 #endif // PYTHON_PROTO2_CPP_IMPL_V2
76 #endif // PYTHON_PROTO2_CPP_IMPL_V1
78 static const char* kImplVersionName = "api_version";
80 static const char* kModuleName = "_api_implementation";
81 static const char kModuleDocstring[] =
82 "_api_implementation is a module that exposes compile-time constants that\n"
83 "determine the default API implementation to use for Python proto2.\n"
85 "It complements api_implementation.py by setting defaults using compile-time\n"
86 "constants defined in C, such that one can set defaults at compilation\n"
87 "(e.g. with blaze flag --copt=-DPYTHON_PROTO2_CPP_IMPL_V2).";
89 #if PY_MAJOR_VERSION >= 3
90 static struct PyModuleDef _module = {
91 PyModuleDef_HEAD_INIT,
101 #define INITFUNC PyInit__api_implementation
102 #define INITFUNC_ERRORVAL NULL
104 #define INITFUNC init_api_implementation
105 #define INITFUNC_ERRORVAL
109 PyMODINIT_FUNC INITFUNC() {
110 #if PY_MAJOR_VERSION >= 3
111 PyObject *module = PyModule_Create(&_module);
113 PyObject *module = Py_InitModule3(
114 const_cast<char*>(kModuleName),
116 const_cast<char*>(kModuleDocstring));
118 if (module == NULL) {
119 return INITFUNC_ERRORVAL;
122 // Adds the module variable "api_version".
123 if (PyModule_AddIntConstant(
125 const_cast<char*>(kImplVersionName),
127 #if PY_MAJOR_VERSION < 3
130 { Py_DECREF(module); return NULL; }
137 } // namespace python
138 } // namespace protobuf
139 } // namespace google