|
3 | 3 | #########################################################################
|
4 | 4 | # Iterating over Python objects in Julia
|
5 | 5 |
|
| 6 | +Base.IteratorSize(::Type{PyObject}) = Base.SizeUnknown() |
6 | 7 | function _start(po::PyObject)
|
7 | 8 | sigatomic_begin()
|
8 | 9 | try
|
|
29 | 30 |
|
30 | 31 | Base.done(po::PyObject, s) = ispynull(s[1])
|
31 | 32 | else
|
32 |
| - function Base.iterate(po::PyObject, s=_start(po)) |
| 33 | + """ |
| 34 | + PyIterator{T}(pyobject) |
| 35 | +
|
| 36 | + Wrap `pyobject::PyObject` into an iterator, that produces items of type `T`. To be more precise `convert(T, item)` is applied in each iteration. This can be useful to avoid automatic conversion of items into corresponding julia types. |
| 37 | + ```jldoctest |
| 38 | + julia> using PyCall |
| 39 | +
|
| 40 | + julia> l = PyObject([PyObject(1), PyObject(2)]) |
| 41 | + PyObject [1, 2] |
| 42 | +
|
| 43 | + julia> piter = PyCall.PyIterator{PyAny}(l) |
| 44 | + PyCall.PyIterator{PyAny,Base.HasLength()}(PyObject [1, 2]) |
| 45 | +
|
| 46 | + julia> collect(piter) |
| 47 | + 2-element Array{Any,1}: |
| 48 | + 1 |
| 49 | + 2 |
| 50 | +
|
| 51 | + julia> piter = PyCall.PyIterator(l) |
| 52 | + PyCall.PyIterator{PyObject,Base.HasLength()}(PyObject [1, 2]) |
| 53 | +
|
| 54 | + julia> collect(piter) |
| 55 | + 2-element Array{PyObject,1}: |
| 56 | + PyObject 1 |
| 57 | + PyObject 2 |
| 58 | + ``` |
| 59 | + """ |
| 60 | + struct PyIterator{T,S} |
| 61 | + o::PyObject |
| 62 | + end |
| 63 | + |
| 64 | + function _compute_IteratorSize(o::PyObject) |
| 65 | + S = try |
| 66 | + length(o) |
| 67 | + Base.HasLength |
| 68 | + catch err |
| 69 | + if !(err isa PyError && pyisinstance(err.val, @pyglobalobjptr :PyExc_TypeError)) |
| 70 | + rethrow() |
| 71 | + end |
| 72 | + Base.SizeUnknown |
| 73 | + end |
| 74 | + end |
| 75 | + function PyIterator(o::PyObject) |
| 76 | + PyIterator{PyObject}(o) |
| 77 | + end |
| 78 | + function (::Type{PyIterator{T}})(o::PyObject) where {T} |
| 79 | + S = _compute_IteratorSize(o) |
| 80 | + PyIterator{T,S}(o) |
| 81 | + end |
| 82 | + |
| 83 | + Base.eltype(::Type{<:PyIterator{T}}) where T = T |
| 84 | + Base.eltype(::Type{<:PyIterator{PyAny}}) = Any |
| 85 | + Base.length(piter::PyIterator) = length(piter.o) |
| 86 | + |
| 87 | + Base.IteratorSize(::Type{<: PyIterator{T,S}}) where {T,S} = S() |
| 88 | + |
| 89 | + _start(piter::PyIterator) = _start(piter.o) |
| 90 | + |
| 91 | + function Base.iterate(piter::PyIterator{T}, s=_start(piter)) where {T} |
33 | 92 | ispynull(s[1]) && return nothing
|
34 | 93 | sigatomic_begin()
|
35 | 94 | try
|
36 | 95 | nxt = PyObject(@pycheck ccall((@pysym :PyIter_Next), PyPtr, (PyPtr,), s[2]))
|
37 |
| - return (convert(PyAny, s[1]), (nxt, s[2])) |
| 96 | + return (convert(T,s[1]), (nxt, s[2])) |
38 | 97 | finally
|
39 | 98 | sigatomic_end()
|
40 | 99 | end
|
41 | 100 | end
|
| 101 | + function Base.iterate(po::PyObject, s=_start(po)) |
| 102 | + # avoid the constructor that calls length |
| 103 | + # since that might be an expensive operation |
| 104 | + # even if length is cheap, this adds 10% performance |
| 105 | + piter = PyIterator{PyAny, Base.SizeUnknown}(po) |
| 106 | + iterate(piter, s) |
| 107 | + end |
42 | 108 | end
|
43 | 109 |
|
44 | 110 | # issue #216
|
|
0 commit comments