Skip to content

Commit f9d6d76

Browse files
committed
[mypyc] Raise ValueError if int too big for native int type
Previously we used OverflowError, which seems inconsistent with CPython, since a conversion/range check is arguably not an arithmetic operation, and OverflowError is a subclass of ArithmeticError. This is an extract for the docs for OverflowError: "Raised when the result of an arithmetic operation is too large to be represented."
1 parent 284ba55 commit f9d6d76

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

mypyc/lib-rt/int_ops.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ int64_t CPyLong_AsInt64_(PyObject *o) {
400400
if (PyErr_Occurred()) {
401401
return CPY_LL_INT_ERROR;
402402
} else if (overflow) {
403-
PyErr_SetString(PyExc_OverflowError, "int too large to convert to i64");
403+
PyErr_SetString(PyExc_ValueError, "int too large to convert to i64");
404404
return CPY_LL_INT_ERROR;
405405
}
406406
}
@@ -453,7 +453,7 @@ int32_t CPyLong_AsInt32_(PyObject *o) {
453453
if (PyErr_Occurred()) {
454454
return CPY_LL_INT_ERROR;
455455
} else if (overflow) {
456-
PyErr_SetString(PyExc_OverflowError, "int too large to convert to i32");
456+
PyErr_SetString(PyExc_ValueError, "int too large to convert to i32");
457457
return CPY_LL_INT_ERROR;
458458
}
459459
}
@@ -495,7 +495,7 @@ int32_t CPyInt32_Remainder(int32_t x, int32_t y) {
495495
}
496496

497497
void CPyInt32_Overflow() {
498-
PyErr_SetString(PyExc_OverflowError, "int too large to convert to i32");
498+
PyErr_SetString(PyExc_ValueError, "int too large to convert to i32");
499499
}
500500

501501
// i16 unboxing slow path
@@ -510,7 +510,7 @@ int16_t CPyLong_AsInt16_(PyObject *o) {
510510
if (PyErr_Occurred()) {
511511
return CPY_LL_INT_ERROR;
512512
} else if (overflow) {
513-
PyErr_SetString(PyExc_OverflowError, "int too large to convert to i16");
513+
PyErr_SetString(PyExc_ValueError, "int too large to convert to i16");
514514
return CPY_LL_INT_ERROR;
515515
}
516516
}
@@ -552,7 +552,7 @@ int16_t CPyInt16_Remainder(int16_t x, int16_t y) {
552552
}
553553

554554
void CPyInt16_Overflow() {
555-
PyErr_SetString(PyExc_OverflowError, "int too large to convert to i16");
555+
PyErr_SetString(PyExc_ValueError, "int too large to convert to i16");
556556
}
557557

558558
// u8 unboxing slow path
@@ -567,15 +567,15 @@ uint8_t CPyLong_AsUInt8_(PyObject *o) {
567567
if (PyErr_Occurred()) {
568568
return CPY_LL_UINT_ERROR;
569569
} else if (overflow) {
570-
PyErr_SetString(PyExc_OverflowError, "int too large or small to convert to u8");
570+
PyErr_SetString(PyExc_ValueError, "int too large or small to convert to u8");
571571
return CPY_LL_UINT_ERROR;
572572
}
573573
}
574574
return result;
575575
}
576576

577577
void CPyUInt8_Overflow() {
578-
PyErr_SetString(PyExc_OverflowError, "int too large or small to convert to u8");
578+
PyErr_SetString(PyExc_ValueError, "int too large or small to convert to u8");
579579
}
580580

581581
double CPyTagged_TrueDivide(CPyTagged x, CPyTagged y) {

0 commit comments

Comments
 (0)