Skip to content

Commit fe26d12

Browse files
committed
[GR-40390] copy the storage of sets for shallow copy.
PullRequest: graalpython/2399
2 parents b43155e + dd6f351 commit fe26d12

File tree

2 files changed

+18
-3
lines changed
  • graalpython
    • com.oracle.graal.python.test/src/tests
    • com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/set

2 files changed

+18
-3
lines changed

graalpython/com.oracle.graal.python.test/src/tests/test_set.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,3 +528,17 @@ def test_inplace_ops_mutate():
528528
exec(f"a {op}= b", v)
529529
assert v['a'] is s1
530530
assert s1 == eval(f"a {op} b", {'a': {1, 2}, 'b': {1, 3}})
531+
532+
533+
def test_graal_4816():
534+
from copy import copy
535+
536+
def do_something(numbers):
537+
assert len(numbers)
538+
539+
foo = set([1, 2, 3, 4, 5, 6, 7, 8, 9])
540+
541+
for _ in copy(foo):
542+
foo.pop()
543+
if foo:
544+
do_something(foo)

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/set/SetBuiltins.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,10 @@ PNone fail(@SuppressWarnings("unused") VirtualFrame frame, @SuppressWarnings("un
138138
@GenerateNodeFactory
139139
public abstract static class CopyNode extends PythonBuiltinNode {
140140

141-
@Specialization
142-
PSet doSet(@SuppressWarnings("unused") VirtualFrame frame, PSet self) {
143-
return factory().createSet(self.getDictStorage());
141+
@Specialization(limit = "1")
142+
PSet doSet(@SuppressWarnings("unused") VirtualFrame frame, PSet self,
143+
@CachedLibrary("self.getDictStorage()") HashingStorageLibrary lib) {
144+
return factory().createSet(lib.copy(self.getDictStorage()));
144145
}
145146
}
146147

0 commit comments

Comments
 (0)