Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9fd5374

Browse files
committed
merge 3.2 (#12878)
2 parents 35c912f + f6f3a35 commit 9fd5374

3 files changed

Lines changed: 27 additions & 0 deletions

File tree

Lib/test/test_io.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,17 @@ def test_RawIOBase_read(self):
610610
self.assertEqual(rawio.read(2), None)
611611
self.assertEqual(rawio.read(2), b"")
612612

613+
def test_types_have_dict(self):
614+
test = (
615+
self.IOBase(),
616+
self.RawIOBase(),
617+
self.TextIOBase(),
618+
self.StringIO(),
619+
self.BytesIO()
620+
)
621+
for obj in test:
622+
self.assertTrue(hasattr(obj, "__dict__"))
623+
613624
class CIOTest(IOTest):
614625

615626
def test_IOBase_finalize(self):

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,8 @@ Core and Builtins
271271
Library
272272
-------
273273

274+
- Issue #12878: Expose a __dict__ attribute on io.IOBase and its subclasses.
275+
274276
- Issue #12636: IDLE reads the coding cookie when executing a Python script.
275277

276278
- Issue #12494: On error, call(), check_call(), check_output() and

Modules/_io/iobase.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,19 @@ iobase_closed_get(PyObject *self, void *context)
156156
return PyBool_FromLong(IS_CLOSED(self));
157157
}
158158

159+
static PyObject *
160+
iobase_get_dict(PyObject *self)
161+
{
162+
PyObject **dictptr = _PyObject_GetDictPtr(self);
163+
PyObject *dict;
164+
assert(dictptr);
165+
dict = *dictptr;
166+
if (dict == NULL)
167+
dict = *dictptr = PyDict_New();
168+
Py_XINCREF(dict);
169+
return dict;
170+
}
171+
159172
PyObject *
160173
_PyIOBase_check_closed(PyObject *self, PyObject *args)
161174
{
@@ -691,6 +704,7 @@ static PyMethodDef iobase_methods[] = {
691704
};
692705

693706
static PyGetSetDef iobase_getset[] = {
707+
{"__dict__", iobase_get_dict, NULL, NULL},
694708
{"closed", (getter)iobase_closed_get, NULL, NULL},
695709
{NULL}
696710
};

0 commit comments

Comments
 (0)