diff -r 253c31930b32 Lib/test/test_io.py --- a/Lib/test/test_io.py Sat Jul 26 00:58:55 2014 +0200 +++ b/Lib/test/test_io.py Sat Jul 26 12:49:05 2014 +0200 @@ -1189,6 +1189,13 @@ with self.assertRaisesRegex(TypeError, "BufferedReader"): self.tp(io.BytesIO(), 1024, 1024, 1024) + def test_resource_warning(self): + with support.check_warnings(('', ResourceWarning)): + stream = open(support.TESTFN, 'r') + self.addCleanup(stream.close) + bufio = self.tp(stream) + del bufio + support.gc_collect() class PyBufferedReaderTest(BufferedReaderTest): tp = pyio.BufferedReader @@ -1497,6 +1504,14 @@ with self.assertRaisesRegex(TypeError, "BufferedWriter"): self.tp(io.BytesIO(), 1024, 1024, 1024) + def test_resource_warning(self): + with support.check_warnings(('', ResourceWarning)): + stream = open(support.TESTFN, 'w') + self.addCleanup(stream.close) + bufio = self.tp(stream) + del bufio + support.gc_collect() + class PyBufferedWriterTest(BufferedWriterTest): tp = pyio.BufferedWriter diff -r 253c31930b32 Modules/_io/textio.c --- a/Modules/_io/textio.c Sat Jul 26 00:58:55 2014 +0200 +++ b/Modules/_io/textio.c Sat Jul 26 12:49:05 2014 +0200 @@ -1160,6 +1160,18 @@ Py_TYPE(self)->tp_free((PyObject *)self); } +static PyObject* +textiowrapper_dealloc_warn(textio *self, PyObject *source) +{ + PyObject* res; + res = _PyObject_CallMethodId(self->buffer, &PyId__dealloc_warn, "O", self); + if (res) + Py_DECREF(res); + else + PyErr_Clear(); + Py_RETURN_NONE; +} + static int textiowrapper_traverse(textio *self, visitproc visit, void *arg) { @@ -2754,6 +2766,7 @@ {"seek", (PyCFunction)textiowrapper_seek, METH_VARARGS}, {"tell", (PyCFunction)textiowrapper_tell, METH_NOARGS}, {"truncate", (PyCFunction)textiowrapper_truncate, METH_VARARGS}, + {"_dealloc_warn", (PyCFunction)textiowrapper_dealloc_warn, METH_O, NULL}, {NULL, NULL} };