]> granicus.if.org Git - python/commitdiff
Issue #1445: Fix a SystemError when accessing the ``cell_contents``
authorAmaury Forgeot d'Arc <amauryfa@gmail.com>
Sat, 24 Nov 2007 13:53:29 +0000 (13:53 +0000)
committerAmaury Forgeot d'Arc <amauryfa@gmail.com>
Sat, 24 Nov 2007 13:53:29 +0000 (13:53 +0000)
attribute of an empty cell object.  Now a ValueError is raised.

Backport of r59170.

Lib/test/test_funcattrs.py
Misc/NEWS
Objects/cellobject.c

index 7a083b70dfbf18a7b22c24b7434903f8e0b43a66..a32be38333dc56a33fef8ad2d8e4fff37b23d8a8 100644 (file)
@@ -242,6 +242,17 @@ def test_func_closure():
     verify(c[0].__class__.__name__ == "cell") # don't have a type object handy
     cantset(f, "func_closure", c)
 
+def test_empty_cell():
+    def f(): print a
+    try:
+        f.func_closure[0].cell_contents
+    except ValueError:
+        pass
+    else:
+        raise TestFailed, "shouldn't be able to read an empty cell"
+
+    a = 12
+
 def test_func_doc():
     def f(): pass
     verify(f.__doc__ is None)
@@ -385,6 +396,7 @@ def test_im_name():
 
 def testmore():
     test_func_closure()
+    test_empty_cell()
     test_func_doc()
     test_func_globals()
     test_func_name()
index c633032e5bf5fb21d6b225208119fa4318f2c053..f856c1f29ba16c98825ef56c68827c9d1c8a15b8 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -1,4 +1,4 @@
-+++++++++++
++++++++++++
 Python News
 +++++++++++
 
@@ -12,6 +12,9 @@ What's New in Python 2.5.2c1?
 Core and builtins
 -----------------
 
+- Issue #1445: Fix a SystemError when accessing the ``cell_contents`` 
+  attribute of an empty cell object.
+
 - Issue #1265: Fix a problem with sys.settrace, if the tracing function uses a
   generator expression when at the same time the executed code is closing a
   paused generator.
index 65a29aaca8364bdb9a5b481c716ff52d09151cf6..e659555f56b4c72b048446e532bacd9c8f6b42b5 100644 (file)
@@ -89,7 +89,12 @@ cell_clear(PyCellObject *op)
 static PyObject *
 cell_get_contents(PyCellObject *op, void *closure)
 {
-       Py_XINCREF(op->ob_ref);
+       if (op->ob_ref == NULL)
+       {
+               PyErr_SetString(PyExc_ValueError, "Cell is empty");
+               return NULL;
+       }
+       Py_INCREF(op->ob_ref);
        return op->ob_ref;
 }