Skip to content

fix: raise ValidationError for out-of-range int→float in convert - #1128

Open
takerukondo wants to merge 2 commits into
msgspec:mainfrom
takerukondo:fix-convert-int-float-overflow
Open

fix: raise ValidationError for out-of-range int→float in convert#1128
takerukondo wants to merge 2 commits into
msgspec:mainfrom
takerukondo:fix-convert-int-float-overflow

Conversation

@takerukondo

Copy link
Copy Markdown

Summary

Fixes #1122.

msgspec.convert(10**400, float) was leaking SystemError because PyLong_AsDouble sets OverflowError while convert still returned a float object. json.decode already reports this as ValidationError: Number out of range.

Clear the overflow error and raise ValidationError (same message as the JSON path). Also reject non-finite results as a belt-and-suspenders check.

Test plan

  • pytest tests/unit/test_convert.py::TestFloat::test_float_from_out_of_range_int tests/unit/test_convert.py::TestFloat::test_float
  • CI

Made with Cursor

PyLong_AsDouble sets OverflowError for ints that cannot fit in a finite
C double. convert was returning a float while that exception stayed set,
which leaked SystemError. Match json.decode and report ValidationError.

Fixes msgspec#1122

Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
Comment thread src/msgspec/_core.c
Comment on lines +20941 to +20942
/* PyLong_AsDouble sets OverflowError and may return ±inf for ints that
* cannot be represented as a finite C double. Surface that as a

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where are you getting the

return ±inf for ints that cannot be represented as a finite C double

part from? It's at least not stated in the documentation: https://docs.python.org/3/c-api/long.html#c.PyLong_AsDouble

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

convert leaks SystemError on an out-of-range intfloat, where json.decode cleanly raises ValidationError

2 participants