Skip to content

Commit

Permalink
Fix pyconvert cache update bugs by clearing the whole cache after any…
Browse files Browse the repository at this point in the history
… 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]>
  • Loading branch information
LilithHafner and Lilith Hafner authored Sep 22, 2023
1 parent f77f2b9 commit dc336d5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/convert.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
16 changes: 16 additions & 0 deletions test/convert.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit dc336d5

Please sign in to comment.