reject empty or whitespace timedelta values in _parse_timedelta#3652
Open
HrachShah wants to merge 1 commit into
Open
reject empty or whitespace timedelta values in _parse_timedelta#3652HrachShah wants to merge 1 commit into
HrachShah wants to merge 1 commit into
Conversation
An empty string or whitespace-only value passed to _parse_timedelta used to
silently parse as datetime.timedelta(0): the while loop body only runs when
start < len(value), so with value="" or value=" " the loop is skipped and
the initial sum = datetime.timedelta() is returned. parse_command_line
then happily accepts '--t=' or '--t=" "', which contradicts every other
type-specific parser in this module (_parse_datetime / _parse_bool /
_parse_string all reject empty strings).
Add an early-exit guard that raises options.Error('Invalid time delta: %r'
% value) for falsy or strip()-empty inputs. The inner try/except is left
in place since it still legitimately catches the float() conversion and
datetime.timedelta(**{units: num}) construction errors that the existing
PR draft (b0ea1c1) was also going to clean up; this commit focuses
narrowly on the silent-accept bug.
A regression test exercises every empty/whitespace value the prior code
silently accepted and asserts Error is raised with the offending value
in the message.
Verified:
- python3 -m pytest tornado/test/options_test.py -v passes all 25 tests
- the new test fails on the pre-fix code (bare return timedelta(0))
- the existing test_types, test_types_with_conf_file, test_run_parse_callbacks
tests still pass
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.
What
_parse_timedeltaintornado/options.pysilently accepted empty ("") and whitespace-only (" ","\t","\n") values asdatetime.timedelta(0). Every other type-specific parser in the same module (_parse_datetime,_parse_bool,_parse_string) rejects empty input withoptions.Error, so this is inconsistent.Passing
--t=or--t=" "to an application usingtornado.optionswould set a 0-second timeout instead of raising the user-visible error that the rest of the module produces.Repro
Fix
Early-exit guard at the top of
_parse_timedelta:The existing inner
try: ... except Exception: raiseis left as-is — it still legitimately catchesfloat()conversion andtimedelta(**{units: num})construction errors. This commit focuses narrowly on the silent-accept bug; a separate PR (the existingfix/options-parse-timedelta-options-errorbranch) was already in flight to clean up theraise Exception()/raiseno-op pair.Test
test_parse_timedelta_empty_or_whitespace_raisesconstructs an_Optiondirectly and calls_parse_timedelta(bad)forbadin("", " ", "\t", "\n", " \t "), assertingoptions.Erroris raised with the offending value in the message. The test fails on the pre-fix code (the call returnstimedelta(0)instead of raising).Verified
python3 -m pytest tornado/test/options_test.py -v— 25 passed, 0 failuresgit stash+ test rerun shows the regression test fails as expectedtest_types,test_types_with_conf_file,test_parse_callbacks, etc.)