Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bb8f59a

Browse files
committed
unpack_iterable(): Add a missing DECREF in an error case. Reported by
Armin Rigo (SF bug #488477). Added a testcase to test_unpack_iter() in test_iter.py.
1 parent 2009aa6 commit bb8f59a

2 files changed

Lines changed: 24 additions & 0 deletions

File tree

Lib/test/test_iter.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -747,6 +747,29 @@ def test_unpack_iter(self):
747747
(a, b), (c,) = IteratingSequenceClass(2), {42: 24}
748748
self.assertEqual((a, b, c), (0, 1, 42))
749749

750+
# Test reference count behavior
751+
752+
class C(object):
753+
count = 0
754+
def __new__(cls):
755+
cls.count += 1
756+
return object.__new__(cls)
757+
def __del__(self):
758+
cls = self.__class__
759+
assert cls.count > 0
760+
cls.count -= 1
761+
x = C()
762+
self.assertEqual(C.count, 1)
763+
del x
764+
self.assertEqual(C.count, 0)
765+
l = [C(), C(), C()]
766+
self.assertEqual(C.count, 3)
767+
try:
768+
a, b = iter(l)
769+
except ValueError:
770+
pass
771+
del l
772+
self.assertEqual(C.count, 0)
750773

751774
def test_main():
752775
run_unittest(TestCase)

Python/ceval.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2796,6 +2796,7 @@ unpack_iterable(PyObject *v, int argcnt, PyObject **sp)
27962796
Py_DECREF(it);
27972797
return 1;
27982798
}
2799+
Py_DECREF(w);
27992800
PyErr_SetString(PyExc_ValueError, "too many values to unpack");
28002801
/* fall through */
28012802
Error:

0 commit comments

Comments
 (0)