1 // Protocol Buffers - Google's data interchange format 2 // Copyright 2008 Google Inc. All rights reserved. 3 // 4 // Use of this source code is governed by a BSD-style 5 // license that can be found in the LICENSE file or at 6 // https://developers.google.com/open-source/licenses/bsd 7 8 #include <Python.h> 9 10 namespace google { 11 namespace protobuf { 12 namespace python { 13 14 // Version constant. 15 // This is either 0 for python, 1 for CPP V1, 2 for CPP V2. 16 // 17 // 0 is default and is equivalent to 18 // PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python 19 // 20 // 1 is set with -DPYTHON_PROTO2_CPP_IMPL_V1 and is equivalent to 21 // PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=cpp 22 // and 23 // PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION_VERSION=1 24 // 25 // 2 is set with -DPYTHON_PROTO2_CPP_IMPL_V2 and is equivalent to 26 // PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=cpp 27 // and 28 // PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION_VERSION=2 29 #ifdef PYTHON_PROTO2_CPP_IMPL_V1 30 #error "PYTHON_PROTO2_CPP_IMPL_V1 is no longer supported." 31 #else 32 #ifdef PYTHON_PROTO2_CPP_IMPL_V2 33 static int kImplVersion = 2; 34 #else 35 #ifdef PYTHON_PROTO2_PYTHON_IMPL 36 static int kImplVersion = 0; 37 #else 38 39 static int kImplVersion = -1; // -1 means "Unspecified by compiler flags". 40 41 #endif // PYTHON_PROTO2_PYTHON_IMPL 42 #endif // PYTHON_PROTO2_CPP_IMPL_V2 43 #endif // PYTHON_PROTO2_CPP_IMPL_V1 44 45 static const char* kImplVersionName = "api_version"; 46 47 static const char* kModuleName = "_api_implementation"; 48 static const char kModuleDocstring[] = 49 "_api_implementation is a module that exposes compile-time constants that\n" 50 "determine the default API implementation to use for Python proto2.\n" 51 "\n" 52 "It complements api_implementation.py by setting defaults using compile-time\n" 53 "constants defined in C, such that one can set defaults at compilation\n" 54 "(e.g. with bazel flag --copt=-DPYTHON_PROTO2_CPP_IMPL_V2)."; 55 56 #if PY_MAJOR_VERSION >= 3 57 static struct PyModuleDef _module = { 58 PyModuleDef_HEAD_INIT, 59 kModuleName, 60 kModuleDocstring, 61 -1, 62 NULL, 63 NULL, 64 NULL, 65 NULL, 66 NULL 67 }; 68 #define INITFUNC PyInit__api_implementation 69 #define INITFUNC_ERRORVAL NULL 70 #else 71 #define INITFUNC init_api_implementation 72 #define INITFUNC_ERRORVAL 73 #endif 74 75 extern "C" { INITFUNC()76 PyMODINIT_FUNC INITFUNC() { 77 #if PY_MAJOR_VERSION >= 3 78 PyObject *module = PyModule_Create(&_module); 79 #else 80 PyObject *module = Py_InitModule3( 81 const_cast<char*>(kModuleName), 82 NULL, 83 const_cast<char*>(kModuleDocstring)); 84 #endif 85 if (module == NULL) { 86 return INITFUNC_ERRORVAL; 87 } 88 89 // Adds the module variable "api_version". 90 if (PyModule_AddIntConstant( 91 module, 92 const_cast<char*>(kImplVersionName), 93 kImplVersion)) 94 #if PY_MAJOR_VERSION < 3 95 return; 96 #else 97 { Py_DECREF(module); return NULL; } 98 99 return module; 100 #endif 101 } 102 } 103 104 } // namespace python 105 } // namespace protobuf 106 } // namespace google 107