Skip to content

Raise DurationParsingException on decimal overflow instead of leaking decimal.Overflow - #29

Open
cognis-digital wants to merge 1 commit into
bolsote:masterfrom
cognis-digital:fix/decimal-overflow-parsing-exception
Open

cognis-digital wants to merge 1 commit into
bolsote:masterfrom
cognis-digital:fix/decimal-overflow-parsing-exception

Conversation

@cognis-digital

Copy link
Copy Markdown

parse_duration() promises to raise DurationParsingException for any malformed input,
but a value whose magnitude overflows the decimal context escapes that contract with a
raw decimal.Overflow:

>>> from isoduration import parse_duration
>>> parse_duration("P1E1000000D")
decimal.Overflow: [<class 'decimal.Overflow'>]

is_number() accepts scientific-notation e/E, so 1E1000000 is accepted as a numeric
token. Decimal("1E1000000") constructs without issue, but the subsequent
sign * Decimal(...) runs under the default decimal context (Emax 999999) and signals
Overflow. Both arithmetic sites in parser/parsing.py only caught InvalidOperation, so
the overflow bypassed the library's exception hierarchy — a problem for callers such as
jsonschema's duration format check that catch only DurationParsingException.

This broadens the handler at both sites from decimal.InvalidOperation to its base class
decimal.DecimalException. Since InvalidOperation is a subclass of DecimalException,
all previously handled inputs behave identically; overflow (and any other decimal signal)
is now reported as UnparseableValue, consistent with other unparseable numeric values.

Adds regression tests for both the date (P1E1000000D) and time (PT1E1000000S) paths.

Full test suite passes (378 passed).

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.

1 participant