bpo-33916: Fix bz2 and lzma init when called twice (GH-7843)
bz2, lzma: When Decompressor.__init__() is called twice, free the old
lock to not leak memory.
diff --git a/Modules/_bz2module.c b/Modules/_bz2module.c
index 0789b61..3890b60 100644
--- a/Modules/_bz2module.c
+++ b/Modules/_bz2module.c
@@ -634,11 +634,15 @@
{
int bzerror;
- self->lock = PyThread_allocate_lock();
- if (self->lock == NULL) {
+ PyThread_type_lock lock = PyThread_allocate_lock();
+ if (lock == NULL) {
PyErr_SetString(PyExc_MemoryError, "Unable to allocate lock");
return -1;
}
+ if (self->lock != NULL) {
+ PyThread_free_lock(self->lock);
+ }
+ self->lock = lock;
self->needs_input = 1;
self->bzs_avail_in_real = 0;