diff --git a/indexed_gzip/indexed_gzip.pyx b/indexed_gzip/indexed_gzip.pyx index 10b45ca0..1b0e9e06 100644 --- a/indexed_gzip/indexed_gzip.pyx +++ b/indexed_gzip/indexed_gzip.pyx @@ -835,8 +835,10 @@ cdef class _IndexedGzipFile: try: vbuf = 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: @@ -844,7 +846,6 @@ cdef class _IndexedGzipFile: # 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 diff --git a/indexed_gzip/tests/test_indexed_gzip.py b/indexed_gzip/tests/test_indexed_gzip.py index ff07f080..7d1c2928 100644 --- a/indexed_gzip/tests/test_indexed_gzip.py +++ b/indexed_gzip/tests/test_indexed_gzip.py @@ -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. """ @@ -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"