40
40
*/
41
41
package com .oracle .graal .python .builtins .modules ;
42
42
43
+ import static com .oracle .graal .python .builtins .PythonBuiltinClassType .NotImplementedError ;
43
44
import static com .oracle .graal .python .builtins .objects .bytes .BytesUtils .HEXDIGITS ;
44
45
import static com .oracle .graal .python .builtins .objects .bytes .BytesUtils .digitValue ;
45
46
import static com .oracle .graal .python .nodes .BuiltinNames ._CODECS ;
47
+ import static com .oracle .graal .python .nodes .ErrorMessages .ARG_MUST_BE_CALLABLE ;
46
48
import static com .oracle .graal .python .nodes .ErrorMessages .BYTESLIKE_OBJ_REQUIRED ;
49
+ import static com .oracle .graal .python .nodes .ErrorMessages .CODEC_SEARCH_MUST_RETURN_4 ;
47
50
import static com .oracle .graal .python .nodes .ErrorMessages .ENCODING_ERROR_WITH_CODE ;
48
51
import static com .oracle .graal .python .nodes .ErrorMessages .HANDLER_MUST_BE_CALLABLE ;
49
52
import static com .oracle .graal .python .nodes .ErrorMessages .INVALID_ESCAPE_AT ;
53
+ import static com .oracle .graal .python .nodes .ErrorMessages .S_MUST_RETURN_TUPLE ;
54
+ import static com .oracle .graal .python .nodes .ErrorMessages .UNKNOWN_ENCODING ;
55
+ import static com .oracle .graal .python .nodes .ErrorMessages .UNKNOWN_ERROR_HANDLER ;
50
56
import static com .oracle .graal .python .runtime .exception .PythonErrorType .LookupError ;
51
57
import static com .oracle .graal .python .runtime .exception .PythonErrorType .MemoryError ;
52
58
import static com .oracle .graal .python .runtime .exception .PythonErrorType .TypeError ;
66
72
import com .oracle .graal .python .annotations .ArgumentClinic ;
67
73
import com .oracle .graal .python .builtins .Builtin ;
68
74
import com .oracle .graal .python .builtins .CoreFunctions ;
69
- import static com .oracle .graal .python .builtins .PythonBuiltinClassType .NotImplementedError ;
70
75
import com .oracle .graal .python .builtins .PythonBuiltins ;
71
76
import com .oracle .graal .python .builtins .objects .PNone ;
72
77
import com .oracle .graal .python .builtins .objects .buffer .PythonBufferAccessLibrary ;
86
91
import com .oracle .graal .python .lib .PyCallableCheckNode ;
87
92
import com .oracle .graal .python .lib .PyObjectCallMethodObjArgs ;
88
93
import com .oracle .graal .python .nodes .ErrorMessages ;
89
- import static com .oracle .graal .python .nodes .ErrorMessages .ARG_MUST_BE_CALLABLE ;
90
- import static com .oracle .graal .python .nodes .ErrorMessages .CODEC_SEARCH_MUST_RETURN_4 ;
91
- import static com .oracle .graal .python .nodes .ErrorMessages .S_MUST_RETURN_TUPLE ;
92
- import static com .oracle .graal .python .nodes .ErrorMessages .UNKNOWN_ENCODING ;
93
- import static com .oracle .graal .python .nodes .ErrorMessages .UNKNOWN_ERROR_HANDLER ;
94
+ import com .oracle .graal .python .nodes .PNodeWithRaise ;
94
95
import com .oracle .graal .python .nodes .PRaiseNode ;
95
96
import com .oracle .graal .python .nodes .call .CallNode ;
96
97
import com .oracle .graal .python .nodes .call .special .CallBinaryMethodNode ;
@@ -477,21 +478,11 @@ public static HandleDecodingErrorNode create() {
477
478
}
478
479
}
479
480
480
- // _codecs.encode(obj, encoding='utf-8', errors='strict')
481
- @ Builtin (name = "__truffle_encode__" , minNumOfPositionalArgs = 1 , parameterNames = {"obj" , "encoding" , "errors" })
482
- @ ArgumentClinic (name = "encoding" , conversion = ArgumentClinic .ClinicConversion .String , defaultValue = "\" utf-8\" " , useDefaultForNone = true )
483
- @ ArgumentClinic (name = "errors" , conversion = ArgumentClinic .ClinicConversion .String , defaultValue = "\" strict\" " , useDefaultForNone = true )
484
- @ GenerateNodeFactory
485
- public abstract static class CodecsEncodeNode extends PythonTernaryClinicBuiltinNode {
486
- public abstract Object execute (Object str , Object encoding , Object errors );
487
-
488
- @ Override
489
- protected ArgumentClinicProvider getArgumentClinic () {
490
- return CodecsModuleBuiltinsClinicProviders .CodecsEncodeNodeClinicProviderGen .INSTANCE ;
491
- }
481
+ public abstract static class CodecsEncodeToJavaBytesNode extends PNodeWithRaise {
482
+ public abstract byte [] execute (Object self , String encoding , String errors );
492
483
493
- @ Specialization ( guards = { "isString(self)" })
494
- Object encode (Object self , String encoding , String errors ,
484
+ @ Specialization
485
+ byte [] encode (Object self , String encoding , String errors ,
495
486
@ Cached CastToJavaStringNode castStr ,
496
487
@ Cached HandleEncodingErrorNode errorHandler ) {
497
488
String input = castStr .execute (self );
@@ -510,7 +501,29 @@ Object encode(Object self, String encoding, String errors,
510
501
CompilerDirectives .transferToInterpreterAndInvalidate ();
511
502
throw raise (MemoryError );
512
503
}
513
- PBytes bytes = factory ().createBytes (encoder .getBytes ());
504
+ return encoder .getBytes ();
505
+ }
506
+ }
507
+
508
+ // _codecs.encode(obj, encoding='utf-8', errors='strict')
509
+ @ Builtin (name = "__truffle_encode__" , minNumOfPositionalArgs = 1 , parameterNames = {"obj" , "encoding" , "errors" })
510
+ @ ArgumentClinic (name = "encoding" , conversion = ArgumentClinic .ClinicConversion .String , defaultValue = "\" utf-8\" " , useDefaultForNone = true )
511
+ @ ArgumentClinic (name = "errors" , conversion = ArgumentClinic .ClinicConversion .String , defaultValue = "\" strict\" " , useDefaultForNone = true )
512
+ @ GenerateNodeFactory
513
+ public abstract static class CodecsEncodeNode extends PythonTernaryClinicBuiltinNode {
514
+ public abstract Object execute (Object str , Object encoding , Object errors );
515
+
516
+ @ Override
517
+ protected ArgumentClinicProvider getArgumentClinic () {
518
+ return CodecsModuleBuiltinsClinicProviders .CodecsEncodeNodeClinicProviderGen .INSTANCE ;
519
+ }
520
+
521
+ @ Specialization (guards = {"isString(self)" })
522
+ Object encode (Object self , String encoding , String errors ,
523
+ @ Cached CastToJavaStringNode castStr ,
524
+ @ Cached CodecsEncodeToJavaBytesNode encode ) {
525
+ String input = castStr .execute (self );
526
+ PBytes bytes = factory ().createBytes (encode .execute (self , encoding , errors ));
514
527
return factory ().createTuple (new Object []{bytes , input .length ()});
515
528
}
516
529
0 commit comments