Skip to content

Commit

Permalink
Merge pull request #131 from stev47/fix-deepcopy
Browse files Browse the repository at this point in the history
fix same-object copies in `deepcopy`
  • Loading branch information
andyferris authored Jan 28, 2024
2 parents 0230039 + a6e6ff2 commit 9202350
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 4 deletions.
2 changes: 0 additions & 2 deletions src/Dictionary.jl
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,6 @@ Base.convert(::Type{T}, dict::T) where {T<:Dictionary} = dict

Base.copy(dict::Dictionary) = Dictionary(dict.indices, copy(dict.values))

Base.deepcopy_internal(dict::Dictionary{I,T}, id::IdDict) where {I,T} = Dictionary{I,T}(Base.deepcopy_internal(keys(dict), id), Base.deepcopy_internal(collect(dict), id))

function Serialization.serialize(s::AbstractSerializer, dict::T) where {T<:Dictionary}
serialize_type(s, T, false)
serialize(s, keys(dict))
Expand Down
6 changes: 5 additions & 1 deletion src/Indices.jl
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,11 @@ function Base.copy(indices::ReverseIndices{I,Indices{I}}, ::Type{I2}) where {I,
end

function Base.deepcopy_internal(ind::Indices{T}, id::IdDict) where {T}
return Indices{T}(Base.deepcopy_internal(collect(ind), id))
if haskey(id, ind)
id[ind]::Indices{T}
else
id[ind] = Indices{T}(Base.deepcopy_internal(collect(ind), id))
end
end

function Serialization.serialize(s::AbstractSerializer, ind::T) where {T<:Indices}
Expand Down
6 changes: 5 additions & 1 deletion src/UnorderedIndices.jl
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,11 @@ function Base.copy(h::UnorderedIndices{T}, ::Type{T}) where {T}
end

function Base.deepcopy_internal(uinds::UnorderedIndices{T}, id::IdDict) where {T}
return UnorderedIndices{T}(Base.deepcopy_internal(collect(keys(uinds)), id))
if haskey(id, uinds)
id[uinds]::UnorderedIndices{T}
else
id[uinds] = UnorderedIndices{T}(Base.deepcopy_internal(collect(keys(uinds)), id))
end
end

function Serialization.serialize(s::AbstractSerializer, uinds::T) where {T<:UnorderedIndices}
Expand Down
2 changes: 2 additions & 0 deletions test/Dictionary.jl
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@
delete!(dmut, foos[3])
dmut_copy = deepcopy(dmut)
@test all(k -> haskey(dmut_copy, k), keys(dmut_copy))
dmut2_copy = deepcopy((dmut, dmut));
@test dmut2_copy[1] === dmut2_copy[2];

mktemp() do path, io
open(io->serialize(io, dmut), path, "w")
Expand Down
2 changes: 2 additions & 0 deletions test/UnorderedDictionary.jl
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@
dmut = UnorderedDictionary([Foo(3), Foo(2)], rand(2))
dmut_copy = deepcopy(dmut)
@test all(k -> haskey(dmut_copy, k), keys(dmut_copy))
dmut2_copy = deepcopy((dmut, dmut));
@test dmut2_copy[1] === dmut2_copy[2];

mktemp() do path, io
open(io->serialize(io, dmut), path, "w")
Expand Down
2 changes: 2 additions & 0 deletions test/UnorderedIndices.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
@test h == copy(h)
@test isempty(h)
@test issetequal(copy(h), h)
h2 = deepcopy((h, h))
@test h2[1] === h2[2];
@test_throws IndexError h[10]
@test length(unset!(h, 10)) == 0
io = IOBuffer(); print(io, h); @test String(take!(io)) == "{}"
Expand Down

0 comments on commit 9202350

Please sign in to comment.