Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enable deepcopy on PyObject #757 #1039

Merged
merged 8 commits into from
Jun 21, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "PyCall"
uuid = "438e738f-606a-5dbb-bf0a-cddfbfd45ab0"
authors = ["Steven G. Johnson <stevenj@mit.edu>", "Yichao Yu <yyc1992@gmail.com>", "Takafumi Arakaki <aka.tkf@gmail.com>", "Simon Kornblith <simon@simonster.com>", "Páll Haraldsson <Pall.Haraldsson@gmail.com>", "Jon Malmaud <malmaud@gmail.com>", "Jake Bolewski <jakebolewski@gmail.com>", "Keno Fischer <keno@alumni.harvard.edu>", "Joel Mason <jobba1@hotmail.com>", "Jameson Nash <vtjnash@gmail.com>", "The JuliaPy development team"]
version = "1.95.1"
version = "1.96"

[deps]
Conda = "8f4d0f93-b110-5947-807f-2305c1781a2d"
Expand Down
10 changes: 10 additions & 0 deletions src/PyCall.jl
Original file line number Diff line number Diff line change
Expand Up @@ -979,6 +979,16 @@ include("serialize.jl")

include("pyinit.jl")

const _deepcopy = PyNULL()

function Base.deepcopy_internal(obj::PyObject, stackdict::Base.IdDict)
haskey(stackdict, obj) && return stackdict[obj]
ispynull(_deepcopy) && copy!(_deepcopy, pyimport("copy")["deepcopy"])
ret = pycall(_deepcopy, PyObject, obj)
stackdict[obj] = ret
ret
end

#########################################################################

include("precompile.jl")
Expand Down
39 changes: 39 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,44 @@ const PyInt = pyversion < v"3" ? Int : Clonglong
@test_throws ArgumentError float(pybuiltin("type"))
end

@testset "deepcopy #757" begin
l = py"[1,2,3]"o
l2 = deepcopy(l)
@test l == l2
l2.append(4)
@test l != l2
@test collect(l2) == [1,2,3,4]
@test collect(l) == [1,2,3]

obj = py"""
class C757:
def __init__(self, a, b):
self.a = a
self.b = b
"""
obj = py"C757(C757(1,2), C757(3,4))"o
obj2 = deepcopy(obj)
stevengj marked this conversation as resolved.
Show resolved Hide resolved
@test obj.a.a == obj2.a.a
@test obj.a.b == obj2.a.b
@test obj.b.a == obj2.b.a
@test obj.b.b == obj2.b.b
obj.a = 3
@test obj.a == 3
@test obj2.a.a == 1
@test obj2.a.b == 2

struct S;a;b;end

c = py"C757(1,2)"
obj = S(c, c)
obj2 = deepcopy(obj)
@test obj.a === obj.b
@test obj2.a === obj2.b
obj.a.a = 4
@test obj.a.a == 4
@test obj2.a.a == 1
end

######################################################################
#@pydef tests: type declarations need to happen at top level

Expand Down Expand Up @@ -855,3 +893,4 @@ end
@test_throws PyCall.PyError a.a = 0
@test_throws KeyError a.a = 1
end