Skip to content

Commit cc005ab

Browse files
committed
o make StopIteration (and the like) a 'sink state' in iter and call-iter, to
fix the test_iter test_sinkstate_* tests o workaround test_iter test_unpack_iter GC related issues
1 parent b0ecc7c commit cc005ab

File tree

3 files changed

+33
-10
lines changed

3 files changed

+33
-10
lines changed

Lib/test/test_iter.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -780,18 +780,24 @@ def test_unpack_iter(self):
780780

781781
# Test reference count behavior
782782

783-
class C(object):
783+
# XXX: Jython new style objects don't support __del__ yet
784+
from test_weakref import extra_collect
785+
#class C(object):
786+
class C:
784787
count = 0
785-
def __new__(cls):
788+
#def __new__(cls):
789+
def __init__(self):
790+
cls = C
786791
cls.count += 1
787-
return object.__new__(cls)
792+
#return object.__new__(cls)
788793
def __del__(self):
789794
cls = self.__class__
790795
assert cls.count > 0
791796
cls.count -= 1
792797
x = C()
793798
self.assertEqual(C.count, 1)
794799
del x
800+
extra_collect()
795801
self.assertEqual(C.count, 0)
796802
l = [C(), C(), C()]
797803
self.assertEqual(C.count, 3)
@@ -800,6 +806,7 @@ def __del__(self):
800806
except ValueError:
801807
pass
802808
del l
809+
extra_collect()
803810
self.assertEqual(C.count, 0)
804811

805812

src/org/python/core/PyCallIter.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,25 @@ public PyCallIter(PyObject callable, PyObject sentinel) {
1515
}
1616

1717
public PyObject __iternext__() {
18-
PyObject val = null;
18+
if (callable == null) {
19+
return null;
20+
}
21+
22+
PyObject result;
1923
try {
20-
val = callable.__call__();
24+
result = callable.__call__();
2125
} catch (PyException exc) {
2226
if (Py.matchException(exc, Py.StopIteration)) {
27+
callable = null;
2328
stopException = exc;
2429
return null;
2530
}
2631
throw exc;
2732
}
28-
if (val._eq(sentinel).__nonzero__()) {
33+
if (result._eq(sentinel).__nonzero__()) {
34+
callable = null;
2935
return null;
3036
}
31-
return val;
37+
return result;
3238
}
3339
}

src/org/python/core/PySequenceIter.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,24 @@ public PySequenceIter(PyObject seq) {
1010
}
1111

1212
public PyObject __iternext__() {
13+
if (seq == null) {
14+
return null;
15+
}
16+
17+
PyObject result;
1318
try {
14-
return seq.__finditem__(idx++);
19+
result = seq.__finditem__(idx++);
1520
} catch (PyException exc) {
16-
if (Py.matchException(exc, Py.StopIteration))
21+
if (Py.matchException(exc, Py.StopIteration)) {
22+
seq = null;
1723
return null;
24+
}
1825
throw exc;
1926
}
27+
if (result == null) {
28+
seq = null;
29+
}
30+
return result;
2031
}
21-
2232
}
2333

0 commit comments

Comments
 (0)