Merge "py3: have Timestamp accept bytestrings, better validate input"
This commit is contained in:
commit
800e585176
@ -939,11 +939,16 @@ class Timestamp(object):
|
|||||||
:param delta: deca-microsecond difference from the base timestamp
|
:param delta: deca-microsecond difference from the base timestamp
|
||||||
param, an int
|
param, an int
|
||||||
"""
|
"""
|
||||||
|
if isinstance(timestamp, bytes):
|
||||||
|
timestamp = timestamp.decode('ascii')
|
||||||
if isinstance(timestamp, six.string_types):
|
if isinstance(timestamp, six.string_types):
|
||||||
parts = timestamp.split('_', 1)
|
base, base_offset = timestamp.partition('_')[::2]
|
||||||
self.timestamp = float(parts.pop(0))
|
self.timestamp = float(base)
|
||||||
if parts:
|
if '_' in base_offset:
|
||||||
self.offset = int(parts[0], 16)
|
raise ValueError('invalid literal for int() with base 16: '
|
||||||
|
'%r' % base_offset)
|
||||||
|
if base_offset:
|
||||||
|
self.offset = int(base_offset, 16)
|
||||||
else:
|
else:
|
||||||
self.offset = 0
|
self.offset = 0
|
||||||
else:
|
else:
|
||||||
|
@ -183,6 +183,7 @@ class TestTimestamp(unittest.TestCase):
|
|||||||
|
|
||||||
def test_invalid_input(self):
|
def test_invalid_input(self):
|
||||||
self.assertRaises(ValueError, utils.Timestamp, time.time(), offset=-1)
|
self.assertRaises(ValueError, utils.Timestamp, time.time(), offset=-1)
|
||||||
|
self.assertRaises(ValueError, utils.Timestamp, '123.456_78_90')
|
||||||
|
|
||||||
def test_invalid_string_conversion(self):
|
def test_invalid_string_conversion(self):
|
||||||
t = utils.Timestamp.now()
|
t = utils.Timestamp.now()
|
||||||
@ -390,6 +391,8 @@ class TestTimestamp(unittest.TestCase):
|
|||||||
expected = '1402436408.91203_00000000000000f0'
|
expected = '1402436408.91203_00000000000000f0'
|
||||||
test_values = (
|
test_values = (
|
||||||
'1402436408.91203_000000f0',
|
'1402436408.91203_000000f0',
|
||||||
|
u'1402436408.91203_000000f0',
|
||||||
|
b'1402436408.91203_000000f0',
|
||||||
'1402436408.912030000_0000000000f0',
|
'1402436408.912030000_0000000000f0',
|
||||||
'1402436408.912029_000000f0',
|
'1402436408.912029_000000f0',
|
||||||
'1402436408.91202999999_0000000000f0',
|
'1402436408.91202999999_0000000000f0',
|
||||||
|
Loading…
Reference in New Issue
Block a user