Skip to content

Commit

Permalink
improve error message when writing to closed writer
Browse files Browse the repository at this point in the history
  • Loading branch information
lonvia committed Apr 27, 2022
1 parent 8b1f892 commit 1957e4f
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
12 changes: 12 additions & 0 deletions lib/simple_writer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ class SimpleWriter

void add_node(py::object o)
{
if (!buffer) {
throw std::runtime_error{"Writer already closed."};
}

if (py::isinstance<osmium::Node>(o)) {
buffer.add_item(o.cast<osmium::Node &>());
} else {
Expand All @@ -57,6 +61,10 @@ class SimpleWriter

void add_way(py::object o)
{
if (!buffer) {
throw std::runtime_error{"Writer already closed."};
}

if (py::isinstance<osmium::Way>(o)) {
buffer.add_item(o.cast<osmium::Way &>());
} else {
Expand All @@ -76,6 +84,10 @@ class SimpleWriter

void add_relation(py::object o)
{
if (!buffer) {
throw std::runtime_error{"Writer already closed."};
}

if (py::isinstance<osmium::Relation>(o)) {
buffer.add_item(o.cast<osmium::Relation &>());
} else {
Expand Down
32 changes: 32 additions & 0 deletions test/test_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,3 +214,35 @@ def test_set_custom_header(tmp_path):
assert h.box().top_right == o.osm.Location(10, 45)
finally:
rd.close()


def test_add_node_after_close(tmp_path, simple_handler):
node_opl = "n235 v1 dV c0 t i0 u Telephant=yes x98.7 y-3.45"

filename = tmp_path / (str(uuid.uuid4()) + '.opl')
writer = o.SimpleWriter(str(filename), 1024*1024)
writer.close()

with pytest.raises(RuntimeError, match='closed'):
simple_handler(node_opl, node=lambda o: writer.add_node(o))


def test_add_way_after_close(tmp_path, simple_handler):
node_opl = "w1 Nn1"

filename = tmp_path / (str(uuid.uuid4()) + '.opl')
writer = o.SimpleWriter(str(filename), 1024*1024)
writer.close()

with pytest.raises(RuntimeError, match='closed'):
simple_handler(node_opl, way=lambda o: writer.add_way(o))

def test_add_relation_after_close(tmp_path, simple_handler):
node_opl = "r54 Mn1@,w3@foo"

filename = tmp_path / (str(uuid.uuid4()) + '.opl')
writer = o.SimpleWriter(str(filename), 1024*1024)
writer.close()

with pytest.raises(RuntimeError, match='closed'):
simple_handler(node_opl, relation=lambda o: writer.add_relation(o))

0 comments on commit 1957e4f

Please sign in to comment.