From 55d039e3429c2be31c432127b57eda61ac0a8f43 Mon Sep 17 00:00:00 2001 From: Sean Gillies Date: Tue, 1 Feb 2022 09:47:05 -0700 Subject: [PATCH] Raise OSError on attempt to append to dataset stored in a file Resolves #1027 --- CHANGES.txt | 2 ++ fiona/__init__.py | 7 ++++++- tests/test_memoryfile.py | 6 ++++++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/CHANGES.txt b/CHANGES.txt index d18a31321..796f8be89 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -8,6 +8,8 @@ All issue numbers are relative to https://github.com/Toblerity/Fiona/issues. Changes: +- OSError is raised on attempts to open a dataset in a Python file object in + "a" mode (see #1027). - Upgrade attrs, cython, etc to open up Python 3.10 support (#1049). Bug fixes: diff --git a/fiona/__init__.py b/fiona/__init__.py index 56b6db5c8..41648c90a 100644 --- a/fiona/__init__.py +++ b/fiona/__init__.py @@ -199,8 +199,8 @@ def open(fp, mode='r', driver=None, schema=None, crs=None, encoding=None, Returns ------- Collection - """ + """ if mode == 'r' and hasattr(fp, 'read'): @contextmanager @@ -243,6 +243,11 @@ def fp_writer(fp): return fp_writer(fp) + elif mode == "a" and hasattr(fp, "write"): + raise OSError( + "Append mode is not supported for datasets in a Python file object." + ) + else: # If a pathlib.Path instance is given, convert it to a string path. if isinstance(fp, Path): diff --git a/tests/test_memoryfile.py b/tests/test_memoryfile.py index fbdbbf83a..ca6490e05 100644 --- a/tests/test_memoryfile.py +++ b/tests/test_memoryfile.py @@ -160,6 +160,12 @@ def test_write_bytesio(profile_first_coutwildrnp_shp): assert len(col) == 1 +def test_append_bytesio_exception(profile_first_coutwildrnp_shp): + """Append is not supported, see #1027.""" + with pytest.raises(OSError): + fiona.open(BytesIO(data_coutwildrnp_json), "a") + + def test_mapinfo_raises(): """Reported to be a crasher in #937""" driver = 'MapInfo File'