Skip to content

Commit dc336d5

Browse files
LilithHafnerLilith Hafner
and
Lilith Hafner
authored
Fix pyconvert cache update bugs by clearing the whole cache after any rule is added (#365)
* fix pyconvert cache update bugs by clearing the whole cache after any rule is added * add trailing newline * test reproducibility and concision improvements --------- Co-authored-by: Lilith Hafner <[email protected]>
1 parent f77f2b9 commit dc336d5

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

src/convert.jl

+1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ Other priorities are reserved for internal use.
6464
function pyconvert_add_rule(pytypename::String, type::Type, func::Function, priority::PyConvertPriority=PYCONVERT_PRIORITY_NORMAL)
6565
@nospecialize type func
6666
push!(get!(Vector{PyConvertRule}, PYCONVERT_RULES, pytypename), PyConvertRule(type, func, priority))
67+
empty!.(values(PYCONVERT_RULES_CACHE))
6768
return
6869
end
6970

test/convert.jl

+16
Original file line numberDiff line numberDiff line change
@@ -222,3 +222,19 @@ end
222222
x1 = pyconvert(DateTime, pydatetime(2001, 2, 3, 4, 5, 6, 7000))
223223
@test x1 === DateTime(2001, 2, 3, 4, 5, 6, 7)
224224
end
225+
226+
@testitem "pyconvert_add_rule (#364)" begin
227+
id = string(rand(UInt128), base=16)
228+
pyexec("""
229+
class Hello_364_$id:
230+
pass
231+
""", @__MODULE__)
232+
x = pyeval("Hello_364_$id()", @__MODULE__)
233+
@test pyconvert(Any, x) === x # This test has a side effect of influencing the rules cache
234+
t = pytype(x)
235+
PythonCall.pyconvert_add_rule("$(t.__module__):$(t.__qualname__)", String, (_, _) -> "Hello!!")
236+
@test pyconvert(String, x) == "Hello!!"
237+
@test pyconvert(Any, x) == "Hello!!" # Broken before PR #365
238+
end
239+
240+
end

0 commit comments

Comments
 (0)