Skip to content

Commit c8198ef

Browse files
Fix stuff
1 parent 080e565 commit c8198ef

File tree

4 files changed

+33
-18
lines changed

4 files changed

+33
-18
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ Headers are installed to `<top level>/include`
119119

120120
Compiled pybind modules are installed into `<original module path>/__pybind__`
121121

122+
For C++ usage, you need only the headers, the compiled code is for pybind/python usage only
123+
122124
For the example below, `common_object_model/common_object_model/v1/common/__pybind__/foo.cpython-311-darwin.so` will
123125
be installed (obviously with corresponding qualifiers for Linux/Windows). `get_pybind_value()` searches this
124126
directory
@@ -286,6 +288,7 @@ will generate the following files:
286288
.value("SUNDAY", Weekday::SUNDAY);
287289

288290
py::class_<DCFoo>(m, "DCFoo")
291+
.def(py::init<>())
289292
.def(py::init<std::optional<std::string>, int>(), py::arg("my_string"), py::arg("my_int"))
290293
.def_readwrite("my_string", &DCFoo::my_string)
291294
.def_readwrite("my_int", &DCFoo::my_int);
@@ -296,13 +299,15 @@ will generate the following files:
296299
.def_readwrite("my_day", &Foo::my_day);
297300

298301
py::class_<Bar>(m, "Bar")
302+
.def(py::init<>())
299303
.def(py::init<std::string, bool, Weekday, int, std::optional<std::string>>(), py::arg("my_string"), py::arg("my_bool")=true,
300304
py::arg("my_day")=SUNDAY, py::arg("my_int")=123, py::arg("my_optional_string")=std::nullopt)
301305
.def_readwrite("my_string", &Bar::my_string)
302306
.def_readwrite("my_int", &Bar::my_int)
303307
.def_readwrite("my_optional_string", &Bar::my_optional_string);
304308

305309
py::class_<Baz>(m, "Baz")
310+
.def(py::init<>())
306311
.def(py::init<DCFoo, Foo, std::chrono::system_clock::time_point, std::variant<std::string, double>>(), py::arg("my_dc_foo"),
307312
py::arg("my_foo"), py::arg("my_date"), py::arg("my_variant")=123.0)
308313
.def_readwrite("my_dc_foo", &Baz::my_dc_foo)

pydantic_bind/base.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -269,11 +269,12 @@ def __init__(self, **kwargs):
269269
self.__pybind_impl = pybind_type(**kwargs)
270270

271271

272-
def __dc_init(init):
272+
def __dataclass_init(init):
273273
@wraps(init)
274274
def wrapper(self, *args, __pybind_impl__=None, **kwargs):
275-
self.__pybind_impl = __pybind_impl__ or get_pybind_type(type(self))()
276-
return init(self, *args, **kwargs)
275+
self.pybind_impl = __pybind_impl__ or get_pybind_type(type(self))()
276+
init(self, *args, **kwargs)
277+
arse = True
277278

278279
return wrapper
279280

