Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions c-ext/compressionreader.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ static void compressionreader_dealloc(ZstdCompressionReader *self) {
memset(&self->buffer, 0, sizeof(self->buffer));
}

Py_CLEAR(self->readResult);

PyObject_Del(self);
}

Expand Down Expand Up @@ -292,6 +294,7 @@ static PyObject *compressionreader_read(ZstdCompressionReader *self,
readResult = read_compressor_input(self);

if (-1 == readResult) {
Py_DECREF(result);
return NULL;
}
else if (0 == readResult) {
Expand Down
70 changes: 70 additions & 0 deletions tests/test_compressor_stream_reader.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import gc
import io
import os
import sys
import unittest
import weakref

import zstandard as zstd

Expand All @@ -10,6 +14,72 @@


class TestCompressor_stream_reader(unittest.TestCase):
def test_read_result_released_on_error(self):
class ReadResult:
pass

refs = []

class Source:
def read(self, size):
result = ReadResult()
refs.append(weakref.ref(result))
return result

reader = zstd.ZstdCompressor().stream_reader(Source())
with self.assertRaises(TypeError):
reader.read(1)
del reader
gc.collect()
self.assertTrue(refs[0]() is None)

def test_read_result_released_on_destruction(self):
class ReadResult(bytearray):
pass

refs = []

class Source:
def read(self, size):
result = ReadResult(os.urandom(1024 * 1024))
refs.append(weakref.ref(result))
return result

reader = zstd.ZstdCompressor().stream_reader(Source())
self.assertEqual(len(reader.read(1)), 1)
self.assertIsNotNone(refs[0]())
del reader
gc.collect()
self.assertTrue(refs[0]() is None)

@unittest.skipUnless(
sys.implementation.name == "cpython", "tracemalloc requires CPython"
)
def test_read_error_releases_output(self):
import tracemalloc

class Source:
def read(self, size):
raise OSError("source read failed")

reader = zstd.ZstdCompressor().stream_reader(Source())
size = 1024 * 1024
already_tracing = tracemalloc.is_tracing()
if not already_tracing:
tracemalloc.start()
try:
gc.collect()
before = tracemalloc.get_traced_memory()[0]
for _ in range(8):
with self.assertRaisesRegex(OSError, "source read failed"):
reader.read(size)
gc.collect()
retained = tracemalloc.get_traced_memory()[0] - before
self.assertLess(retained, size)
finally:
if not already_tracing:
tracemalloc.stop()

def test_context_manager(self):
cctx = zstd.ZstdCompressor()

Expand Down