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..10e871d5 100644 --- a/test/convert.jl +++ b/test/convert.jl @@ -222,3 +222,19 @@ 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 + id = string(rand(UInt128), base=16) + pyexec(""" + class Hello_364_$id: + pass + """, @__MODULE__) + 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("$(t.__module__):$(t.__qualname__)", String, (_, _) -> "Hello!!") + @test pyconvert(String, x) == "Hello!!" + @test pyconvert(Any, x) == "Hello!!" # Broken before PR #365 +end + +end