Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions tornado/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,8 @@ def _parse_datetime(self, value: str) -> datetime.datetime:
)

def _parse_timedelta(self, value: str) -> datetime.timedelta:
if not value or not value.strip():
raise Error("Invalid time delta: %r" % value)
try:
sum = datetime.timedelta()
start = 0
Expand Down
16 changes: 15 additions & 1 deletion tornado/test/options_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from io import StringIO
from unittest import mock

from tornado.options import Error, OptionParser
from tornado.options import Error, OptionParser, _Option
from tornado.util import basestring_type


Expand Down Expand Up @@ -261,6 +261,20 @@ def test_error_redefine(self):
options.define("foo")
self.assertRegex(str(cm.exception), "Option.*foo.*already defined")

def test_parse_timedelta_empty_or_whitespace_raises(self):
# Empty or whitespace-only timedelta values used to be silently
# accepted as 0 (the while loop body never ran), so `--t=` and
# `--t=" "` both parsed as datetime.timedelta(0). They now raise
# options.Error with the offending value in the message, matching
# the other type-specific parsers in this module.
for bad in ("", " ", "\t", "\n", " \t "):
option = _Option(
"t", type=datetime.timedelta, default=datetime.timedelta(0)
)
with self.assertRaises(Error) as cm:
option._parse_timedelta(bad)
self.assertIn(repr(bad), str(cm.exception))

def test_error_redefine_underscore(self):
# Ensure that the dash/underscore normalization doesn't
# interfere with the redefinition error.
Expand Down
Loading