Repository navigation
Fix DecimalField crash on long numbers when max_digits is None - #10123
Open
mohamed-alired wants to merge 1 commit into
Open
mohamed-alired wants to merge 1 commit into
mohamed-alired wants to merge 1 commit into
Conversation
quantize() only set the decimal context precision when max_digits was given, so with max_digits=None it kept the default precision of 28 and Decimal.quantize() raised InvalidOperation for any value needing more digits (e.g. '1' * 27 with decimal_places=2, or '1e30'): a 500 instead of a valid value. Give the context enough precision for the quantized value, bounded by MAX_STRING_LENGTH like the input itself, and report values beyond that bound (e.g. '1e1000') as a validation error.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
DecimalField.quantize()only sets the decimal context precision whenmax_digitsis given. Withmax_digits=None(documented as allowed, and covered byTestNoMaxDigitsDecimalField) it keeps the default context precision of 28, soDecimal.quantize()raisesdecimal.InvalidOperationfor any value that needs more digits once quantized. The exception is not caught, so the request fails with a 500:to_representation()has the same problem for such values.This change:
max_digitsisNone, bounded byMAX_STRING_LENGTHlike the input string itself, so a short input with a huge exponent (e.g.'1e999999999') cannot make the server build an enormous number;max_string_length) instead of a 500.Behaviour with
max_digitsset is unchanged.Tests:
TestNoMaxDigitsLargeDecimalFieldcovers 27-digit and1e30inputs, an integer input, an oversized exponent and a 30-digit output. It fails onmainwithInvalidOperationand passes with this change.