Skip to content

Commit

Permalink
Merge pull request #25 from brianhelba/fixbuf
Browse files Browse the repository at this point in the history
  • Loading branch information
brianhelba authored Jan 5, 2022
2 parents ecaeeea + 99b6de1 commit 566046c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
3 changes: 1 addition & 2 deletions tests/test_deflate64.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,12 @@ def test_decompress_output_type(data_dir, deflate64):
assert isinstance(decompressed_content, bytes)


@pytest.mark.xfail(raises=SystemError)
def test_decompress_repeated(data_dir, deflate64):
"""Ensure that resources are properly shared by repeated invocations of Deflate64.decompress."""
with open(data_dir / '10_lines.deflate64', 'rb') as compressed_content_stream:
decompressed_content_10 = deflate64.decompress(compressed_content_stream.read())

with open(data_dir / '10_lines.deflate64', 'rb') as compressed_content_stream:
with open(data_dir / '100_lines.deflate64', 'rb') as compressed_content_stream:
decompressed_content_100 = deflate64.decompress(compressed_content_stream.read())

assert len(decompressed_content_10) == 180
Expand Down
21 changes: 11 additions & 10 deletions zipfile_deflate64/deflate64/deflate64module.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,21 +66,13 @@ static int Deflate64_init(Deflate64Object* self, PyObject* args, PyObject* kwds)
return -1;
}

// Allocate now, but with no size; this will be resized later
self->output_buffer = PyBytes_FromStringAndSize(NULL, 0);
if (self->output_buffer == NULL) {
PyErr_NoMemory();
return -1;
}

// Default eof to false
self->eof = 0;

return 0;
}

static void Deflate64_dealloc(Deflate64Object* self) {
Py_XDECREF(self->output_buffer);
if (self->strm != NULL) {
int err = inflateBack9End(self->strm);
switch (err) {
Expand Down Expand Up @@ -148,6 +140,13 @@ static PyObject* Deflate64_decompress(Deflate64Object* self, PyObject *args) {
return NULL;
}

// Allocate now, but with no size; this will be resized later
self->output_buffer = PyBytes_FromStringAndSize(NULL, 0);
if (self->output_buffer == NULL) {
PyErr_NoMemory();
return NULL;
}

self->strm->next_in = input_buffer.buf;
self->strm->avail_in = (uInt) input_buffer.len;

Expand Down Expand Up @@ -184,13 +183,15 @@ static PyObject* Deflate64_decompress(Deflate64Object* self, PyObject *args) {
goto error;
}

// This method returns a new reference to output_buffer, which should persist even after this
// object is deallocated (which decrements output_buffer)
// This method returns a new reference to output_buffer
Py_INCREF(self->output_buffer);
ret = self->output_buffer;

error:
PyBuffer_Release(&input_buffer);
// Release and clear the internal reference to output_buffer, as it's intended to only be
// used during the lifetime of this decompress function
Py_CLEAR(self->output_buffer);
return ret;
}

Expand Down

0 comments on commit 566046c

Please sign in to comment.