Skip to content

Commit 77662b8

Browse files
Merge branch 'stable-1.3'
2 parents f986d1a + cedc84c commit 77662b8

3 files changed

Lines changed: 52 additions & 6 deletions

File tree

src/errors.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
// along with this program. If not, see <http://www.gnu.org/licenses/>.
1717
//
1818

19+
#include "errors.hpp"
20+
1921
#include <stdexcept>
2022
#include <string>
2123

src/errors.hpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//
2+
// libsemigroups_pybind11
3+
// Copyright (C) 2026 James D. Mitchell
4+
//
5+
// This program is free software: you can redistribute it and/or modify
6+
// it under the terms of the GNU General Public License as published by
7+
// the Free Software Foundation, either version 3 of the License, or
8+
// (at your option) any later version.
9+
//
10+
// This program is distributed in the hope that it will be useful,
11+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
// GNU General Public License for more details.
14+
//
15+
// You should have received a copy of the GNU General Public License
16+
// along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
//
18+
19+
#ifndef SRC_ERRORS_HPP_
20+
#define SRC_ERRORS_HPP_
21+
22+
#include <stdexcept>
23+
#include <string>
24+
25+
namespace libsemigroups {
26+
27+
std::string formatted_error_message(std::runtime_error const& e);
28+
}
29+
#endif // SRC_ERRORS_HPP_

src/transf.cpp

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
// along with this program. If not, see <http://www.gnu.org/licenses/>.
1717
//
1818

19+
#include <stdexcept>
20+
1921
// libsemigroups headers
2022
#include <libsemigroups/ranges.hpp>
2123
#include <libsemigroups/transf.hpp>
@@ -27,7 +29,8 @@
2729

2830
// libsemigroups_pybind11....
2931
#include "debug.hpp"
30-
#include "main.hpp" // for init_transf
32+
#include "errors.hpp" // for formatted_error_message
33+
#include "main.hpp" // for init_transf
3134

3235
namespace libsemigroups {
3336

@@ -104,16 +107,27 @@ the image of the point ``i`` under the {0} is ``imgs[i]``.
104107
"__getitem__",
105108
[](PTransfSubclass const& a,
106109
size_t b) -> int_or_unsigned_constant<Scalar> {
107-
auto result = a.at(b);
108-
if (result != UNDEFINED) {
109-
return {result};
110+
// __getitem__ is expected by python to throw an IndexError which
111+
// corresponds to a std::out_of_range for things like list(a) to
112+
// work.
113+
try {
114+
auto result = a.at(b);
115+
if (result != UNDEFINED) {
116+
return {result};
117+
}
118+
return {UNDEFINED};
119+
} catch (LibsemigroupsException const& e) {
120+
throw std::out_of_range(formatted_error_message(e));
110121
}
111-
return {UNDEFINED};
112122
},
113123
py::is_operator());
114124

115125
thing.def("__hash__", &PTransfSubclass::hash_value, py::is_operator());
116126

127+
thing.def("__iter__", [](PTransfSubclass const& self) {
128+
return py::make_iterator(self.begin(), self.end());
129+
});
130+
117131
////////////////////////////////////////////////////////////////////////
118132
// Non-special methods
119133
////////////////////////////////////////////////////////////////////////
@@ -179,6 +193,7 @@ yielding these values.
179193
doc_type_name,
180194
long_name)
181195
.c_str());
196+
182197
thing.def(
183198
"increase_degree_by",
184199
[](PTransfSubclass& self, size_t m) -> PTransfSubclass& {
@@ -718,7 +733,7 @@ fewer points requiring less space per point.
718733
m.def("transf_inverse",
719734
py::overload_cast<Perm_ const&>(&inverse<N, Scalar>));
720735
} // bind_perm
721-
} // namespace
736+
} // namespace
722737

723738
void init_transf(py::module& m) {
724739
// Transformations

0 commit comments

Comments
 (0)