Skip to content

Commit

Permalink
Merge branch 'darkryder-Dict_methods'
Browse files Browse the repository at this point in the history
  • Loading branch information
freakboy3742 committed Oct 2, 2016
2 parents 73c3ed7 + a39c695 commit 992ba01
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
14 changes: 11 additions & 3 deletions python/common/org/python/types/Dict.java
Original file line number Diff line number Diff line change
Expand Up @@ -309,10 +309,18 @@ public org.python.Object fromkeys(org.python.Object other) {
}

@org.python.Method(
__doc__ = ""
__doc__ = "",
default_args = {"other", "default_value"}
)
public org.python.Object get(org.python.Object other) {
throw new org.python.exceptions.NotImplementedError("dict.get() has not been implemented.");
public org.python.Object get(org.python.Object other, org.python.Object default_value) {
try {
return this.__getitem__(other);
} catch (org.python.exceptions.KeyError e){ // allow unhashable type error to be percolated up.
if (default_value == null) {
return org.python.types.NoneType.NONE;
}
return default_value;
}
}

@org.python.Method(
Expand Down
15 changes: 15 additions & 0 deletions tests/datatypes/test_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,21 @@ def test_method_setdefault(self):
x.setdefault([], 42)
""")

def test_method_get(self):
self.assertCodeExecution("""
x = {1: 2}
print(x.get(1))
print(x.get(2))
print(x.get(3,4))
""")

# check for unhashable type errors
self.assertCodeExecution("""
x = {1: 2}
print(x.get([]))
print(x.get([], 1))
""")


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

0 comments on commit 992ba01

Please sign in to comment.