Skip to content

Commit 7d5fb53

Browse files
committed
[GR-23218] Make test_descr pass - dir().
PullRequest: graalpython/1149
2 parents 7a6c21c + 8083f0f commit 7d5fb53

File tree

15 files changed

+171
-61
lines changed

15 files changed

+171
-61
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,3 +168,11 @@ class B: pass
168168
class C(A, B, metaclass = M): pass
169169

170170
assert C.__mro__ == (C, B, A, object)
171+
172+
def test_dir_sorted():
173+
class C:
174+
b = 1
175+
a = 2
176+
177+
assert dir(C) == sorted(dir(C))
178+
assert dir(C()) == sorted(dir(C()))

graalpython/com.oracle.graal.python.test/src/tests/unittest_tags/test_descr.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
*graalpython.lib-python.3.test.test_descr.ClassPropertiesAndMethods.test_altmro
33
*graalpython.lib-python.3.test.test_descr.ClassPropertiesAndMethods.test_assign_slice
44
*graalpython.lib-python.3.test.test_descr.ClassPropertiesAndMethods.test_binary_operator_override
5+
*graalpython.lib-python.3.test.test_descr.ClassPropertiesAndMethods.test_bound_method_repr
56
*graalpython.lib-python.3.test.test_descr.ClassPropertiesAndMethods.test_bpo25750
67
*graalpython.lib-python.3.test.test_descr.ClassPropertiesAndMethods.test_buffer_inheritance
8+
*graalpython.lib-python.3.test.test_descr.ClassPropertiesAndMethods.test_builtin_bases
79
*graalpython.lib-python.3.test.test_descr.ClassPropertiesAndMethods.test_builtin_function_or_method
810
*graalpython.lib-python.3.test.test_descr.ClassPropertiesAndMethods.test_classic
911
*graalpython.lib-python.3.test.test_descr.ClassPropertiesAndMethods.test_classic_comparisons
@@ -50,6 +52,7 @@
5052
*graalpython.lib-python.3.test.test_descr.ClassPropertiesAndMethods.test_proxy_super
5153
*graalpython.lib-python.3.test.test_descr.ClassPropertiesAndMethods.test_python_dicts
5254
*graalpython.lib-python.3.test.test_descr.ClassPropertiesAndMethods.test_python_lists
55+
*graalpython.lib-python.3.test.test_descr.ClassPropertiesAndMethods.test_qualname_dict
5356
*graalpython.lib-python.3.test.test_descr.ClassPropertiesAndMethods.test_recursive_call
5457
*graalpython.lib-python.3.test.test_descr.ClassPropertiesAndMethods.test_refleaks_in_classmethod___init__
5558
*graalpython.lib-python.3.test.test_descr.ClassPropertiesAndMethods.test_refleaks_in_staticmethod___init__
@@ -79,6 +82,7 @@
7982
*graalpython.lib-python.3.test.test_descr.MroTest.test_incomplete_super
8083
*graalpython.lib-python.3.test.test_descr.MroTest.test_reent_set_bases_on_base
8184
*graalpython.lib-python.3.test.test_descr.MroTest.test_reent_set_bases_on_direct_base
85+
*graalpython.lib-python.3.test.test_descr.MroTest.test_reent_set_bases_tp_base_cycle
8286
*graalpython.lib-python.3.test.test_descr.OperatorsTest.test_complexes
8387
*graalpython.lib-python.3.test.test_descr.OperatorsTest.test_dicts
8488
*graalpython.lib-python.3.test.test_descr.OperatorsTest.test_explicit_reverse_methods

