Skip to content

Commit fefc070

Browse files
authored
[mypyc] Raise ValueError if int too big for native int type (#20385)
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 fefc070

File tree

5 files changed

+55
-55
lines changed

5 files changed

+55
-55
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) {

mypyc/test-data/run-i16.test

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ def test_box_and_unbox() -> None:
1515
o2: Any = x
1616
assert o == o2
1717
assert x == i
18-
with assertRaises(OverflowError, "int too large to convert to i16"):
18+
with assertRaises(ValueError, "int too large to convert to i16"):
1919
o = 2**15
2020
x2: i16 = o
21-
with assertRaises(OverflowError, "int too large to convert to i16"):
21+
with assertRaises(ValueError, "int too large to convert to i16"):
2222
o = -2**15 - 1
2323
x3: i16 = o
2424

@@ -209,13 +209,13 @@ def test_mixed_comparisons() -> None:
209209

210210
int_too_big = int() + (1 << 15)
211211
int_too_small = int() - (1 << 15) - 1
212-
with assertRaises(OverflowError):
212+
with assertRaises(ValueError):
213213
assert i16_3 < int_too_big
214-
with assertRaises(OverflowError):
214+
with assertRaises(ValueError):
215215
assert int_too_big < i16_3
216-
with assertRaises(OverflowError):
216+
with assertRaises(ValueError):
217217
assert i16_3 > int_too_small
218-
with assertRaises(OverflowError):
218+
with assertRaises(ValueError):
219219
assert int_too_small < i16_3
220220

221221
def test_mixed_arithmetic_and_bitwise_ops() -> None:
@@ -235,9 +235,9 @@ def test_mixed_arithmetic_and_bitwise_ops() -> None:
235235

236236
int_too_big = int() + (1 << 15)
237237
int_too_small = int() - (1 << 15) - 1
238-
with assertRaises(OverflowError):
238+
with assertRaises(ValueError):
239239
assert i16_3 & int_too_big
240-
with assertRaises(OverflowError):
240+
with assertRaises(ValueError):
241241
assert int_too_small & i16_3
242242

243243
def test_coerce_to_and_from_int() -> None:
@@ -277,11 +277,11 @@ def test_explicit_conversion_overflow() -> None:
277277
assert int(y) == min_i16
278278

279279
too_big = int() + 2**15
280-
with assertRaises(OverflowError):
280+
with assertRaises(ValueError):
281281
x = i16(too_big)
282282

283283
too_small = int() - 2**15 - 1
284-
with assertRaises(OverflowError):
284+
with assertRaises(ValueError):
285285
x = i16(too_small)
286286

287287
def test_i16_from_large_small_literal() -> None:
@@ -320,9 +320,9 @@ def test_explicit_conversion_from_float() -> None:
320320
assert from_float(2**15 - 1) == 2**15 - 1
321321
assert from_float(-2**15) == -2**15
322322
# The error message could be better, but this is acceptable
323-
with assertRaises(OverflowError, "int too large to convert to i16"):
323+
with assertRaises(ValueError, "int too large to convert to i16"):
324324
assert from_float(float(2**15))
325-
with assertRaises(OverflowError, "int too large to convert to i16"):
325+
with assertRaises(ValueError, "int too large to convert to i16"):
326326
# One ulp below the lowest valid i64 value
327327
from_float(float(-2**15 - 1))
328328

mypyc/test-data/run-i32.test

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ def test_box_and_unbox() -> None:
1515
o2: Any = x
1616
assert o == o2
1717
assert x == i
18-
with assertRaises(OverflowError, "int too large to convert to i32"):
18+
with assertRaises(ValueError, "int too large to convert to i32"):
1919
o = 2**31
2020
x2: i32 = o
21-
with assertRaises(OverflowError, "int too large to convert to i32"):
21+
with assertRaises(ValueError, "int too large to convert to i32"):
2222
o = -2**32 - 1
2323
x3: i32 = o
2424

@@ -209,13 +209,13 @@ def test_mixed_comparisons() -> None:
209209

210210
int_too_big = int() + (1 << 31)
211211
int_too_small = int() - (1 << 31) - 1
212-
with assertRaises(OverflowError):
212+
with assertRaises(ValueError):
213213
assert i32_3 < int_too_big
214-
with assertRaises(OverflowError):
214+
with assertRaises(ValueError):
215215
assert int_too_big < i32_3
216-
with assertRaises(OverflowError):
216+
with assertRaises(ValueError):
217217
assert i32_3 > int_too_small
218-
with assertRaises(OverflowError):
218+
with assertRaises(ValueError):
219219
assert int_too_small < i32_3
220220

221221
def test_mixed_arithmetic_and_bitwise_ops() -> None:
@@ -235,9 +235,9 @@ def test_mixed_arithmetic_and_bitwise_ops() -> None:
235235

236236
int_too_big = int() + (1 << 31)
237237
int_too_small = int() - (1 << 31) - 1
238-
with assertRaises(OverflowError):
238+
with assertRaises(ValueError):
239239
assert i32_3 & int_too_big
240-
with assertRaises(OverflowError):
240+
with assertRaises(ValueError):
241241
assert int_too_small & i32_3
242242

243243
def test_coerce_to_and_from_int() -> None:
@@ -281,11 +281,11 @@ def test_explicit_conversion_overflow() -> None:
281281
assert int(y) == min_i32
282282

283283
too_big = int() + 2**31
284-
with assertRaises(OverflowError):
284+
with assertRaises(ValueError):
285285
x = i32(too_big)
286286

287287
too_small = int() - 2**31 - 1
288-
with assertRaises(OverflowError):
288+
with assertRaises(ValueError):
289289
x = i32(too_small)
290290

291291
def test_i32_from_large_small_literal() -> None:
@@ -318,9 +318,9 @@ def test_explicit_conversion_from_float() -> None:
318318
assert from_float(2**31 - 1) == 2**31 - 1
319319
assert from_float(-2**31) == -2**31
320320
# The error message could be better, but this is acceptable
321-
with assertRaises(OverflowError, "int too large to convert to i32"):
321+
with assertRaises(ValueError, "int too large to convert to i32"):
322322
assert from_float(float(2**31))
323-
with assertRaises(OverflowError, "int too large to convert to i32"):
323+
with assertRaises(ValueError, "int too large to convert to i32"):
324324
# One ulp below the lowest valid i64 value
325325
from_float(float(-2**31 - 2048))
326326

mypyc/test-data/run-i64.test

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -299,11 +299,11 @@ def test_explicit_conversion_overflow() -> None:
299299
assert int(y) == min_i64
300300

301301
too_big = int() + 2**63
302-
with assertRaises(OverflowError):
302+
with assertRaises(ValueError):
303303
x = i64(too_big)
304304

305305
too_small = int() - 2**63 - 1
306-
with assertRaises(OverflowError):
306+
with assertRaises(ValueError):
307307
x = i64(too_small)
308308

309309
def test_i64_from_large_small_literal() -> None:
@@ -323,9 +323,9 @@ def test_explicit_conversion_from_float() -> None:
323323
assert from_float(2**63 - 1024) == 2**63 - 1024
324324
assert from_float(-2**63) == -2**63
325325
# The error message could be better, but this is acceptable
326-
with assertRaises(OverflowError, "int too large to convert to i64"):
326+
with assertRaises(ValueError, "int too large to convert to i64"):
327327
assert from_float(float(2**63))
328-
with assertRaises(OverflowError, "int too large to convert to i64"):
328+
with assertRaises(ValueError, "int too large to convert to i64"):
329329
# One ulp below the lowest valid i64 value
330330
from_float(float(-2**63 - 2048))
331331

@@ -430,13 +430,13 @@ def test_mixed_comparisons() -> None:
430430

431431
int_too_big = int() + (1 << 63)
432432
int_too_small = int() - (1 << 63) - 1
433-
with assertRaises(OverflowError):
433+
with assertRaises(ValueError):
434434
assert i64_3 < int_too_big
435-
with assertRaises(OverflowError):
435+
with assertRaises(ValueError):
436436
assert int_too_big < i64_3
437-
with assertRaises(OverflowError):
437+
with assertRaises(ValueError):
438438
assert i64_3 > int_too_small
439-
with assertRaises(OverflowError):
439+
with assertRaises(ValueError):
440440
assert int_too_small < i64_3
441441

442442
def test_mixed_comparisons_32bit() -> None:
@@ -476,9 +476,9 @@ def test_mixed_arithmetic_and_bitwise_ops() -> None:
476476

477477
int_too_big = int() + (1 << 63)
478478
int_too_small = int() - (1 << 63) - 1
479-
with assertRaises(OverflowError):
479+
with assertRaises(ValueError):
480480
assert i64_3 & int_too_big
481-
with assertRaises(OverflowError):
481+
with assertRaises(ValueError):
482482
assert int_too_small & i64_3
483483

484484
def test_for_loop() -> None:
@@ -591,10 +591,10 @@ def test_unbox_int_fails() -> None:
591591
with assertRaises(TypeError, msg):
592592
x: i64 = o
593593
o2: Any = 1 << 63
594-
with assertRaises(OverflowError, "int too large to convert to i64"):
594+
with assertRaises(ValueError, "int too large to convert to i64"):
595595
y: i64 = o2
596596
o3: Any = -(1 << 63 + 1)
597-
with assertRaises(OverflowError, "int too large to convert to i64"):
597+
with assertRaises(ValueError, "int too large to convert to i64"):
598598
z: i64 = o3
599599

600600
class Uninit:

mypyc/test-data/run-u8.test

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ def test_box_and_unbox() -> None:
1414
o2: Any = x
1515
assert o == o2
1616
assert x == i
17-
with assertRaises(OverflowError, "int too large or small to convert to u8"):
17+
with assertRaises(ValueError, "int too large or small to convert to u8"):
1818
o = 256
1919
x2: u8 = o
20-
with assertRaises(OverflowError, "int too large or small to convert to u8"):
20+
with assertRaises(ValueError, "int too large or small to convert to u8"):
2121
o = -1
2222
x3: u8 = o
2323

@@ -166,13 +166,13 @@ def test_mixed_comparisons() -> None:
166166

167167
int_too_big = int() + 256
168168
int_too_small = int() -1
169-
with assertRaises(OverflowError):
169+
with assertRaises(ValueError):
170170
assert u8_3 < int_too_big
171-
with assertRaises(OverflowError):
171+
with assertRaises(ValueError):
172172
assert int_too_big < u8_3
173-
with assertRaises(OverflowError):
173+
with assertRaises(ValueError):
174174
assert u8_3 > int_too_small
175-
with assertRaises(OverflowError):
175+
with assertRaises(ValueError):
176176
assert int_too_small < u8_3
177177

178178
def test_mixed_arithmetic_and_bitwise_ops() -> None:
@@ -192,9 +192,9 @@ def test_mixed_arithmetic_and_bitwise_ops() -> None:
192192

193193
int_too_big = int() + 256
194194
int_too_small = int() - 1
195-
with assertRaises(OverflowError):
195+
with assertRaises(ValueError):
196196
assert u8_3 & int_too_big
197-
with assertRaises(OverflowError):
197+
with assertRaises(ValueError):
198198
assert int_too_small & u8_3
199199

200200
def test_coerce_to_and_from_int() -> None:
@@ -233,11 +233,11 @@ def test_explicit_conversion_overflow() -> None:
233233
assert int(y) == min_u8
234234

235235
too_big = int() + 256
236-
with assertRaises(OverflowError):
236+
with assertRaises(ValueError):
237237
x = u8(too_big)
238238

239239
too_small = int() - 1
240-
with assertRaises(OverflowError):
240+
with assertRaises(ValueError):
241241
x = u8(too_small)
242242

243243
def test_u8_from_large_small_literal() -> None:
@@ -277,9 +277,9 @@ def test_explicit_conversion_from_float() -> None:
277277
assert from_float(0) == 0
278278
assert from_float(-0.999) == 0
279279
# The error message could be better, but this is acceptable
280-
with assertRaises(OverflowError, "int too large or small to convert to u8"):
280+
with assertRaises(ValueError, "int too large or small to convert to u8"):
281281
assert from_float(float(256))
282-
with assertRaises(OverflowError, "int too large or small to convert to u8"):
282+
with assertRaises(ValueError, "int too large or small to convert to u8"):
283283
# One ulp below the lowest valid i64 value
284284
from_float(float(-1.0))
285285

0 commit comments

Comments
 (0)