5050import com .oracle .graal .python .builtins .Python3Core ;
5151import com .oracle .graal .python .builtins .PythonBuiltinClassType ;
5252import com .oracle .graal .python .builtins .PythonBuiltins ;
53+ import com .oracle .graal .python .builtins .modules .BuiltinConstructors .BytesNode ;
5354import com .oracle .graal .python .builtins .modules .BuiltinFunctions .IsInstanceNode ;
5455import com .oracle .graal .python .builtins .modules .BuiltinFunctions .IsSubClassNode ;
5556import com .oracle .graal .python .builtins .modules .cext .PythonCextBuiltins .CastArgsNode ;
7576import com .oracle .graal .python .lib .PyObjectReprAsObjectNode ;
7677import com .oracle .graal .python .lib .PyObjectSetItem ;
7778import 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__ ;
8179import com .oracle .graal .python .nodes .attributes .GetAttributeNode .GetAnyAttributeNode ;
8280import com .oracle .graal .python .nodes .call .CallNode ;
83- import com .oracle .graal .python .nodes .call .special .CallUnaryMethodNode ;
8481import com .oracle .graal .python .nodes .classes .IsSubtypeNode ;
8582import com .oracle .graal .python .nodes .expression .BinaryComparisonNode ;
8683import com .oracle .graal .python .nodes .function .PythonBuiltinBaseNode ;
9491import com .oracle .graal .python .util .OverflowException ;
9592import com .oracle .graal .python .util .PythonUtils ;
9693import com .oracle .truffle .api .CompilerDirectives ;
97- import com .oracle .truffle .api .dsl .Bind ;
9894import com .oracle .truffle .api .dsl .Cached ;
9995import com .oracle .truffle .api .dsl .Cached .Shared ;
10096import com .oracle .truffle .api .dsl .GenerateNodeFactory ;
@@ -486,7 +482,7 @@ Object getAttr(VirtualFrame frame, Object obj, Object attr,
486482 @ Cached GetAttributeNode getAttrNode ,
487483 @ Cached TransformExceptionToNativeNode transformExceptionToNativeNode ) {
488484 try {
489- return getAttrNode .execute (frame , getCore (). lookupType ( PythonBuiltinClassType . PythonObject ), SpecialMethodNames . __GETATTRIBUTE__ );
485+ return getAttrNode .execute (frame , obj , attr );
490486 } catch (PException e ) {
491487 transformExceptionToNativeNode .execute (frame , e );
492488 return getContext ().getNativeNull ();
@@ -502,7 +498,7 @@ int setAttr(VirtualFrame frame, Object obj, Object attr, Object value,
502498 @ Cached SetattrNode setAttrNode ,
503499 @ Cached TransformExceptionToNativeNode transformExceptionToNativeNode ) {
504500 try {
505- setAttrNode .execute (frame , getCore (). lookupType ( PythonBuiltinClassType . PythonObject ), __SETATTR__ , value );
501+ setAttrNode .execute (frame , obj , attr , value );
506502 return 0 ;
507503 } catch (PException e ) {
508504 transformExceptionToNativeNode .execute (frame , e );
@@ -568,49 +564,35 @@ Object bytes(VirtualFrame frame, Object bytes,
568564 return bytes ;
569565 }
570566
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" )
572568 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 ,
575571 @ 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 ,
580573 @ Cached TransformExceptionToNativeNode transformExceptionToNativeNode ) {
581574 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 );
588576 } catch (PException e ) {
589577 transformExceptionToNativeNode .execute (e );
590578 return getContext ().getNativeNull ();
591579 }
592580 }
593581
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 ,
598586 @ 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 );
607589 }
608590
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 ;
611593 }
612594
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 ) {
614596 return isSubtypeNode .execute (frame , getClassNode .execute (obj ), PythonBuiltinClassType .PBytes );
615597 }
616598 }
0 commit comments