4141package com .oracle .graal .python .builtins .objects .itertools ;
4242
4343import static com .oracle .graal .python .builtins .PythonBuiltinClassType .TypeError ;
44+ import static com .oracle .graal .python .builtins .PythonBuiltinClassType .ValueError ;
4445import static com .oracle .graal .python .nodes .ErrorMessages .ARG_CANNOT_BE_NEGATIVE ;
4546import static com .oracle .graal .python .nodes .SpecialMethodNames .J___REDUCE__ ;
4647import static com .oracle .graal .python .nodes .SpecialMethodNames .J___SETSTATE__ ;
48+ import static com .oracle .graal .python .nodes .SpecialMethodNames .T___SETSTATE__ ;
4749
4850import java .util .List ;
4951
5961import com .oracle .graal .python .builtins .modules .ItertoolsModuleBuiltins .DeprecatedSetStateBuiltin ;
6062import com .oracle .graal .python .builtins .objects .PNone ;
6163import com .oracle .graal .python .builtins .objects .iterator .IteratorNodes ;
62- import com .oracle .graal .python .builtins .objects .list .PList ;
6364import com .oracle .graal .python .builtins .objects .tuple .PTuple ;
6465import com .oracle .graal .python .builtins .objects .type .TpSlots ;
6566import com .oracle .graal .python .builtins .objects .type .TypeNodes ;
6667import com .oracle .graal .python .builtins .objects .type .slots .TpSlotIterNext .TpIterNextBuiltin ;
67- import com .oracle .graal .python .lib .PyLongAsIntNode ;
6868import com .oracle .graal .python .lib .PyNumberAsSizeNode ;
69+ import com .oracle .graal .python .lib .PyTupleCheckNode ;
6970import com .oracle .graal .python .lib .PyTupleGetItem ;
71+ import com .oracle .graal .python .lib .PyTupleSizeNode ;
7072import com .oracle .graal .python .nodes .ErrorMessages ;
7173import com .oracle .graal .python .nodes .PRaiseNode ;
7274import com .oracle .graal .python .nodes .function .PythonBuiltinBaseNode ;
7375import com .oracle .graal .python .nodes .function .PythonBuiltinNode ;
7476import com .oracle .graal .python .nodes .function .builtins .PythonUnaryBuiltinNode ;
7577import com .oracle .graal .python .nodes .object .GetClassNode ;
78+ import com .oracle .graal .python .nodes .util .CannotCastException ;
79+ import com .oracle .graal .python .nodes .util .CastToJavaIntExactNode ;
7680import com .oracle .graal .python .runtime .object .PFactory ;
7781import com .oracle .graal .python .util .PythonUtils ;
82+ import com .oracle .truffle .api .CompilerDirectives .TruffleBoundary ;
7883import com .oracle .truffle .api .dsl .Bind ;
7984import com .oracle .truffle .api .dsl .Cached ;
8085import com .oracle .truffle .api .dsl .Cached .Exclusive ;
@@ -129,7 +134,7 @@ static Object constructRepeat(VirtualFrame frame, Object cls, Object[] iterables
129134 int repeatInt = asSizeNode .executeExact (frame , inliningTarget , repeat );
130135 if (repeatInt < 0 ) {
131136 errorProfile .enter (inliningTarget );
132- throw PRaiseNode .raiseStatic (inliningTarget , TypeError , ARG_CANNOT_BE_NEGATIVE , "repeat" );
137+ throw PRaiseNode .raiseStatic (inliningTarget , ValueError , ARG_CANNOT_BE_NEGATIVE , "repeat" );
133138 }
134139 PProduct self = PFactory .createProduct (cls , getInstanceShape .execute (cls ));
135140 if (repeatInt == 0 ) {
@@ -314,37 +319,46 @@ static Object reduce(PProduct self) {
314319 }
315320
316321 private static PTuple createGearTuple (PProduct self , PythonLanguage language ) {
317- PList [] lists = new PList [self .getGears ().length ];
318- for (int i = 0 ; i < lists .length ; i ++) {
319- lists [i ] = PFactory .createList (language , self .getGears ()[i ]);
322+ PTuple [] tuples = new PTuple [self .getGears ().length ];
323+ for (int i = 0 ; i < tuples .length ; i ++) {
324+ tuples [i ] = PFactory .createTuple (language , self .getGears ()[i ]);
320325 }
321- return PFactory .createTuple (language , lists );
326+ return PFactory .createTuple (language , tuples );
322327 }
323328 }
324329
325330 @ Builtin (name = J___SETSTATE__ , minNumOfPositionalArgs = 2 )
326331 @ GenerateNodeFactory
327332 public abstract static class SetStateNode extends DeprecatedSetStateBuiltin {
328333 @ Specialization
329- static Object setState (PProduct self , Object state ) {
334+ @ TruffleBoundary
335+ static Object setState (PProduct self , Object state ,
336+ @ Bind Node inliningTarget ) {
330337 Object [][] gears = self .getGears ();
338+ if (!PyTupleCheckNode .executeUncached (state ) || PyTupleSizeNode .executeUncached (state ) != gears .length ) {
339+ throw PRaiseNode .raiseStatic (inliningTarget , ValueError , ErrorMessages .INVALID_ARGS , T___SETSTATE__ );
340+ }
331341 Object [] lst = new Object [gears .length ];
332342 int [] indices = self .getIndices ();
333- for (int i = 0 ; i < gears .length ; i ++) {
334- Object o = PyTupleGetItem .executeUncached (state , i );
335- int index = PyLongAsIntNode .executeUncached (o );
336- int gearSize = gears [i ].length ;
337- if (indices == null || gearSize == 0 ) {
338- self .setStopped (true );
339- return PNone .NONE ;
340- }
341- if (index < 0 ) {
342- index = 0 ;
343- } else if (index > gearSize - 1 ) {
344- index = gearSize - 1 ;
343+ try {
344+ for (int i = 0 ; i < gears .length ; i ++) {
345+ Object o = PyTupleGetItem .executeUncached (state , i );
346+ int index = CastToJavaIntExactNode .executeUncached (o );
347+ int gearSize = gears [i ].length ;
348+ if (indices == null || gearSize == 0 ) {
349+ self .setStopped (true );
350+ return PNone .NONE ;
351+ }
352+ if (index < 0 ) {
353+ index = 0 ;
354+ } else if (index > gearSize - 1 ) {
355+ index = gearSize - 1 ;
356+ }
357+ indices [i ] = index ;
358+ lst [i ] = gears [i ][index ];
345359 }
346- indices [ i ] = index ;
347- lst [ i ] = gears [ i ][ index ] ;
360+ } catch ( CannotCastException e ) {
361+ throw PRaiseNode . raiseStatic ( inliningTarget , TypeError , ErrorMessages . INTEGER_REQUIRED ) ;
348362 }
349363 self .setLst (lst );
350364 return PNone .NONE ;
0 commit comments