@@ -767,15 +767,23 @@ abstract static class NativeBuiltin extends PythonBuiltinNode {
767767 @ Child private GetByteArrayNode getByteArrayNode ;
768768
769769 protected void transformToNative (PException p ) {
770+ NativeBuiltin .transformToNative (getContext (), p );
771+ }
772+
773+ protected static void transformToNative (PythonContext context , PException p ) {
770774 p .getExceptionObject ().reifyException ();
771- getContext () .setCurrentException (p );
775+ context .setCurrentException (p );
772776 }
773777
774778 protected <T > T raiseNative (T defaultValue , PythonBuiltinClassType errType , String fmt , Object ... args ) {
779+ return NativeBuiltin .raiseNative (this , defaultValue , errType , fmt , args );
780+ }
781+
782+ protected static <T > T raiseNative (PNodeWithContext n , T defaultValue , PythonBuiltinClassType errType , String fmt , Object ... args ) {
775783 try {
776- throw raise (errType , fmt , args );
784+ throw n . raise (errType , fmt , args );
777785 } catch (PException p ) {
778- transformToNative (p );
786+ NativeBuiltin . transformToNative (n . getContext (), p );
779787 return defaultValue ;
780788 }
781789 }
@@ -881,23 +889,23 @@ protected static String getUTF32Name(int byteorder) {
881889 }
882890 }
883891
884- @ Builtin (name = "TrufflePInt_AsPrimitive" , fixedNumOfPositionalArgs = 4 )
892+ @ Builtin (name = "TrufflePInt_AsPrimitive" , fixedNumOfPositionalArgs = 3 )
885893 @ GenerateNodeFactory
886- abstract static class TrufflePInt_AsPrimitive extends NativeBuiltin {
894+ abstract static class TrufflePInt_AsPrimitive extends PythonTernaryBuiltinNode {
887895
888- public abstract Object executeWith (Object o , int signed , long targetTypeSize , String targetTypeName );
896+ public abstract Object executeWith (Object o , int signed , long targetTypeSize );
889897
890- public abstract long executeLong (Object o , int signed , long targetTypeSize , String targetTypeName );
898+ public abstract long executeLong (Object o , int signed , long targetTypeSize );
891899
892- public abstract int executeInt (Object o , int signed , long targetTypeSize , String targetTypeName );
900+ public abstract int executeInt (Object o , int signed , long targetTypeSize );
893901
894902 @ Specialization (guards = "targetTypeSize == 4" )
895- int doInt4 (int obj , @ SuppressWarnings ("unused" ) int signed , @ SuppressWarnings ("unused" ) long targetTypeSize , @ SuppressWarnings ( "unused" ) String targetTypeName ) {
903+ int doInt4 (int obj , @ SuppressWarnings ("unused" ) int signed , @ SuppressWarnings ("unused" ) long targetTypeSize ) {
896904 return obj ;
897905 }
898906
899907 @ Specialization (guards = "targetTypeSize == 8" )
900- long doInt8 (int obj , int signed , @ SuppressWarnings ("unused" ) long targetTypeSize , @ SuppressWarnings ( "unused" ) String targetTypeName ) {
908+ long doInt8 (int obj , int signed , @ SuppressWarnings ("unused" ) long targetTypeSize ) {
901909 if (signed != 0 ) {
902910 return obj ;
903911 } else {
@@ -906,32 +914,32 @@ long doInt8(int obj, int signed, @SuppressWarnings("unused") long targetTypeSize
906914 }
907915
908916 @ Specialization (guards = {"targetTypeSize != 4" , "targetTypeSize != 8" })
909- int doIntOther (@ SuppressWarnings ("unused" ) int obj , @ SuppressWarnings ("unused" ) int signed , long targetTypeSize , @ SuppressWarnings ( "unused" ) String targetTypeName ) {
910- return raiseNative (- 1 , PythonErrorType . SystemError , "Unsupported target size: %d" , targetTypeSize );
917+ int doIntOther (@ SuppressWarnings ("unused" ) int obj , @ SuppressWarnings ("unused" ) int signed , long targetTypeSize ) {
918+ return raiseUnsupportedSize ( targetTypeSize );
911919 }
912920
913921 @ Specialization (guards = "targetTypeSize == 4" )
914- int doLong4 (@ SuppressWarnings ("unused" ) long obj , @ SuppressWarnings ("unused" ) int signed , @ SuppressWarnings ("unused" ) long targetTypeSize , String targetTypeName ) {
915- return raiseNative (- 1 , PythonErrorType . OverflowError , "Python int too large to convert to C %s" , targetTypeName );
922+ int doLong4 (@ SuppressWarnings ("unused" ) long obj , @ SuppressWarnings ("unused" ) int signed , @ SuppressWarnings ("unused" ) long targetTypeSize ) {
923+ return raiseTooLarge ( targetTypeSize );
916924 }
917925
918926 @ Specialization (guards = "targetTypeSize == 8" )
919- long doLong8 (long obj , @ SuppressWarnings ("unused" ) int signed , @ SuppressWarnings ("unused" ) int targetTypeSize , @ SuppressWarnings ( "unused" ) String targetTypeName ) {
927+ long doLong8 (long obj , @ SuppressWarnings ("unused" ) int signed , @ SuppressWarnings ("unused" ) int targetTypeSize ) {
920928 return obj ;
921929 }
922930
923931 @ Specialization (guards = "targetTypeSize == 8" )
924- long doLong8 (long obj , @ SuppressWarnings ("unused" ) int signed , @ SuppressWarnings ("unused" ) long targetTypeSize , @ SuppressWarnings ( "unused" ) String targetTypeName ) {
932+ long doLong8 (long obj , @ SuppressWarnings ("unused" ) int signed , @ SuppressWarnings ("unused" ) long targetTypeSize ) {
925933 return obj ;
926934 }
927935
928936 @ Specialization (guards = {"targetTypeSize != 4" , "targetTypeSize != 8" })
929- int doPInt (@ SuppressWarnings ("unused" ) long obj , @ SuppressWarnings ("unused" ) int signed , long targetTypeSize , @ SuppressWarnings ( "unused" ) String targetTypeName ) {
930- return raiseNative (- 1 , PythonErrorType . SystemError , "Unsupported target size: %d" , targetTypeSize );
937+ int doPInt (@ SuppressWarnings ("unused" ) long obj , @ SuppressWarnings ("unused" ) int signed , long targetTypeSize ) {
938+ return raiseUnsupportedSize ( targetTypeSize );
931939 }
932940
933941 @ Specialization (guards = "targetTypeSize == 4" )
934- int doPInt4 (PInt obj , int signed , @ SuppressWarnings ("unused" ) long targetTypeSize , String targetTypeName ) {
942+ int doPInt4 (PInt obj , int signed , @ SuppressWarnings ("unused" ) long targetTypeSize ) {
935943 try {
936944 if (signed != 0 ) {
937945 return obj .intValueExact ();
@@ -941,12 +949,12 @@ int doPInt4(PInt obj, int signed, @SuppressWarnings("unused") long targetTypeSiz
941949 throw new ArithmeticException ();
942950 }
943951 } catch (ArithmeticException e ) {
944- return raiseNative (- 1 , PythonErrorType . OverflowError , "Python int too large to convert to C %s" , targetTypeName );
952+ return raiseTooLarge ( targetTypeSize );
945953 }
946954 }
947955
948956 @ Specialization (guards = "targetTypeSize == 8" )
949- long doPInt8 (PInt obj , int signed , @ SuppressWarnings ("unused" ) long targetTypeSize , String targetTypeName ) {
957+ long doPInt8 (PInt obj , int signed , @ SuppressWarnings ("unused" ) long targetTypeSize ) {
950958 try {
951959 if (signed != 0 ) {
952960 return obj .longValueExact ();
@@ -956,20 +964,32 @@ long doPInt8(PInt obj, int signed, @SuppressWarnings("unused") long targetTypeSi
956964 throw new ArithmeticException ();
957965 }
958966 } catch (ArithmeticException e ) {
959- return raiseNative (- 1 , PythonErrorType . OverflowError , "Python int too large to convert to C %s" , targetTypeName );
967+ return raiseTooLarge ( targetTypeSize );
960968 }
961969 }
962970
963971 @ Specialization (guards = {"targetTypeSize != 4" , "targetTypeSize != 8" })
964- int doPInt (@ SuppressWarnings ("unused" ) PInt obj , @ SuppressWarnings ("unused" ) int signed , long targetTypeSize , @ SuppressWarnings ( "unused" ) String targetTypeName ) {
965- return raiseNative (- 1 , PythonErrorType . SystemError , "Unsupported target size: %d" , targetTypeSize );
972+ int doPInt (@ SuppressWarnings ("unused" ) PInt obj , @ SuppressWarnings ("unused" ) int signed , long targetTypeSize ) {
973+ return raiseUnsupportedSize ( targetTypeSize );
966974 }
967975
968976 @ Specialization (guards = {"!isInteger(obj)" , "!isPInt(obj)" })
969977 @ SuppressWarnings ("unused" )
970- int doGeneric (Object obj , boolean signed , int targetTypeSize , String targetTypeName ) {
978+ int doGeneric (Object obj , boolean signed , int targetTypeSize ) {
971979 return raiseNative (-1 , PythonErrorType .TypeError , "an integer is required" , obj );
972980 }
981+
982+ private int raiseTooLarge (long targetTypeSize ) {
983+ return raiseNative (-1 , PythonErrorType .OverflowError , "Python int too large to convert to %s-byte C type" , targetTypeSize );
984+ }
985+
986+ private Integer raiseUnsupportedSize (long targetTypeSize ) {
987+ return raiseNative (-1 , PythonErrorType .SystemError , "Unsupported target size: %d" , targetTypeSize );
988+ }
989+
990+ private <T > T raiseNative (T defaultValue , PythonBuiltinClassType errType , String fmt , Object ... args ) {
991+ return NativeBuiltin .raiseNative (this , defaultValue , errType , fmt , args );
992+ }
973993 }
974994
975995 @ Builtin (name = "PyTruffle_Unicode_FromWchar" , fixedNumOfPositionalArgs = 3 )
@@ -1155,7 +1175,7 @@ public static GetByteArrayNode create() {
11551175
11561176 @ Specialization
11571177 byte [] doCArrayWrapper (CByteArrayWrapper o , @ SuppressWarnings ("unused" ) long size ) {
1158- return o .getDelegate ();
1178+ return o .getByteArray ();
11591179 }
11601180
11611181 @ Specialization
@@ -1998,9 +2018,9 @@ TruffleObject doPointer(PythonNativeVoidPtr n) {
19982018 long doGeneric (Object n ) {
19992019 if (asPrimitiveNode == null ) {
20002020 CompilerDirectives .transferToInterpreterAndInvalidate ();
2001- asPrimitiveNode = insert (TrufflePInt_AsPrimitiveFactory .create (null ));
2021+ asPrimitiveNode = insert (TrufflePInt_AsPrimitiveFactory .create ());
20022022 }
2003- return asPrimitiveNode .executeLong (n , 0 , Long .BYTES , "void*" );
2023+ return asPrimitiveNode .executeLong (n , 0 , Long .BYTES );
20042024 }
20052025 }
20062026}
0 commit comments