41
41
package com .oracle .graal .python .builtins .modules .cext ;
42
42
43
43
import static com .oracle .graal .python .builtins .PythonBuiltinClassType .NotImplementedError ;
44
+ import static com .oracle .graal .python .builtins .PythonBuiltinClassType .TypeError ;
44
45
import static com .oracle .graal .python .builtins .modules .cext .PythonCextBuiltins .CApiCallPath .Direct ;
45
46
import static com .oracle .graal .python .builtins .modules .cext .PythonCextBuiltins .CApiCallPath .Ignored ;
46
47
import static com .oracle .graal .python .builtins .objects .cext .capi .transitions .ArgDescriptor .ConstCharPtrAsTruffleString ;
66
67
67
68
import com .oracle .graal .python .PythonLanguage ;
68
69
import com .oracle .graal .python .builtins .PythonBuiltinClassType ;
69
- import com .oracle .graal .python .builtins .modules .BuiltinConstructors .BytesNode ;
70
70
import com .oracle .graal .python .builtins .modules .BuiltinFunctions .FormatNode ;
71
71
import com .oracle .graal .python .builtins .modules .BuiltinFunctions .IsInstanceNode ;
72
72
import com .oracle .graal .python .builtins .modules .BuiltinFunctions .IsSubClassNode ;
79
79
import com .oracle .graal .python .builtins .modules .cext .PythonCextBuiltins .CApiUnaryBuiltinNode ;
80
80
import com .oracle .graal .python .builtins .modules .cext .PythonCextBuiltins .CastArgsNode ;
81
81
import com .oracle .graal .python .builtins .modules .cext .PythonCextBuiltins .CastKwargsNode ;
82
- import com .oracle .graal .python .builtins .modules .cext .PythonCextBytesBuiltins .PyBytes_FromObject ;
83
82
import com .oracle .graal .python .builtins .objects .PNone ;
84
83
import com .oracle .graal .python .builtins .objects .PNotImplemented ;
84
+ import com .oracle .graal .python .builtins .objects .bytes .BytesNodes ;
85
85
import com .oracle .graal .python .builtins .objects .bytes .BytesUtils ;
86
- import com .oracle .graal .python .builtins .objects .bytes .PBytesLike ;
86
+ import com .oracle .graal .python .builtins .objects .bytes .PBytes ;
87
87
import com .oracle .graal .python .builtins .objects .cext .capi .CApiGuards ;
88
88
import com .oracle .graal .python .builtins .objects .cext .capi .CExtNodes .ResolvePointerNode ;
89
89
import com .oracle .graal .python .builtins .objects .cext .capi .PythonNativeWrapper ;
104
104
import com .oracle .graal .python .builtins .objects .object .ObjectBuiltins .GetAttributeNode ;
105
105
import com .oracle .graal .python .builtins .objects .object .ObjectBuiltins .SetattrNode ;
106
106
import com .oracle .graal .python .builtins .objects .tuple .PTuple ;
107
+ import com .oracle .graal .python .builtins .objects .type .SpecialMethodSlot ;
107
108
import com .oracle .graal .python .builtins .objects .type .TypeNodes ;
109
+ import com .oracle .graal .python .lib .PyBytesCheckNode ;
108
110
import com .oracle .graal .python .lib .PyCallableCheckNode ;
109
111
import com .oracle .graal .python .lib .PyLongCheckNode ;
110
112
import com .oracle .graal .python .lib .PyObjectAsFileDescriptor ;
116
118
import com .oracle .graal .python .lib .PyObjectGetIter ;
117
119
import com .oracle .graal .python .lib .PyObjectHashNode ;
118
120
import com .oracle .graal .python .lib .PyObjectIsTrueNode ;
119
- import com .oracle .graal .python .lib .PyObjectLookupAttr ;
120
121
import com .oracle .graal .python .lib .PyObjectLookupAttrO ;
121
122
import com .oracle .graal .python .lib .PyObjectReprAsObjectNode ;
122
123
import com .oracle .graal .python .lib .PyObjectSetItem ;
127
128
import com .oracle .graal .python .nodes .StringLiterals ;
128
129
import com .oracle .graal .python .nodes .argument .keywords .ExpandKeywordStarargsNode ;
129
130
import com .oracle .graal .python .nodes .call .CallNode ;
130
- import com .oracle .graal .python .nodes .classes .IsSubtypeNode ;
131
+ import com .oracle .graal .python .nodes .call .special .CallUnaryMethodNode ;
132
+ import com .oracle .graal .python .nodes .call .special .LookupSpecialMethodSlotNode ;
131
133
import com .oracle .graal .python .nodes .object .GetClassNode ;
132
134
import com .oracle .graal .python .nodes .object .GetOrCreateDictNode ;
133
135
import com .oracle .graal .python .nodes .util .CannotCastException ;
147
149
import com .oracle .truffle .api .CompilerDirectives .TruffleBoundary ;
148
150
import com .oracle .truffle .api .dsl .Bind ;
149
151
import com .oracle .truffle .api .dsl .Cached ;
150
- import com .oracle .truffle .api .dsl .Cached .Exclusive ;
152
+ import com .oracle .truffle .api .dsl .Fallback ;
153
+ import com .oracle .truffle .api .dsl .ImportStatic ;
151
154
import com .oracle .truffle .api .dsl .Specialization ;
152
155
import com .oracle .truffle .api .interop .InteropException ;
153
156
import com .oracle .truffle .api .interop .InteropLibrary ;
154
157
import com .oracle .truffle .api .library .CachedLibrary ;
155
158
import com .oracle .truffle .api .nodes .Node ;
156
159
import com .oracle .truffle .api .profiles .InlinedBranchProfile ;
160
+ import com .oracle .truffle .api .profiles .InlinedConditionProfile ;
157
161
import com .oracle .truffle .api .strings .TruffleString ;
158
162
159
163
public abstract class PythonCextObjectBuiltins {
@@ -503,40 +507,13 @@ static int isTrue(Object obj,
503
507
}
504
508
505
509
@ CApiBuiltin (ret = PyObjectTransfer , args = {PyObject }, call = Direct )
510
+ @ ImportStatic (SpecialMethodSlot .class )
506
511
abstract static class PyObject_Bytes extends CApiUnaryBuiltinNode {
507
- @ Specialization
508
- static Object bytes (PBytesLike bytes ) {
509
- return bytes ;
510
- }
511
-
512
- @ Specialization (guards = {"!isBytes(bytes)" , "isBytesSubtype(inliningTarget, bytes, getClassNode, isSubtypeNode)" }, limit = "1" )
513
- static Object bytes (Object bytes ,
514
- @ SuppressWarnings ("unused" ) @ Bind ("this" ) Node inliningTarget ,
515
- @ SuppressWarnings ("unused" ) @ Exclusive @ Cached GetClassNode getClassNode ,
516
- @ SuppressWarnings ("unused" ) @ Exclusive @ Cached IsSubtypeNode isSubtypeNode ) {
512
+ @ Specialization (guards = "isBuiltinBytes(bytes)" )
513
+ static Object bytes (PBytes bytes ) {
517
514
return bytes ;
518
515
}
519
516
520
- @ Specialization (guards = {"!isBytes(obj)" , "!isBytesSubtype(this, obj, getClassNode, isSubtypeNode)" , "!isNoValue(obj)" , "hasBytes(inliningTarget, obj, lookupAttrNode)" }, limit = "1" )
521
- static Object bytes (Object obj ,
522
- @ SuppressWarnings ("unused" ) @ Bind ("this" ) Node inliningTarget ,
523
- @ SuppressWarnings ("unused" ) @ Exclusive @ Cached GetClassNode getClassNode ,
524
- @ SuppressWarnings ("unused" ) @ Exclusive @ Cached IsSubtypeNode isSubtypeNode ,
525
- @ SuppressWarnings ("unused" ) @ Exclusive @ Cached PyObjectLookupAttr lookupAttrNode ,
526
- @ Cached BytesNode bytesNode ) {
527
- return bytesNode .execute (null , PythonBuiltinClassType .PBytes , obj , PNone .NO_VALUE , PNone .NO_VALUE );
528
- }
529
-
530
- @ Specialization (guards = {"!isBytes(obj)" , "!isBytesSubtype(this, obj, getClassNode, isSubtypeNode)" , "!isNoValue(obj)" , "!hasBytes(inliningTarget, obj, lookupAttrNode)" }, limit = "1" )
531
- static Object bytes (Object obj ,
532
- @ SuppressWarnings ("unused" ) @ Bind ("this" ) Node inliningTarget ,
533
- @ SuppressWarnings ("unused" ) @ Exclusive @ Cached GetClassNode getClassNode ,
534
- @ SuppressWarnings ("unused" ) @ Exclusive @ Cached IsSubtypeNode isSubtypeNode ,
535
- @ SuppressWarnings ("unused" ) @ Exclusive @ Cached PyObjectLookupAttr lookupAttrNode ,
536
- @ Cached PyBytes_FromObject fromObjectNode ) {
537
- return fromObjectNode .execute (obj );
538
- }
539
-
540
517
@ Specialization (guards = "isNoValue(obj)" )
541
518
static Object bytesNoValue (@ SuppressWarnings ("unused" ) Object obj ,
542
519
@ Bind PythonLanguage language ) {
@@ -547,12 +524,27 @@ static Object bytesNoValue(@SuppressWarnings("unused") Object obj,
547
524
return PFactory .createBytes (language , BytesUtils .NULL_STRING );
548
525
}
549
526
550
- protected static boolean hasBytes (Node inliningTarget , Object obj , PyObjectLookupAttr lookupAttrNode ) {
551
- return lookupAttrNode .execute (null , inliningTarget , obj , T___BYTES__ ) != PNone .NO_VALUE ;
552
- }
553
-
554
- protected static boolean isBytesSubtype (Node inliningTarget , Object obj , GetClassNode getClassNode , IsSubtypeNode isSubtypeNode ) {
555
- return isSubtypeNode .execute (getClassNode .execute (inliningTarget , obj ), PythonBuiltinClassType .PBytes );
527
+ @ Fallback
528
+ static Object doGeneric (Object obj ,
529
+ @ Bind ("this" ) Node inliningTarget ,
530
+ @ Cached GetClassNode getClassNode ,
531
+ @ Cached InlinedConditionProfile hasBytes ,
532
+ @ Cached ("create(Bytes)" ) LookupSpecialMethodSlotNode lookupBytes ,
533
+ @ Cached CallUnaryMethodNode callBytes ,
534
+ @ Cached PyBytesCheckNode check ,
535
+ @ Cached BytesNodes .BytesFromObject fromObject ,
536
+ @ Cached PRaiseNode raiseNode ) {
537
+ Object bytesMethod = lookupBytes .execute (null , getClassNode .execute (inliningTarget , obj ), obj );
538
+ if (hasBytes .profile (inliningTarget , bytesMethod != PNone .NO_VALUE )) {
539
+ Object bytes = callBytes .executeObject (null , bytesMethod , obj );
540
+ if (check .execute (inliningTarget , bytes )) {
541
+ return bytes ;
542
+ } else {
543
+ throw raiseNode .raise (inliningTarget , TypeError , ErrorMessages .RETURNED_NONBYTES , T___BYTES__ , bytes );
544
+ }
545
+ }
546
+ byte [] bytes = fromObject .execute (null , obj );
547
+ return PFactory .createBytes (PythonLanguage .get (inliningTarget ), bytes );
556
548
}
557
549
}
558
550
0 commit comments