From 03bde5e4a9fc23419bb1f0b5864eba35668652de Mon Sep 17 00:00:00 2001 From: Lilith Hafner Date: Wed, 20 Sep 2023 09:37:15 -0500 Subject: [PATCH 1/3] fix pyconvert cache update bugs by clearing the whole cache after any rule is added --- src/convert.jl | 1 + test/convert.jl | 15 +++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/src/convert.jl b/src/convert.jl index 498293a5..21b8ac4e 100644 --- a/src/convert.jl +++ b/src/convert.jl @@ -64,6 +64,7 @@ Other priorities are reserved for internal use. function pyconvert_add_rule(pytypename::String, type::Type, func::Function, priority::PyConvertPriority=PYCONVERT_PRIORITY_NORMAL) @nospecialize type func push!(get!(Vector{PyConvertRule}, PYCONVERT_RULES, pytypename), PyConvertRule(type, func, priority)) + empty!.(values(PYCONVERT_RULES_CACHE)) return end diff --git a/test/convert.jl b/test/convert.jl index 0051bf17..5e1eb603 100644 --- a/test/convert.jl +++ b/test/convert.jl @@ -222,3 +222,18 @@ end x1 = pyconvert(DateTime, pydatetime(2001, 2, 3, 4, 5, 6, 7000)) @test x1 === DateTime(2001, 2, 3, 4, 5, 6, 7) end + +@testitem "pyconvert_add_rule (#364)" begin + pyexec(""" + class Hello_364: + pass + """, @__MODULE__) + x = pyeval("Hello_364()", @__MODULE__) + @test pyconvert(Any, x) === x + t = pytype(x) + PythonCall.pyconvert_add_rule(pyconvert(Any, t.__module__)*":"*pyconvert(Any, t.__qualname__), String, (_, _) -> "Hello!!") + @test pyconvert(String, x) == "Hello!!" + @test pyconvert(Any, x) == "Hello!!" # Broken before PR #365 +end + +end \ No newline at end of file From 81a6826e627dbb04729f0b3ace03f1accbee224b Mon Sep 17 00:00:00 2001 From: Lilith Hafner Date: Wed, 20 Sep 2023 09:40:29 -0500 Subject: [PATCH 2/3] add trailing newline --- test/convert.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/convert.jl b/test/convert.jl index 5e1eb603..c5eb0d3a 100644 --- a/test/convert.jl +++ b/test/convert.jl @@ -236,4 +236,4 @@ end @test pyconvert(Any, x) == "Hello!!" # Broken before PR #365 end -end \ No newline at end of file +end From 771e4ed45aa9edf3a48d75e4ca433beafdf84c00 Mon Sep 17 00:00:00 2001 From: Lilith Hafner Date: Fri, 22 Sep 2023 08:48:01 -0500 Subject: [PATCH 3/3] test reproducibility and concision improvements --- test/convert.jl | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/test/convert.jl b/test/convert.jl index c5eb0d3a..10e871d5 100644 --- a/test/convert.jl +++ b/test/convert.jl @@ -224,14 +224,15 @@ end end @testitem "pyconvert_add_rule (#364)" begin + id = string(rand(UInt128), base=16) pyexec(""" - class Hello_364: + class Hello_364_$id: pass """, @__MODULE__) - x = pyeval("Hello_364()", @__MODULE__) - @test pyconvert(Any, x) === x + x = pyeval("Hello_364_$id()", @__MODULE__) + @test pyconvert(Any, x) === x # This test has a side effect of influencing the rules cache t = pytype(x) - PythonCall.pyconvert_add_rule(pyconvert(Any, t.__module__)*":"*pyconvert(Any, t.__qualname__), String, (_, _) -> "Hello!!") + PythonCall.pyconvert_add_rule("$(t.__module__):$(t.__qualname__)", String, (_, _) -> "Hello!!") @test pyconvert(String, x) == "Hello!!" @test pyconvert(Any, x) == "Hello!!" # Broken before PR #365 end