Skip to content

Commit b4c3bef

Browse files
committed
raise KeyError when trying to pop a non-existing key from dict
1 parent de5d5e5 commit b4c3bef

File tree

1 file changed

+7
-12
lines changed
  • graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/dict

1 file changed

+7
-12
lines changed

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

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -183,26 +183,21 @@ protected void removeItem(VirtualFrame frame, PDict dict, Object key,
183183
}
184184
}
185185

186-
@SuppressWarnings("unused")
187-
@Specialization(guards = "isNoValue(arg1)", limit = "3")
188-
public Object pop(VirtualFrame frame, PDict dict, Object arg0, PNone arg1,
189-
@Cached BranchProfile updatedStorage,
190-
@Cached("createBinaryProfile()") ConditionProfile hasFrame,
191-
@CachedLibrary("dict.getDictStorage()") HashingStorageLibrary lib) {
192-
return popDefault(frame, dict, arg0, PNone.NONE, updatedStorage, hasFrame, lib);
193-
}
194-
195186
@Specialization(limit = "3")
196187
public Object popDefault(VirtualFrame frame, PDict dict, Object key, Object defaultValue,
197188
@Cached BranchProfile updatedStorage,
198-
@Cached("createBinaryProfile()") ConditionProfile hasFrame,
189+
@Cached ConditionProfile hasKey,
190+
@Cached ConditionProfile hasDefault,
191+
@Cached ConditionProfile hasFrame,
199192
@CachedLibrary("dict.getDictStorage()") HashingStorageLibrary lib) {
200193
Object retVal = lib.getItemWithFrame(dict.getDictStorage(), key, hasFrame, frame);
201-
if (retVal != null) {
194+
if (hasKey.profile(retVal != null)) {
202195
removeItem(frame, dict, key, lib, hasFrame, updatedStorage);
203196
return retVal;
204-
} else {
197+
} else if (hasDefault.profile(defaultValue != PNone.NO_VALUE)) {
205198
return defaultValue;
199+
} else {
200+
throw raise(PythonBuiltinClassType.KeyError, "%s", key);
206201
}
207202
}
208203
}

0 commit comments

Comments
 (0)