75
75
import com .oracle .graal .python .lib .PyObjectReprAsObjectNode ;
76
76
import com .oracle .graal .python .lib .PyObjectStrAsTruffleStringNode ;
77
77
import com .oracle .graal .python .nodes .PRaiseNode ;
78
- import com .oracle .graal .python .runtime .exception .PException ;
79
78
import com .oracle .truffle .api .CompilerDirectives .TruffleBoundary ;
80
79
import com .oracle .truffle .api .nodes .Node ;
81
80
import com .oracle .truffle .api .strings .TruffleString ;
@@ -262,11 +261,7 @@ private Object getArgument(Node node, String name, PyObjectGetItem getItemNode)
262
261
if (isEmpty ) {
263
262
index = -1 ;
264
263
} else {
265
- try {
266
- index = toInt (node , intString );
267
- } catch (NumberFormatException | ArrayIndexOutOfBoundsException nfe1 ) {
268
- index = -1 ;
269
- }
264
+ index = toInt (node , intString );
270
265
}
271
266
boolean useNumeric = isEmpty || index != -1 ;
272
267
if (this .autoNumberingState == ANS_INIT && useNumeric ) {
@@ -348,19 +343,12 @@ private Object resolveLookups(Node node, Object obj, String name, int startArg,
348
343
if (!gotBracket ) {
349
344
throw PRaiseNode .raiseUncached (node , ValueError , MISSING_S , "']'" );
350
345
}
351
- Object item = null ;
352
- int index = -1 ;
353
- try {
354
- index = toInt (node , name .substring (start , i ));
355
- } catch (NumberFormatException | ArrayIndexOutOfBoundsException nfe1 ) {
356
- item = toTruffleStringUncached (name .substring (start , i ));
357
- }
358
- if (index > -1 ) {
359
- if (index > SysModuleBuiltins .MAXSIZE ) {
360
- throw PRaiseNode .raiseUncached (node , ValueError , TOO_MANY_DECIMAL_DIGITS_IN_FORMAT_STRING );
361
- }
362
- item = index ;
346
+ String s = name .substring (start , i );
347
+ if (s .isEmpty ()) {
348
+ throw PRaiseNode .raiseUncached (node , ValueError , EMPTY_ATTR_IN_FORMAT_STR );
363
349
}
350
+ int index = toInt (node , s );
351
+ Object item = index != -1 ? index : toTruffleStringUncached (s );
364
352
i += 1 ; // # Skip "]"
365
353
if (result != null ) {
366
354
result = getItemNode .execute (null , result , item );
@@ -374,12 +362,18 @@ private Object resolveLookups(Node node, Object obj, String name, int startArg,
374
362
return result ;
375
363
}
376
364
377
- private static int toInt (Node node , String s ) throws PException {
378
- BigInteger bigInt = new BigInteger (s );
379
- if (bigInt .compareTo (MAXSIZE ) > 0 ) {
365
+ private static int toInt (Node node , String s ) {
366
+ try {
367
+ BigInteger bigInt = new BigInteger (s );
368
+ if (bigInt .signum () >= 0 ) {
369
+ return bigInt .intValueExact ();
370
+ }
371
+ return -1 ;
372
+ } catch (NumberFormatException e ) {
373
+ return -1 ;
374
+ } catch (ArithmeticException e ) {
380
375
throw PRaiseNode .raiseUncached (node , ValueError , TOO_MANY_DECIMAL_DIGITS_IN_FORMAT_STRING );
381
376
}
382
- return bigInt .intValue ();
383
377
}
384
378
385
379
private Object renderField (Node node , int start , int end , boolean recursive , int level , FormatNode formatNode , PyObjectGetItem getItemNode ) {
0 commit comments