diff --git a/python/google/protobuf/link_error_fast_cpp.cc b/python/google/protobuf/link_error_fast_cpp.cc new file mode 100644 index 0000000000..d1b7ebb211 --- /dev/null +++ b/python/google/protobuf/link_error_fast_cpp.cc @@ -0,0 +1,23 @@ +// go/fastpythonproto + +namespace google { +namespace protobuf { +namespace python { + +// This exists solely to cause a build/link time error when both +// :use_fast_cpp_protos and :use_pure_python are specified in a build.; + +extern "C" { + +// MUST match link_error_pure_python.cc's function signature. + +__attribute__((noinline)) __attribute__((optnone)) int +go_SLASH_build_deps_on_BOTH_use_fast_cpp_protos_AND_use_pure_python() { + return 1; +} + +} // extern "C" + +} // namespace python +} // namespace protobuf +} // namespace google diff --git a/python/google/protobuf/link_error_pure_python.cc b/python/google/protobuf/link_error_pure_python.cc new file mode 100644 index 0000000000..978e897a65 --- /dev/null +++ b/python/google/protobuf/link_error_pure_python.cc @@ -0,0 +1,23 @@ +// go/fastpythonproto + +namespace google { +namespace protobuf { +namespace python { + +// This exists solely to cause a build/link time error when both +// :use_fast_cpp_protos and :use_pure_python are specified in a build.; + +extern "C" { + +// MUST match link_error_pure_python.cc's function signature. + +__attribute__((noinline)) __attribute__((optnone)) int +go_SLASH_build_deps_on_BOTH_use_fast_cpp_protos_AND_use_pure_python() { + return 0; +} + +} // extern "C" + +} // namespace python +} // namespace protobuf +} // namespace google diff --git a/python/google/protobuf/link_error_upb.cc b/python/google/protobuf/link_error_upb.cc new file mode 100644 index 0000000000..2225a10dc2 --- /dev/null +++ b/python/google/protobuf/link_error_upb.cc @@ -0,0 +1,24 @@ +// go/fastpythonproto + +namespace google { +namespace protobuf { +namespace python { + +// This exists solely to cause a build/link time error when either +// :use_fast_cpp_protos and :use_pure_python are specified in a build +// in addition to :use_upb_protos.; + +extern "C" { + +// MUST match link_error_pure_python.cc's function signature. + +__attribute__((noinline)) __attribute__((optnone)) int +go_SLASH_build_deps_on_BOTH_use_fast_cpp_protos_AND_use_pure_python() { + return 2; +} + +} // extern "C" + +} // namespace python +} // namespace protobuf +} // namespace google diff --git a/python/google/protobuf/use_fast_cpp_protos.cc b/python/google/protobuf/use_fast_cpp_protos.cc new file mode 100644 index 0000000000..21cde5dbe9 --- /dev/null +++ b/python/google/protobuf/use_fast_cpp_protos.cc @@ -0,0 +1,40 @@ +// go/fastpythonproto +#include + +namespace google { +namespace protobuf { +namespace python { + +static const char* kModuleName = "_use_fast_cpp_protos"; +static const char kModuleDocstring[] = + "The presence of this module in a build's deps signals to\n" + "net.google.protobuf.internal.api_implementation that the fast\n" + "C++ protobuf implementation should be the default.\n"; + +static struct PyModuleDef _module = {PyModuleDef_HEAD_INIT, + kModuleName, + kModuleDocstring, + -1, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr}; + +extern "C" { + + +PyMODINIT_FUNC PyInit__use_fast_cpp_protos() { + PyObject* module = PyModule_Create(&_module); + if (module == nullptr) { + return nullptr; + } + + return module; +} + +} // extern "C" + +} // namespace python +} // namespace protobuf +} // namespace google diff --git a/python/google/protobuf/use_pure_python.cc b/python/google/protobuf/use_pure_python.cc new file mode 100644 index 0000000000..48c9d89151 --- /dev/null +++ b/python/google/protobuf/use_pure_python.cc @@ -0,0 +1,40 @@ +// The opposite side of go/fastpythonproto. +#include + +namespace google { +namespace protobuf { +namespace python { + +static const char* kModuleName = "use_pure_python"; +static const char kModuleDocstring[] = + "The presence of this module in a build's deps signals to\n" + "net.google.protobuf.internal.api_implementation that the pure\n" + "Python protobuf implementation should be used.\n"; + +static struct PyModuleDef _module = {PyModuleDef_HEAD_INIT, + kModuleName, + kModuleDocstring, + -1, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr}; + +extern "C" { + + +PyMODINIT_FUNC PyInit_use_pure_python() { + PyObject* module = PyModule_Create(&_module); + if (module == nullptr) { + return nullptr; + } + + + return module; +} +} + +} // namespace python +} // namespace protobuf +} // namespace google diff --git a/python/google/protobuf/use_upb_protos.cc b/python/google/protobuf/use_upb_protos.cc new file mode 100644 index 0000000000..f6eacd1151 --- /dev/null +++ b/python/google/protobuf/use_upb_protos.cc @@ -0,0 +1,40 @@ +// go/fastpythonproto +#include + +namespace google { +namespace protobuf { +namespace python { + +static const char* kModuleName = "_use_upb_protos"; +static const char kModuleDocstring[] = + "The presence of this module in a build's deps signals to\n" + "net.google.protobuf.internal.api_implementation that the upb\n" + "protobuf implementation should be the default.\n"; + +static struct PyModuleDef _module = {PyModuleDef_HEAD_INIT, + kModuleName, + kModuleDocstring, + -1, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr}; + +extern "C" { + + +PyMODINIT_FUNC PyInit__use_upb_protos() { + PyObject* module = PyModule_Create(&_module); + if (module == nullptr) { + return nullptr; + } + + return module; +} + +} // extern "C" + +} // namespace python +} // namespace protobuf +} // namespace google diff --git a/python/pb_unit_tests/pyproto_test_wrapper.bzl b/python/pb_unit_tests/pyproto_test_wrapper.bzl index 5691e886fa..bd5d0c0704 100644 --- a/python/pb_unit_tests/pyproto_test_wrapper.bzl +++ b/python/pb_unit_tests/pyproto_test_wrapper.bzl @@ -35,7 +35,7 @@ def pyproto_test_wrapper(name, deps = []): # main = src, # deps = [ # "//third_party/py/google/protobuf/internal:" + name + "_for_deps", -# "//net/proto2/python/public:use_upb_protos", +# "//third_party/py/google/protobuf:use_upb_protos", # ], # target_compatible_with = select({ # "@platforms//os:windows": ["@platforms//:incompatible"],