]> granicus.if.org Git - python/commitdiff
add a __dict__ descr for IOBase (closes #12878)
authorBenjamin Peterson <benjamin@python.org>
Sat, 3 Sep 2011 13:26:20 +0000 (09:26 -0400)
committerBenjamin Peterson <benjamin@python.org>
Sat, 3 Sep 2011 13:26:20 +0000 (09:26 -0400)
Lib/test/test_io.py
Misc/NEWS
Modules/_io/iobase.c

index 72c9a2d8a730c1df71ddabeb3971bc8ede46cfc0..0dc9d6dff42e404ee3db8f830b2a7d5098bdfcff 100644 (file)
@@ -610,6 +610,17 @@ class IOTest(unittest.TestCase):
         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):
index f489ab257eae93e99d70f00619466a3c19a7fa06..514d19afedd2846b200449593c3fb6e244cced54 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -28,6 +28,8 @@ Core and Builtins
 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
index f06f562947eaeabc3b998e76c1cc6254a503890e..2c59d42421e4aed0507eb93ec22ef40091e0538c 100644 (file)
@@ -156,6 +156,19 @@ iobase_closed_get(PyObject *self, void *context)
     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 PyMethodDef iobase_methods[] = {
 };
 
 static PyGetSetDef iobase_getset[] = {
+    {"__dict__", iobase_get_dict, NULL, NULL},
     {"closed", (getter)iobase_closed_get, NULL, NULL},
     {NULL}
 };