92
92
import com .oracle .truffle .api .frame .VirtualFrame ;
93
93
import com .oracle .truffle .api .library .CachedLibrary ;
94
94
import com .oracle .truffle .api .nodes .Node ;
95
- import com .oracle .truffle .api .profiles .ValueProfile ;
95
+ import com .oracle .truffle .api .profiles .ConditionProfile ;
96
96
97
97
@ CoreFunctions (defineModule = "_codecs" )
98
98
public class CodecsModuleBuiltins extends PythonBuiltins {
@@ -116,12 +116,12 @@ public abstract static class RaiseEncodingErrorNode extends Node {
116
116
public abstract RuntimeException execute (TruffleEncoder encoder , Object inputObject );
117
117
118
118
@ Specialization
119
- public RuntimeException doRaise (TruffleEncoder encoder , Object inputObject ,
119
+ static RuntimeException doRaise (TruffleEncoder encoder , Object inputObject ,
120
120
@ Cached CallNode callNode ,
121
121
@ Cached PRaiseNode raiseNode ,
122
122
@ CachedLanguage PythonLanguage pythonLanguage ) {
123
123
int start = encoder .getInputPosition ();
124
- int end = start + encoder .getErrorLenght ();
124
+ int end = start + encoder .getErrorLength ();
125
125
Object exception = callNode .execute (UnicodeEncodeError , encoder .getEncodingName (), inputObject , start , end , encoder .getErrorReason ());
126
126
if (exception instanceof PBaseException ) {
127
127
throw raiseNode .raiseExceptionObject ((PBaseException ) exception , pythonLanguage );
@@ -139,38 +139,36 @@ public abstract static class HandleEncodingErrorNode extends Node {
139
139
public abstract void execute (TruffleEncoder encoder , String errorAction , Object inputObject );
140
140
141
141
@ Specialization
142
- void handle (TruffleEncoder encoder , String errorAction , Object inputObject ,
143
- @ Cached ("createIdentityProfile()" ) ValueProfile actionProfile ,
142
+ static void handle (TruffleEncoder encoder , String errorAction , Object inputObject ,
143
+ @ Cached ConditionProfile strictProfile ,
144
+ @ Cached ConditionProfile backslashreplaceProfile ,
145
+ @ Cached ConditionProfile surrogatepassProfile ,
144
146
@ Cached RaiseEncodingErrorNode raiseEncodingErrorNode ,
145
147
@ Cached PRaiseNode raiseNode ) {
148
+ boolean fixed ;
146
149
try {
147
150
// Ignore and replace are handled by Java Charset
148
- switch (actionProfile .profile (errorAction )) {
149
- case STRICT :
150
- break ;
151
- case BACKSLASHREPLACE :
152
- if (backslashreplace (encoder )) {
153
- return ;
154
- }
155
- break ;
156
- case SURROGATEPASS :
157
- if (surrogatepass (encoder )) {
158
- return ;
159
- }
160
- break ;
161
- default :
162
- raiseNode .raise (LookupError , ErrorMessages .UNKNOWN_ERROR_HANDLER , errorAction );
151
+ if (strictProfile .profile (STRICT .equals (errorAction ))) {
152
+ fixed = false ;
153
+ } else if (backslashreplaceProfile .profile (BACKSLASHREPLACE .equals (errorAction ))) {
154
+ fixed = backslashreplace (encoder );
155
+ } else if (surrogatepassProfile .profile (SURROGATEPASS .equals (errorAction ))) {
156
+ fixed = surrogatepass (encoder );
157
+ } else {
158
+ throw raiseNode .raise (LookupError , ErrorMessages .UNKNOWN_ERROR_HANDLER , errorAction );
163
159
}
164
160
} catch (OutOfMemoryError e ) {
165
161
CompilerDirectives .transferToInterpreterAndInvalidate ();
166
162
throw raiseNode .raise (MemoryError );
167
163
}
168
- throw raiseEncodingErrorNode .execute (encoder , inputObject );
164
+ if (!fixed ) {
165
+ throw raiseEncodingErrorNode .execute (encoder , inputObject );
166
+ }
169
167
}
170
168
171
169
@ TruffleBoundary
172
170
private static boolean backslashreplace (TruffleEncoder encoder ) {
173
- String p = new String (encoder .getInputChars (encoder .getErrorLenght ()));
171
+ String p = new String (encoder .getInputChars (encoder .getErrorLength ()));
174
172
StringBuilder sb = new StringBuilder ();
175
173
byte [] buf = new byte [10 ];
176
174
for (int i = 0 ; i < p .length ();) {
@@ -195,7 +193,7 @@ private static boolean backslashreplace(TruffleEncoder encoder) {
195
193
private static boolean surrogatepass (TruffleEncoder encoder ) {
196
194
// UTF-8 only for now. The name should be normalized already
197
195
if (encoder .getEncodingName ().equals ("utf_8" )) {
198
- String p = new String (encoder .getInputChars (encoder .getErrorLenght ()));
196
+ String p = new String (encoder .getInputChars (encoder .getErrorLength ()));
199
197
byte [] replacement = new byte [p .length () * 3 ];
200
198
int outp = 0 ;
201
199
for (int i = 0 ; i < p .length ();) {
@@ -209,7 +207,7 @@ private static boolean surrogatepass(TruffleEncoder encoder) {
209
207
replacement [outp ++] = (byte ) (0x80 | (ch & 0x3f ));
210
208
i += Character .charCount (ch );
211
209
}
212
- encoder .replace (encoder .getErrorLenght (), replacement , 0 , outp );
210
+ encoder .replace (encoder .getErrorLength (), replacement , 0 , outp );
213
211
return true ;
214
212
}
215
213
return false ;
@@ -225,12 +223,12 @@ public abstract static class RaiseDecodingErrorNode extends Node {
225
223
public abstract RuntimeException execute (TruffleDecoder decoder , Object inputObject );
226
224
227
225
@ Specialization
228
- public RuntimeException doRaise (TruffleDecoder decoder , Object inputObject ,
226
+ static RuntimeException doRaise (TruffleDecoder decoder , Object inputObject ,
229
227
@ Cached CallNode callNode ,
230
228
@ Cached PRaiseNode raiseNode ,
231
229
@ CachedLanguage PythonLanguage pythonLanguage ) {
232
230
int start = decoder .getInputPosition ();
233
- int end = start + decoder .getErrorLenght ();
231
+ int end = start + decoder .getErrorLength ();
234
232
Object exception = callNode .execute (UnicodeDecodeError , decoder .getEncodingName (), inputObject , start , end , decoder .getErrorReason ());
235
233
if (exception instanceof PBaseException ) {
236
234
throw raiseNode .raiseExceptionObject ((PBaseException ) exception , pythonLanguage );
@@ -248,33 +246,36 @@ public abstract static class HandleDecodingErrorNode extends Node {
248
246
public abstract void execute (TruffleDecoder decoder , String errorAction , Object inputObject );
249
247
250
248
@ Specialization
251
- void doStrict (TruffleDecoder decoder , String errorAction , Object inputObject ,
252
- @ Cached ("createIdentityProfile()" ) ValueProfile actionProfile ,
249
+ static void doStrict (TruffleDecoder decoder , String errorAction , Object inputObject ,
250
+ @ Cached ConditionProfile strictProfile ,
251
+ @ Cached ConditionProfile backslashreplaceProfile ,
252
+ @ Cached ConditionProfile surrogatepassProfile ,
253
253
@ Cached RaiseDecodingErrorNode raiseDecodingErrorNode ,
254
254
@ Cached PRaiseNode raiseNode ) {
255
- // Ignore and replace are handled by Java Charset
256
- switch (actionProfile .profile (errorAction )) {
257
- case STRICT :
258
- break ;
259
- case BACKSLASHREPLACE :
260
- if (backslashreplace (decoder )) {
261
- return ;
262
- }
263
- break ;
264
- case SURROGATEPASS :
265
- if (surrogatepass (decoder )) {
266
- return ;
267
- }
268
- break ;
269
- default :
270
- raiseNode .raise (LookupError , ErrorMessages .UNKNOWN_ERROR_HANDLER , errorAction );
255
+ boolean fixed ;
256
+ try {
257
+ // Ignore and replace are handled by Java Charset
258
+ if (strictProfile .profile (STRICT .equals (errorAction ))) {
259
+ fixed = false ;
260
+ } else if (backslashreplaceProfile .profile (BACKSLASHREPLACE .equals (errorAction ))) {
261
+ fixed = backslashreplace (decoder );
262
+ } else if (surrogatepassProfile .profile (SURROGATEPASS .equals (errorAction ))) {
263
+ fixed = surrogatepass (decoder );
264
+ } else {
265
+ throw raiseNode .raise (LookupError , ErrorMessages .UNKNOWN_ERROR_HANDLER , errorAction );
266
+ }
267
+ } catch (OutOfMemoryError e ) {
268
+ CompilerDirectives .transferToInterpreterAndInvalidate ();
269
+ throw raiseNode .raise (MemoryError );
270
+ }
271
+ if (!fixed ) {
272
+ throw raiseDecodingErrorNode .execute (decoder , inputObject );
271
273
}
272
- throw raiseDecodingErrorNode .execute (decoder , inputObject );
273
274
}
274
275
275
276
@ TruffleBoundary
276
277
private static boolean backslashreplace (TruffleDecoder decoder ) {
277
- byte [] p = decoder .getInputBytes (decoder .getErrorLenght ());
278
+ byte [] p = decoder .getInputBytes (decoder .getErrorLength ());
278
279
char [] replacement = new char [p .length * 4 ];
279
280
int outp = 0 ;
280
281
byte [] buf = new byte [4 ];
@@ -515,7 +516,7 @@ private void handleDecodingError(TruffleDecoder encoder, String errorAction, Obj
515
516
@ GenerateNodeFactory
516
517
abstract static class CodecsLookupNode extends PythonBuiltinNode {
517
518
@ Specialization
518
- Object lookup (String encoding ) {
519
+ static Object lookup (String encoding ) {
519
520
if (CharsetMapping .getCharset (encoding ) != null ) {
520
521
return true ;
521
522
} else {
@@ -623,7 +624,7 @@ public int getInputPosition() {
623
624
}
624
625
625
626
@ TruffleBoundary
626
- public int getErrorLenght () {
627
+ public int getErrorLength () {
627
628
return coderResult .length ();
628
629
}
629
630
@@ -757,7 +758,7 @@ public int getInputPosition() {
757
758
}
758
759
759
760
@ TruffleBoundary
760
- public int getErrorLenght () {
761
+ public int getErrorLength () {
761
762
return coderResult .length ();
762
763
}
763
764
0 commit comments