Skip to content

Commit 20ca4cf

Browse files
committed
already submitted changes
1 parent 4ec6ff7 commit 20ca4cf

File tree

2 files changed

+58
-27
lines changed

2 files changed

+58
-27
lines changed

clif/pybind11/generator.py

+17-27
Original file line numberDiff line numberDiff line change
@@ -181,27 +181,12 @@ def generate_from(self, ast: ast_pb2.AST):
181181

182182
yield 'namespace {'
183183
yield ''
184-
yield 'PyObject * this_module_init() noexcept {'
185-
yield I + 'PYBIND11_CHECK_PYTHON_VERSION'
186-
yield I + 'PYBIND11_ENSURE_INTERNALS_READY'
187-
yield I + ('static pybind11::module_::module_def '
188-
f'module_def_{self._module_name};')
189-
yield I + ('auto m = pybind11::module_::create_extension_module('
190-
f'"{self._module_name}", nullptr, '
191-
f'&module_def_{self._module_name});')
192-
yield I + 'try {'
193-
yield I + I + 'm.attr("__pyclif_codegen_mode__") = "pybind11";'
184+
yield '// When manually converting this code to a pure pybind11 extension,'
185+
yield '// change this function to:'
186+
yield f'// PYBIND11_MODULE({self._module_name}, m)'
187+
yield 'void PyclifPybind11ModuleInit(py::module_ m) {'
194188
for s in self._generate_import_modules(ast):
195189
yield I + s
196-
yield I + I + f'm.doc() = "CLIF-generated module for {ast.source}";'
197-
if self._codegen_info.requires_status:
198-
yield I + I + ('pybind11::module_::import('
199-
'"util.task.python.error");')
200-
yield I + I + 'pybind11_protobuf::check_unknown_fields::'
201-
yield I + I + ' ExtensionsWithUnknownFieldsPolicy::'
202-
yield I + I + ' WeakEnableFallbackToSerializeParse();'
203-
yield I + I + 'pybind11_protobuf::ImportNativeProtoCasters();'
204-
205190
for decl in ast.decls:
206191
if decl.decltype == ast_pb2.Decl.Type.FUNC:
207192
for s in function.generate_from(
@@ -217,26 +202,30 @@ def generate_from(self, ast: ast_pb2.AST):
217202
elif decl.decltype == ast_pb2.Decl.Type.ENUM:
218203
for s in enums.generate_from('m', decl.enum):
219204
yield I + s
220-
yield I + I + 'return m.ptr();'
221-
yield I + '}'
222-
yield I + 'PYBIND11_CATCH_INIT_EXCEPTIONS'
223205
yield '}'
224206
yield ''
225207
yield '} // namespace'
226208
yield ''
227209
mangled_module_name = utils.generate_mangled_name_for_module(
228210
self._module_path)
229-
yield f'extern "C" PyObject* GooglePyInit_{mangled_module_name}() {{'
230-
yield I + 'return this_module_init();'
231-
yield '}'
232-
yield ''
233-
211+
yield '// When manually converting this code to a pure pybind11 extension,'
212+
yield '// remove this macro invocation entirely.'
213+
yield 'PYCLIF_PYBIND11_MODULE('
214+
yield f' "{ast.source}",'
215+
yield f' GooglePyInit_{mangled_module_name},'
216+
yield f' "{self._module_name}")'
217+
218+
insert_empty_line = True
234219
for namespace, typedefs in itertools.groupby(
235220
self._types, lambda gen_type: gen_type.cpp_namespace):
221+
if insert_empty_line:
222+
insert_empty_line = False
223+
yield ''
236224
namespace = namespace.strip(':') or 'clif'
237225
yield ' '.join('namespace %s {' % ns for ns in namespace.split('::'))
238226
for t in typedefs:
239227
yield from t.generate_converters()
228+
yield ''
240229
yield '} ' * (1 + namespace.count('::')) + ' // namespace ' + namespace
241230

242231
def _generate_import_modules(self,
@@ -280,6 +269,7 @@ def _generate_headlines(self):
280269
yield f'#include "{include}"'
281270
yield f'#include "{self._header_path}"'
282271
yield '#include "clif/pybind11/clif_type_casters.h"'
272+
yield '#include "clif/pybind11/pyclif_pybind11_module_macro.h"'
283273
yield '#include "clif/pybind11/runtime.h"'
284274
yield '#include "clif/pybind11/type_casters.h"'
285275
yield '#include "third_party/pybind11_protobuf/native_proto_caster.h"'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright 2024 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#ifndef CLIF_PYBIND11_PYCLIF_PYBIND11_MODULE_MACRO_H_
16+
#define CLIF_PYBIND11_PYCLIF_PYBIND11_MODULE_MACRO_H_
17+
18+
#include "third_party/pybind11/include/pybind11/pybind11.h"
19+
#include "third_party/pybind11_protobuf/native_proto_caster.h"
20+
21+
#define PYCLIF_PYBIND11_MODULE(CLIF_SOURCE_FILENAME, PYINIT_NAME, MODULE_NAME) \
22+
extern "C" PyObject* PYINIT_NAME() { \
23+
PYBIND11_CHECK_PYTHON_VERSION \
24+
PYBIND11_ENSURE_INTERNALS_READY \
25+
static pybind11::module_::module_def this_module_def; \
26+
auto m = pybind11::module_::create_extension_module(MODULE_NAME, nullptr, \
27+
&this_module_def); \
28+
try { \
29+
m.attr("__pyclif_codegen_mode__") = "pybind11"; \
30+
m.doc() = "CLIF-generated module for " CLIF_SOURCE_FILENAME; \
31+
pybind11_protobuf::check_unknown_fields:: \
32+
ExtensionsWithUnknownFieldsPolicy:: \
33+
WeakEnableFallbackToSerializeParse(); \
34+
pybind11_protobuf::ImportNativeProtoCasters(); \
35+
PyclifPybind11ModuleInit(m); \
36+
return m.ptr(); \
37+
} \
38+
PYBIND11_CATCH_INIT_EXCEPTIONS \
39+
}
40+
41+
#endif // CLIF_PYBIND11_PYCLIF_PYBIND11_MODULE_MACRO_H_

0 commit comments

Comments
 (0)