Skip to content

Commit

Permalink
Unify error handling for overflow values across Python 2 and 3 in saf…
Browse files Browse the repository at this point in the history
…eXtime
  • Loading branch information
jamadden committed Aug 9, 2017
1 parent 450883f commit a67d9d4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 22 deletions.
20 changes: 10 additions & 10 deletions src/zope/datetime/__init__.py
Expand Up @@ -470,20 +470,20 @@ def _correctYear(year):
def safegmtime(t):
'''gmtime with a safety zone.'''
try:
t_int = int(t)
except OverflowError:
raise TimeError('The time %f is beyond the range '
'of this Python implementation.' % float(t))
return _time.gmtime(t_int)
return _time.gmtime(t)
except (ValueError, OverflowError): # Py2/Py3 respectively
raise TimeError('The time %r is beyond the range '
'of this Python implementation.' % t)


def safelocaltime(t):
'''localtime with a safety zone.'''
try:
t_int = int(t)
except OverflowError:
raise TimeError('The time %f is beyond the range '
'of this Python implementation.' % float(t))
return _time.localtime(t_int)
return _time.localtime(t)
except (ValueError, OverflowError): # Py2/Py3 respectively
raise TimeError('The time %r is beyond the range '
'of this Python implementation.' % t)


class DateTimeParser(object):

Expand Down
20 changes: 8 additions & 12 deletions src/zope/datetime/tests/test_datetime.py
Expand Up @@ -33,18 +33,14 @@ def test_correctYear(self):


def test_safegmtime_safelocaltime_overflow(self):
def i(*args):
raise OverflowError()
try:
datetime.int = i
with self.assertRaises(datetime.TimeError):
datetime.safegmtime(1)

with self.assertRaises(datetime.TimeError):
datetime.safelocaltime(1)

finally:
del datetime.int
# Use values that are practically guaranteed to overflow on all
# platforms
v = 2**64 + 1
fv = float(v)
for func in (datetime.safegmtime, datetime.safelocaltime):
for x in (v, fv):
with self.assertRaises(datetime.TimeError):
func(x)

def test_safegmtime(self):
self.assertIsNotNone(datetime.safegmtime(6000))
Expand Down

0 comments on commit a67d9d4

Please sign in to comment.