Skip to content
Merged
Changes from 3 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
47 changes: 10 additions & 37 deletions Modules/_bz2module.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,20 +97,11 @@ OutputBuffer_OnError(_BlocksOutputBuffer *buffer)
#endif /* ! BZ_CONFIG_ERROR */


#define ACQUIRE_LOCK(obj) do { \
if (!PyThread_acquire_lock((obj)->lock, 0)) { \
Py_BEGIN_ALLOW_THREADS \
PyThread_acquire_lock((obj)->lock, 1); \
Py_END_ALLOW_THREADS \
} } while (0)
#define RELEASE_LOCK(obj) PyThread_release_lock((obj)->lock)


typedef struct {
PyObject_HEAD
bz_stream bzs;
int flushed;
PyThread_type_lock lock;
PyMutex mutex;
} BZ2Compressor;

typedef struct {
Expand All @@ -126,7 +117,7 @@ typedef struct {
separately. Conversion and looping is encapsulated in
decompress_buf() */
size_t bzs_avail_in_real;
PyThread_type_lock lock;
PyMutex mutex;
} BZ2Decompressor;

#define _BZ2Compressor_CAST(op) ((BZ2Compressor *)(op))
Expand Down Expand Up @@ -271,12 +262,12 @@ _bz2_BZ2Compressor_compress_impl(BZ2Compressor *self, Py_buffer *data)
{
PyObject *result = NULL;

ACQUIRE_LOCK(self);
PyMutex_Lock(&self->mutex);
if (self->flushed)
PyErr_SetString(PyExc_ValueError, "Compressor has been flushed");
else
result = compress(self, data->buf, data->len, BZ_RUN);
RELEASE_LOCK(self);
PyMutex_Unlock(&self->mutex);
return result;
}

Expand All @@ -296,14 +287,14 @@ _bz2_BZ2Compressor_flush_impl(BZ2Compressor *self)
{
PyObject *result = NULL;

ACQUIRE_LOCK(self);
PyMutex_Lock(&self->mutex);
if (self->flushed)
PyErr_SetString(PyExc_ValueError, "Repeated call to flush()");
else {
self->flushed = 1;
result = compress(self, NULL, 0, BZ_FINISH);
}
RELEASE_LOCK(self);
PyMutex_Unlock(&self->mutex);
return result;
}

Expand Down Expand Up @@ -357,13 +348,7 @@ _bz2_BZ2Compressor_impl(PyTypeObject *type, int compresslevel)
return NULL;
}

self->lock = PyThread_allocate_lock();
if (self->lock == NULL) {
Py_DECREF(self);
PyErr_SetString(PyExc_MemoryError, "Unable to allocate lock");
return NULL;
}

self->mutex = (PyMutex){0};
self->bzs.opaque = NULL;
self->bzs.bzalloc = BZ2_Malloc;
self->bzs.bzfree = BZ2_Free;
Expand All @@ -383,9 +368,6 @@ BZ2Compressor_dealloc(PyObject *op)
{
BZ2Compressor *self = _BZ2Compressor_CAST(op);
BZ2_bzCompressEnd(&self->bzs);
if (self->lock != NULL) {
PyThread_free_lock(self->lock);
}
PyTypeObject *tp = Py_TYPE(self);
tp->tp_free((PyObject *)self);
Py_DECREF(tp);
Expand Down Expand Up @@ -619,12 +601,12 @@ _bz2_BZ2Decompressor_decompress_impl(BZ2Decompressor *self, Py_buffer *data,
{
PyObject *result = NULL;

ACQUIRE_LOCK(self);
PyMutex_Lock(&self->mutex);
if (self->eof)
PyErr_SetString(PyExc_EOFError, "End of stream already reached");
else
result = decompress(self, data->buf, data->len, max_length);
RELEASE_LOCK(self);
PyMutex_Unlock(&self->mutex);
return result;
}

Expand All @@ -650,13 +632,7 @@ _bz2_BZ2Decompressor_impl(PyTypeObject *type)
return NULL;
}

self->lock = PyThread_allocate_lock();
if (self->lock == NULL) {
Py_DECREF(self);
PyErr_SetString(PyExc_MemoryError, "Unable to allocate lock");
return NULL;
}

self->mutex = (PyMutex){0};
self->needs_input = 1;
self->bzs_avail_in_real = 0;
self->input_buffer = NULL;
Expand Down Expand Up @@ -684,9 +660,6 @@ BZ2Decompressor_dealloc(PyObject *op)
}
BZ2_bzDecompressEnd(&self->bzs);
Py_CLEAR(self->unused_data);
if (self->lock != NULL) {
PyThread_free_lock(self->lock);
}

PyTypeObject *tp = Py_TYPE(self);
tp->tp_free((PyObject *)self);
Expand Down
Loading