Skip to content

Commit

Permalink
Merge pull request beeware#713 from JvMaiia/master
Browse files Browse the repository at this point in the history
implement dict.update method
  • Loading branch information
freakboy3742 authored Dec 7, 2017
2 parents 4f2a4d1 + e86c119 commit cad52e3
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 4 deletions.
68 changes: 64 additions & 4 deletions python/common/org/python/types/Dict.java
Original file line number Diff line number Diff line change
Expand Up @@ -472,10 +472,70 @@ public org.python.Object setdefault(org.python.Object k, org.python.Object d) {
}

@org.python.Method(
__doc__ = "D.update([E, ]**F) -> None. Update D from dict/iterable E and F.\nIf E is present and has a .keys() method, then does: for k in E: D[k] = E[k]\nIf E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v\nIn either case, this is followed by: for k in F: D[k] = F[k]"
)
public org.python.Object update(org.python.Object other) {
throw new org.python.exceptions.NotImplementedError("dict.update() has not been implemented.");
__doc__ = "D.update([E, ]**F) -> None. Update D from dict/iterable E and F.\n" +
"If E is present and has a .keys() method, then does: for k in E: D[k] = E[k]\n" +
"If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v\n" +
"In either case, this is followed by: for k in F: D[k] = F[k]",
default_args = {"iterable"},
kwargs = "kwargs"
)
public org.python.Object update(org.python.Object iterable, org.python.types.Dict kwargs) {
if (iterable == null) {
if (kwargs != null) {
org.python.Object iterator = org.Python.iter(kwargs);
while (true) {
try {
org.python.Object key = iterator.__next__();
org.python.Object value = kwargs.__getitem__(key);
this.value.put(key, value);
} catch (org.python.exceptions.StopIteration si) {
break;
}
}
}
} else if (iterable instanceof org.python.types.Dict) {
org.python.Object iterator = org.Python.iter(iterable);
while (true) {
try {
org.python.Object key = iterator.__next__();
org.python.Object value = iterable.__getitem__(key);
this.value.put(key, value);
} catch (org.python.exceptions.StopIteration si) {
break;
}
}
} else {
org.python.Object iterator = org.Python.iter(iterable);
int size = 0;
java.util.List<org.python.Object> pair;
while (true) {
try {
org.python.Object next = iterator.__next__();
if (next instanceof org.python.types.List) {
pair = ((org.python.types.List) next).value;
} else if (next instanceof org.python.types.Tuple) {
pair = ((org.python.types.Tuple) next).value;
} else if (next instanceof org.python.types.Str) {
throw new org.python.exceptions.ValueError(
"dictionary update sequence element #" + size + " has length 1; 2 is required");
} else {
throw new org.python.exceptions.TypeError("cannot convert dictionary update sequence element #" + size + " to a sequence");
}

if (pair.size() != 2) {
throw new org.python.exceptions.ValueError(
"dictionary update sequence element #" + size + " has length " + pair.size() +"; 2 is required");
}
org.python.Object key = pair.get(0);
org.python.Object value = pair.get(1);
this.value.put(key, value);
size++;
} catch (org.python.exceptions.StopIteration si) {
break;
}
}
}
return org.python.types.NoneType.NONE;
}

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

def test_update(self):
self.assertCodeExecution("""
a = {}
a.update([('a', 1), ('b', 2)])
print(sorted(a))
b = {}
b.update({'a': 1, 'b':2})
print(sorted(b))
c = {}
c.update(a=1, b=2)
print(sorted(c))
""")

self.assertCodeExecution("""
try:
a = {}
a.update([('a', 1, 2), ('b',2)])
print('An error should have been raised!')
except ValueError:
print('Received a ValueError as expected')
""")

self.assertCodeExecution("""
try:
a = {}
a.update('1')
print('An error should have been raised')
except ValueError:
print('Received a ValueError as expected')
""")

self.assertCodeExecution("""
try:
a = {}
a.update(1)
print('An error should have been raised')
except TypeError:
print('Received a TypeError as expected')
""")

self.assertCodeExecution("""
try:
a = {}
x = set([1, 2])
a.update(x)
print('An error should have been raised')
except TypeError:
print('Received a TypeError as expected')
""")

@expectedFailure
def test_fromkeys_missing_iterable(self):
self.assertCodeExecution("""
Expand Down

0 comments on commit cad52e3

Please sign in to comment.