8989import com .oracle .graal .python .lib .PyObjectRichCompareBool ;
9090import com .oracle .graal .python .lib .RichCmpOp ;
9191import com .oracle .graal .python .nodes .ErrorMessages ;
92+ import com .oracle .graal .python .nodes .PGuards ;
9293import com .oracle .graal .python .nodes .PRaiseNode ;
9394import com .oracle .graal .python .nodes .builtins .ListNodes ;
9495import com .oracle .graal .python .nodes .builtins .ListNodes .AppendNode ;
113114import com .oracle .graal .python .runtime .sequence .storage .IntSequenceStorage ;
114115import com .oracle .graal .python .runtime .sequence .storage .LongSequenceStorage ;
115116import com .oracle .graal .python .runtime .sequence .storage .SequenceStorage ;
117+ import com .oracle .truffle .api .CompilerDirectives ;
116118import com .oracle .truffle .api .HostCompilerDirectives .InliningCutoff ;
117119import com .oracle .truffle .api .dsl .Bind ;
118120import com .oracle .truffle .api .dsl .Cached ;
124126import com .oracle .truffle .api .dsl .NeverDefault ;
125127import com .oracle .truffle .api .dsl .Specialization ;
126128import 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 ;
127132import com .oracle .truffle .api .nodes .LoopNode ;
128133import com .oracle .truffle .api .nodes .Node ;
129134import com .oracle .truffle .api .profiles .InlinedBranchProfile ;
@@ -184,6 +189,7 @@ abstract static class ReprNode extends PythonUnaryBuiltinNode {
184189 public TruffleString repr (VirtualFrame frame , Object self ,
185190 @ Bind ("this" ) Node inliningTarget ,
186191 @ Cached GetListStorageNode getStorageNode ,
192+ @ CachedLibrary (limit = "2" ) InteropLibrary interopLib ,
187193 @ Cached SequenceStorageNodes .GetItemNode getItem ,
188194 @ Cached PyObjectReprAsTruffleStringNode reprNode ,
189195 @ Cached TruffleStringBuilder .AppendStringNode appendStringNode ,
@@ -193,7 +199,21 @@ public TruffleString repr(VirtualFrame frame, Object self,
193199 if (length == 0 ) {
194200 return T_EMPTY_BRACKETS ;
195201 }
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 )) {
197217 return T_ELLIPSIS_IN_BRACKETS ;
198218 }
199219 try {
@@ -212,7 +232,7 @@ public TruffleString repr(VirtualFrame frame, Object self,
212232 appendStringNode .execute (buf , T_RBRACKET );
213233 return toStringNode .execute (buf );
214234 } finally {
215- PythonContext . get ( this ). reprLeave (self );
235+ context . reprLeave (reprIdentity );
216236 }
217237 }
218238 }
0 commit comments