Repository navigation
Commit 3f67b34
authored
Fix SystemError when convert()'ing an out-of-range int to float (#1162)
## Summary
`msgspec.convert(obj, float)` converts a Python `int` to a C `double`
via `PyLong_AsDouble` without checking for overflow. For an `int` too
large to represent as a finite `float` (e.g. `10**400`),
`PyLong_AsDouble` sets `OverflowError` internally but still returns
`-1.0`, and that value was passed straight through to `ms_decode_float`
and returned to the interpreter with the exception still set — which
surfaces as:
```
SystemError: <built-in function convert> returned a result with an exception set
```
This bypasses the documented `ValidationError` contract entirely, so a
caller wrapping the call in `except msgspec.ValidationError` doesn't
catch it.
`json.decode` already handles the equivalent case correctly, raising
`ValidationError: Number out of range`. This PR applies the same "check
`PyErr_Occurred()` after a lossy C conversion, then report via
`ms_error_with_path`" pattern already used elsewhere in this file (e.g.
`_constr_as_f64`), so `convert()` reports the overflow the same way —
for both bare (`convert(big, float)`) and nested (`convert({"x": big},
dict[str, float])`) targets, under both `strict=True` and
`strict=False`.
Fixes #1122.
## Testing
- Reverted the fix locally and reproduced the exact reported
`SystemError` before reapplying it.
- Added `test_float_from_int_out_of_range` to `TestFloat` in
`tests/unit/test_convert.py`, covering bare and nested targets under
both strict modes.
- Ran the full `tests/unit/` suite: 6052 passed, 473 skipped, 0
failures.1 parent 7c2f473 commit 3f67b34
2 files changed
Lines changed: 19 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
20934 | 20934 | | |
20935 | 20935 | | |
20936 | 20936 | | |
20937 | | - | |
| 20937 | + | |
| 20938 | + | |
| 20939 | + | |
| 20940 | + | |
| 20941 | + | |
| 20942 | + | |
| 20943 | + | |
| 20944 | + | |
20938 | 20945 | | |
20939 | 20946 | | |
20940 | 20947 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
429 | 429 | | |
430 | 430 | | |
431 | 431 | | |
| 432 | + | |
| 433 | + | |
| 434 | + | |
| 435 | + | |
| 436 | + | |
| 437 | + | |
| 438 | + | |
| 439 | + | |
| 440 | + | |
| 441 | + | |
| 442 | + | |
432 | 443 | | |
433 | 444 | | |
434 | 445 | | |
| |||
0 commit comments