-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce
PYCLIF_PYBIND11_MODULE()
macro.
Proactive prevention of accumulating tech debt: The main purpose of this change is to make it less likely that the boilerplate code usually hidden behind the [`PYBIND11_MODULE()`](https://github.com/pybind/pybind11/blob/8b48ff878c168b51fe5ef7b8c728815b9e1a9857/include/pybind11/detail/common.h#L463-L479) macro leaks into checked-in code when PyCLIF-generated pybind11 bindings are manually converted to pure pybind11 extension modules. This change also makes it much less likely that the auto-generated module docstring and the (currently) unconditional `pybind11_protobuf::ImportNativeProtoCasters()` call leak into checked-in code. GitHub testing: #92 TGP testing is currently not needed (PyCLIF-C-API is still the default). PiperOrigin-RevId: 609818592
- Loading branch information
Showing
2 changed files
with
58 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// Copyright 2024 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#ifndef CLIF_PYBIND11_PYCLIF_PYBIND11_MODULE_MACRO_H_ | ||
#define CLIF_PYBIND11_PYCLIF_PYBIND11_MODULE_MACRO_H_ | ||
|
||
#include "third_party/pybind11/include/pybind11/pybind11.h" | ||
#include "third_party/pybind11_protobuf/native_proto_caster.h" | ||
|
||
#define PYCLIF_PYBIND11_MODULE(CLIF_SOURCE_FILENAME, PYINIT_NAME, MODULE_NAME) \ | ||
extern "C" PyObject* PYINIT_NAME() { \ | ||
PYBIND11_CHECK_PYTHON_VERSION \ | ||
PYBIND11_ENSURE_INTERNALS_READY \ | ||
static pybind11::module_::module_def this_module_def; \ | ||
auto m = pybind11::module_::create_extension_module(MODULE_NAME, nullptr, \ | ||
&this_module_def); \ | ||
try { \ | ||
m.attr("__pyclif_codegen_mode__") = "pybind11"; \ | ||
m.doc() = "CLIF-generated module for " CLIF_SOURCE_FILENAME; \ | ||
pybind11_protobuf::check_unknown_fields:: \ | ||
ExtensionsWithUnknownFieldsPolicy:: \ | ||
WeakEnableFallbackToSerializeParse(); \ | ||
pybind11_protobuf::ImportNativeProtoCasters(); \ | ||
PyclifPybind11ModuleInit(m); \ | ||
return m.ptr(); \ | ||
} \ | ||
PYBIND11_CATCH_INIT_EXCEPTIONS \ | ||
} | ||
|
||
#endif // CLIF_PYBIND11_PYCLIF_PYBIND11_MODULE_MACRO_H_ |