diff --git a/Lib/test/test_io/test_bufferedio.py b/Lib/test/test_io/test_bufferedio.py index f186c30d3d51d34..974c574600629f0 100644 --- a/Lib/test/test_io/test_bufferedio.py +++ b/Lib/test/test_io/test_bufferedio.py @@ -1,4 +1,5 @@ import array +import os import pickle import random import sys @@ -1273,6 +1274,23 @@ def test_weakref_clearing(self): class CBufferedRWPairTest(BufferedRWPairTest, CTestCase): tp = io.BufferedRWPair + def _check_warn_on_dealloc(self, make): + f = make() + r = repr(f) + with self.assertWarns(ResourceWarning) as cm: + f = None + support.gc_collect() + self.assertEqual(str(cm.warning), f"unclosed file {r}") + + def test_warn_on_dealloc(self): + r, w = os.pipe() + self._check_warn_on_dealloc(lambda: self.tp( + self.FileIO(r, "rb"), self.FileIO(w, "wb"))) + r, w = os.pipe() + self._check_warn_on_dealloc(lambda: self.TextIOWrapper( + self.tp(self.FileIO(r, "rb"), self.FileIO(w, "wb")), + encoding="ascii")) + class PyBufferedRWPairTest(BufferedRWPairTest, PyTestCase): tp = pyio.BufferedRWPair diff --git a/Misc/NEWS.d/next/Library/2026-11-18-21-30-00.gh-issue-162246.rwpWrn.rst b/Misc/NEWS.d/next/Library/2026-11-18-21-30-00.gh-issue-162246.rwpWrn.rst new file mode 100644 index 000000000000000..1fef9f0b2ddde37 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-18-21-30-00.gh-issue-162246.rwpWrn.rst @@ -0,0 +1,2 @@ +Fix :class:`io.BufferedRWPair` to match other buffered writers and raise +:exc:`ResourceWarning` if deleted without being closed. diff --git a/Modules/_io/bufferedio.c b/Modules/_io/bufferedio.c index ad2c09367931035..a9e14218b627c9a 100644 --- a/Modules/_io/bufferedio.c +++ b/Modules/_io/bufferedio.c @@ -2270,6 +2270,7 @@ typedef struct { PyObject_HEAD buffered *reader; buffered *writer; + char finalizing; } rwpair; #define rwpair_CAST(op) ((rwpair *)(op)) @@ -2353,6 +2354,9 @@ bufferedrwpair_dealloc(PyObject *op) { rwpair *self = rwpair_CAST(op); PyTypeObject *tp = Py_TYPE(self); + if (_PyIOBase_finalize(op) < 0) { + return; + } _PyObject_GC_UNTRACK(self); PyObject_ClearWeakRefs(op); (void)bufferedrwpair_clear(op); @@ -2508,6 +2512,24 @@ _io_BufferedRWPair_writable_impl(rwpair *self) return _forward_call(self->writer, &_Py_ID(writable), NULL); } +/*[clinic input] +_io.BufferedRWPair._dealloc_warn + + source: object + / + +[clinic start generated code]*/ + +static PyObject * +_io_BufferedRWPair__dealloc_warn_impl(rwpair *self, PyObject *source) +/*[clinic end generated code: output=c11503976cd2ffd8 input=9a6b9c837bfc037b]*/ +{ + if (self->writer == NULL) { + Py_RETURN_NONE; + } + return _io__Buffered__dealloc_warn_impl(self->writer, source); +} + /*[clinic input] _io.BufferedRWPair.close [clinic start generated code]*/ @@ -2516,6 +2538,11 @@ static PyObject * _io_BufferedRWPair_close_impl(rwpair *self) /*[clinic end generated code: output=5924ba5ecc78752a input=4087d69f2d8fc368]*/ { + if (self->finalizing) { + PyObject *r = _io_BufferedRWPair__dealloc_warn_impl(self, + (PyObject *)self); + Py_DECREF(r); + } PyObject *exc = NULL; PyObject *ret = _forward_call(self->writer, &_Py_ID(close), NULL); if (ret == NULL) { @@ -2784,11 +2811,17 @@ static PyMethodDef bufferedrwpair_methods[] = { _IO_BUFFEREDRWPAIR_WRITABLE_METHODDEF _IO_BUFFEREDRWPAIR_CLOSE_METHODDEF + _IO_BUFFEREDRWPAIR__DEALLOC_WARN_METHODDEF _IO_BUFFEREDRWPAIR_ISATTY_METHODDEF {NULL, NULL} }; +static PyMemberDef bufferedrwpair_members[] = { + {"_finalizing", Py_T_BOOL, offsetof(rwpair, finalizing), 0}, + {NULL} +}; + static PyGetSetDef bufferedrwpair_getset[] = { _IO_BUFFEREDRWPAIR_CLOSED_GETSETDEF {NULL} @@ -2800,6 +2833,7 @@ static PyType_Slot bufferedrwpair_slots[] = { {Py_tp_traverse, bufferedrwpair_traverse}, {Py_tp_clear, bufferedrwpair_clear}, {Py_tp_methods, bufferedrwpair_methods}, + {Py_tp_members, bufferedrwpair_members}, {Py_tp_getset, bufferedrwpair_getset}, {Py_tp_init, _io_BufferedRWPair___init__}, {0, NULL}, diff --git a/Modules/_io/clinic/bufferedio.c.h b/Modules/_io/clinic/bufferedio.c.h index 00152c8b74fb9dc..6e83cb9d5faccfe 100644 --- a/Modules/_io/clinic/bufferedio.c.h +++ b/Modules/_io/clinic/bufferedio.c.h @@ -1307,6 +1307,27 @@ _io_BufferedRWPair_writable(PyObject *self, PyObject *Py_UNUSED(ignored)) return _io_BufferedRWPair_writable_impl((rwpair *)self); } +PyDoc_STRVAR(_io_BufferedRWPair__dealloc_warn__doc__, +"_dealloc_warn($self, source, /)\n" +"--\n" +"\n"); + +#define _IO_BUFFEREDRWPAIR__DEALLOC_WARN_METHODDEF \ + {"_dealloc_warn", (PyCFunction)_io_BufferedRWPair__dealloc_warn, METH_O, _io_BufferedRWPair__dealloc_warn__doc__}, + +static PyObject * +_io_BufferedRWPair__dealloc_warn_impl(rwpair *self, PyObject *source); + +static PyObject * +_io_BufferedRWPair__dealloc_warn(PyObject *self, PyObject *source) +{ + PyObject *return_value = NULL; + + return_value = _io_BufferedRWPair__dealloc_warn_impl((rwpair *)self, source); + + return return_value; +} + PyDoc_STRVAR(_io_BufferedRWPair_close__doc__, "close($self, /)\n" "--\n" @@ -1429,4 +1450,4 @@ _io_BufferedRandom___init__(PyObject *self, PyObject *args, PyObject *kwargs) #define _IO_BUFFEREDRWPAIR_CLOSED_GETSETDEF {"closed", (getter)_io_BufferedRWPair_closed_get, (setter)NULL, NULL}, -/*[clinic end generated code: output=901ead01a5201538 input=a9049054013a1b77]*/ +/*[clinic end generated code: output=138736d7cf1e7de1 input=a9049054013a1b77]*/