Modules/zlibmodule.c | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/Modules/zlibmodule.c b/Modules/zlibmodule.c index 480bc1ca2a..f904f992af 100644 --- a/Modules/zlibmodule.c +++ b/Modules/zlibmodule.c @@ -85,10 +85,12 @@ Buffer_OnError(_BlocksOutputBuffer *buffer) } -#define ENTER_ZLIB(obj) \ - Py_BEGIN_ALLOW_THREADS; \ - PyThread_acquire_lock((obj)->lock, 1); \ - Py_END_ALLOW_THREADS; +#define ENTER_ZLIB(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 LEAVE_ZLIB(obj) PyThread_release_lock((obj)->lock); #if defined(ZLIB_VERNUM) && ZLIB_VERNUM >= 0x1221 @@ -670,14 +672,13 @@ zlib_Compress_compress_impl(compobject *self, PyTypeObject *cls, PyObject *RetVal; int err; _BlocksOutputBuffer buffer = {.list = NULL}; - zlibstate *state = PyType_GetModuleState(cls); + ENTER_ZLIB(self); + self->zst.next_in = data->buf; Py_ssize_t ibuflen = data->len; - ENTER_ZLIB(self); - if (Buffer_InitAndGrow(&buffer, -1, &self->zst.next_out, &self->zst.avail_out) < 0) { goto error; } @@ -803,11 +804,11 @@ zlib_Decompress_decompress_impl(compobject *self, PyTypeObject *cls, } else if (max_length == 0) max_length = -1; + ENTER_ZLIB(self); + self->zst.next_in = data->buf; ibuflen = data->len; - ENTER_ZLIB(self); - if (Buffer_InitAndGrow(&buffer, max_length, &self->zst.next_out, &self->zst.avail_out) < 0) { goto abort; }