Skip to content

Commit 08dd692

Browse files
qunaibitsteve-s
authored andcommitted
Filter out hidden keys in all __dir__ functions
1 parent eff0da6 commit 08dd692

File tree

4 files changed

+55
-11
lines changed

4 files changed

+55
-11
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/module/ModuleBuiltins.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
package com.oracle.graal.python.builtins.objects.module;
4242

4343
import static com.oracle.graal.python.builtins.PythonBuiltinClassType.TypeError;
44+
import static com.oracle.graal.python.lib.PyObjectDir.filterHiddenKeys;
4445
import static com.oracle.graal.python.nodes.SpecialAttributeNames.J___ANNOTATIONS__;
4546
import static com.oracle.graal.python.nodes.SpecialAttributeNames.J___DICT__;
4647
import static com.oracle.graal.python.nodes.SpecialAttributeNames.T___ANNOTATIONS__;
@@ -72,6 +73,7 @@
7273
import com.oracle.graal.python.builtins.objects.common.HashingStorageNodes.HashingStorageGetItem;
7374
import com.oracle.graal.python.builtins.objects.common.PHashingCollection;
7475
import com.oracle.graal.python.builtins.objects.dict.PDict;
76+
import com.oracle.graal.python.builtins.objects.list.PList;
7577
import com.oracle.graal.python.builtins.objects.module.ModuleBuiltinsClinicProviders.ModuleNodeClinicProviderGen;
7678
import com.oracle.graal.python.builtins.objects.object.ObjectBuiltins;
7779
import com.oracle.graal.python.lib.PyObjectLookupAttr;
@@ -173,7 +175,9 @@ Object dir(VirtualFrame frame, PythonModule self,
173175
if (dirFunc != null) {
174176
return callNode.execute(frame, dirFunc);
175177
} else {
176-
return constructListNode.execute(frame, dict);
178+
PList list = constructListNode.execute(frame, dict);
179+
filterHiddenKeys(list.getSequenceStorage());
180+
return list;
177181
}
178182
} else {
179183
throw raise(PythonBuiltinClassType.TypeError, ErrorMessages.IS_NOT_A_DICTIONARY, "<module>.__dict__");

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/object/ObjectBuiltins.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
package com.oracle.graal.python.builtins.objects.object;
2828

29+
import static com.oracle.graal.python.lib.PyObjectDir.filterHiddenKeys;
2930
import static com.oracle.graal.python.nodes.PGuards.isDeleteMarker;
3031
import static com.oracle.graal.python.nodes.PGuards.isDict;
3132
import static com.oracle.graal.python.nodes.PGuards.isNoValue;
@@ -79,6 +80,7 @@
7980
import com.oracle.graal.python.builtins.objects.PNotImplemented;
8081
import com.oracle.graal.python.builtins.objects.cext.PythonAbstractNativeObject;
8182
import com.oracle.graal.python.builtins.objects.cext.capi.CExtNodes;
83+
import com.oracle.graal.python.builtins.objects.common.HashingStorageNodes.HashingStorageDelItem;
8284
import com.oracle.graal.python.builtins.objects.dict.PDict;
8385
import com.oracle.graal.python.builtins.objects.function.BuiltinMethodDescriptor;
8486
import com.oracle.graal.python.builtins.objects.function.PBuiltinFunction;
@@ -105,6 +107,7 @@
105107
import com.oracle.graal.python.nodes.attributes.LookupCallableSlotInMRONode;
106108
import com.oracle.graal.python.nodes.attributes.ReadAttributeFromObjectNode;
107109
import com.oracle.graal.python.nodes.attributes.WriteAttributeToObjectNode;
110+
import com.oracle.graal.python.nodes.builtins.ListNodes;
108111
import com.oracle.graal.python.nodes.call.CallNode;
109112
import com.oracle.graal.python.nodes.call.special.CallBinaryMethodNode;
110113
import com.oracle.graal.python.nodes.call.special.CallTernaryMethodNode;
@@ -926,13 +929,16 @@ Object dir(VirtualFrame frame, Object obj,
926929
@Cached CallNode callNode,
927930
@Cached GetClassNode getClassNode,
928931
@Cached IsSubtypeNode isSubtypeNode,
932+
@Cached ListNodes.ConstructListNode constructListNode,
933+
@Cached HashingStorageDelItem delItem,
929934
@Cached com.oracle.graal.python.builtins.objects.type.TypeBuiltins.DirNode dirNode) {
930935
PSet names = factory().createSet();
931936
Object updateCallable = lookupAttrNode.execute(frame, inliningTarget, names, T_UPDATE);
932937
Object ns = lookupAttrNode.execute(frame, inliningTarget, obj, T___DICT__);
933938
if (isSubtypeNode.execute(frame, getClassNode.execute(inliningTarget, ns), PythonBuiltinClassType.PDict)) {
934939
callNode.execute(frame, updateCallable, ns);
935940
}
941+
filterHiddenKeys(frame, names, inliningTarget, constructListNode, delItem);
936942
Object klass = lookupAttrNode.execute(frame, inliningTarget, obj, T___CLASS__);
937943
if (klass != PNone.NO_VALUE) {
938944
callNode.execute(frame, updateCallable, dirNode.execute(frame, klass));

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/type/TypeBuiltins.java

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
import static com.oracle.graal.python.builtins.objects.cext.structs.CFields.PyTypeObject__tp_name;
3030
import static com.oracle.graal.python.builtins.objects.object.ObjectBuiltins.InitNode.overridesBuiltinMethod;
31+
import static com.oracle.graal.python.lib.PyObjectDir.filterHiddenKeys;
3132
import static com.oracle.graal.python.nodes.BuiltinNames.T_BUILTINS;
3233
import static com.oracle.graal.python.nodes.SpecialAttributeNames.J___ABSTRACTMETHODS__;
3334
import static com.oracle.graal.python.nodes.SpecialAttributeNames.J___ANNOTATIONS__;
@@ -97,6 +98,7 @@
9798
import com.oracle.graal.python.builtins.objects.cext.structs.CFields;
9899
import com.oracle.graal.python.builtins.objects.cext.structs.CStructAccess;
99100
import com.oracle.graal.python.builtins.objects.common.DynamicObjectStorage;
101+
import com.oracle.graal.python.builtins.objects.common.HashingStorageNodes.HashingStorageDelItem;
100102
import com.oracle.graal.python.builtins.objects.common.SequenceNodes.GetObjectArrayNode;
101103
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes.ToArrayNode;
102104
import com.oracle.graal.python.builtins.objects.dict.PDict;
@@ -146,6 +148,7 @@
146148
import com.oracle.graal.python.nodes.attributes.ReadAttributeFromObjectNode;
147149
import com.oracle.graal.python.nodes.attributes.WriteAttributeToObjectNode;
148150
import com.oracle.graal.python.nodes.builtins.FunctionNodes;
151+
import com.oracle.graal.python.nodes.builtins.ListNodes;
149152
import com.oracle.graal.python.nodes.call.special.CallTernaryMethodNode;
150153
import com.oracle.graal.python.nodes.call.special.CallVarargsMethodNode;
151154
import com.oracle.graal.python.nodes.call.special.LookupAndCallBinaryNode;
@@ -1404,26 +1407,37 @@ Object dir(VirtualFrame frame, Object klass,
14041407
@Cached PyObjectLookupAttr lookupAttrNode,
14051408
@Cached com.oracle.graal.python.nodes.call.CallNode callNode,
14061409
@Cached ToArrayNode toArrayNode,
1407-
@Cached("createGetAttrNode()") GetFixedAttributeNode getBasesNode) {
1408-
PSet names = dir(frame, inliningTarget, klass, lookupAttrNode, callNode, getBasesNode, toArrayNode);
1410+
@Cached("createGetAttrNode()") GetFixedAttributeNode getBasesNode,
1411+
@Cached ListNodes.ConstructListNode constructListNode,
1412+
@Cached HashingStorageDelItem delItem) {
1413+
PSet names = dir(frame, klass,
1414+
inliningTarget, lookupAttrNode, callNode, getBasesNode, toArrayNode, constructListNode, delItem);
14091415
return names;
14101416
}
14111417

1412-
private PSet dir(VirtualFrame frame, Node inliningTarget, Object klass, PyObjectLookupAttr lookupAttrNode, com.oracle.graal.python.nodes.call.CallNode callNode,
1413-
GetFixedAttributeNode getBasesNode, ToArrayNode toArrayNode) {
1418+
private PSet dir(VirtualFrame frame, Object klass,
1419+
Node inliningTarget,
1420+
PyObjectLookupAttr lookupAttrNode,
1421+
com.oracle.graal.python.nodes.call.CallNode callNode,
1422+
GetFixedAttributeNode getBasesNode,
1423+
ToArrayNode toArrayNode,
1424+
ListNodes.ConstructListNode constructListNode,
1425+
HashingStorageDelItem delItem) {
14141426
PSet names = factory().createSet();
14151427
Object updateCallable = lookupAttrNode.execute(frame, inliningTarget, names, T_UPDATE);
14161428
Object ns = lookupAttrNode.execute(frame, inliningTarget, klass, T___DICT__);
14171429
if (ns != PNone.NO_VALUE) {
14181430
callNode.execute(frame, updateCallable, ns);
14191431
}
1432+
filterHiddenKeys(frame, names, inliningTarget, constructListNode, delItem);
14201433
Object basesAttr = getBasesNode.execute(frame, klass);
14211434
if (basesAttr instanceof PTuple) {
14221435
Object[] bases = toArrayNode.execute(inliningTarget, ((PTuple) basesAttr).getSequenceStorage());
14231436
for (Object cls : bases) {
14241437
// Note that since we are only interested in the keys, the order
14251438
// we merge classes is unimportant
1426-
Object baseNames = dir(frame, inliningTarget, cls, lookupAttrNode, callNode, getBasesNode, toArrayNode);
1439+
Object baseNames = dir(frame, cls,
1440+
inliningTarget, lookupAttrNode, callNode, getBasesNode, toArrayNode, constructListNode, delItem);
14271441
callNode.execute(frame, updateCallable, baseNames);
14281442
}
14291443
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/lib/PyObjectDir.java

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,12 @@
4343
import static com.oracle.graal.python.builtins.PythonBuiltinClassType.TypeError;
4444

4545
import com.oracle.graal.python.builtins.objects.PNone;
46+
import com.oracle.graal.python.builtins.objects.common.EmptyStorage;
47+
import com.oracle.graal.python.builtins.objects.common.HashingStorage;
48+
import com.oracle.graal.python.builtins.objects.common.HashingStorageNodes.HashingStorageDelItem;
4649
import com.oracle.graal.python.builtins.objects.list.ListBuiltins;
4750
import com.oracle.graal.python.builtins.objects.list.PList;
51+
import com.oracle.graal.python.builtins.objects.set.PSet;
4852
import com.oracle.graal.python.nodes.ErrorMessages;
4953
import com.oracle.graal.python.nodes.PNodeWithContext;
5054
import com.oracle.graal.python.nodes.PRaiseNode;
@@ -86,10 +90,8 @@ static PList dir(VirtualFrame frame, Node inliningTarget, Object object,
8690
return list;
8791
}
8892

89-
static void filterHiddenKeys(SequenceStorage s) {
90-
if (s instanceof EmptySequenceStorage) {
91-
// noting to do.
92-
} else if (s instanceof ObjectSequenceStorage storage) {
93+
public static void filterHiddenKeys(SequenceStorage s) {
94+
if (s instanceof ObjectSequenceStorage storage) {
9395
// String do not have a special storage
9496
Object[] oldarray = storage.getInternalArray();
9597
Object[] newarray = new Object[storage.length()];
@@ -103,8 +105,26 @@ static void filterHiddenKeys(SequenceStorage s) {
103105
}
104106
storage.setInternalArrayObject(newarray);
105107
storage.setNewLength(j);
106-
} else {
108+
} else if (!(s instanceof EmptySequenceStorage)) {
107109
assert false : "Unexpected storage type!";
108110
}
109111
}
112+
113+
public static void filterHiddenKeys(VirtualFrame frame, PSet names,
114+
Node inliningTarget,
115+
ListNodes.ConstructListNode constructListNode,
116+
HashingStorageDelItem delItem) {
117+
HashingStorage set = names.getDictStorage();
118+
if (set != EmptyStorage.INSTANCE) {
119+
PList list = constructListNode.execute(frame, names);
120+
ObjectSequenceStorage storage = (ObjectSequenceStorage) list.getSequenceStorage();
121+
Object[] array = storage.getInternalArray();
122+
for (int i = 0; i < storage.length(); i++) {
123+
Object o = array[i];
124+
if (o instanceof HiddenKey) {
125+
delItem.execute(frame, inliningTarget, set, o, names);
126+
}
127+
}
128+
}
129+
}
110130
}

0 commit comments

Comments
 (0)