Skip to content

Commit 59a16f9

Browse files
authored
comparisons return Bool not Py (#702)
* comparisons now return Bool not Py * fix bug in JlIO.fileno() from change of return type of fd() --------- Co-authored-by: Christopher Doris <github.com/cjdoris>
1 parent ae5a02d commit 59a16f9

File tree

5 files changed

+106
-21
lines changed

5 files changed

+106
-21
lines changed

docs/src/releasenotes.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
# Release Notes
22

33
## Unreleased (v1)
4-
* `PythonCall.GC` is now more like `Base.GC`: `enable(true)` replaces `enable()`, `enable(false)` replaces `disable()`, and `gc()` is added.
4+
* Breaking changes to `PythonCall.GC`, which is now more like `Base.GC`:
5+
* `enable(true)` replaces `enable()`.
6+
* `enable(false)` replaces `disable()`.
7+
* `gc()` added.
58
* Breaking changes to Julia wrapper types:
69
* Classes renamed: `ValueBase` to `JlBase`, `AnyValue` to `Jl`, `ArrayValue` to `JlArray`, etc.
710
* Classes removed: `RawValue`, `ModuleValue`, `TypeValue`, `NumberValue`, `ComplexValue`, `RealValue`, `RationalValue`, `IntegerValue`.
@@ -12,6 +15,8 @@
1215
* Methods removed: `_jl_raw()`.
1316
* `pyjl(x)` now always returns a `juliacall.Jl` (it used to select a wrapper type if possible).
1417
* `pyjltype(x)` removed.
18+
* Other breaking changes:
19+
* Comparisons like `==(::Py, ::Py)`, `<(::Py, ::Number)`, `isless(::Number, ::Py)` now return `Bool` instead of `Py`.
1520
* New functions: `pyjlarray`, `pyjldict`, `pyjlset`.
1621

1722
## Unreleased

src/Core/Py.jl

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -363,31 +363,31 @@ Base.broadcastable(x::Py) = Ref(x)
363363
(f::Py)(args...; kwargs...) = pycall(f, args...; kwargs...)
364364

365365
# comparisons
366-
Base.:(==)(x::Py, y::Py) = pyeq(x, y)
367-
Base.:(!=)(x::Py, y::Py) = pyne(x, y)
368-
Base.:(<=)(x::Py, y::Py) = pyle(x, y)
369-
Base.:(<)(x::Py, y::Py) = pylt(x, y)
370-
Base.:(>=)(x::Py, y::Py) = pyge(x, y)
371-
Base.:(>)(x::Py, y::Py) = pygt(x, y)
366+
Base.:(==)(x::Py, y::Py) = pyeq(Bool, x, y)
367+
Base.:(!=)(x::Py, y::Py) = pyne(Bool, x, y)
368+
Base.:(<=)(x::Py, y::Py) = pyle(Bool, x, y)
369+
Base.:(<)(x::Py, y::Py) = pylt(Bool, x, y)
370+
Base.:(>=)(x::Py, y::Py) = pyge(Bool, x, y)
371+
Base.:(>)(x::Py, y::Py) = pygt(Bool, x, y)
372372
Base.isless(x::Py, y::Py) = pylt(Bool, x, y)
373373
Base.isequal(x::Py, y::Py) = pyeq(Bool, x, y)
374374

375375
# we also allow comparison with numbers
376-
Base.:(==)(x::Py, y::Number) = pyeq(x, y)
377-
Base.:(!=)(x::Py, y::Number) = pyne(x, y)
378-
Base.:(<=)(x::Py, y::Number) = pyle(x, y)
379-
Base.:(<)(x::Py, y::Number) = pylt(x, y)
380-
Base.:(>=)(x::Py, y::Number) = pyge(x, y)
381-
Base.:(>)(x::Py, y::Number) = pygt(x, y)
376+
Base.:(==)(x::Py, y::Number) = pyeq(Bool, x, y)
377+
Base.:(!=)(x::Py, y::Number) = pyne(Bool, x, y)
378+
Base.:(<=)(x::Py, y::Number) = pyle(Bool, x, y)
379+
Base.:(<)(x::Py, y::Number) = pylt(Bool, x, y)
380+
Base.:(>=)(x::Py, y::Number) = pyge(Bool, x, y)
381+
Base.:(>)(x::Py, y::Number) = pygt(Bool, x, y)
382382
Base.isless(x::Py, y::Number) = pylt(Bool, x, y)
383383
Base.isequal(x::Py, y::Number) = pyeq(Bool, x, y)
384384

385-
Base.:(==)(x::Number, y::Py) = pyeq(x, y)
386-
Base.:(!=)(x::Number, y::Py) = pyne(x, y)
387-
Base.:(<=)(x::Number, y::Py) = pyle(x, y)
388-
Base.:(<)(x::Number, y::Py) = pylt(x, y)
389-
Base.:(>=)(x::Number, y::Py) = pyge(x, y)
390-
Base.:(>)(x::Number, y::Py) = pygt(x, y)
385+
Base.:(==)(x::Number, y::Py) = pyeq(Bool, x, y)
386+
Base.:(!=)(x::Number, y::Py) = pyne(Bool, x, y)
387+
Base.:(<=)(x::Number, y::Py) = pyle(Bool, x, y)
388+
Base.:(<)(x::Number, y::Py) = pylt(Bool, x, y)
389+
Base.:(>=)(x::Number, y::Py) = pyge(Bool, x, y)
390+
Base.:(>)(x::Number, y::Py) = pygt(Bool, x, y)
391391
Base.isless(x::Number, y::Py) = pylt(Bool, x, y)
392392
Base.isequal(x::Number, y::Py) = pyeq(Bool, x, y)
393393

src/JlWrap/io.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ pyjlio_closed(io::IO) = Py(!isopen(io))
1010
pyjl_handle_error_type(::typeof(pyjlio_closed), io, exc) =
1111
exc isa MethodError && exc.f === isopen ? pybuiltins.ValueError : PyNULL
1212

13-
pyjlio_fileno(io::IO) = Py(fd(io))
13+
pyjlio_fileno(io::IO) = Py(Base.cconvert(Cint, fd(io))::Cint)
1414
pyjl_handle_error_type(::typeof(pyjlio_fileno), io, exc) =
1515
exc isa MethodError && exc.f === fd ? pybuiltins.ValueError : PyNULL
1616

test/Core.jl

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,86 @@
220220
@test Base.Docs.getdoc(pybuiltins.int) isa Markdown.MD
221221
@test Base.Docs.getdoc(PythonCall.PyNULL) === nothing
222222
end
223+
@testset "comparisons" begin
224+
@testset "Py vs Py" begin
225+
# ==
226+
@test Py(1) == Py(1)
227+
@test !(Py(1) == Py(2))
228+
@test !(Py(1) == Py(0))
229+
# !=
230+
@test Py(2) != Py(1)
231+
@test Py(2) != Py(3)
232+
@test !(Py(2) != Py(2))
233+
# <
234+
@test Py(3) < Py(4)
235+
@test !(Py(3) < Py(3))
236+
@test !(Py(3) < Py(2))
237+
# <=
238+
@test Py(4) <= Py(5)
239+
@test Py(4) <= Py(4)
240+
@test !(Py(4) <= Py(3))
241+
# >
242+
@test Py(5) > Py(4)
243+
@test !(Py(5) > Py(5))
244+
@test !(Py(5) > Py(6))
245+
# >=
246+
@test Py(5) >= Py(4)
247+
@test Py(5) >= Py(5)
248+
@test !(Py(5) >= Py(6))
249+
end
250+
@testset "Py vs Number" begin
251+
# ==
252+
@test Py(1) == 1
253+
@test !(Py(1) == 2)
254+
@test !(Py(1) == 0)
255+
# !=
256+
@test Py(2) != 1
257+
@test Py(2) != 3
258+
@test !(Py(2) != 2)
259+
# <
260+
@test Py(3) < 4
261+
@test !(Py(3) < 3)
262+
@test !(Py(3) < 2)
263+
# <=
264+
@test Py(4) <= 5
265+
@test Py(4) <= 4
266+
@test !(Py(4) <= 3)
267+
# >
268+
@test Py(5) > 4
269+
@test !(Py(5) > 5)
270+
@test !(Py(5) > 6)
271+
# >=
272+
@test Py(5) >= 4
273+
@test Py(5) >= 5
274+
@test !(Py(5) >= 6)
275+
end
276+
@testset "Number vs Py" begin
277+
# ==
278+
@test 1 == Py(1)
279+
@test !(1 == Py(2))
280+
@test !(1 == Py(0))
281+
# !=
282+
@test 2 != Py(1)
283+
@test 2 != Py(3)
284+
@test !(2 != Py(2))
285+
# <
286+
@test 3 < Py(4)
287+
@test !(3 < Py(3))
288+
@test !(3 < Py(2))
289+
# <=
290+
@test 4 <= Py(5)
291+
@test 4 <= Py(4)
292+
@test !(4 <= Py(3))
293+
# >
294+
@test 5 > Py(4)
295+
@test !(5 > Py(5))
296+
@test !(5 > Py(6))
297+
# >=
298+
@test 5 >= Py(4)
299+
@test 5 >= Py(5)
300+
@test !(5 >= Py(6))
301+
end
302+
end
223303
end
224304

225305
@testitem "iter" begin

test/JlWrap.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -718,7 +718,7 @@ end
718718
y = pytextio(x)
719719
z = y.fileno()
720720
@test pyisinstance(z, pybuiltins.int)
721-
@test pyeq(Bool, z, fd(x))
721+
@test z == Base.cconvert(Cint, fd(x))
722722
end
723723
end
724724
@testset "flush" begin

0 commit comments

Comments
 (0)