Skip to content

Commit cd641e7

Browse files
committed
[GR-9750] dict creation from dict and kwargs
PullRequest: graalpython-open/38
2 parents 540240d + 6fbb6f9 commit cd641e7

File tree

3 files changed

+17
-3
lines changed

3 files changed

+17
-3
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
@@ -35,6 +35,7 @@
3535
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
3636
# SOFTWARE.
3737

38+
3839
def assert_raises(err, fn, *args, **kwargs):
3940
raised = False
4041
try:
@@ -278,3 +279,9 @@ def test_in_dict_keys():
278279
d = {'a': 1, 'b': 2, 'c': 3}
279280
keys = d.keys()
280281
assert 'a' in keys
282+
283+
284+
def test_create_seq_and_kw():
285+
d = dict({'a': 1, 'b': 2, 'c': 3}, d=4)
286+
for k in ['a', 'b', 'c', 'd']:
287+
assert k in d

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/common/HashingStorageNodes.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,11 +295,18 @@ protected boolean hasKeysAttribute(PythonObject o) {
295295
return lookupKeysAttributeNode.execute(o, KEYS) != PNone.NO_VALUE;
296296
}
297297

298-
@Specialization(guards = {"!isNoValue(iterable)"})
298+
@Specialization(guards = {"!isNoValue(iterable)", "isEmpty(kwargs)"})
299299
public void doPDict(PDict self, PDict iterable, @SuppressWarnings("unused") PKeyword[] kwargs) {
300300
self.setDictStorage(iterable.getDictStorage().copy(HashingStorage.DEFAULT_EQIVALENCE));
301301
}
302302

303+
@Specialization(guards = {"!isNoValue(iterable)", "!isEmpty(kwargs)"})
304+
public void doPDictKwargs(PDict self, PDict iterable, PKeyword[] kwargs) {
305+
HashingStorage dictStorage = iterable.getDictStorage().copy(HashingStorage.DEFAULT_EQIVALENCE);
306+
dictStorage.addAll(new KeywordsStorage(kwargs));
307+
self.setDictStorage(dictStorage);
308+
}
309+
303310
@Specialization(guards = {"!isNoValue(mapping)", "!isPDict(mapping)", "hasKeysAttribute(mapping)"})
304311
public void doMapping(PDict self, PythonObject mapping, @SuppressWarnings("unused") PKeyword[] kwargs,
305312
@Cached("create(KEYS)") LookupAndCallUnaryNode callKeysNode,

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ private HashingStorageNodes.InitNode getInitNode() {
9090
}
9191

9292
@Specialization(guards = "args.length == 1")
93-
public Object doVarargs(PDict self, Object[] args, @SuppressWarnings("unused") PKeyword[] kwargs) {
94-
getInitNode().execute(self, args[0], PKeyword.EMPTY_KEYWORDS);
93+
public Object doVarargs(PDict self, Object[] args, PKeyword[] kwargs) {
94+
getInitNode().execute(self, args[0], kwargs);
9595
return PNone.NONE;
9696
}
9797

0 commit comments

Comments
 (0)