Skip to content

Commit 0a38ee8

Browse files
committed
merge values provided in dict.__init__()
1 parent 4f9659e commit 0a38ee8

File tree

2 files changed

+24
-5
lines changed
  • graalpython

2 files changed

+24
-5
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,13 @@ def test_init():
140140

141141
assert_raises(TypeError, dict.fromkeys, 10)
142142

143+
d = {'a':1, 'b':2}
144+
d.__init__()
145+
assert d == {'a':1, 'b':2}
146+
d.__init__({'c':3})
147+
assert d == {'a':1, 'b':2, 'c':3}
148+
d.__init__({'d':4})
149+
assert d == {'a':1, 'b':2, 'c':3, 'd':4}
143150

144151
def test_init1():
145152
try:

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/dict/DictBuiltins.java

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,11 @@ private HashingStorage.InitNode getInitNode() {
136136
@Specialization(guards = {"args.length == 1", "firstArgIterable(args, lib)", "!firstArgString(args)"})
137137
Object doVarargs(VirtualFrame frame, PDict self, Object[] args, PKeyword[] kwargs,
138138
@Cached SetDictStorageNode setStorage,
139-
@SuppressWarnings("unused") @CachedLibrary(limit = "1") PythonObjectLibrary lib) {
140-
setStorage.execute(self, getInitNode().execute(frame, args[0], kwargs));
139+
@SuppressWarnings("unused") @CachedLibrary(limit = "1") PythonObjectLibrary lib,
140+
@CachedLibrary(limit = "1") HashingStorageLibrary storageLib) {
141+
HashingStorage storage = self.getDictStorage();
142+
storage = storageLib.addAllToOther(getInitNode().execute(frame, args[0], kwargs), storage);
143+
setStorage.execute(self, storage);
141144
return PNone.NONE;
142145
}
143146

@@ -155,10 +158,19 @@ static Object doString(Object self, Object[] args, PKeyword[] kwargs,
155158
throw raise.raise(ValueError, ErrorMessages.DICT_UPDATE_SEQ_ELEM_HAS_LENGTH_2_REQUIRED, 0, 1);
156159
}
157160

158-
@Specialization(guards = "args.length == 0")
161+
@Specialization(guards = {"args.length == 0", "kwargs.length > 0"})
159162
Object doKeywords(VirtualFrame frame, PDict self, @SuppressWarnings("unused") Object[] args, PKeyword[] kwargs,
160-
@Cached SetDictStorageNode setStorage) {
161-
setStorage.execute(self, getInitNode().execute(frame, NO_VALUE, kwargs));
163+
@Cached SetDictStorageNode setStorage,
164+
@CachedLibrary(limit = "1") HashingStorageLibrary storageLib) {
165+
HashingStorage storage = self.getDictStorage();
166+
storage = storageLib.addAllToOther(getInitNode().execute(frame, NO_VALUE, kwargs), storage);
167+
setStorage.execute(self, storage);
168+
return PNone.NONE;
169+
}
170+
171+
@SuppressWarnings("unused")
172+
@Specialization(guards = {"args.length == 0", "kwargs.length == 0"})
173+
Object doEmpty(PDict self, Object[] args, PKeyword[] kwargs) {
162174
return PNone.NONE;
163175
}
164176

0 commit comments

Comments
 (0)