| // Protocol Buffers - Google's data interchange format |
| // Copyright 2008 Google Inc. All rights reserved. |
| // |
| // Use of this source code is governed by a BSD-style |
| // license that can be found in the LICENSE file or at |
| // https://developers.google.com/open-source/licenses/bsd |
| |
| #include <Python.h> |
| |
| namespace google { |
| namespace protobuf { |
| namespace python { |
| |
| // Version constant. |
| // This is either 0 for python, 1 for CPP V1, 2 for CPP V2. |
| // |
| // 0 is default and is equivalent to |
| // PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python |
| // |
| // 1 is set with -DPYTHON_PROTO2_CPP_IMPL_V1 and is equivalent to |
| // PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=cpp |
| // and |
| // PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION_VERSION=1 |
| // |
| // 2 is set with -DPYTHON_PROTO2_CPP_IMPL_V2 and is equivalent to |
| // PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=cpp |
| // and |
| // PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION_VERSION=2 |
| #ifdef PYTHON_PROTO2_CPP_IMPL_V1 |
| #error "PYTHON_PROTO2_CPP_IMPL_V1 is no longer supported." |
| #else |
| #ifdef PYTHON_PROTO2_CPP_IMPL_V2 |
| static int kImplVersion = 2; |
| #else |
| #ifdef PYTHON_PROTO2_PYTHON_IMPL |
| static int kImplVersion = 0; |
| #else |
| |
| static int kImplVersion = -1; // -1 means "Unspecified by compiler flags". |
| |
| #endif // PYTHON_PROTO2_PYTHON_IMPL |
| #endif // PYTHON_PROTO2_CPP_IMPL_V2 |
| #endif // PYTHON_PROTO2_CPP_IMPL_V1 |
| |
| static const char* kImplVersionName = "api_version"; |
| |
| static const char* kModuleName = "_api_implementation"; |
| static const char kModuleDocstring[] = |
| "_api_implementation is a module that exposes compile-time constants that\n" |
| "determine the default API implementation to use for Python proto2.\n" |
| "\n" |
| "It complements api_implementation.py by setting defaults using compile-time\n" |
| "constants defined in C, such that one can set defaults at compilation\n" |
| "(e.g. with blaze flag --copt=-DPYTHON_PROTO2_CPP_IMPL_V2)."; |
| |
| #if PY_MAJOR_VERSION >= 3 |
| static struct PyModuleDef _module = { |
| PyModuleDef_HEAD_INIT, |
| kModuleName, |
| kModuleDocstring, |
| -1, |
| NULL, |
| NULL, |
| NULL, |
| NULL, |
| NULL |
| }; |
| #define INITFUNC PyInit__api_implementation |
| #define INITFUNC_ERRORVAL NULL |
| #else |
| #define INITFUNC init_api_implementation |
| #define INITFUNC_ERRORVAL |
| #endif |
| |
| extern "C" { |
| PyMODINIT_FUNC INITFUNC() { |
| #if PY_MAJOR_VERSION >= 3 |
| PyObject *module = PyModule_Create(&_module); |
| #else |
| PyObject *module = Py_InitModule3( |
| const_cast<char*>(kModuleName), |
| NULL, |
| const_cast<char*>(kModuleDocstring)); |
| #endif |
| if (module == NULL) { |
| return INITFUNC_ERRORVAL; |
| } |
| |
| // Adds the module variable "api_version". |
| if (PyModule_AddIntConstant( |
| module, |
| const_cast<char*>(kImplVersionName), |
| kImplVersion)) |
| #if PY_MAJOR_VERSION < 3 |
| return; |
| #else |
| { Py_DECREF(module); return NULL; } |
| |
| return module; |
| #endif |
| } |
| } |
| |
| } // namespace python |
| } // namespace protobuf |
| } // namespace google |