-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathPyCallIter.java
More file actions
56 lines (46 loc) · 1.52 KB
/
Copy pathPyCallIter.java
File metadata and controls
56 lines (46 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package org.python.core;
import org.python.expose.ExposedType;
@ExposedType(name = "callable_iterator")
public class PyCallIter extends PyIterator {
//note: Already implements Traverseproc, inheriting it from PyIterator
private PyObject callable;
private PyObject sentinel;
public PyCallIter(PyObject callable, PyObject sentinel) {
if (!callable.isCallable()) {
throw Py.TypeError("iter(v, w): v must be callable");
}
this.callable = callable;
this.sentinel = sentinel;
}
public PyObject __next__() {
if (callable == null) {
throw Py.StopIteration();
}
PyObject result;
result = callable.__call__();
if (result == null || sentinel.richCompare(result, CompareOp.EQ).__bool__()) {
callable = null;
throw Py.StopIteration();
}
return result;
}
/* Traverseproc implementation */
@Override
public int traverse(Visitproc visit, Object arg) {
int retValue = super.traverse(visit, arg);
if (retValue != 0) {
return retValue;
}
if (callable != null) {
retValue = visit.visit(callable, arg);
if (retValue != 0) {
return retValue;
}
}
return sentinel != null ? visit.visit(sentinel, arg) : 0;
}
@Override
public boolean refersDirectlyTo(PyObject ob) {
return ob != null && (ob == callable || ob == sentinel || super.refersDirectlyTo(ob));
}
}