Skip to content

Commit 44edbff

Browse files
committed
fix(python): fix compile errors encountered
1 parent 94882a7 commit 44edbff

File tree

2 files changed

+27
-29
lines changed

2 files changed

+27
-29
lines changed

binding/exceptions_binding.cpp

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ void bind_exceptions(py::module &m) {
2626

2727
m.def("severity_to_string", &SeverityToString, "Convert ErrorSeverity to string");
2828

29-
// Bind BaseException
30-
py::exception<BaseException>(m, "BaseException")
29+
// Bind BaseException as regular class
30+
py::class_<BaseException>(m, "BaseException")
3131
.def(py::init<std::string_view, ErrorSeverity>(), py::arg("message"),
3232
py::arg("severity") = ErrorSeverity::kError)
33-
.def("what", &BaseException::what)
33+
.def("what", [](const BaseException &e) { return std::string(e.what()); })
3434
.def("get_severity", &BaseException::GetSeverity)
3535
.def("get_location", &BaseException::GetLocation, py::return_value_policy::reference_internal)
3636
.def("get_formatted_message", &BaseException::GetFormattedMessage)
@@ -40,7 +40,7 @@ void bind_exceptions(py::module &m) {
4040
});
4141

4242
// Bind ValidationException
43-
py::exception<ValidationException>(m, "ValidationException", m.attr("BaseException"))
43+
py::class_<ValidationException, BaseException>(m, "ValidationException")
4444
.def(py::init<std::string_view, std::optional<std::string_view>>(), py::arg("message"),
4545
py::arg("field_name") = std::nullopt)
4646
.def("get_field_name", &ValidationException::GetFieldName)
@@ -52,7 +52,7 @@ void bind_exceptions(py::module &m) {
5252
});
5353

5454
// Bind ResourceException
55-
py::exception<ResourceException>(m, "ResourceException", m.attr("BaseException"))
55+
py::class_<ResourceException, BaseException>(m, "ResourceException")
5656
.def(py::init<std::string_view, std::optional<std::string_view>>(), py::arg("message"),
5757
py::arg("resource_name") = std::nullopt)
5858
.def("get_resource_name", &ResourceException::GetResourceName)
@@ -64,7 +64,7 @@ void bind_exceptions(py::module &m) {
6464
});
6565

