From ce7d10ccc4d04fd96dfd5c0d239ed81357efd3f8 Mon Sep 17 00:00:00 2001 From: Amaury Forgeot d'Arc Date: Sat, 24 Nov 2007 13:44:17 +0000 Subject: Issue #1445: Fix a SystemError when accessing the ``cell_contents`` attribute of an empty cell object. Now a ValueError is raised. --- Lib/test/test_funcattrs.py | 12 ++++++++++++ Misc/NEWS | 3 +++ Objects/cellobject.c | 7 ++++++- 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_funcattrs.py b/Lib/test/test_funcattrs.py index 7a083b7..a32be38 100644 --- a/Lib/test/test_funcattrs.py +++ b/Lib/test/test_funcattrs.py @@ -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() diff --git a/Misc/NEWS b/Misc/NEWS index c2ea956..15fa9be 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -12,6 +12,9 @@ What's New in Python 2.6 alpha 1? Core and builtins ----------------- +- Issue #1445: Fix a SystemError when accessing the ``cell_contents`` + attribute of an empty cell object. + - Issue #1460: The utf-7 incremental decoder did not accept truncated input. It now correctly saves its state between chunks of data. diff --git a/Objects/cellobject.c b/Objects/cellobject.c index 63322c1..dc684d5 100644 --- a/Objects/cellobject.c +++ b/Objects/cellobject.c @@ -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; } -- cgit v0.12