@@ -288,7 +289,7 @@ def dataclass(cls=None, /, *, init=True, repr=True, eq=True, order=False,
288289
for name, field in ret.__dataclass_fields__.items():
289290
setattr(cls, name, property(fget=_getter(name, field.type), fset=_setter(name, field.type)))
290291

291-
ret.__init__ = __dc_init(ret.__init__)
292+
ret.__init__ = __dataclass_init(ret.__init__)
292293
ret.__no_copy__ = True
293294

294295
return ret

pydantic_bind/cpp_generator.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ def generate_class(model_class: ModelMetaclass, indent_size: int = 0, max_width:
198198
cls_name = model_class.__name__
199199
bases = f" : public {', '.join(b.__name__ for b in base_init.keys())}" if base_init else ""
200200
default_constructor = ""
201+
default_pydantic_init = ""
201202
indent = " " * indent_size
202203
newline = "\n"
203204
newline_indent = f"{newline} {indent}"
@@ -209,6 +210,7 @@ def generate_class(model_class: ModelMetaclass, indent_size: int = 0, max_width:
209210

210211
if default_init_args:
211212
default_constructor = f"{indent}{cls_name}() :\n"
213+
default_pydantic_init = f"{indent}.def(py::init<>())\n"
212214

213215
if base_init:
214216
default_constructor += \
@@ -244,8 +246,7 @@ def generate_class(model_class: ModelMetaclass, indent_size: int = 0, max_width:
244246
pydantic_bases = ", " + ", ".join(base.__name__ for base in base_init.keys()) if base_init else ""
245247
pydantic_init = "\n".join(args_wrapper.wrap(f"{', '.join(types)}>(), {', '.join(kwargs)}"))
246248
pydantic_def = f"""{indent}py::class_<{cls_name}{pydantic_bases}>(m, "{cls_name}")
247-
{indent}.def(py::init<>())
248-
{indent}.def(py::init<{pydantic_init})
249+
{default_pydantic_init}{indent}.def(py::init<{pydantic_init})
249250
{indent}{newline_indent.join(pydantic_attrs)};"""
250251

251252
return struct_def, pydantic_def, tuple(f"#include {i}" for i in sorted(all_includes))
@@ -271,8 +272,9 @@ def generate_enum(enum_typ: EnumType, indent_size: int = 0, max_width: int = 110
271272
def generate_module(module_name: str, output_dir: str, indent_size: int = 4, max_width: int = 110):
272273
single_newline = "\n"
273274
double_newline = "\n\n"
274-
dot = r'.'
275-
slash = r'/'
275+
dot = r"."
276+
slash = r"/"
277+
indent = " " * indent_size
276278

277279
module = import_module(module_name)
278280
generated_root = Path(output_dir)
@@ -304,9 +306,17 @@ def generate_module(module_name: str, output_dir: str, indent_size: int = 4, max
304306
if self_include in includes:
305307
includes.remove(self_include)
306308

309+
imports = []
310+
for include in (i for i in includes if namespace in i):
311+
import_parts = include.split(slash)
312+
import_parts.insert(-2, "__pybind__")
313+
imprt = '.'.join(import_parts).replace('#include ', '').replace('.h', '')
314+
imports.append(f"{indent}py::module_::import({imprt});")
315+
307316
enum_contents = f"\n{double_newline.join(enum_defs)}{single_newline if struct_defs else ''}" if enum_defs else ""
308317
struct_contents = f"\n{double_newline.join(struct_defs)}" if struct_defs else ""
309318
include_contents = f"\n{single_newline.join(includes)}\n" if includes else ""
319+
import_contents = f"\n{single_newline.join(imports)}\n" if imports else ""
310320

311321
header_contents = f"""
312322
#ifndef {guard}
@@ -331,7 +341,7 @@ def generate_module(module_name: str, output_dir: str, indent_size: int = 4, max
331341
332342
333343
PYBIND11_MODULE({module_base_name}, m)
334-
{{
344+
{{{import_contents}
335345
{double_newline.join(pydantic_defs)}
336346
}}
337347
"""

pydantic_bind/include/msgpack/msgpack.h

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,14 @@ namespace msgpack {
199199
pack_type(value.time_since_epoch().count());
200200
}
201201

202+
template<class T>
203+
void pack_type(const std::optional<T> &value) {
204+
if (!value)
205+
serialized_object.emplace_back(nil);
206+
else
207+
pack_type(*value);
208+
}
209+
202210
template<class T>
203211
void pack_array(const T &array) {
204212
if (array.size() < 16) {
@@ -514,15 +522,6 @@ namespace msgpack {
514522
}
515523
}
516524

517-
template<>
518-
inline
519-
void Packer::pack_type(const std::optional<T> &value) {
520-
if (!value)
521-
serialized_object.emplace_back(nil);
522-
else
523-
pack_type(*value);
524-
}
525-
526525
class Unpacker {
527526
public:
528527
Unpacker() : data_pointer(nullptr), data_end(nullptr) {};

0 commit comments

Comments
 (0)