fix: reject stray pipe in sign by using [+-] character class

Inside a character class '|' is a literal, so [+|-] matched '+', '-', or
'|'. A leading pipe was captured as a sign and stripped, letting malformed
input like '|5m' parse to 300 instead of returning None. Restrict the
class to the two intended sign characters.
This commit is contained in:
dualfroz committed 2026-09-04 22:00:22 +02:00
1 parent 9b5ce3ede3
commit a0b5007d40
2 files changed
+5 -1

No files matched your search

+4
View File
@@ -83,6 +83,10 @@ class TestTimeparse(unittest.TestCase):
self.assertEqual(timeparse.timeparse('- 32 m 1 s'), -1921)
self.assertIsNone(timeparse.timeparse('32 m - 1 s'))
self.assertIsNone(timeparse.timeparse('32 m + 1 s'))
# A stray pipe is not a valid sign and must not be silently accepted.
self.assertIsNone(timeparse.timeparse('|5m'))
self.assertIsNone(timeparse.timeparse('| 5m'))
self.assertIsNone(timeparse.timeparse('|1:24'))
def test_timeparse_1(self):
'''timeparse test case 1.'''
+1 -1
View File
@@ -33,7 +33,7 @@ kinds of time expressions.
import re
SIGN = r'(?P<sign>[+|-])?'
SIGN = r'(?P<sign>[+-])?'
#YEARS = r'(?P<years>\d+)\s*(?:ys?|yrs?.?|years?)'
#MONTHS = r'(?P<months>\d+)\s*(?:mos?.?|mths?.?|months?)'
WEEKS = r'(?P<weeks>[\d.]+)\s*(?:w|wks?|weeks?)'