mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
[3.12] gh-111049: Fix crash during garbage collection of the BytesIO buffer object (GH-111221) (GH-113096)
(cherry picked from commit bb36f72efc
)
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
This commit is contained in:
parent
916ef4fbee
commit
9f5209f3c2
3 changed files with 27 additions and 10 deletions
|
@ -6,10 +6,12 @@ BytesIO -- for bytes
|
||||||
import unittest
|
import unittest
|
||||||
from test import support
|
from test import support
|
||||||
|
|
||||||
|
import gc
|
||||||
import io
|
import io
|
||||||
import _pyio as pyio
|
import _pyio as pyio
|
||||||
import pickle
|
import pickle
|
||||||
import sys
|
import sys
|
||||||
|
import weakref
|
||||||
|
|
||||||
class IntLike:
|
class IntLike:
|
||||||
def __init__(self, num):
|
def __init__(self, num):
|
||||||
|
@ -477,6 +479,25 @@ class PyBytesIOTest(MemoryTestMixin, MemorySeekTestMixin, unittest.TestCase):
|
||||||
buf2.release()
|
buf2.release()
|
||||||
memio.write(b'x')
|
memio.write(b'x')
|
||||||
|
|
||||||
|
def test_getbuffer_gc_collect(self):
|
||||||
|
memio = self.ioclass(b"1234567890")
|
||||||
|
buf = memio.getbuffer()
|
||||||
|
memiowr = weakref.ref(memio)
|
||||||
|
bufwr = weakref.ref(buf)
|
||||||
|
# Create a reference loop.
|
||||||
|
a = [buf]
|
||||||
|
a.append(a)
|
||||||
|
# The Python implementation emits an unraisable exception.
|
||||||
|
with support.catch_unraisable_exception():
|
||||||
|
del memio
|
||||||
|
del buf
|
||||||
|
del a
|
||||||
|
# The C implementation emits an unraisable exception.
|
||||||
|
with support.catch_unraisable_exception():
|
||||||
|
gc.collect()
|
||||||
|
self.assertIsNone(memiowr())
|
||||||
|
self.assertIsNone(bufwr())
|
||||||
|
|
||||||
def test_read1(self):
|
def test_read1(self):
|
||||||
buf = self.buftype("1234567890")
|
buf = self.buftype("1234567890")
|
||||||
self.assertEqual(self.ioclass(buf).read1(), buf)
|
self.assertEqual(self.ioclass(buf).read1(), buf)
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
Fix crash during garbage collection of the :class:`io.BytesIO` buffer
|
||||||
|
object.
|
|
@ -988,7 +988,9 @@ static int
|
||||||
bytesio_clear(bytesio *self)
|
bytesio_clear(bytesio *self)
|
||||||
{
|
{
|
||||||
Py_CLEAR(self->dict);
|
Py_CLEAR(self->dict);
|
||||||
Py_CLEAR(self->buf);
|
if (self->exports == 0) {
|
||||||
|
Py_CLEAR(self->buf);
|
||||||
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1093,13 +1095,6 @@ bytesiobuf_releasebuffer(bytesiobuf *obj, Py_buffer *view)
|
||||||
b->exports--;
|
b->exports--;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
|
||||||
bytesiobuf_clear(bytesiobuf *self)
|
|
||||||
{
|
|
||||||
Py_CLEAR(self->source);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int
|
static int
|
||||||
bytesiobuf_traverse(bytesiobuf *self, visitproc visit, void *arg)
|
bytesiobuf_traverse(bytesiobuf *self, visitproc visit, void *arg)
|
||||||
{
|
{
|
||||||
|
@ -1114,7 +1109,7 @@ bytesiobuf_dealloc(bytesiobuf *self)
|
||||||
PyTypeObject *tp = Py_TYPE(self);
|
PyTypeObject *tp = Py_TYPE(self);
|
||||||
/* bpo-31095: UnTrack is needed before calling any callbacks */
|
/* bpo-31095: UnTrack is needed before calling any callbacks */
|
||||||
PyObject_GC_UnTrack(self);
|
PyObject_GC_UnTrack(self);
|
||||||
(void)bytesiobuf_clear(self);
|
Py_CLEAR(self->source);
|
||||||
tp->tp_free(self);
|
tp->tp_free(self);
|
||||||
Py_DECREF(tp);
|
Py_DECREF(tp);
|
||||||
}
|
}
|
||||||
|
@ -1122,7 +1117,6 @@ bytesiobuf_dealloc(bytesiobuf *self)
|
||||||
static PyType_Slot bytesiobuf_slots[] = {
|
static PyType_Slot bytesiobuf_slots[] = {
|
||||||
{Py_tp_dealloc, bytesiobuf_dealloc},
|
{Py_tp_dealloc, bytesiobuf_dealloc},
|
||||||
{Py_tp_traverse, bytesiobuf_traverse},
|
{Py_tp_traverse, bytesiobuf_traverse},
|
||||||
{Py_tp_clear, bytesiobuf_clear},
|
|
||||||
|
|
||||||
// Buffer protocol
|
// Buffer protocol
|
||||||
{Py_bf_getbuffer, bytesiobuf_getbuffer},
|
{Py_bf_getbuffer, bytesiobuf_getbuffer},
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue