Skip to content

Commit

Permalink
adds copy method to dict datatype
Browse files Browse the repository at this point in the history
  • Loading branch information
deep110 committed Mar 12, 2017
1 parent 4823682 commit 4de0451
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
4 changes: 2 additions & 2 deletions python/common/org/python/types/Dict.java
Original file line number Diff line number Diff line change
Expand Up @@ -348,10 +348,10 @@ public org.python.Object clear() {
}

@org.python.Method(
__doc__ = ""
__doc__ = "D.copy() -> dict -- a shallow copy of D"
)
public org.python.Object copy() {
throw new org.python.exceptions.NotImplementedError("dict.copy() has not been implemented.");
return new org.python.types.Dict(new java.util.HashMap<org.python.Object, org.python.Object>(this.value));
}

@org.python.Method(
Expand Down
32 changes: 32 additions & 0 deletions tests/datatypes/test_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,38 @@ def test_method_get(self):
print(err)
""")

def test_copy(self):
self.assertCodeExecution("""
x = {42: 'Babel'}
y = x.copy()
print(y)
""")

self.assertCodeExecution("""
x = {42: 'Babel'}
y = x.copy()
print(x == y)
""")

self.assertCodeExecution("""
x = {42: 'Babel'}
y = x.copy()
print(x is not y)
""")

self.assertCodeExecution("""
x = {42: 'Babel'}
y = x.copy()
y['42'] = 'Babel'
print(x == y)
""")

self.assertCodeExecution("""
x = {'key': {42: 'Babel'}, 42: 'Babel'}
y = x.copy()
print(x['key'] is y['key'])
""")


class UnaryDictOperationTests(UnaryOperationTestCase, TranspileTestCase):
data_type = 'dict'
Expand Down

0 comments on commit 4de0451

Please sign in to comment.