6666
// Bind CalculationException
67-
py::exception<CalculationException>(m, "CalculationException", m.attr("BaseException"))
67+
py::class_<CalculationException, BaseException>(m, "CalculationException")
6868
.def(py::init<std::string_view, double>(), py::arg("message"), py::arg("input_value") = 0.0)
6969
.def("get_input_value", &CalculationException::GetInputValue)
7070
.def("__repr__", [](const CalculationException &e) {
@@ -77,16 +77,19 @@ void bind_exceptions(py::module &m) {
7777
.def(py::init<int>())
7878
.def(py::init<BaseException>())
7979
.def("has_value", &Result<int>::HasValue)
80-
.def("get_value", &Result<int>::GetValue, py::return_value_policy::reference_internal)
81-
.def("get_exception", &Result<int>::GetException, py::return_value_policy::reference_internal)
82-
.def("visit", &Result<int>::Visit<std::function<py::object(const auto &)>>)
80+
.def("get_value",
81+
[](const Result<int> &r) -> int {
82+
return r.GetValue(); // This will throw if error, which pybind11 will convert to
83+
// Python exception
84+
})
85+
.def("get_exception", [](const Result<int> &r) -> BaseException { return r.GetException(); })
8386
.def("map", &Result<int>::Map<std::function<int(const int &)>>)
8487
.def("then", &Result<int>::Then<std::function<Result<int>(const int &)>>)
8588
.def("__bool__", &Result<int>::HasValue)
8689
.def("__str__",
8790
[](const Result<int> &r) {
8891
return r.HasValue() ? std::format("Result({})", r.GetValue())
89-
: std::format("Result(Error: {})", r.GetException().GetMessage());
92+
: std::format("Result(Error: {})", r.GetException().what());
9093
})
9194
.def("__repr__", [](const Result<int> &r) {
9295
return std::format("<IntResult({}) at {}>",
@@ -99,17 +102,16 @@ void bind_exceptions(py::module &m) {
99102
.def(py::init<double>())
100103
.def(py::init<BaseException>())
101104
.def("has_value", &Result<double>::HasValue)
102-
.def("get_value", &Result<double>::GetValue, py::return_value_policy::reference_internal)
103-
.def("get_exception", &Result<double>::GetException,
104-
py::return_value_policy::reference_internal)
105-
.def("visit", &Result<double>::Visit<std::function<py::object(const auto &)>>)
105+
.def("get_value", [](const Result<double> &r) -> double { return r.GetValue(); })
106+
.def("get_exception",
107+
[](const Result<double> &r) -> BaseException { return r.GetException(); })
106108
.def("map", &Result<double>::Map<std::function<double(const double &)>>)
107109
.def("then", &Result<double>::Then<std::function<Result<double>(const double &)>>)
108110
.def("__bool__", &Result<double>::HasValue)
109111
.def("__str__",
110112
[](const Result<double> &r) {
111113
return r.HasValue() ? std::format("Result({})", r.GetValue())
112-
: std::format("Result(Error: {})", r.GetException().GetMessage());
114+
: std::format("Result(Error: {})", r.GetException().what());
113115
})
114116
.def("__repr__", [](const Result<double> &r) {
115117
return std::format("<DoubleResult({}) at {}>",
@@ -122,18 +124,17 @@ void bind_exceptions(py::module &m) {
122124
.def(py::init<std::string>())
123125
.def(py::init<BaseException>())
124126
.def("has_value", &Result<std::string>::HasValue)
125-
.def("get_value", &Result<std::string>::GetValue, py::return_value_policy::reference_internal)
126-
.def("get_exception", &Result<std::string>::GetException,
127-
py::return_value_policy::reference_internal)
128-
.def("visit", &Result<std::string>::Visit<std::function<py::object(const auto &)>>)
127+
.def("get_value", [](const Result<std::string> &r) -> std::string { return r.GetValue(); })
128+
.def("get_exception",
129+
[](const Result<std::string> &r) -> BaseException { return r.GetException(); })
129130
.def("map", &Result<std::string>::Map<std::function<std::string(const std::string &)>>)
130131
.def("then",
131132
&Result<std::string>::Then<std::function<Result<std::string>(const std::string &)>>)
132133
.def("__bool__", &Result<std::string>::HasValue)
133134
.def("__str__",
134135
[](const Result<std::string> &r) {
135136
return r.HasValue() ? std::format("Result('{}')", r.GetValue())
136-
: std::format("Result(Error: {})", r.GetException().GetMessage());
137+
: std::format("Result(Error: {})", r.GetException().what());
137138
})
138139
.def("__repr__", [](const Result<std::string> &r) {
139140
return std::format("<StringResult({}) at {}>",

binding/timing_binding.cpp

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,20 +43,17 @@ void bind_timing(py::module &m) {
4343
static_cast<const void *>(&t));
4444
});
4545

46-
// Bind ScopedTimer class
46+
// Bind ScopedTimer class with context manager support
4747
py::class_<ScopedTimer>(m, "ScopedTimer")
4848
.def(py::init<std::string_view>())
4949
.def(py::init<std::string_view, std::function<void(std::int64_t)>>())
50-
.def("__repr__", [](const ScopedTimer &st) {
51-
return std::format("<ScopedTimer at {}>", static_cast<const void *>(&st));
52-
});
53-
54-
// Context manager support for ScopedTimer
55-
py::class_<ScopedTimer>(m, "ScopedTimerContext")
56-
.def(py::init<std::string_view>())
50+
.def("__repr__",
51+
[](const ScopedTimer &st) {
52+
return std::format("<ScopedTimer at {}>", static_cast<const void *>(&st));
53+
})
5754
.def(
5855
"__enter__", [](ScopedTimer &self) { return &self; }, py::return_value_policy::reference)
59-
.def("__exit__", [](ScopedTimer &self, py::object, py::object, py::object) {
56+
.def("__exit__", [](ScopedTimer & /*self*/, py::object, py::object, py::object) {
6057
// Destructor will be called automatically
6158
return false;
6259
});

0 commit comments

Comments
 (0)