blob: 6db12e8dc676579657103e327495422f94b807a2 [file] [log] [blame]
Brian Silverman9c614bc2016-02-15 20:20:02 -05001// 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
33namespace google {
34namespace protobuf {
35namespace python {
36
37// Version constant.
38// This is either 0 for python, 1 for CPP V1, 2 for CPP V2.
39//
40// 0 is default and is equivalent to
41// PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python
42//
43// 1 is set with -DPYTHON_PROTO2_CPP_IMPL_V1 and is equivalent to
44// PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=cpp
45// and
46// PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION_VERSION=1
47//
48// 2 is set with -DPYTHON_PROTO2_CPP_IMPL_V2 and is equivalent to
49// PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=cpp
50// and
51// PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION_VERSION=2
52#ifdef PYTHON_PROTO2_CPP_IMPL_V1
53#error "PYTHON_PROTO2_CPP_IMPL_V1 is no longer supported."
54#else
55#ifdef PYTHON_PROTO2_CPP_IMPL_V2
56static int kImplVersion = 2;
57#else
58#ifdef PYTHON_PROTO2_PYTHON_IMPL
59static int kImplVersion = 0;
60#else
61
62static int kImplVersion = -1; // -1 means "Unspecified by compiler flags".
63
64#endif // PYTHON_PROTO2_PYTHON_IMPL
65#endif // PYTHON_PROTO2_CPP_IMPL_V2
66#endif // PYTHON_PROTO2_CPP_IMPL_V1
67
68static const char* kImplVersionName = "api_version";
69
70static const char* kModuleName = "_api_implementation";
71static const char kModuleDocstring[] =
72"_api_implementation is a module that exposes compile-time constants that\n"
73"determine the default API implementation to use for Python proto2.\n"
74"\n"
75"It complements api_implementation.py by setting defaults using compile-time\n"
76"constants defined in C, such that one can set defaults at compilation\n"
77"(e.g. with blaze flag --copt=-DPYTHON_PROTO2_CPP_IMPL_V2).";
78
79#if PY_MAJOR_VERSION >= 3
80static struct PyModuleDef _module = {
81 PyModuleDef_HEAD_INIT,
82 kModuleName,
83 kModuleDocstring,
84 -1,
85 NULL,
86 NULL,
87 NULL,
88 NULL,
89 NULL
90};
91#define INITFUNC PyInit__api_implementation
92#define INITFUNC_ERRORVAL NULL
93#else
94#define INITFUNC init_api_implementation
95#define INITFUNC_ERRORVAL
96#endif
97
98extern "C" {
99 PyMODINIT_FUNC INITFUNC() {
100#if PY_MAJOR_VERSION >= 3
101 PyObject *module = PyModule_Create(&_module);
102#else
103 PyObject *module = Py_InitModule3(
104 const_cast<char*>(kModuleName),
105 NULL,
106 const_cast<char*>(kModuleDocstring));
107#endif
108 if (module == NULL) {
109 return INITFUNC_ERRORVAL;
110 }
111
112 // Adds the module variable "api_version".
113 if (PyModule_AddIntConstant(
114 module,
115 const_cast<char*>(kImplVersionName),
116 kImplVersion))
117#if PY_MAJOR_VERSION < 3
118 return;
119#else
120 { Py_DECREF(module); return NULL; }
121
122 return module;
123#endif
124 }
125}
126
127} // namespace python
128} // namespace protobuf
129} // namespace google