Skip to content

Commit

Permalink
Raise OSError on attempt to append to dataset stored in a file
Browse files Browse the repository at this point in the history
Resolves #1027
  • Loading branch information
sgillies committed Feb 1, 2022
1 parent 9057cb3 commit 55d039e
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
7 changes: 6 additions & 1 deletion fiona/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down
6 changes: 6 additions & 0 deletions tests/test_memoryfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down

0 comments on commit 55d039e

Please # to comment.