Skip to content

Commit

Permalink
Merge pull request #161 from camillol/camillo/readexc2
Browse files Browse the repository at this point in the history
Fix exception propagation in readinto
  • Loading branch information
pauldmccarthy authored Nov 26, 2024
2 parents 14a8b47 + cc12bdb commit adef89d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
7 changes: 4 additions & 3 deletions indexed_gzip/indexed_gzip.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -835,16 +835,17 @@ cdef class _IndexedGzipFile:
try:

vbuf = <void *>pbuf.buf
with self.__file_handle(), nogil:
ret = zran.zran_read(index, vbuf, bufsz)
with self.__file_handle():
with nogil:
ret = zran.zran_read(index, vbuf, bufsz)
exc = get_python_exception()

# release the py_buffer
finally:
PyBuffer_Release(&pbuf)

# see how the read went
if ret == zran.ZRAN_READ_FAIL:
exc = get_python_exception()
raise ZranError('zran_read returned error: {} (file: {})'
.format(ZRAN_ERRORS.ZRAN_READ[ret], self.errname)) from exc

Expand Down
12 changes: 8 additions & 4 deletions indexed_gzip/tests/test_indexed_gzip.py
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,8 @@ def test_read_beyond_end(concat, drop):
os.remove(testfile)


def test_read_exception(testfile, nelems):
@pytest.mark.parametrize('use_readinto', [False, True])
def test_read_exception(testfile, nelems, use_readinto):
"""When wrapping a python file object, if it raises an exception
it should be preserved.
"""
Expand Down Expand Up @@ -539,10 +540,13 @@ def my_error_fn(*args, **kwargs):
gzf = igzip._IndexedGzipFile(fileobj=f)
f.read = my_error_fn
try:
gzf.read(1)
if use_readinto:
ba = bytearray(1)
gzf.readinto(ba)
else:
gzf.read(1)
except Exception as e:
assert (MY_ERROR in str(e) or MY_ERROR in str(e.__cause__),
"Exception was not preserved; got {}".format(e))
assert MY_ERROR in str(e) or MY_ERROR in str(e.__cause__), "Exception was not preserved; got {}".format(e)
del e
else:
assert False, "Expected an exception to be raised"
Expand Down

0 comments on commit adef89d

Please sign in to comment.