graalpython/com.oracle.graal.python.test/src/tests/unittest_tags/test_ordered_dict.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
*graalpython.lib-python.3.test.test_ordered_dict.CPythonOrderedDictSubclassTests.test_equality
3939
*graalpython.lib-python.3.test.test_ordered_dict.CPythonOrderedDictSubclassTests.test_fromkeys
4040
*graalpython.lib-python.3.test.test_ordered_dict.CPythonOrderedDictSubclassTests.test_highly_nested
41+
*graalpython.lib-python.3.test.test_ordered_dict.CPythonOrderedDictSubclassTests.test_init
4142
*graalpython.lib-python.3.test.test_ordered_dict.CPythonOrderedDictSubclassTests.test_init_calls
4243
*graalpython.lib-python.3.test.test_ordered_dict.CPythonOrderedDictSubclassTests.test_issue24347
4344
*graalpython.lib-python.3.test.test_ordered_dict.CPythonOrderedDictSubclassTests.test_issue24348
@@ -153,6 +154,7 @@
153154
*graalpython.lib-python.3.test.test_ordered_dict.PurePythonOrderedDictSubclassTests.test_equality
154155
*graalpython.lib-python.3.test.test_ordered_dict.PurePythonOrderedDictSubclassTests.test_fromkeys
155156
*graalpython.lib-python.3.test.test_ordered_dict.PurePythonOrderedDictSubclassTests.test_highly_nested
157+
*graalpython.lib-python.3.test.test_ordered_dict.PurePythonOrderedDictSubclassTests.test_init
156158
*graalpython.lib-python.3.test.test_ordered_dict.PurePythonOrderedDictSubclassTests.test_init_calls
157159
*graalpython.lib-python.3.test.test_ordered_dict.PurePythonOrderedDictSubclassTests.test_issue24347
158160
*graalpython.lib-python.3.test.test_ordered_dict.PurePythonOrderedDictSubclassTests.test_issue24348

graalpython/com.oracle.graal.python.test/src/tests/unittest_tags/test_posixpath.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@
4949
*graalpython.lib-python.3.test.test_posixpath.PosixPathTest.test_ismount_symlinks
5050
*graalpython.lib-python.3.test.test_posixpath.PosixPathTest.test_join
5151
*graalpython.lib-python.3.test.test_posixpath.PosixPathTest.test_normpath
52-
*graalpython.lib-python.3.test.test_posixpath.PosixPathTest.test_realpath_repeated_indirect_symlinks
5352
*graalpython.lib-python.3.test.test_posixpath.PosixPathTest.test_realpath_resolve_before_normalizing
5453
*graalpython.lib-python.3.test.test_posixpath.PosixPathTest.test_realpath_resolve_first
5554
*graalpython.lib-python.3.test.test_posixpath.PosixPathTest.test_realpath_resolve_parents

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@
101101
import com.oracle.graal.python.builtins.objects.function.PArguments;
102102
import com.oracle.graal.python.builtins.objects.function.PKeyword;
103103
import com.oracle.graal.python.builtins.objects.ints.PInt;
104+
import com.oracle.graal.python.builtins.objects.list.ListBuiltins;
105+
import com.oracle.graal.python.builtins.objects.list.PList;
104106
import com.oracle.graal.python.builtins.objects.module.PythonModule;
105107
import com.oracle.graal.python.builtins.objects.object.PythonObject;
106108
import com.oracle.graal.python.builtins.objects.object.PythonObjectLibrary;
@@ -484,17 +486,24 @@ Object locals(VirtualFrame frame, @SuppressWarnings("unused") Object object,
484486
@Cached MaterializeFrameNode materializeNode,
485487
@Cached("createBinaryProfile()") ConditionProfile inGenerator,
486488
@Cached("create(KEYS)") LookupAndCallUnaryNode callKeysNode,
489+
@Cached ListBuiltins.ListSortNode sortNode,
487490
@Cached ListNodes.ConstructListNode constructListNode) {
488491

489492
Object localsDict = LocalsNode.getLocalsDict(frame, this, readLocalsNode, readCallerFrameNode, materializeNode, inGenerator);
490493
Object keysObj = callKeysNode.executeObject(frame, localsDict);
491-
return constructListNode.execute(keysObj);
494+
PList list = constructListNode.execute(keysObj);
495+
sortNode.sort(frame, list);
496+
return list;
492497
}
493498

494499
@Specialization(guards = "!isNoValue(object)")
495500
Object dir(VirtualFrame frame, Object object,
501+
@Cached ListBuiltins.ListSortNode sortNode,
502+
@Cached ListNodes.ConstructListNode constructListNode,
496503
@Cached("create(__DIR__)") LookupAndCallUnaryNode dirNode) {
497-
return dirNode.executeObject(frame, object);
504+
PList list = constructListNode.execute(dirNode.executeObject(frame, object));
505+
sortNode.sort(frame, list);
506+
return list;
498507
}
499508
}
500509

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,12 @@ public MaterializedFrame getFrame() {
8888
}
8989

