72
72
import com .oracle .graal .python .annotations .ArgumentClinic ;
73
73
import com .oracle .graal .python .builtins .Builtin ;
74
74
import com .oracle .graal .python .builtins .CoreFunctions ;
75
+ import com .oracle .graal .python .builtins .PythonBuiltinClassType ;
75
76
import com .oracle .graal .python .builtins .PythonBuiltins ;
76
77
import com .oracle .graal .python .builtins .objects .PNone ;
77
78
import com .oracle .graal .python .builtins .objects .buffer .PythonBufferAccessLibrary ;
87
88
import com .oracle .graal .python .builtins .objects .exception .PBaseException ;
88
89
import com .oracle .graal .python .builtins .objects .module .PythonModule ;
89
90
import com .oracle .graal .python .builtins .objects .tuple .PTuple ;
90
- import com .oracle .graal .python .builtins .objects .tuple .TupleBuiltins ;
91
91
import com .oracle .graal .python .lib .PyCallableCheckNode ;
92
+ import com .oracle .graal .python .lib .PyObjectSizeNode ;
93
+ import com .oracle .graal .python .lib .PyObjectTypeCheck ;
92
94
import static com .oracle .graal .python .nodes .BuiltinNames .ENCODE ;
93
95
import com .oracle .graal .python .nodes .ErrorMessages ;
96
+ import com .oracle .graal .python .nodes .PNodeWithContext ;
94
97
import com .oracle .graal .python .nodes .PNodeWithRaise ;
95
98
import com .oracle .graal .python .nodes .PRaiseNode ;
96
99
import static com .oracle .graal .python .nodes .SpecialMethodNames .DECODE ;
111
114
import com .oracle .graal .python .nodes .util .CastToJavaStringNode ;
112
115
import com .oracle .graal .python .runtime .PythonContext ;
113
116
import com .oracle .graal .python .runtime .exception .PException ;
117
+ import com .oracle .graal .python .runtime .object .PythonObjectFactory ;
114
118
import com .oracle .graal .python .util .CharsetMapping ;
115
119
import com .oracle .truffle .api .CompilerDirectives ;
116
120
import com .oracle .truffle .api .CompilerDirectives .TruffleBoundary ;
120
124
import com .oracle .truffle .api .dsl .GenerateUncached ;
121
125
import com .oracle .truffle .api .dsl .NodeFactory ;
122
126
import com .oracle .truffle .api .dsl .Specialization ;
127
+ import com .oracle .truffle .api .frame .Frame ;
123
128
import com .oracle .truffle .api .frame .VirtualFrame ;
124
129
import com .oracle .truffle .api .library .CachedLibrary ;
125
130
import com .oracle .truffle .api .nodes .Node ;
@@ -551,15 +556,32 @@ protected ArgumentClinicProvider getArgumentClinic() {
551
556
return CodecsModuleBuiltinsClinicProviders .CodecsDecodeNodeClinicProviderGen .INSTANCE ;
552
557
}
553
558
559
+ @ Specialization
560
+ Object decode (PBytesLike input , Object encoding , Object errors , Object finalData ,
561
+ @ Cached InternalCodecsDecodeNode internalNode ) {
562
+ return internalNode .execute (input , encoding , errors , finalData );
563
+ }
564
+ }
565
+
566
+ @ GenerateUncached
567
+ public abstract static class InternalCodecsDecodeNode extends PNodeWithContext {
568
+ abstract Object execute (Object input , Object encoding , Object errors , Object finalData );
569
+
570
+ public final Object call (Object input , Object encoding , Object errors , Object finalData ) {
571
+ return execute (input , encoding , errors , finalData );
572
+ }
573
+
554
574
@ Specialization
555
575
Object decode (PBytesLike input , String encoding , String errors , boolean finalData ,
556
576
@ Cached GetInternalByteArrayNode getBytes ,
557
- @ Cached HandleDecodingErrorNode errorHandler ) {
577
+ @ Cached HandleDecodingErrorNode errorHandler ,
578
+ @ Cached PRaiseNode raiseNode ,
579
+ @ Cached PythonObjectFactory factory ) {
558
580
byte [] bytes = getBytes .execute (input .getSequenceStorage ());
559
581
CodingErrorAction errorAction = convertCodingErrorAction (errors );
560
582
Charset charset = CharsetMapping .getCharset (encoding );
561
583
if (charset == null ) {
562
- throw raise (LookupError , ErrorMessages .UNKNOWN_ENCODING , encoding );
584
+ throw raiseNode . raise (LookupError , ErrorMessages .UNKNOWN_ENCODING , encoding );
563
585
}
564
586
TruffleDecoder decoder ;
565
587
try {
@@ -569,14 +591,15 @@ Object decode(PBytesLike input, String encoding, String errors, boolean finalDat
569
591
}
570
592
} catch (OutOfMemoryError e ) {
571
593
CompilerDirectives .transferToInterpreterAndInvalidate ();
572
- throw raise (MemoryError );
594
+ throw raiseNode . raise (MemoryError );
573
595
}
574
- return factory () .createTuple (new Object []{decoder .getString (), decoder .getInputPosition ()});
596
+ return factory .createTuple (new Object []{decoder .getString (), decoder .getInputPosition ()});
575
597
}
576
598
577
599
@ Fallback
578
- Object decode (Object bytes , @ SuppressWarnings ("unused" ) Object encoding , @ SuppressWarnings ("unused" ) Object errors , @ SuppressWarnings ("unused" ) Object finalData ) {
579
- throw raise (TypeError , BYTESLIKE_OBJ_REQUIRED , bytes );
600
+ Object decode (Object bytes , @ SuppressWarnings ("unused" ) Object encoding , @ SuppressWarnings ("unused" ) Object errors , @ SuppressWarnings ("unused" ) Object finalData ,
601
+ @ Cached PRaiseNode raiseNode ) {
602
+ throw raiseNode .raise (TypeError , BYTESLIKE_OBJ_REQUIRED , bytes );
580
603
}
581
604
}
582
605
@@ -822,25 +845,40 @@ private static boolean hasTruffleEncoding(String encoding) {
822
845
@ Builtin (name = "lookup" , minNumOfPositionalArgs = 1 )
823
846
@ GenerateNodeFactory
824
847
abstract static class LookupNode extends PythonUnaryBuiltinNode {
848
+ @ Specialization
849
+ PTuple lookup (VirtualFrame frame , Object encoding ,
850
+ @ Cached InternalLookupNode internalNode ) {
851
+ return internalNode .execute (frame , encoding );
852
+ }
853
+ }
854
+
855
+ @ GenerateUncached
856
+ abstract static class InternalLookupNode extends PNodeWithContext {
857
+ abstract PTuple execute (Frame frame , Object encoding );
858
+
825
859
@ Specialization
826
860
PTuple lookup (VirtualFrame frame , PBytesLike encoding ,
827
- @ Cached AsciiDecodeNode asciiDecodeNode ,
861
+ @ Cached InternalCodecsDecodeNode decodeNode ,
862
+ @ Cached PyObjectTypeCheck typeCheck ,
828
863
@ Cached CallUnaryMethodNode callNode ,
829
- @ Cached TupleBuiltins . LenNode lenNode ,
864
+ @ Cached PyObjectSizeNode sizeNode ,
830
865
@ Cached ConditionProfile hasSearchPathProfile ,
831
866
@ Cached ConditionProfile hasTruffleEncodingProfile ,
832
- @ Cached ConditionProfile isTupleProfile ) {
833
- String decoded = (String ) ((PTuple ) asciiDecodeNode .execute (frame , encoding , PNone .NO_VALUE )).getSequenceStorage ().getInternalArray ()[0 ];
834
- return lookup (frame , decoded , callNode , lenNode , hasSearchPathProfile , hasTruffleEncodingProfile , isTupleProfile );
867
+ @ Cached ConditionProfile isTupleProfile ,
868
+ @ Cached PRaiseNode raiseNode ) {
869
+ String decoded = (String ) ((PTuple ) decodeNode .execute (encoding , "ascii" , PNone .NO_VALUE , true )).getSequenceStorage ().getInternalArray ()[0 ];
870
+ return lookup (frame , decoded , callNode , typeCheck , sizeNode , hasSearchPathProfile , hasTruffleEncodingProfile , isTupleProfile , raiseNode );
835
871
}
836
872
837
873
@ Specialization
838
874
PTuple lookup (VirtualFrame frame , String encoding ,
839
875
@ Cached CallUnaryMethodNode callNode ,
840
- @ Cached TupleBuiltins .LenNode lenNode ,
876
+ @ Cached PyObjectTypeCheck typeCheck ,
877
+ @ Cached PyObjectSizeNode sizeNode ,
841
878
@ Cached ConditionProfile hasSearchPathProfile ,
842
879
@ Cached ConditionProfile hasTruffleEncodingProfile ,
843
- @ Cached ConditionProfile isTupleProfile ) {
880
+ @ Cached ConditionProfile isTupleProfile ,
881
+ @ Cached PRaiseNode raiseNode ) {
844
882
String normalized_encoding = normalizeString (encoding );
845
883
PythonContext context = getContext ();
846
884
PTuple result = getSearchPath (context , normalized_encoding );
@@ -849,13 +887,13 @@ PTuple lookup(VirtualFrame frame, String encoding,
849
887
}
850
888
if (hasTruffleEncodingProfile .profile (hasTruffleEncoding (encoding ))) {
851
889
PythonModule codecs = context .getCore ().lookupBuiltinModule ("_codecs_truffle" );
852
- result = CodecsTruffleModuleBuiltins .codecsInfo (codecs , encoding , context , factory ());
890
+ result = CodecsTruffleModuleBuiltins .codecsInfo (codecs , encoding , context , getContext (). getCore (). factory ());
853
891
} else {
854
892
for (Object func : getSearchPaths (context )) {
855
893
Object obj = callNode .executeObject (func , normalized_encoding );
856
894
if (obj != PNone .NONE ) {
857
- if (isTupleProfile .profile (!isTupleInstanceCheck (frame , obj , 4 , lenNode ))) {
858
- throw raise (TypeError , CODEC_SEARCH_MUST_RETURN_4 );
895
+ if (isTupleProfile .profile (!isTupleInstanceCheck (frame , obj , 4 , typeCheck , sizeNode ))) {
896
+ throw raiseNode . raise (TypeError , CODEC_SEARCH_MUST_RETURN_4 );
859
897
}
860
898
result = (PTuple ) obj ;
861
899
break ;
@@ -866,7 +904,7 @@ PTuple lookup(VirtualFrame frame, String encoding,
866
904
putSearchPath (context , normalized_encoding , result );
867
905
return result ;
868
906
}
869
- throw raise (LookupError , UNKNOWN_ENCODING , encoding );
907
+ throw raiseNode . raise (LookupError , UNKNOWN_ENCODING , encoding );
870
908
}
871
909
872
910
@ TruffleBoundary
@@ -886,8 +924,8 @@ private static Object[] getSearchPaths(PythonContext ctx) {
886
924
}
887
925
}
888
926
889
- private static boolean isTupleInstanceCheck (VirtualFrame frame , Object result , int len , TupleBuiltins . LenNode lenNode ) throws PException {
890
- return (result instanceof PTuple ) && (( int ) lenNode .execute (frame , result ) == len ) ;
927
+ private static boolean isTupleInstanceCheck (VirtualFrame frame , Object result , int len , PyObjectTypeCheck typeCheck , PyObjectSizeNode sizeNode ) throws PException {
928
+ return typeCheck . execute (result , PythonBuiltinClassType . PTuple ) && sizeNode .execute (frame , result ) == len ;
891
929
}
892
930
893
931
@ TruffleBoundary
@@ -1024,11 +1062,12 @@ Object encode(VirtualFrame frame, Object obj, String encoding, String errors,
1024
1062
@ Cached SequenceStorageNodes .GetItemNode getResultItemNode ,
1025
1063
@ Cached LookupNode lookupNode ,
1026
1064
@ Cached CallBinaryMethodNode callEncoderNode ,
1027
- @ Cached TupleBuiltins .LenNode lenNode ,
1065
+ @ Cached PyObjectSizeNode sizeNode ,
1066
+ @ Cached PyObjectTypeCheck typeCheck ,
1028
1067
@ Cached ConditionProfile isTupleProfile ) {
1029
1068
Object encoder = CodecsModuleBuiltins .encoder (frame , encoding , lookupNode , getItemNode );
1030
1069
Object result = callEncoderNode .executeObject (encoder , obj , errors );
1031
- if (isTupleProfile .profile (!isTupleInstanceCheck (frame , result , 2 , lenNode ))) {
1070
+ if (isTupleProfile .profile (!isTupleInstanceCheck (frame , result , 2 , typeCheck , sizeNode ))) {
1032
1071
throw raise (TypeError , S_MUST_RETURN_TUPLE , "encoder" );
1033
1072
}
1034
1073
return getResultItemNode .execute (frame , ((PTuple ) result ).getSequenceStorage (), 0 );
@@ -1052,11 +1091,12 @@ Object decode(VirtualFrame frame, Object obj, String encoding, String errors,
1052
1091
@ Cached SequenceStorageNodes .GetItemNode getResultItemNode ,
1053
1092
@ Cached LookupNode lookupNode ,
1054
1093
@ Cached CallBinaryMethodNode callEncoderNode ,
1055
- @ Cached TupleBuiltins .LenNode lenNode ,
1094
+ @ Cached PyObjectSizeNode sizeNode ,
1095
+ @ Cached PyObjectTypeCheck typeCheck ,
1056
1096
@ Cached ConditionProfile isTupleProfile ) {
1057
1097
Object decoder = CodecsModuleBuiltins .decoder (frame , encoding , lookupNode , getItemNode );
1058
1098
Object result = callEncoderNode .executeObject (decoder , obj , errors );
1059
- if (isTupleProfile .profile (!isTupleInstanceCheck (frame , result , 2 , lenNode ))) {
1099
+ if (isTupleProfile .profile (!isTupleInstanceCheck (frame , result , 2 , typeCheck , sizeNode ))) {
1060
1100
throw raise (TypeError , S_MUST_RETURN_TUPLE , "decoder" );
1061
1101
}
1062
1102
return getResultItemNode .execute (frame , ((PTuple ) result ).getSequenceStorage (), 0 );
0 commit comments