Commits

Anonymous committed 1c4d19b

SF bug #770485: cStringIO does not set closed attr

  • Participants
  • Parent commits 74ef8b8
  • Branches 2.3

Comments (0)

Files changed (3)

Lib/test/test_StringIO.py

         f.close()
         self.assertRaises(ValueError, f.write, 'frobnitz')
 
+    def test_closed_flag(self):
+        f = self.MODULE.StringIO()
+        self.assertEqual(f.closed, False)
+        f.close()
+        self.assertEqual(f.closed, True)
+        f = self.MODULE.StringIO("abc")
+        self.assertEqual(f.closed, False)
+        f.close()
+        self.assertEqual(f.closed, True)
+
     def test_iterator(self):
         eq = self.assertEqual
         unless = self.failUnless
 Extension modules
 -----------------
 
+- Bug #770485: cStringIO did not support the f.closed attribute.
+
 - Patch #781722: Gracefully reject AF_INET6 in socket.inet_pton
   if IPv6 is disabled.
 

Modules/cStringIO.c

 }
 
 static PyObject *
+IO_get_closed(IOobject *self, void *closure)
+{
+	PyObject *result = Py_False;
+
+	if (self->buf == NULL)
+		result = Py_True;
+	Py_INCREF(result);
+	return result;
+}
+
+static PyGetSetDef file_getsetlist[] = {
+	{"closed", (getter)IO_get_closed, NULL, "True if the file is closed"},
+	{0},
+};
+
+static PyObject *
 IO_flush(IOobject *self, PyObject *unused) {
 
         UNLESS (IO__opencheck(self)) return NULL;
 static PyMemberDef O_memberlist[] = {
 	{"softspace",	T_INT,	offsetof(Oobject, softspace),	0,
 	 "flag indicating that a space needs to be printed; used by print"},
+	 /* getattr(f, "closed") is implemented without this table */
 	{NULL} /* Sentinel */
 };
 
   PyObject_SelfIter,		/*tp_iter */
   (iternextfunc)IO_iternext,	/*tp_iternext */
   O_methods,			/*tp_methods */
-  O_memberlist			/*tp_members */
+  O_memberlist,			/*tp_members */
+  file_getsetlist,		/*tp_getset */
 };
 
 static PyObject *
   0,					/* tp_weaklistoffset */
   PyObject_SelfIter,			/* tp_iter */
   (iternextfunc)IO_iternext,		/* tp_iternext */
-  I_methods				/* tp_methods */
+  I_methods,				/* tp_methods */
+  0,					/* tp_members */
+  file_getsetlist,			/* tp_getset */
 };
 
 static PyObject *