Skip to content

Commit 27bcc01

Browse files
committed
also exclude KEYS starting with '__' unless internal keys are requested, and allow KEY_INFO for 'internal' mapping keys
1 parent 7eababa commit 27bcc01

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/interop/PythonMessageResolution.java

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ public Object execute(Object object, Object key) {
225225
}
226226

227227
private static final class KeysNode extends Node {
228+
private static final String PRIVATE_PREFIX = "__";
228229
@Child private LookupAndCallUnaryNode keysNode = LookupAndCallUnaryNode.create(SpecialMethodNames.KEYS);
229230
@Child private CastToListNode castToList = CastToListNode.create();
230231
@Child private GetClassNode getClass = GetClassNode.create();
@@ -243,10 +244,10 @@ public Object execute(Object obj, boolean includeInternal) {
243244
HashSet<String> keys = new HashSet<>();
244245
PythonClass klass = getClass.execute(object);
245246
for (PythonObject o : klass.getMethodResolutionOrder()) {
246-
addKeysFromObject(keys, o);
247+
addKeysFromObject(keys, o, includeInternal);
247248
}
248249
if (object instanceof PythonObject) {
249-
addKeysFromObject(keys, (PythonObject) object);
250+
addKeysFromObject(keys, (PythonObject) object, includeInternal);
250251
}
251252
if (includeInternal) {
252253
// we use the internal flag to also return dictionary keys for mappings
@@ -272,12 +273,18 @@ public Object execute(Object obj, boolean includeInternal) {
272273
return factory.createTuple(keys.toArray(new String[keys.size()]));
273274
}
274275

275-
private static void addKeysFromObject(HashSet<String> keys, PythonObject o) {
276+
private static void addKeysFromObject(HashSet<String> keys, PythonObject o, boolean includeInternal) {
276277
for (Object k : o.getStorage().getShape().getKeys()) {
278+
String strKey;
277279
if (k instanceof String) {
278-
keys.add((String) k);
280+
strKey = (String) k;
279281
} else if (k instanceof PString) {
280-
keys.add(((PString) k).getValue());
282+
strKey = ((PString) k).getValue();
283+
} else {
284+
continue;
285+
}
286+
if (includeInternal || !strKey.startsWith(PRIVATE_PREFIX)) {
287+
keys.add(strKey);
281288
}
282289
}
283290
}
@@ -622,9 +629,15 @@ abstract static class PKeyInfoNode extends Node {
622629
@Child private LookupInheritedAttributeNode getSetNode;
623630
@Child private LookupInheritedAttributeNode getDeleteNode;
624631
@Child private GetClassNode getClassNode = GetClassNode.create();
625-
@Child IsImmutable isImmutable = new IsImmutable();
632+
@Child private IsImmutable isImmutable = new IsImmutable();
633+
@Child private KeyForItemAccess itemKey = new KeyForItemAccess();
626634

627635
public int access(Object object, Object fieldName) {
636+
String itemFieldName = itemKey.execute(fieldName);
637+
if (itemFieldName != null) {
638+
return KeyInfo.READABLE | KeyInfo.MODIFIABLE | KeyInfo.REMOVABLE | KeyInfo.REMOVABLE;
639+
}
640+
628641
Object owner = object;
629642
int info = KeyInfo.NONE;
630643
Object attr = PNone.NO_VALUE;

0 commit comments

Comments
 (0)