Skip to content

Commit c6962a2

Browse files
Fix make_generator function
1 parent ae1dbd0 commit c6962a2

2 files changed

Lines changed: 18 additions & 13 deletions

File tree

src/mobase/wrappers/basic_classes.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <format>
66
#include <memory>
77

8+
#include <pybind11_utils/generator.h>
89
#include <uibase/executableinfo.h>
910
#include <uibase/filemapping.h>
1011
#include <uibase/game_features/igamefeatures.h>
@@ -215,7 +216,8 @@ namespace mo2::python {
215216
py::class_<IExecutablesList>(m, "IExecutablesList")
216217
.def("executables",
217218
[](IExecutablesList* executablesList) {
218-
return make_generator(executablesList->executables());
219+
return make_generator(executablesList->executables(),
220+
py::return_value_policy::reference);
219221
})
220222
.def("getByTitle", &IExecutablesList::getByTitle, "title"_a)
221223
.def("getByBinary", &IExecutablesList::getByBinary, "info"_a)

src/pybind11-utils/include/pybind11_utils/generator.h

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ namespace mo2::python {
2222

2323
// create a Python generator from a C++ generator
2424
//
25-
template <typename T>
26-
auto make_generator(std::generator<T> g)
25+
template <typename T, typename... Args>
26+
auto make_generator(std::generator<T> g, Args&&... args)
2727
{
2828
using state = detail::generator_state<T>;
2929

@@ -34,16 +34,19 @@ namespace mo2::python {
3434
[](state& s) -> state& {
3535
return s;
3636
})
37-
.def("__next__", [](state& s) -> decltype(auto) {
38-
if (s.it != s.g.end()) {
39-
decltype(auto) v = *s.it;
40-
s.it++;
41-
return v;
42-
}
43-
else {
44-
throw py::stop_iteration();
45-
}
46-
});
37+
.def(
38+
"__next__",
39+
[](state& s) -> T {
40+
if (s.it != s.g.end()) {
41+
decltype(auto) v = *s.it;
42+
s.it++;
43+
return v;
44+
}
45+
else {
46+
throw py::stop_iteration();
47+
}
48+
},
49+
std::forward<Args>(args)...);
4750
}
4851

4952
return py::cast(state{std::move(g)});

0 commit comments

Comments
 (0)