26
26
import com .oracle .graal .python .builtins .objects .ints .PInt ;
27
27
import com .oracle .graal .python .builtins .objects .object .PythonObjectLibrary ;
28
28
import com .oracle .graal .python .builtins .objects .str .PString ;
29
+ import com .oracle .graal .python .builtins .objects .tuple .TupleBuiltins ;
29
30
import com .oracle .graal .python .nodes .ErrorMessages ;
30
31
import com .oracle .graal .python .nodes .PGuards ;
31
32
import com .oracle .graal .python .nodes .call .CallNode ;
@@ -67,7 +68,7 @@ public StringFormatter(PythonCore core, String format) {
67
68
buffer = new StringBuilder (format .length () + 100 );
68
69
}
69
70
70
- Object getarg (LookupAndCallBinaryNode getItemNode ) {
71
+ Object getarg (TupleBuiltins . GetItemNode getTupleItemNode ) {
71
72
Object ret = null ;
72
73
switch (argIndex ) {
73
74
case -3 : // special index indicating a mapping
@@ -79,7 +80,14 @@ Object getarg(LookupAndCallBinaryNode getItemNode) {
79
80
return args ;
80
81
default :
81
82
// NOTE: passing 'null' frame means we already took care of the global state earlier
82
- ret = getItemNode .executeObject (null , args , argIndex ++);
83
+ // args is definitely a tuple at this point. CPython access the tuple storage
84
+ // directly, so the only error can be IndexError, which we ignore and transform into
85
+ // the TypeError below.
86
+ try {
87
+ ret = getTupleItemNode .execute (null , args , argIndex ++);
88
+ } catch (PException e ) {
89
+ // fall through
90
+ }
83
91
break ;
84
92
}
85
93
if (ret == null ) {
@@ -88,10 +96,10 @@ Object getarg(LookupAndCallBinaryNode getItemNode) {
88
96
return ret ;
89
97
}
90
98
91
- int getNumber (LookupAndCallBinaryNode getItemNode ) {
99
+ int getNumber (TupleBuiltins . GetItemNode getTupleItemNode ) {
92
100
char c = pop ();
93
101
if (c == '*' ) {
94
- Object o = getarg (getItemNode );
102
+ Object o = getarg (getTupleItemNode );
95
103
if (o instanceof Long ) {
96
104
return ((Long ) o ).intValue ();
97
105
} else if (o instanceof Integer ) {
@@ -164,7 +172,7 @@ private static Object asFloat(Object arg, CallNode callNode, BiFunction<Object,
164
172
* construction.
165
173
*/
166
174
@ TruffleBoundary
167
- public Object format (Object args1 , CallNode callNode , BiFunction <Object , String , Object > lookupAttribute , LookupAndCallBinaryNode getItemNode ) {
175
+ public Object format (Object args1 , CallNode callNode , BiFunction <Object , String , Object > lookupAttribute , LookupAndCallBinaryNode getItemNode , TupleBuiltins . GetItemNode getTupleItemNode ) {
168
176
Object mapping = null ;
169
177
this .args = args1 ;
170
178
@@ -272,7 +280,7 @@ public Object format(Object args1, CallNode callNode, BiFunction<Object, String,
272
280
* after the minimum field width and optional precision. A custom getNumber() takes care
273
281
* of the '*' case.
274
282
*/
275
- width = getNumber (getItemNode );
283
+ width = getNumber (getTupleItemNode );
276
284
if (width < 0 ) {
277
285
width = -width ;
278
286
align = '<' ;
@@ -286,7 +294,7 @@ public Object format(Object args1, CallNode callNode, BiFunction<Object, String,
286
294
*/
287
295
c = pop ();
288
296
if (c == '.' ) {
289
- precision = getNumber (getItemNode );
297
+ precision = getNumber (getTupleItemNode );
290
298
if (precision < -1 ) {
291
299
precision = 0 ;
292
300
}
@@ -341,7 +349,7 @@ public Object format(Object args1, CallNode callNode, BiFunction<Object, String,
341
349
342
350
switch (spec .type ) {
343
351
case 'b' :
344
- Object arg = getarg (getItemNode );
352
+ Object arg = getarg (getTupleItemNode );
345
353
f = ft = new TextFormatter (core , buffer , spec );
346
354
ft .setBytes (true );
347
355
Object bytesAttribute ;
@@ -360,7 +368,7 @@ public Object format(Object args1, CallNode callNode, BiFunction<Object, String,
360
368
break ;
361
369
case 's' : // String: converts any object using __str__(), __unicode__() ...
362
370
case 'r' : // ... or repr().
363
- arg = getarg (getItemNode );
371
+ arg = getarg (getTupleItemNode );
364
372
// Get hold of the actual object to display (may set needUnicode)
365
373
Object attribute = spec .type == 's' ? lookupAttribute .apply (arg , __STR__ ) : lookupAttribute .apply (arg , __REPR__ );
366
374
if (attribute != PNone .NO_VALUE ) {
@@ -383,7 +391,7 @@ public Object format(Object args1, CallNode callNode, BiFunction<Object, String,
383
391
case 'i' : // Compatibility with scanf().
384
392
// Format the argument using this Spec.
385
393
386
- arg = getarg (getItemNode );
394
+ arg = getarg (getTupleItemNode );
387
395
388
396
// Note various types accepted here as long as they have an __int__ method.
389
397
Object argAsNumber = asNumber (arg , callNode , lookupAttribute );
@@ -422,7 +430,7 @@ public Object format(Object args1, CallNode callNode, BiFunction<Object, String,
422
430
f = ff = new FloatFormatter (core , buffer , spec );
423
431
424
432
// Note various types accepted here as long as they have a __float__ method.
425
- arg = getarg (getItemNode );
433
+ arg = getarg (getTupleItemNode );
426
434
Object argAsFloat = asFloat (arg , callNode , lookupAttribute );
427
435
428
436
// We have to check what we got back..
0 commit comments