Skip to content

Commit e9175d5

Browse files
author
Christopher Doris
committed
remove pyconvert_and_del, since it is now possible for the return value to alias the input
1 parent 18459ed commit e9175d5

14 files changed

+55
-55
lines changed

src/abstract/collection.jl

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ function _pyconvert_rule_iterable(ans::Vector{T0}, it::Py, ::Type{T1}) where {T0
77
pydel!(it)
88
return pyconvert_return(ans)
99
end
10-
x = @pyconvert_and_del(T1, x_)
10+
x = @pyconvert(T1, x_)
1111
if x isa T0
1212
push!(ans, x)
1313
@goto again
@@ -33,7 +33,7 @@ function _pyconvert_rule_iterable(ans::Set{T0}, it::Py, ::Type{T1}) where {T0,T1
3333
pydel!(it)
3434
return pyconvert_return(ans)
3535
end
36-
x = @pyconvert_and_del(T1, x_)
36+
x = @pyconvert(T1, x_)
3737
if x isa T0
3838
push!(ans, x)
3939
@goto again
@@ -60,8 +60,8 @@ function _pyconvert_rule_mapping(ans::Dict{K0,V0}, x::Py, it::Py, ::Type{K1}, ::
6060
return pyconvert_return(ans)
6161
end
6262
v_ = pygetitem(x, k_)
63-
k = @pyconvert_and_del(K1, k_)
64-
v = @pyconvert_and_del(V1, v_)
63+
k = @pyconvert(K1, k_)
64+
v = @pyconvert(V1, v_)
6565
if k isa K0 && v isa V0
6666
push!(ans, k => v)
6767
@goto again
@@ -101,7 +101,7 @@ function pyconvert_rule_iterable(::Type{T}, xs::Py) where {T<:Tuple}
101101
else
102102
return pyconvert_unconverted()
103103
end
104-
z = @pyconvert_and_del(t, x)
104+
z = @pyconvert(t, x)
105105
push!(zs, z)
106106
end
107107
return length(zs) < length(ts) ? pyconvert_unconverted() : pyconvert_return(T(zs))
@@ -116,7 +116,7 @@ for N in 0:16
116116
n = pylen(xs)
117117
n == $N || return pyconvert_unconverted()
118118
$((
119-
:($z = @pyconvert_and_del($T, pytuple_getitem(xs, $(i-1))))
119+
:($z = @pyconvert($T, pytuple_getitem(xs, $(i-1))))
120120
for (i, T, z) in zip(1:N, Ts, zs)
121121
)...)
122122
return pyconvert_return(($(zs...),))
@@ -127,12 +127,12 @@ for N in 0:16
127127
n = pylen(xs)
128128
n $N || return pyconvert_unconverted()
129129
$((
130-
:($z = @pyconvert_and_del($T, pytuple_getitem(xs, $(i-1))))
130+
:($z = @pyconvert($T, pytuple_getitem(xs, $(i-1))))
131131
for (i, T, z) in zip(1:N, Ts, zs)
132132
)...)
133133
vs = V[]
134134
for i in $(N+1):n
135-
v = @pyconvert_and_del(V, pytuple_getitem(xs, i-1))
135+
v = @pyconvert(V, pytuple_getitem(xs, i-1))
136136
push!(vs, v)
137137
end
138138
return pyconvert_return(($(zs...), vs...))
@@ -149,14 +149,14 @@ function pyconvert_rule_iterable(::Type{R}, x::Py, ::Type{Pair{K0,V0}}=Utils._ty
149149
pydel!(k_)
150150
return pyconvert_unconverted()
151151
end
152-
k = @pyconvert_and_del(K1, k_)
152+
k = @pyconvert(K1, k_)
153153
v_ = unsafe_pynext(it)
154154
if pyisnull(v_)
155155
pydel!(it)
156156
pydel!(v_)
157157
return pyconvert_unconverted()
158158
end
159-
v = @pyconvert_and_del(V1, v_)
159+
v = @pyconvert(V1, v_)
160160
z_ = unsafe_pynext(it)
161161
pydel!(it)
162162
if pyisnull(z_)

src/concrete/code.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ function pyeval(::Type{T}, code, globals, locals=nothing) where {T}
4242
globals_, locals_ = _pyeval_args(globals, locals)
4343
ans = pybuiltins.eval(code, globals_, locals_)
4444
pydel!(locals_)
45-
return T == Py ? ans : pyconvert_and_del(T, ans)
45+
return T == Py ? ans : pyconvert(T, ans)
4646
end
4747
pyeval(code, globals, locals=nothing) = pyeval(Py, code, globals, locals)
4848
export pyeval
@@ -57,7 +57,7 @@ _pyexec_ans(::Type{Nothing}, globals, locals) = nothing
5757
for i in 1:n
5858
v = Symbol(:ans, i)
5959
push!(vars, v)
60-
push!(code, :($v = pyconvert_and_del($(types.parameters[i]), pygetitem(locals, $(string(names[i]))))))
60+
push!(code, :($v = pyconvert($(types.parameters[i]), pygetitem(locals, $(string(names[i]))))))
6161
end
6262
push!(code, :(return $(NamedTuple{names, types})(($(vars...),))))
6363
return Expr(:block, code...)

src/concrete/datetime.jl

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -66,18 +66,18 @@ end
6666
function pyconvert_rule_date(::Type{Date}, x::Py)
6767
# datetime is a subtype of date, but we shouldn't convert datetime to Date since it's lossy
6868
pyisinstance(x, pydatetimetype) && return pyconvert_unconverted()
69-
year = pyconvert_and_del(Int, x.year)
70-
month = pyconvert_and_del(Int, x.month)
71-
day = pyconvert_and_del(Int, x.day)
69+
year = pyconvert(Int, x.year)
70+
month = pyconvert(Int, x.month)
71+
day = pyconvert(Int, x.day)
7272
pyconvert_return(Date(year, month, day))
7373
end
7474

7575
function pyconvert_rule_time(::Type{Time}, x::Py)
7676
pytime_isaware(x) && return pyconvert_unconverted()
77-
hour = pyconvert_and_del(Int, x.hour)
78-
minute = pyconvert_and_del(Int, x.minute)
79-
second = pyconvert_and_del(Int, x.second)
80-
microsecond = pyconvert_and_del(Int, x.microsecond)
77+
hour = pyconvert(Int, x.hour)
78+
minute = pyconvert(Int, x.minute)
79+
second = pyconvert(Int, x.second)
80+
microsecond = pyconvert(Int, x.microsecond)
8181
return pyconvert_return(Time(hour, minute, second, div(microsecond, 1000), mod(microsecond, 1000)))
8282
end
8383

@@ -86,9 +86,9 @@ function pyconvert_rule_datetime(::Type{DateTime}, x::Py)
8686
# compute the time since _base_datetime
8787
# this accounts for fold
8888
d = x - _base_pydatetime
89-
days = pyconvert_and_del(Int, d.days)
90-
seconds = pyconvert_and_del(Int, d.seconds)
91-
microseconds = pyconvert_and_del(Int, d.microseconds)
89+
days = pyconvert(Int, d.days)
90+
seconds = pyconvert(Int, d.seconds)
91+
microseconds = pyconvert(Int, d.microseconds)
9292
pydel!(d)
9393
iszero(mod(microseconds, 1000)) || return pyconvert_unconverted()
9494
return pyconvert_return(_base_datetime + Millisecond(div(microseconds, 1000) + 1000 * (seconds + 60 * 60 * 24 * days)))

src/concrete/fraction.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ export pyfraction
66

77
# works for any collections.abc.Rational
88
function pyconvert_rule_fraction(::Type{R}, x::Py, ::Type{Rational{T0}}=Utils._type_lb(R), ::Type{Rational{T1}}=Utils._type_ub(R)) where {R<:Rational,T0,T1}
9-
a = @pyconvert_and_del(Utils._typeintersect(Integer, T1), x.numerator)
10-
b = @pyconvert_and_del(Utils._typeintersect(Integer, T1), x.denominator)
9+
a = @pyconvert(Utils._typeintersect(Integer, T1), x.numerator)
10+
b = @pyconvert(Utils._typeintersect(Integer, T1), x.denominator)
1111
a, b = promote(a, b)
1212
T2 = Utils._promote_type_bounded(T0, typeof(a), typeof(b), T1)
1313
pyconvert_return(Rational{T2}(a, b))

src/jlwrap/array.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ const pyjlarraytype = pynew()
22

33
function pyjl_getaxisindex(x::AbstractUnitRange{<:Integer}, k::Py)
44
if pyisslice(k)
5-
a = @pyconvert_and_del Union{Int,Nothing} k.start begin
5+
a = @pyconvert Union{Int,Nothing} k.start begin
66
errset(pybuiltins.TypeError, "slice components must be integers")
77
pythrow()
88
end
9-
b = @pyconvert_and_del Union{Int,Nothing} k.step begin
9+
b = @pyconvert Union{Int,Nothing} k.step begin
1010
errset(pybuiltins.TypeError, "slice components must be integers")
1111
pythrow()
1212
end
13-
c = @pyconvert_and_del Union{Int,Nothing} k.stop begin
13+
c = @pyconvert Union{Int,Nothing} k.stop begin
1414
errset(pybuiltins.TypeError, "slice components must be integers")
1515
pythrow()
1616
end

src/jlwrap/dict.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ pyjldict_delitem(x::AbstractDict, k::Py) = (delete!(x, pyconvert(keytype(x), k))
2323

2424
function pyjldict_update(x::AbstractDict, items_::Py)
2525
for item_ in items_
26-
(k, v) = pyconvert_and_del(Tuple{keytype(x), valtype(x)}, item_)
26+
(k, v) = pyconvert(Tuple{keytype(x), valtype(x)}, item_)
2727
x[k] = v
2828
end
2929
Py(nothing)

src/jlwrap/vector.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ end
5353

5454
function pyjlvector_extend(x::AbstractVector, vs_::Py)
5555
for v_ in vs_
56-
v = pyconvert_and_del(eltype(x), v_)
56+
v = pyconvert(eltype(x), v_)
5757
push!(x, v)
5858
end
5959
Py(nothing)

src/pywrap/PyArray.jl

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -193,15 +193,15 @@ function PyArraySource_ArrayInterface(x::Py)
193193
d = x.__array_interface__
194194
# offset
195195
# TODO: how is the offset measured?
196-
offset = pyconvert_and_del(Int, @py d.get("offset", 0))
196+
offset = pyconvert(Int, @py d.get("offset", 0))
197197
offset == 0 || error("not supported: non-zero offset")
198198
# mask
199199
@py("mask" in d) && error("not supported: mask")
200200
# data
201201
data = @py d.get("data")
202202
if pyistuple(data)
203-
ptr = Ptr{Cvoid}(pyconvert_and_del(UInt, data[0]))
204-
readonly = pyconvert_and_del(Bool, data[1])
203+
ptr = Ptr{Cvoid}(pyconvert(UInt, data[0]))
204+
readonly = pyconvert(Bool, data[1])
205205
pydel!(data)
206206
handle = Py((x, d))
207207
else
@@ -268,7 +268,7 @@ pyarray_typestrdescr_to_type(ts::String, descr::Py) = begin
268268
end
269269

270270
function pyarray_get_R(src::PyArraySource_ArrayInterface)
271-
typestr = pyconvert_and_del(String, src.dict["typestr"])
271+
typestr = pyconvert(String, src.dict["typestr"])
272272
descr = @py @jl(src.dict).get("descr")
273273
R = pyarray_typestrdescr_to_type(typestr, descr)::DataType
274274
pydel!(descr)
@@ -279,15 +279,15 @@ pyarray_get_ptr(src::PyArraySource_ArrayInterface, ::Type{R}) where {R} = Ptr{R}
279279

280280
pyarray_get_N(src::PyArraySource_ArrayInterface) = Int(@py jllen(@jl(src.dict)["shape"]))
281281

282-
pyarray_get_size(src::PyArraySource_ArrayInterface, ::Val{N}) where {N} = pyconvert_and_del(NTuple{N,Int}, src.dict["shape"])
282+
pyarray_get_size(src::PyArraySource_ArrayInterface, ::Val{N}) where {N} = pyconvert(NTuple{N,Int}, src.dict["shape"])
283283

284284
function pyarray_get_strides(src::PyArraySource_ArrayInterface, ::Val{N}, ::Type{R}, size::NTuple{N,Int}) where {R,N}
285285
@py strides = @jl(src.dict).get("strides")
286286
if pyisnone(strides)
287287
pydel!(strides)
288288
return Utils.size_to_cstrides(sizeof(R), size)
289289
else
290-
return pyconvert_and_del(NTuple{N,Int}, strides)
290+
return pyconvert(NTuple{N,Int}, strides)
291291
end
292292
end
293293

@@ -434,7 +434,7 @@ pyarray_load(::Type{R}, p::Ptr{R}) where {R} = unsafe_load(p)
434434
pyarray_load(::Type{T}, p::Ptr{UnsafePyObject}) where {T} = begin
435435
u = unsafe_load(p)
436436
o = u.ptr == C_NULL ? pynew(Py(nothing)) : pynew(incref(u.ptr))
437-
T == Py ? o : pyconvert_and_del(T, o)
437+
T == Py ? o : pyconvert(T, o)
438438
end
439439

440440
pyarray_store!(p::Ptr{R}, x::R) where {R} = unsafe_store!(p, x)

src/pywrap/PyDict.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,20 +34,20 @@ function Base.iterate(x::PyDict{K,V}, it::Py=pyiter(x)) where {K,V}
3434
k_ = unsafe_pynext(it)
3535
pyisnull(k_) && return nothing
3636
v_ = pygetitem(x, k_)
37-
k = pyconvert_and_del(K, k_)
38-
v = pyconvert_and_del(V, v_)
37+
k = pyconvert(K, k_)
38+
v = pyconvert(V, v_)
3939
return (k => v, it)
4040
end
4141

4242
function Base.iterate(x::Base.KeySet{K,PyDict{K,V}}, it::Py=pyiter(x.dict)) where {K,V}
4343
k_ = unsafe_pynext(it)
4444
pyisnull(k_) && return nothing
45-
k = pyconvert_and_del(K, k_)
45+
k = pyconvert(K, k_)
4646
return (k, it)
4747
end
4848

4949
function Base.getindex(x::PyDict{K,V}, k) where {K,V}
50-
return pyconvert_and_del(V, pygetitem(x, convert(K, k)))
50+
return pyconvert(V, pygetitem(x, convert(K, k)))
5151
end
5252

5353
function Base.setindex!(x::PyDict{K,V}, v, k) where {K,V}

src/pywrap/PyIO.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -126,13 +126,13 @@ function Base.eof(io::PyIO)
126126
end
127127
end
128128

129-
Base.fd(io::PyIO) = pyconvert_and_del(Int, @py io.fileno())
129+
Base.fd(io::PyIO) = pyconvert(Int, @py io.fileno())
130130

131-
Base.isreadable(io::PyIO) = pyconvert_and_del(Bool, @py io.readable())
131+
Base.isreadable(io::PyIO) = pyconvert(Bool, @py io.readable())
132132

133-
Base.iswritable(io::PyIO) = pyconvert_and_del(Bool, @py io.writable())
133+
Base.iswritable(io::PyIO) = pyconvert(Bool, @py io.writable())
134134

135-
Base.isopen(io::PyIO) = !pyconvert_and_del(Bool, @py io.closed)
135+
Base.isopen(io::PyIO) = !pyconvert(Bool, @py io.closed)
136136

137137
function Base.unsafe_write(io::PyIO, ptr::Ptr{UInt8}, n::UInt)
138138
ntodo = n
@@ -235,11 +235,11 @@ function Base.position(io::PyIO)
235235
putobuf(io)
236236
if io.text
237237
if isempty(io.ibuf)
238-
return pyconvert_and_del(Int, @py io.tell())
238+
return pyconvert(Int, @py io.tell())
239239
else
240240
error("`position(io)` text PyIO streams only implemented for empty input buffer (e.g. do `read(io, length(io.ibuf))` first)")
241241
end
242242
else
243-
return pyconvert_and_del(Int, @py io.tell()) - length(io.ibuf)
243+
return pyconvert(Int, @py io.tell()) - length(io.ibuf)
244244
end
245245
end

src/pywrap/PyIterable.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ function Base.iterate(x::PyIterable{T}, it::Py=pyiter(x)) where {T}
2323
pydel!(it)
2424
return nothing
2525
else
26-
return (pyconvert_and_del(T, y), it)
26+
return (pyconvert(T, y), it)
2727
end
2828
end
2929

src/pywrap/PyList.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ Base.size(x::PyList) = (length(x),)
2929

3030
Base.@propagate_inbounds function Base.getindex(x::PyList{T}, i::Int) where {T}
3131
@boundscheck checkbounds(x, i)
32-
return pyconvert_and_del(T, @py x[@jl(i-1)])
32+
return pyconvert(T, @py x[@jl(i-1)])
3333
end
3434

3535
Base.@propagate_inbounds function Base.setindex!(x::PyList{T}, v, i::Int) where {T}
@@ -67,19 +67,19 @@ end
6767

6868
Base.@propagate_inbounds function Base.pop!(x::PyList{T}) where {T}
6969
@boundscheck (isempty(x) && throw(BoundsError(x)))
70-
return pyconvert_and_del(T, @py x.pop())
70+
return pyconvert(T, @py x.pop())
7171
end
7272

7373
if isdefined(Base, :popat!)
7474
Base.@propagate_inbounds function Base.popat!(x::PyList{T}, i::Integer) where {T}
7575
@boundscheck checkbounds(x, i)
76-
return pyconvert_and_del(T, @py x.pop(@jl(i-1)))
76+
return pyconvert(T, @py x.pop(@jl(i-1)))
7777
end
7878
end
7979

8080
Base.@propagate_inbounds function Base.popfirst!(x::PyList{T}) where {T}
8181
@boundscheck checkbounds(x, 1)
82-
return pyconvert_and_del(T, @py x.pop(0))
82+
return pyconvert(T, @py x.pop(0))
8383
end
8484

8585
function Base.reverse!(x::PyList)

src/pywrap/PyPandasDataFrame.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ pyconvert_rule_pandasdataframe(::Type{PyPandasDataFrame}, x::Py) = pyconvert_ret
2929
### Show
3030

3131
function Base.show(io::IO, mime::MIME"text/plain", df::PyPandasDataFrame)
32-
nrows = pyconvert_and_del(Int, @py df.shape[0])
33-
ncols = pyconvert_and_del(Int, @py df.shape[1])
32+
nrows = pyconvert(Int, @py df.shape[0])
33+
ncols = pyconvert(Int, @py df.shape[1])
3434
printstyled(io, nrows, '×', ncols, ' ', typeof(df), '\n', bold=true)
3535
pyshow(io, mime, df)
3636
end

src/pywrap/PySet.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ function Base.iterate(x::PySet{T}, it::Py=pyiter(x)) where {T}
3333
pydel!(it)
3434
return nothing
3535
else
36-
return (pyconvert_and_del(T, y), it)
36+
return (pyconvert(T, y), it)
3737
end
3838
end
3939

@@ -69,7 +69,7 @@ end
6969

7070
Base.@propagate_inbounds function Base.pop!(x::PySet{T}) where {T}
7171
@boundscheck (isempty(x) && throw(ArgumentError("set must be non-empty")))
72-
return pyconvert_and_del(T, @py x.pop())
72+
return pyconvert(T, @py x.pop())
7373
end
7474

7575
function Base.pop!(x::PySet, v)

0 commit comments

Comments
 (0)