Skip to content

Commit 1957e4f

Browse files
committed
improve error message when writing to closed writer
1 parent 8b1f892 commit 1957e4f

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

lib/simple_writer.cc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ class SimpleWriter
3636

3737
void add_node(py::object o)
3838
{
39+
if (!buffer) {
40+
throw std::runtime_error{"Writer already closed."};
41+
}
42+
3943
if (py::isinstance<osmium::Node>(o)) {
4044
buffer.add_item(o.cast<osmium::Node &>());
4145
} else {
@@ -57,6 +61,10 @@ class SimpleWriter
5761

5862
void add_way(py::object o)
5963
{
64+
if (!buffer) {
65+
throw std::runtime_error{"Writer already closed."};
66+
}
67+
6068
if (py::isinstance<osmium::Way>(o)) {
6169
buffer.add_item(o.cast<osmium::Way &>());
6270
} else {
@@ -76,6 +84,10 @@ class SimpleWriter
7684

7785
void add_relation(py::object o)
7886
{
87+
if (!buffer) {
88+
throw std::runtime_error{"Writer already closed."};
89+
}
90+
7991
if (py::isinstance<osmium::Relation>(o)) {
8092
buffer.add_item(o.cast<osmium::Relation &>());
8193
} else {

test/test_writer.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,3 +214,35 @@ def test_set_custom_header(tmp_path):
214214
assert h.box().top_right == o.osm.Location(10, 45)
215215
finally:
216216
rd.close()
217+
218+
219+
def test_add_node_after_close(tmp_path, simple_handler):
220+
node_opl = "n235 v1 dV c0 t i0 u Telephant=yes x98.7 y-3.45"
221+
222+
filename = tmp_path / (str(uuid.uuid4()) + '.opl')
223+
writer = o.SimpleWriter(str(filename), 1024*1024)
224+
writer.close()
225+
226+
with pytest.raises(RuntimeError, match='closed'):
227+
simple_handler(node_opl, node=lambda o: writer.add_node(o))
228+
229+
230+
def test_add_way_after_close(tmp_path, simple_handler):
231+
node_opl = "w1 Nn1"
232+
233+
filename = tmp_path / (str(uuid.uuid4()) + '.opl')
234+
writer = o.SimpleWriter(str(filename), 1024*1024)
235+
writer.close()
236+
237+
with pytest.raises(RuntimeError, match='closed'):
238+
simple_handler(node_opl, way=lambda o: writer.add_way(o))
239+
240+
def test_add_relation_after_close(tmp_path, simple_handler):
241+
node_opl = "r54 Mn1@,w3@foo"
242+
243+
filename = tmp_path / (str(uuid.uuid4()) + '.opl')
244+
writer = o.SimpleWriter(str(filename), 1024*1024)
245+
writer.close()
246+
247+
with pytest.raises(RuntimeError, match='closed'):
248+
simple_handler(node_opl, relation=lambda o: writer.add_relation(o))

0 commit comments

Comments
 (0)