9090
private Object getValue(FrameSlot slot) {
91+
return getValue(this.frame, slot);
92+
}
93+
94+
private static Object getValue(MaterializedFrame frame, FrameSlot slot) {
9195
if (slot != null) {
92-
Object value = this.frame.getValue(slot);
96+
Object value = frame.getValue(slot);
9397
if (value instanceof PCell) {
9498
return ((PCell) value).getRef();
9599
}
@@ -113,7 +117,7 @@ private void calculateLength() {
113117
this.len = this.frame.getFrameDescriptor().getSize();
114118
for (FrameSlot slot : this.frame.getFrameDescriptor().getSlots()) {
115119
Object identifier = slot.getIdentifier();
116-
if (!FrameSlotIDs.isUserFrameSlot(identifier) || this.frame.getValue(slot) == null) {
120+
if (!FrameSlotIDs.isUserFrameSlot(identifier) || getValue(frame, slot) == null) {
117121
this.len--;
118122
}
119123
}
@@ -369,7 +373,7 @@ protected boolean loadNext() {
369373
Object identifier = nextCandidate.getIdentifier();
370374
if (identifier instanceof String) {
371375
if (FrameSlotIDs.isUserFrameSlot(identifier)) {
372-
Object nextValue = this.frame.getValue(nextCandidate);
376+
Object nextValue = getValue(this.frame, nextCandidate);
373377
if (nextValue != null) {
374378
this.nextFrameSlot = nextCandidate;
375379
return true;
@@ -396,7 +400,7 @@ protected boolean loadNext() {
396400
Object identifier = nextCandidate.getIdentifier();
397401
if (identifier instanceof String) {
398402
if (FrameSlotIDs.isUserFrameSlot(identifier)) {
399-
Object nextValue = this.frame.getValue(nextCandidate);
403+
Object nextValue = getValue(this.frame, nextCandidate);
400404
if (nextValue != null) {
401405
this.nextFrameSlot = nextCandidate;
402406
return true;

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/list/ListBuiltins.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
import com.oracle.graal.python.builtins.objects.tuple.PTuple;
8282
import com.oracle.graal.python.nodes.ErrorMessages;
8383
import com.oracle.graal.python.nodes.PGuards;
84+
import static com.oracle.graal.python.nodes.SpecialMethodNames.SORT;
8485
import com.oracle.graal.python.nodes.argument.ReadArgumentNode;
8586
import com.oracle.graal.python.nodes.attributes.GetAttributeNode;
8687
import com.oracle.graal.python.nodes.builtins.ListNodes;
@@ -850,7 +851,7 @@ public static ListReverseNode create() {
850851
}
851852

852853
// list.sort(key=, reverse=)
853-
@Builtin(name = "sort", minNumOfPositionalArgs = 1, takesVarArgs = true, takesVarKeywordArgs = true, needsFrame = true)
854+
@Builtin(name = SORT, minNumOfPositionalArgs = 1, takesVarArgs = true, takesVarKeywordArgs = true, needsFrame = true)
854855
@GenerateNodeFactory
855856
public abstract static class ListSortNode extends PythonVarargsBuiltinNode {
856857

@@ -876,6 +877,12 @@ protected static boolean maySideEffect(PList list, PKeyword[] keywords) {
876877
return false;
877878
}
878879

880+
public final Object sort(VirtualFrame frame, PList list) {
881+
return this.execute(frame, list, PArguments.EMPTY_VARARGS, PKeyword.EMPTY_KEYWORDS);
882+
}
883+
884+
public abstract Object execute(VirtualFrame frame, PList list, Object[] arguments, PKeyword[] keywords);
885+
879886
@Specialization(guards = "!isSortable(list, lenNode)")
880887
@SuppressWarnings("unused")
881888
Object none(VirtualFrame frame, PList list, Object[] arguments, PKeyword[] keywords,
@@ -906,6 +913,10 @@ Object defaultSort(VirtualFrame frame, PList list, Object[] arguments, PKeyword[
906913
callSort.execute(sortMethod, arguments, keywords);
907914
return PNone.NONE;
908915
}
916+
917+
public static ListSortNode create() {
918+
return ListBuiltinsFactory.ListSortNodeFactory.create();
919+
}
909920
}
910921

911922
@Builtin(name = __LEN__, minNumOfPositionalArgs = 1)

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,11 @@
4040
*/
4141
package com.oracle.graal.python.builtins.objects.module;
4242

43-
import static com.oracle.graal.python.nodes.SpecialAttributeNames.__DICT__;
4443
import static com.oracle.graal.python.nodes.SpecialAttributeNames.__DOC__;
4544
import static com.oracle.graal.python.nodes.SpecialAttributeNames.__LOADER__;
4645
import static com.oracle.graal.python.nodes.SpecialAttributeNames.__NAME__;
4746
import static com.oracle.graal.python.nodes.SpecialAttributeNames.__PACKAGE__;
4847
import static com.oracle.graal.python.nodes.SpecialAttributeNames.__SPEC__;
49-
import static com.oracle.graal.python.nodes.SpecialMethodNames.__DIR__;
50-
import static com.oracle.graal.python.nodes.SpecialMethodNames.__GETATTRIBUTE__;
5148
import static com.oracle.graal.python.nodes.SpecialMethodNames.__GETATTR__;
5249
import static com.oracle.graal.python.nodes.SpecialMethodNames.__INIT__;
5350
import static com.oracle.graal.python.runtime.exception.PythonErrorType.AttributeError;
@@ -68,6 +65,9 @@
6865
import com.oracle.graal.python.builtins.objects.object.ObjectBuiltins;
6966
import com.oracle.graal.python.builtins.objects.object.PythonObjectLibrary;
7067
import com.oracle.graal.python.nodes.ErrorMessages;
68+
import static com.oracle.graal.python.nodes.SpecialAttributeNames.__DICT__;
69+
import static com.oracle.graal.python.nodes.SpecialMethodNames.__DIR__;
70+
import static com.oracle.graal.python.nodes.SpecialMethodNames.__GETATTRIBUTE__;
7171
import com.oracle.graal.python.nodes.attributes.ReadAttributeFromObjectNode;
7272
import com.oracle.graal.python.nodes.attributes.WriteAttributeToObjectNode;
7373
import com.oracle.graal.python.nodes.builtins.ListNodes;
@@ -161,7 +161,7 @@ Object dir(PythonModule self,
161161
}
162162
} else {
163163
String name = getName(self, pol, hashLib, castToJavaStringNode);
164-
throw this.raise(PythonBuiltinClassType.TypeError, "%s.__dict__ is not a dictionary", name);
164+
throw raise(PythonBuiltinClassType.TypeError, ErrorMessages.IS_NOT_A_DICTIONARY, name);
165165
}
166166
}
167167

@@ -173,7 +173,7 @@ private String getName(PythonModule self, PythonObjectLibrary pol, HashingStorag
173173
return castToJavaStringNode.execute(name);
174174
}
175175
}
176-
throw raise(PythonBuiltinClassType.SystemError, "nameless module");
176+
throw raise(PythonBuiltinClassType.SystemError, ErrorMessages.NAMELESS_MODULE);
177177
}
178178

179179
protected static boolean isDict(Object object, IsBuiltinClassProfile profile) {

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

Lines changed: 56 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
import com.oracle.truffle.api.library.ExportMessage;
5656
import com.oracle.truffle.api.object.DynamicObjectLibrary;
5757
import com.oracle.truffle.api.object.Shape;
58+
import java.util.ArrayList;
5859

5960
public abstract class PythonManagedClass extends PythonObject implements PythonAbstractClass {
6061

@@ -210,31 +211,69 @@ private void unsafeSetSuperClass(PythonAbstractClass... newBaseClasses) {
210211

211212
@TruffleBoundary
212213
public void setSuperClass(PythonAbstractClass... newBaseClasses) {
213-
PythonAbstractClass[] oldBaseClasses = getBaseClasses();
214-
try {
215-
setSuperClassInternal(newBaseClasses);
216-
} catch (PException pe) {
217-
setSuperClassInternal(oldBaseClasses);
218-
throw pe;
219-
}
220-
}
221-
222-
private void setSuperClassInternal(PythonAbstractClass[] basses) {
223-
for (PythonAbstractClass base : basses) {
224-
if (base != null) {
225-
GetSubclassesNode.getUncached().execute(base).add(this);
214+
ArrayList<Set<PythonAbstractClass>> newBasesSubclasses = new ArrayList<>(newBaseClasses.length);
215+
for (int i = 0; i < newBaseClasses.length; i++) {
216+
if (newBaseClasses[i] != null) {
217+
newBasesSubclasses.add(GetSubclassesNode.getUncached().execute(newBaseClasses[i]));
226218
}
227219
}
228220

229-
this.baseClasses = basses;
230-
this.methodResolutionOrder.setInternalArrayObject(ComputeMroNode.doSlowPath(this));
221+
PythonAbstractClass[] oldBaseClasses = getBaseClasses();
222+
Object[] oldMRO = this.methodResolutionOrder.getInternalArray();
231223

232224
Set<PythonAbstractClass> subclasses = GetSubclassesNode.getUncached().execute(this);
233-
for (PythonAbstractClass scls : subclasses) {
225+
PythonAbstractClass[] subclassesArray = subclasses.toArray(new PythonAbstractClass[subclasses.size()]);
226+
Object[][] oldSubClasssMROs = new Object[subclasses.size()][];
227+
for (int i = 0; i < subclassesArray.length; i++) {
228+
PythonAbstractClass scls = subclassesArray[i];
234229
if (scls instanceof PythonManagedClass) {
235-
((PythonManagedClass) scls).methodResolutionOrder.setInternalArrayObject(ComputeMroNode.doSlowPath(scls));
230+
oldSubClasssMROs[i] = ((PythonManagedClass) scls).methodResolutionOrder.getInternalArray();
236231
}
237232
}
233+
234+
try {
235+
for (PythonAbstractClass base : newBaseClasses) {
236+
if (base != null) {
237+
GetSubclassesNode.getUncached().execute(base).add(this);
238+
}
239+
}
240+
241+
this.baseClasses = newBaseClasses;
242+
this.methodResolutionOrder.setInternalArrayObject(ComputeMroNode.doSlowPath(this));
243+
this.methodResolutionOrder.lookupChanged();
244+
245+
for (PythonAbstractClass scls : subclasses) {
246+
if (scls instanceof PythonManagedClass) {
247+
PythonManagedClass pmc = (PythonManagedClass) scls;
248+
pmc.methodResolutionOrder.setInternalArrayObject(ComputeMroNode.doSlowPath(scls));
249+
pmc.methodResolutionOrder.lookupChanged();
250+
}
251+
}
252+
} catch (PException pe) {
253+
// undo
254+
for (int i = 0; i < newBaseClasses.length; i++) {
255+
PythonAbstractClass base = newBaseClasses[i];
256+
if (base != null) {
257+
Set<PythonAbstractClass> s = GetSubclassesNode.getUncached().execute(base);
258+
s.clear();
259+
s.addAll(newBasesSubclasses.get(i));
260+
}
261+
}
262+
263+
this.baseClasses = oldBaseClasses;
264+
this.methodResolutionOrder.setInternalArrayObject(oldMRO);
265+
this.methodResolutionOrder.lookupChanged();
266+
267+
for (int i = 0; i < subclassesArray.length; i++) {
268+
PythonAbstractClass scls = subclassesArray[i];
269+
if (oldSubClasssMROs[i] != null) {
270+
PythonManagedClass pmc = (PythonManagedClass) scls;
271+
pmc.methodResolutionOrder.setInternalArrayObject(oldSubClasssMROs[i]);
272+
pmc.methodResolutionOrder.lookupChanged();
273+
}
274+
}
275+
throw pe;
276+
}
238277
}
239278

240279
final Set<PythonAbstractClass> getSubClasses() {

0 commit comments

Comments
 (0)