[Python-checkins] cpython (3.2): add a __dict__ descr for IOBase (closes #12878)

benjamin.peterson python-checkins at python.org
Sat Sep 3 15:32:30 CEST 2011


http://hg.python.org/cpython/rev/8ec8a4579788
changeset:   72216:8ec8a4579788
branch:      3.2
parent:      72213:14c97947e87c
user:        Benjamin Peterson <benjamin at python.org>
date:        Sat Sep 03 09:26:20 2011 -0400
summary:
  add a __dict__ descr for IOBase (closes #12878)

files:
  Lib/test/test_io.py  |  11 +++++++++++
  Misc/NEWS            |   2 ++
  Modules/_io/iobase.c |  14 ++++++++++++++
  3 files changed, 27 insertions(+), 0 deletions(-)


diff --git a/Lib/test/test_io.py b/Lib/test/test_io.py
--- a/Lib/test/test_io.py
+++ b/Lib/test/test_io.py
@@ -610,6 +610,17 @@
         self.assertEqual(rawio.read(2), None)
         self.assertEqual(rawio.read(2), b"")
 
+    def test_types_have_dict(self):
+        test = (
+            self.IOBase(),
+            self.RawIOBase(),
+            self.TextIOBase(),
+            self.StringIO(),
+            self.BytesIO()
+        )
+        for obj in test:
+            self.assertTrue(hasattr(obj, "__dict__"))
+
 class CIOTest(IOTest):
 
     def test_IOBase_finalize(self):
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -28,6 +28,8 @@
 Library
 -------
 
+- Issue #12878: Expose a __dict__ attribute on io.IOBase and its subclasses.
+
 - Issue #12636: IDLE reads the coding cookie when executing a Python script.
 
 - Issue #10946: The distutils commands bdist_dumb, bdist_wininst and bdist_msi
diff --git a/Modules/_io/iobase.c b/Modules/_io/iobase.c
--- a/Modules/_io/iobase.c
+++ b/Modules/_io/iobase.c
@@ -156,6 +156,19 @@
     return PyBool_FromLong(IS_CLOSED(self));
 }
 
+static PyObject *
+iobase_get_dict(PyObject *self)
+{
+    PyObject **dictptr = _PyObject_GetDictPtr(self);
+    PyObject *dict;
+    assert(dictptr);
+    dict = *dictptr;
+    if (dict == NULL)
+        dict = *dictptr = PyDict_New();
+    Py_XINCREF(dict);
+    return dict;
+}
+
 PyObject *
 _PyIOBase_check_closed(PyObject *self, PyObject *args)
 {
@@ -691,6 +704,7 @@
 };
 
 static PyGetSetDef iobase_getset[] = {
+    {"__dict__", iobase_get_dict, NULL, NULL},
     {"closed", (getter)iobase_closed_get, NULL, NULL},
     {NULL}
 };

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list