Skip to content

Commit ce7d10c

Browse files
committed
Issue #1445: Fix a SystemError when accessing the cell_contents
attribute of an empty cell object. Now a ValueError is raised.
1 parent 6dae85f commit ce7d10c

3 files changed

Lines changed: 21 additions & 1 deletion

File tree

Lib/test/test_funcattrs.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,17 @@ def f(): print a
242242
verify(c[0].__class__.__name__ == "cell") # don't have a type object handy
243243
cantset(f, "func_closure", c)
244244

245+
def test_empty_cell():
246+
def f(): print a
247+
try:
248+
f.func_closure[0].cell_contents
249+
except ValueError:
250+
pass
251+
else:
252+
raise TestFailed, "shouldn't be able to read an empty cell"
253+
254+
a = 12
255+
245256
def test_func_doc():
246257
def f(): pass
247258
verify(f.__doc__ is None)
@@ -385,6 +396,7 @@ def foo(self): pass
385396

386397
def testmore():
387398
test_func_closure()
399+
test_empty_cell()
388400
test_func_doc()
389401
test_func_globals()
390402
test_func_name()

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ What's New in Python 2.6 alpha 1?
1212
Core and builtins
1313
-----------------
1414

15+
- Issue #1445: Fix a SystemError when accessing the ``cell_contents``
16+
attribute of an empty cell object.
17+
1518
- Issue #1460: The utf-7 incremental decoder did not accept truncated input.
1619
It now correctly saves its state between chunks of data.
1720

Objects/cellobject.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,12 @@ cell_clear(PyCellObject *op)
8989
static PyObject *
9090
cell_get_contents(PyCellObject *op, void *closure)
9191
{
92-
Py_XINCREF(op->ob_ref);
92+
if (op->ob_ref == NULL)
93+
{
94+
PyErr_SetString(PyExc_ValueError, "Cell is empty");
95+
return NULL;
96+
}
97+
Py_INCREF(op->ob_ref);
9398
return op->ob_ref;
9499
}
95100

0 commit comments

Comments
 (0)