40
40
import com .oracle .graal .python .nodes .expression .BinaryComparisonNode ;
41
41
import com .oracle .graal .python .nodes .expression .CastToBooleanNode ;
42
42
import com .oracle .graal .python .nodes .util .CastToIndexNode ;
43
+ import com .oracle .graal .python .runtime .exception .PException ;
43
44
import com .oracle .graal .python .runtime .object .PythonObjectFactory ;
44
45
import com .oracle .graal .python .runtime .sequence .PSequence ;
45
46
import com .oracle .graal .python .runtime .sequence .storage .BasicSequenceStorage ;
@@ -826,7 +827,7 @@ protected byte doIntOvf(int value) {
826
827
try {
827
828
return PInt .byteValueExact (value );
828
829
} catch (ArithmeticException e ) {
829
- throw raise ( ValueError , "byte must be in range(0, 256)" );
830
+ throw raiseByteRangeError ( );
830
831
}
831
832
}
832
833
@@ -840,21 +841,21 @@ protected byte doLongOvf(long value) {
840
841
try {
841
842
return PInt .byteValueExact (value );
842
843
} catch (ArithmeticException e ) {
843
- throw raise ( ValueError , "byte must be in range(0, 256)" );
844
+ throw raiseByteRangeError ( );
844
845
}
845
846
}
846
847
847
848
@ Specialization (rewriteOn = ArithmeticException .class )
848
849
protected byte doPInt (PInt value ) {
849
- return value .byteValueExact ();
850
+ return PInt .byteValueExact (value . longValueExact () );
850
851
}
851
852
852
853
@ Specialization (replaces = "doPInt" )
853
854
protected byte doPIntOvf (PInt value ) {
854
855
try {
855
- return value .byteValueExact ();
856
+ return PInt .byteValueExact (value . longValueExact () );
856
857
} catch (ArithmeticException e ) {
857
- throw raise ( ValueError , "byte must be in range(0, 256)" );
858
+ throw raiseByteRangeError ( );
858
859
}
859
860
}
860
861
@@ -868,6 +869,10 @@ protected byte doGeneric(@SuppressWarnings("unused") Object val) {
868
869
throw raise (TypeError , "an integer is required" );
869
870
}
870
871
872
+ private PException raiseByteRangeError () {
873
+ throw raise (ValueError , "byte must be in range(0, 256)" );
874
+ }
875
+
871
876
public static CastToByteNode create () {
872
877
return CastToByteNodeGen .create ();
873
878
}
0 commit comments