Skip to content

Commit 28e3c15

Browse files
committed
Fix set recursive repr
1 parent fee1226 commit 28e3c15

File tree

1 file changed

+26
-11
lines changed
  • graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/set

1 file changed

+26
-11
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/set/BaseSetBuiltins.java

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
import java.util.Iterator;
5656
import java.util.List;
5757

58+
import com.oracle.graal.python.PythonLanguage;
5859
import com.oracle.graal.python.builtins.Builtin;
5960
import com.oracle.graal.python.builtins.CoreFunctions;
6061
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
@@ -79,10 +80,12 @@
7980
import com.oracle.graal.python.nodes.function.builtins.PythonUnaryBuiltinNode;
8081
import com.oracle.graal.python.nodes.object.GetClassNode;
8182
import com.oracle.graal.python.nodes.object.IsBuiltinClassProfile;
83+
import com.oracle.graal.python.runtime.PythonContext;
8284
import com.oracle.graal.python.runtime.exception.PException;
8385
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
8486
import com.oracle.graal.python.util.PythonUtils;
8587
import com.oracle.truffle.api.dsl.Cached;
88+
import com.oracle.truffle.api.dsl.CachedContext;
8689
import com.oracle.truffle.api.dsl.Fallback;
8790
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
8891
import com.oracle.truffle.api.dsl.ImportStatic;
@@ -123,27 +126,39 @@ private static void fillItems(VirtualFrame frame, StringBuilder sb, LookupAndCal
123126

124127
@Specialization(limit = "3")
125128
public static Object repr(VirtualFrame frame, PBaseSet self,
126-
@Cached IsBuiltinClassProfile isBuiltinClass,
129+
@CachedContext(PythonLanguage.class) PythonContext ctxt,
127130
@Cached("create(__REPR__)") LookupAndCallUnaryNode repr,
128131
@Cached TypeNodes.GetNameNode getNameNode,
129132
@Cached GetClassNode getClassNode,
130133
@CachedLibrary("self.getDictStorage()") HashingStorageLibrary hlib) {
131134
StringBuilder sb = PythonUtils.newStringBuilder();
132-
int len = hlib.length(self.getDictStorage());
133-
HashingStorageLibrary.HashingStorageIterator<Object> iter = hlib.keys(self.getDictStorage()).iterator();
134135
Object clazz = getClassNode.execute(self);
135-
if (len > 0 && clazz == PythonBuiltinClassType.PSet && isBuiltinClass.profileIsAnyBuiltinClass(clazz)) {
136-
fillItems(frame, sb, repr, iter);
136+
int len = hlib.length(self.getDictStorage());
137+
if (len == 0) {
138+
PythonUtils.append(sb, getNameNode.execute(clazz));
139+
PythonUtils.append(sb, "()");
140+
return PythonUtils.sbToString(sb);
141+
}
142+
if (!ctxt.reprEnter(self)) {
143+
PythonUtils.append(sb, getNameNode.execute(clazz));
144+
PythonUtils.append(sb, "(...)");
137145
return PythonUtils.sbToString(sb);
138146
}
139-
String typeName = getNameNode.execute(clazz);
140-
PythonUtils.append(sb, typeName);
141-
PythonUtils.append(sb, "(");
142-
if (len > 0) {
147+
try {
148+
HashingStorageLibrary.HashingStorageIterator<Object> iter = hlib.keys(self.getDictStorage()).iterator();
149+
boolean showType = clazz != PythonBuiltinClassType.PSet;
150+
if (showType) {
151+
PythonUtils.append(sb, getNameNode.execute(clazz));
152+
PythonUtils.append(sb, '(');
153+
}
143154
fillItems(frame, sb, repr, iter);
155+
if (showType) {
156+
PythonUtils.append(sb, ')');
157+
}
158+
return PythonUtils.sbToString(sb);
159+
} finally {
160+
ctxt.reprLeave(self);
144161
}
145-
PythonUtils.append(sb, ")");
146-
return PythonUtils.sbToString(sb);
147162
}
148163
}
149164

0 commit comments

Comments
 (0)