Skip to content

Commit

Permalink
python: convert std::system_error to IOError or derived exceptions (#119
Browse files Browse the repository at this point in the history
)
  • Loading branch information
wojdyr committed May 24, 2021
1 parent 3dab16b commit 6ee7016
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
11 changes: 11 additions & 0 deletions python/gemmi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,17 @@ PYBIND11_MODULE(gemmi, mg) {
mg.doc() = "Python bindings to GEMMI - a library used in macromolecular\n"
"crystallography and related fields";
mg.attr("__version__") = GEMMI_VERSION;

py::register_exception_translator([](std::exception_ptr p) {
try {
if (p)
std::rethrow_exception(p);
} catch (const std::system_error &e) {
const int errornum = e.code().value();
PyErr_SetObject(PyExc_IOError, py::make_tuple(errornum, e.what()).ptr());
}
});

py::module cif = mg.def_submodule("cif", "CIF file format");
add_cif(cif);
add_symmetry(mg);
Expand Down
4 changes: 4 additions & 0 deletions tests/test_cif.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,10 @@ def test_reading_gzipped_file(self):
self.assertEqual(len(nonexistent), 0)
self.assertEqual(nonexistent.width(), 0)

def test_file_not_found(self):
with self.assertRaises(IOError):
cif.read('file-that-does-not-exist.cif')

def test_line_endings(self):
lines = ['data_a', '_a_field', ';', 'text line', '2nd line', ';']
for eol in ('\r\n', '\n'):
Expand Down

0 comments on commit 6ee7016

Please sign in to comment.