Skip to content

Commit ad0e446

Browse files
committed
Fix: Correctly convert 'PInt' value to byte.
1 parent 9613805 commit ad0e446

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/common/SequenceStorageNodes.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import com.oracle.graal.python.nodes.expression.BinaryComparisonNode;
4141
import com.oracle.graal.python.nodes.expression.CastToBooleanNode;
4242
import com.oracle.graal.python.nodes.util.CastToIndexNode;
43+
import com.oracle.graal.python.runtime.exception.PException;
4344
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
4445
import com.oracle.graal.python.runtime.sequence.PSequence;
4546
import com.oracle.graal.python.runtime.sequence.storage.BasicSequenceStorage;
@@ -826,7 +827,7 @@ protected byte doIntOvf(int value) {
826827
try {
827828
return PInt.byteValueExact(value);
828829
} catch (ArithmeticException e) {
829-
throw raise(ValueError, "byte must be in range(0, 256)");
830+
throw raiseByteRangeError();
830831
}
831832
}
832833

@@ -840,21 +841,21 @@ protected byte doLongOvf(long value) {
840841
try {
841842
return PInt.byteValueExact(value);
842843
} catch (ArithmeticException e) {
843-
throw raise(ValueError, "byte must be in range(0, 256)");
844+
throw raiseByteRangeError();
844845
}
845846
}
846847

847848
@Specialization(rewriteOn = ArithmeticException.class)
848849
protected byte doPInt(PInt value) {
849-
return value.byteValueExact();
850+
return PInt.byteValueExact(value.longValueExact());
850851
}
851852

852853
@Specialization(replaces = "doPInt")
853854
protected byte doPIntOvf(PInt value) {
854855
try {
855-
return value.byteValueExact();
856+
return PInt.byteValueExact(value.longValueExact());
856857
} catch (ArithmeticException e) {
857-
throw raise(ValueError, "byte must be in range(0, 256)");
858+
throw raiseByteRangeError();
858859
}
859860
}
860861

@@ -868,6 +869,10 @@ protected byte doGeneric(@SuppressWarnings("unused") Object val) {
868869
throw raise(TypeError, "an integer is required");
869870
}
870871

872+
private PException raiseByteRangeError() {
873+
throw raise(ValueError, "byte must be in range(0, 256)");
874+
}
875+
871876
public static CastToByteNode create() {
872877
return CastToByteNodeGen.create();
873878
}

0 commit comments

Comments
 (0)