50
50
import com .oracle .graal .python .builtins .Python3Core ;
51
51
import com .oracle .graal .python .builtins .PythonBuiltinClassType ;
52
52
import com .oracle .graal .python .builtins .PythonBuiltins ;
53
+ import com .oracle .graal .python .builtins .modules .BuiltinConstructors .BytesNode ;
53
54
import com .oracle .graal .python .builtins .modules .BuiltinFunctions .IsInstanceNode ;
54
55
import com .oracle .graal .python .builtins .modules .BuiltinFunctions .IsSubClassNode ;
55
56
import com .oracle .graal .python .builtins .modules .cext .PythonCextBuiltins .CastArgsNode ;
75
76
import com .oracle .graal .python .lib .PyObjectReprAsObjectNode ;
76
77
import com .oracle .graal .python .lib .PyObjectSetItem ;
77
78
import com .oracle .graal .python .lib .PyObjectStrAsObjectNode ;
78
- import static com .oracle .graal .python .nodes .ErrorMessages .RETURNED_NONBYTES ;
79
- import com .oracle .graal .python .nodes .SpecialMethodNames ;
80
- import static com .oracle .graal .python .nodes .SpecialMethodNames .__SETATTR__ ;
81
79
import com .oracle .graal .python .nodes .attributes .GetAttributeNode .GetAnyAttributeNode ;
82
80
import com .oracle .graal .python .nodes .call .CallNode ;
83
- import com .oracle .graal .python .nodes .call .special .CallUnaryMethodNode ;
84
81
import com .oracle .graal .python .nodes .classes .IsSubtypeNode ;
85
82
import com .oracle .graal .python .nodes .expression .BinaryComparisonNode ;
86
83
import com .oracle .graal .python .nodes .function .PythonBuiltinBaseNode ;
94
91
import com .oracle .graal .python .util .OverflowException ;
95
92
import com .oracle .graal .python .util .PythonUtils ;
96
93
import com .oracle .truffle .api .CompilerDirectives ;
97
- import com .oracle .truffle .api .dsl .Bind ;
98
94
import com .oracle .truffle .api .dsl .Cached ;
99
95
import com .oracle .truffle .api .dsl .Cached .Shared ;
100
96
import com .oracle .truffle .api .dsl .GenerateNodeFactory ;
@@ -486,7 +482,7 @@ Object getAttr(VirtualFrame frame, Object obj, Object attr,
486
482
@ Cached GetAttributeNode getAttrNode ,
487
483
@ Cached TransformExceptionToNativeNode transformExceptionToNativeNode ) {
488
484
try {
489
- return getAttrNode .execute (frame , getCore (). lookupType ( PythonBuiltinClassType . PythonObject ), SpecialMethodNames . __GETATTRIBUTE__ );
485
+ return getAttrNode .execute (frame , obj , attr );
490
486
} catch (PException e ) {
491
487
transformExceptionToNativeNode .execute (frame , e );
492
488
return getContext ().getNativeNull ();
@@ -502,7 +498,7 @@ int setAttr(VirtualFrame frame, Object obj, Object attr, Object value,
502
498
@ Cached SetattrNode setAttrNode ,
503
499
@ Cached TransformExceptionToNativeNode transformExceptionToNativeNode ) {
504
500
try {
505
- setAttrNode .execute (frame , getCore (). lookupType ( PythonBuiltinClassType . PythonObject ), __SETATTR__ , value );
501
+ setAttrNode .execute (frame , obj , attr , value );
506
502
return 0 ;
507
503
} catch (PException e ) {
508
504
transformExceptionToNativeNode .execute (frame , e );
@@ -568,49 +564,35 @@ Object bytes(VirtualFrame frame, Object bytes,
568
564
return bytes ;
569
565
}
570
566
571
- @ Specialization (guards = {"!isBytes(obj)" , "!isBytesSubtype(frame, obj, getClassNode, isSubtypeNode)" , "!isPNone(bytesCallable )" })
567
+ @ Specialization (guards = {"!isBytes(obj)" , "!isBytesSubtype(frame, obj, getClassNode, isSubtypeNode)" , "hasBytes(frame, obj, lookupAttrNode )" }, limit = "1" )
572
568
Object bytes (VirtualFrame frame , Object obj ,
573
- @ SuppressWarnings ("unused" ) @ Cached GetClassNode getClassNode ,
574
- @ SuppressWarnings ("unused" ) @ Cached IsSubtypeNode isSubtypeNode ,
569
+ @ Shared ( "getClass" ) @ SuppressWarnings ("unused" ) @ Cached GetClassNode getClassNode ,
570
+ @ Shared ( "isSubtype" ) @ SuppressWarnings ("unused" ) @ Cached IsSubtypeNode isSubtypeNode ,
575
571
@ Cached PyObjectLookupAttr lookupAttrNode ,
576
- @ Bind ("getBytes(frame, obj, lookupAttrNode)" ) Object bytesCallable ,
577
- @ Cached CallUnaryMethodNode callNode ,
578
- @ Cached BranchProfile branchProfile ,
579
- @ Cached PRaiseNativeNode raiseNativeNode ,
572
+ @ Cached BytesNode bytesNode ,
580
573
@ Cached TransformExceptionToNativeNode transformExceptionToNativeNode ) {
581
574
try {
582
- Object res = callNode .executeObject (frame , bytesCallable , obj );
583
- if (!isBytesSubtype (frame , res , getClassNode , isSubtypeNode )) {
584
- branchProfile .enter ();
585
- return raiseNativeNode .execute (frame , getContext ().getNativeNull (), TypeError , RETURNED_NONBYTES , new Object []{__BYTES__ , res });
586
- }
587
- return res ;
575
+ return bytesNode .execute (frame , PythonBuiltinClassType .PBytes , obj , PNone .NO_VALUE , PNone .NO_VALUE );
588
576
} catch (PException e ) {
589
577
transformExceptionToNativeNode .execute (e );
590
578
return getContext ().getNativeNull ();
591
579
}
592
580
}
593
581
594
- @ Specialization (guards = {"!isBytes(obj)" , "!isBytesSubtype(frame, obj, getClassNode, isSubtypeNode)" , "isPNone(getBytes( frame, obj, lookupAttrNode))" } )
595
- Object bytes (VirtualFrame frame , Object obj ,
596
- @ SuppressWarnings ("unused" ) @ Cached GetClassNode getClassNode ,
597
- @ SuppressWarnings ("unused" ) @ Cached IsSubtypeNode isSubtypeNode ,
582
+ @ Specialization (guards = {"!isBytes(obj)" , "!isBytesSubtype(frame, obj, getClassNode, isSubtypeNode)" , "!hasBytes( frame, obj, lookupAttrNode)" }, limit = "1" )
583
+ static Object bytes (VirtualFrame frame , Object obj ,
584
+ @ Shared ( "getClass" ) @ SuppressWarnings ("unused" ) @ Cached GetClassNode getClassNode ,
585
+ @ Shared ( "isSubtype" ) @ SuppressWarnings ("unused" ) @ Cached IsSubtypeNode isSubtypeNode ,
598
586
@ Cached PyObjectLookupAttr lookupAttrNode ,
599
- @ Cached PyBytesFromObjectNode fromObjectNode ,
600
- @ Cached TransformExceptionToNativeNode transformExceptionToNativeNode ) {
601
- try {
602
- return fromObjectNode .execute (frame , obj );
603
- } catch (PException e ) {
604
- transformExceptionToNativeNode .execute (e );
605
- return getContext ().getNativeNull ();
606
- }
587
+ @ Cached PyBytesFromObjectNode fromObjectNode ) {
588
+ return fromObjectNode .execute (frame , obj );
607
589
}
608
590
609
- protected Object getBytes (VirtualFrame frame , Object obj , PyObjectLookupAttr lookupAttrNode ) {
610
- return lookupAttrNode .execute (frame , obj , __BYTES__ );
591
+ protected static boolean hasBytes (VirtualFrame frame , Object obj , PyObjectLookupAttr lookupAttrNode ) {
592
+ return lookupAttrNode .execute (frame , obj , __BYTES__ ) != PNone . NO_VALUE ;
611
593
}
612
594
613
- protected boolean isBytesSubtype (VirtualFrame frame , Object obj , GetClassNode getClassNode , IsSubtypeNode isSubtypeNode ) {
595
+ protected static boolean isBytesSubtype (VirtualFrame frame , Object obj , GetClassNode getClassNode , IsSubtypeNode isSubtypeNode ) {
614
596
return isSubtypeNode .execute (frame , getClassNode .execute (obj ), PythonBuiltinClassType .PBytes );
615
597
}
616
598
}
0 commit comments