Skip to content

Commit 075cdae

Browse files
author
Christopher Doris
committed
fix reverse operands __radd__ etc
1 parent 38deefe commit 075cdae

File tree

2 files changed

+127
-2
lines changed

2 files changed

+127
-2
lines changed

src/jlwrap/any.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ end
102102
function (op::pyjlany_rev_op)(self, other_::Py)
103103
if pyisjl(other_)
104104
other = pyjlvalue(other_)
105-
Py(op.op(self, other))
105+
Py(op.op(other, self))
106106
else
107107
pybuiltins.NotImplemented
108108
end
@@ -111,7 +111,7 @@ function (op::pyjlany_rev_op)(self, other_::Py, other2_::Py)
111111
if pyisjl(other_) && pyisjl(other2_)
112112
other = pyjlvalue(other_)
113113
other2 = pyjlvalue(other2_)
114-
Py(op.op(self, other, other2))
114+
Py(op.op(other, self, other2))
115115
else
116116
pybuiltins.NotImplemented
117117
end

test/jlwrap.jl

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,128 @@
1+
@testitem "any" begin
2+
struct Foo
3+
value::Int
4+
end
5+
Base.:(+)(x::Foo) = "+ $(x.value)"
6+
Base.:(-)(x::Foo) = "- $(x.value)"
7+
Base.abs(x::Foo) = "abs $(x.value)"
8+
Base.:(~)(x::Foo) = "~ $(x.value)"
9+
Base.:(+)(x::Foo, y::Foo) = "$(x.value) + $(y.value)"
10+
Base.:(-)(x::Foo, y::Foo) = "$(x.value) - $(y.value)"
11+
Base.:(*)(x::Foo, y::Foo) = "$(x.value) * $(y.value)"
12+
Base.:(/)(x::Foo, y::Foo) = "$(x.value) / $(y.value)"
13+
Base.:(÷)(x::Foo, y::Foo) = "$(x.value) ÷ $(y.value)"
14+
Base.:(%)(x::Foo, y::Foo) = "$(x.value) % $(y.value)"
15+
Base.:(^)(x::Foo, y::Foo) = "$(x.value) ^ $(y.value)"
16+
Base.:(<<)(x::Foo, y::Foo) = "$(x.value) << $(y.value)"
17+
Base.:(>>)(x::Foo, y::Foo) = "$(x.value) >> $(y.value)"
18+
Base.:(&)(x::Foo, y::Foo) = "$(x.value) & $(y.value)"
19+
Base.:(|)(x::Foo, y::Foo) = "$(x.value) | $(y.value)"
20+
@testset "pos" begin
21+
z = pyjl(+Foo(1))
22+
@test pyconvert(String, z) == "+ 1"
23+
end
24+
@testset "neg" begin
25+
z = pyjl(-Foo(1))
26+
@test pyconvert(String, z) == "- 1"
27+
end
28+
@testset "abs" begin
29+
z = pyjl(abs(Foo(1)))
30+
@test pyconvert(String, z) == "abs 1"
31+
end
32+
@testset "inv" begin
33+
z = pyjl(~Foo(1))
34+
@test pyconvert(String, z) == "~ 1"
35+
end
36+
@testset "add" begin
37+
z = pyjl(Foo(1)) + pyjl(Foo(2))
38+
@test pyconvert(String, z) == "1 + 2"
39+
end
40+
@testset "radd" begin
41+
z = pyjlraw(Foo(1)) + pyjl(Foo(2))
42+
@test pyconvert(String, z) == "1 + 2"
43+
end
44+
@testset "sub" begin
45+
z = pyjl(Foo(1)) - pyjl(Foo(2))
46+
@test pyconvert(String, z) == "1 - 2"
47+
end
48+
@testset "rsub" begin
49+
z = pyjlraw(Foo(1)) - pyjl(Foo(2))
50+
@test pyconvert(String, z) == "1 - 2"
51+
end
52+
@testset "mul" begin
53+
z = pyjl(Foo(1)) * pyjl(Foo(2))
54+
@test pyconvert(String, z) == "1 * 2"
55+
end
56+
@testset "rmul" begin
57+
z = pyjlraw(Foo(1)) * pyjl(Foo(2))
58+
@test pyconvert(String, z) == "1 * 2"
59+
end
60+
@testset "truediv" begin
61+
z = pyjl(Foo(1)) / pyjl(Foo(2))
62+
@test pyconvert(String, z) == "1 / 2"
63+
end
64+
@testset "rtruediv" begin
65+
z = pyjlraw(Foo(1)) / pyjl(Foo(2))
66+
@test pyconvert(String, z) == "1 / 2"
67+
end
68+
@testset "floordiv" begin
69+
z = pyjl(Foo(1)) ÷ pyjl(Foo(2))
70+
@test pyconvert(String, z) == "1 ÷ 2"
71+
end
72+
@testset "rfloordiv" begin
73+
z = pyjlraw(Foo(1)) ÷ pyjl(Foo(2))
74+
@test pyconvert(String, z) == "1 ÷ 2"
75+
end
76+
@testset "mod" begin
77+
z = pyjl(Foo(1)) % pyjl(Foo(2))
78+
@test pyconvert(String, z) == "1 % 2"
79+
end
80+
@testset "rmod" begin
81+
z = pyjlraw(Foo(1)) % pyjl(Foo(2))
82+
@test pyconvert(String, z) == "1 % 2"
83+
end
84+
@testset "pow" begin
85+
z = pyjl(Foo(1)) ^ pyjl(Foo(2))
86+
@test pyconvert(String, z) == "1 ^ 2"
87+
end
88+
@testset "rpow" begin
89+
z = pyjlraw(Foo(1)) ^ pyjl(Foo(2))
90+
@test pyconvert(String, z) == "1 ^ 2"
91+
end
92+
@testset "lshift" begin
93+
z = pyjl(Foo(1)) << pyjl(Foo(2))
94+
@test pyconvert(String, z) == "1 << 2"
95+
end
96+
@testset "rlshift" begin
97+
z = pyjlraw(Foo(1)) << pyjl(Foo(2))
98+
@test pyconvert(String, z) == "1 << 2"
99+
end
100+
@testset "rshift" begin
101+
z = pyjl(Foo(1)) >> pyjl(Foo(2))
102+
@test pyconvert(String, z) == "1 >> 2"
103+
end
104+
@testset "rrshift" begin
105+
z = pyjlraw(Foo(1)) >> pyjl(Foo(2))
106+
@test pyconvert(String, z) == "1 >> 2"
107+
end
108+
@testset "and" begin
109+
z = pyjl(Foo(1)) & pyjl(Foo(2))
110+
@test pyconvert(String, z) == "1 & 2"
111+
end
112+
@testset "rand" begin
113+
z = pyjlraw(Foo(1)) & pyjl(Foo(2))
114+
@test pyconvert(String, z) == "1 & 2"
115+
end
116+
@testset "or" begin
117+
z = pyjl(Foo(1)) | pyjl(Foo(2))
118+
@test pyconvert(String, z) == "1 | 2"
119+
end
120+
@testset "ror" begin
121+
z = pyjlraw(Foo(1)) | pyjl(Foo(2))
122+
@test pyconvert(String, z) == "1 | 2"
123+
end
124+
end
125+
1126
@testitem "iter" begin
2127
x1 = [1,2,3,4,5]
3128
x2 = pyjl(x1)

0 commit comments

Comments
 (0)