89
89
import com .oracle .graal .python .lib .PyObjectRichCompareBool ;
90
90
import com .oracle .graal .python .lib .RichCmpOp ;
91
91
import com .oracle .graal .python .nodes .ErrorMessages ;
92
+ import com .oracle .graal .python .nodes .PGuards ;
92
93
import com .oracle .graal .python .nodes .PRaiseNode ;
93
94
import com .oracle .graal .python .nodes .builtins .ListNodes ;
94
95
import com .oracle .graal .python .nodes .builtins .ListNodes .AppendNode ;
113
114
import com .oracle .graal .python .runtime .sequence .storage .IntSequenceStorage ;
114
115
import com .oracle .graal .python .runtime .sequence .storage .LongSequenceStorage ;
115
116
import com .oracle .graal .python .runtime .sequence .storage .SequenceStorage ;
117
+ import com .oracle .truffle .api .CompilerDirectives ;
116
118
import com .oracle .truffle .api .HostCompilerDirectives .InliningCutoff ;
117
119
import com .oracle .truffle .api .dsl .Bind ;
118
120
import com .oracle .truffle .api .dsl .Cached ;
124
126
import com .oracle .truffle .api .dsl .NeverDefault ;
125
127
import com .oracle .truffle .api .dsl .Specialization ;
126
128
import com .oracle .truffle .api .frame .VirtualFrame ;
129
+ import com .oracle .truffle .api .interop .InteropLibrary ;
130
+ import com .oracle .truffle .api .interop .UnsupportedMessageException ;
131
+ import com .oracle .truffle .api .library .CachedLibrary ;
127
132
import com .oracle .truffle .api .nodes .LoopNode ;
128
133
import com .oracle .truffle .api .nodes .Node ;
129
134
import com .oracle .truffle .api .profiles .InlinedBranchProfile ;
@@ -184,6 +189,7 @@ abstract static class ReprNode extends PythonUnaryBuiltinNode {
184
189
public TruffleString repr (VirtualFrame frame , Object self ,
185
190
@ Bind ("this" ) Node inliningTarget ,
186
191
@ Cached GetListStorageNode getStorageNode ,
192
+ @ CachedLibrary (limit = "2" ) InteropLibrary interopLib ,
187
193
@ Cached SequenceStorageNodes .GetItemNode getItem ,
188
194
@ Cached PyObjectReprAsTruffleStringNode reprNode ,
189
195
@ Cached TruffleStringBuilder .AppendStringNode appendStringNode ,
@@ -193,7 +199,21 @@ public TruffleString repr(VirtualFrame frame, Object self,
193
199
if (length == 0 ) {
194
200
return T_EMPTY_BRACKETS ;
195
201
}
196
- if (!PythonContext .get (this ).reprEnter (self )) {
202
+ Object reprIdentity = self ;
203
+ if (!PGuards .isAnyPythonObject (self )) {
204
+ // The interop library dispatch initialization acts as branch profile. Hash codes
205
+ // may clash, but in this case the only downside is that we print an ellipsis
206
+ // instead of expanding more.
207
+ if (interopLib .hasIdentity (self )) {
208
+ try {
209
+ reprIdentity = interopLib .identityHashCode (self );
210
+ } catch (UnsupportedMessageException e ) {
211
+ throw CompilerDirectives .shouldNotReachHere (e );
212
+ }
213
+ }
214
+ }
215
+ PythonContext context = PythonContext .get (this );
216
+ if (!context .reprEnter (reprIdentity )) {
197
217
return T_ELLIPSIS_IN_BRACKETS ;
198
218
}
199
219
try {
@@ -212,7 +232,7 @@ public TruffleString repr(VirtualFrame frame, Object self,
212
232
appendStringNode .execute (buf , T_RBRACKET );
213
233
return toStringNode .execute (buf );
214
234
} finally {
215
- PythonContext . get ( this ). reprLeave (self );
235
+ context . reprLeave (reprIdentity );
216
236
}
217
237
}
218
238
}
0 commit comments