2014-08-12 21:10:30 +00:00
|
|
|
// Protocol Buffers - Google's data interchange format
|
|
|
|
|
// Copyright 2008 Google Inc. All rights reserved.
|
|
|
|
|
//
|
2023-09-08 18:03:16 -07:00
|
|
|
// 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
|
2014-08-12 21:10:30 +00:00
|
|
|
|
|
|
|
|
#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
|
2015-02-25 16:39:11 -08:00
|
|
|
#error "PYTHON_PROTO2_CPP_IMPL_V1 is no longer supported."
|
2014-08-12 21:10:30 +00:00
|
|
|
#else
|
|
|
|
|
#ifdef PYTHON_PROTO2_CPP_IMPL_V2
|
|
|
|
|
static int kImplVersion = 2;
|
|
|
|
|
#else
|
|
|
|
|
#ifdef PYTHON_PROTO2_PYTHON_IMPL
|
|
|
|
|
static int kImplVersion = 0;
|
|
|
|
|
#else
|
|
|
|
|
|
2015-02-25 16:39:11 -08:00
|
|
|
static int kImplVersion = -1; // -1 means "Unspecified by compiler flags".
|
2014-08-12 21:10:30 +00:00
|
|
|
|
|
|
|
|
#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[] =
|
2022-12-08 16:50:05 -08:00
|
|
|
"_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"
|
2024-07-22 07:24:22 -07:00
|
|
|
"(e.g. with bazel flag --copt=-DPYTHON_PROTO2_CPP_IMPL_V2).";
|
2014-08-12 21:10:30 +00:00
|
|
|
|
2022-12-08 16:50:05 -08:00
|
|
|
#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
|
2014-08-12 21:10:30 +00:00
|
|
|
|
|
|
|
|
extern "C" {
|
2022-12-08 16:50:05 -08:00
|
|
|
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;
|
|
|
|
|
}
|
2014-08-12 21:10:30 +00:00
|
|
|
|
2025-10-28 18:46:25 -07:00
|
|
|
#ifdef Py_GIL_DISABLED
|
|
|
|
|
if (PyUnstable_Module_SetGIL(module, Py_MOD_GIL_NOT_USED) != 0) {
|
|
|
|
|
Py_DECREF(module);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
#endif // Py_GIL_DISABLED
|
|
|
|
|
|
2022-12-08 16:50:05 -08:00
|
|
|
// 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; }
|
2014-08-12 21:10:30 +00:00
|
|
|
|
2022-12-08 16:50:05 -08:00
|
|
|
return module;
|
|
|
|
|
#endif
|
|
|
|
|
}
|
2014-08-12 21:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace python
|
|
|
|
|
} // namespace protobuf
|
|
|
|
|
} // namespace google
|