Skip to content

Commit

Permalink
Merge pull request #276 from glotzerlab/fix/file-exists-error
Browse files Browse the repository at this point in the history
Better error handling for GSD_IO_ERRORs
  • Loading branch information
joaander authored Aug 3, 2023
2 parents b5177c4 + 9c22066 commit 367469c
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 4 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ Change Log
3.x
---

3.1.1 (2023-??-??)
^^^^^^^^^^^^^^^^^^

*Fixed:*

* Raise a ``FileExistsError`` when opening a file that already exists with ``mode = 'x'``

3.1.0 (2023-07-28)
^^^^^^^^^^^^^^^^^^

Expand Down
1 change: 1 addition & 0 deletions doc/credits.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ The following people contributed to GSD.
* Arthur Zamarin, Gentoo Linux
* Alexander Stukowski, OVITO GmbH
* Charlotte Shiqi Zhao, University of Michigan
* Tim Moore, University of Michigan
20 changes: 16 additions & 4 deletions gsd/gsd.c
Original file line number Diff line number Diff line change
Expand Up @@ -1623,7 +1623,10 @@ int gsd_create(const char* fname,
O_RDWR | O_CREAT | O_TRUNC | extra_flags,
S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
int retval = gsd_initialize_file(fd, application, schema, schema_version);
close(fd);
if (fd != -1)
{
close(fd);
}
return retval;
}

Expand Down Expand Up @@ -1670,14 +1673,20 @@ int gsd_create_and_open(struct gsd_handle* handle,
int retval = gsd_initialize_file(handle->fd, application, schema, schema_version);
if (retval != 0)
{
close(handle->fd);
if (handle->fd != -1)
{
close(handle->fd);
}
return retval;
}

retval = gsd_initialize_handle(handle);
if (retval != 0)
{
close(handle->fd);
if (handle->fd != -1)
{
close(handle->fd);
}
}
return retval;
}
Expand Down Expand Up @@ -1712,7 +1721,10 @@ int gsd_open(struct gsd_handle* handle, const char* fname, const enum gsd_open_f
int retval = gsd_initialize_handle(handle);
if (retval != 0)
{
close(handle->fd);
if (handle->fd != -1)
{
close(handle->fd);
}
}
return retval;
}
Expand Down
11 changes: 11 additions & 0 deletions gsd/test/test_fl.py
Original file line number Diff line number Diff line change
Expand Up @@ -1030,3 +1030,14 @@ def test_index_entries_to_buffer(tmp_path, open_mode):

with pytest.raises(RuntimeError):
f.index_entries_to_buffer = 0


def test_file_exists_error():
"""Test that IO errors throw the correct Python Excetion."""
with pytest.raises(FileExistsError):
with gsd.fl.open(name=test_path / 'test_gsd_v1.gsd',
mode='x',
application='test_gsd_v1',
schema='none',
schema_version=[1, 2]):
pass

0 comments on commit 367469c

Please sign in to comment.