4747import static com .oracle .graal .python .builtins .PythonBuiltinClassType .StopIteration ;
4848import static com .oracle .graal .python .builtins .PythonBuiltinClassType .TypeError ;
4949import static com .oracle .graal .python .builtins .PythonBuiltinClassType .ValueError ;
50+ import static com .oracle .graal .python .nodes .SpecialAttributeNames .__DICT__ ;
5051import static com .oracle .graal .python .nodes .SpecialMethodNames .__ADD__ ;
5152import static com .oracle .graal .python .nodes .SpecialMethodNames .__CONTAINS__ ;
5253import static com .oracle .graal .python .nodes .SpecialMethodNames .__DELITEM__ ;
7475import com .oracle .graal .python .annotations .ArgumentClinic .ClinicConversion ;
7576import com .oracle .graal .python .builtins .Builtin ;
7677import com .oracle .graal .python .builtins .CoreFunctions ;
78+ import com .oracle .graal .python .builtins .Python3Core ;
7779import com .oracle .graal .python .builtins .PythonBuiltinClassType ;
7880import com .oracle .graal .python .builtins .PythonBuiltins ;
7981import com .oracle .graal .python .builtins .objects .PNone ;
9395import com .oracle .graal .python .builtins .objects .type .TypeNodes .GetNameNode ;
9496import com .oracle .graal .python .lib .PyNumberAsSizeNode ;
9597import com .oracle .graal .python .lib .PyObjectIsTrueNode ;
98+ import com .oracle .graal .python .lib .PyObjectLookupAttr ;
99+ import com .oracle .graal .python .lib .PyObjectSizeNode ;
96100import com .oracle .graal .python .nodes .ErrorMessages ;
97101import com .oracle .graal .python .nodes .PGuards ;
98102import com .oracle .graal .python .nodes .PRaiseNode ;
117121import com .oracle .graal .python .nodes .util .CastToJavaIntExactNode ;
118122import com .oracle .graal .python .nodes .util .CastToJavaStringNode ;
119123import com .oracle .graal .python .runtime .PythonContext ;
120- import com .oracle .graal .python .builtins .Python3Core ;
121124import com .oracle .graal .python .runtime .exception .PException ;
122125import com .oracle .graal .python .runtime .object .PythonObjectFactory ;
123126import com .oracle .truffle .api .CompilerDirectives ;
@@ -269,8 +272,9 @@ static PNone doGeneric(PDeque self) {
269272 public abstract static class DequeCopyNode extends PythonUnaryBuiltinNode {
270273
271274 @ Specialization
272- PDeque doGeneric (PDeque self ) {
273- PDeque copy = factory ().createDeque ();
275+ PDeque doGeneric (PDeque self ,
276+ @ Cached GetClassNode getClassNode ) {
277+ PDeque copy = factory ().createDeque (getClassNode .execute (self ));
274278 copy .setMaxLength (self .getMaxLength ());
275279 copy .addAll (self );
276280 return copy ;
@@ -853,31 +857,9 @@ protected ArgumentClinicProvider getArgumentClinic() {
853857 static PNone doGeneric (PDeque self , int idx , Object value ,
854858 @ Cached NormalizeIndexCustomMessageNode normalizeIndexNode ) {
855859 int normIdx = normalizeIndexNode .execute (idx , self .getSize (), ErrorMessages .DEQUE_INDEX_OUT_OF_RANGE );
856- doSetItem ( self , normIdx , value );
860+ self . setItem ( normIdx , value != PNone . NO_VALUE ? value : null );
857861 return PNone .NONE ;
858862 }
859-
860- @ TruffleBoundary
861- static void doSetItem (PDeque self , int idx , Object value ) {
862- assert 0 <= idx && idx < self .getSize ();
863- int n = self .getSize () - idx - 1 ;
864- Object [] savedItems = new Object [n ];
865- for (int i = 0 ; i < savedItems .length ; i ++) {
866- savedItems [i ] = self .pop ();
867- }
868- // this removes the item we want to replace
869- self .pop ();
870- assert self .getSize () == idx ;
871- if (value != PNone .NO_VALUE ) {
872- self .append (value );
873- }
874-
875- // re-add saved items
876- for (int i = savedItems .length - 1 ; i >= 0 ; i --) {
877- self .append (savedItems [i ]);
878- }
879- assert value != PNone .NO_VALUE && self .getSize () == n + idx + 1 || value == PNone .NO_VALUE && self .getSize () == n + idx ;
880- }
881863 }
882864
883865 @ Builtin (name = __DELITEM__ , minNumOfPositionalArgs = 2 , parameterNames = {"$self" , "n" })
@@ -894,7 +876,7 @@ protected ArgumentClinicProvider getArgumentClinic() {
894876 static PNone doGeneric (PDeque self , int idx ,
895877 @ Cached NormalizeIndexCustomMessageNode normalizeIndexNode ) {
896878 int normIdx = normalizeIndexNode .execute (idx , self .getSize (), ErrorMessages .DEQUE_INDEX_OUT_OF_RANGE );
897- DequeSetItemNode . doSetItem ( self , normIdx , PNone . NO_VALUE );
879+ self . setItem ( normIdx , null );
898880 return PNone .NONE ;
899881 }
900882 }
@@ -966,12 +948,17 @@ static Object repr(PDeque self) {
966948 abstract static class DequeReduceNode extends PythonUnaryBuiltinNode {
967949
968950 @ Specialization (limit = "1" )
969- Object doGeneric (PDeque self ,
951+ Object doGeneric (VirtualFrame frame , PDeque self ,
970952 @ CachedLibrary ("self" ) PythonObjectLibrary lib ,
953+ @ Cached PyObjectLookupAttr lookupAttr ,
954+ @ Cached PyObjectSizeNode sizeNode ,
971955 @ Cached GetClassNode getClassNode ,
972956 @ Cached ConditionProfile profile ) {
973957 Object clazz = getPythonClass (getClassNode .execute (self ), profile );
974- Object dict = lib .hasDict (self ) ? lib .getDict (self ) : PNone .NONE ;
958+ Object dict = lookupAttr .execute (frame , self , __DICT__ );
959+ if (PGuards .isNoValue (dict ) || sizeNode .execute (frame , dict ) <= 0 ) {
960+ dict = PNone .NONE ;
961+ }
975962 Object it = lib .getIterator (self );
976963 PTuple emptyTuple = factory ().createEmptyTuple ();
977964 int maxLength = self .getMaxLength ();
0 commit comments