Skip to content

Commit 472eac6

Browse files
author
attwad
committed
more coverage on nto, osc types and osc bundle+builders
1 parent 22f479e commit 472eac6

4 files changed

Lines changed: 25 additions & 2 deletions

File tree

pythonosc/parsing/ntp.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
_NTP_DELTA = (_SYSTEM_EPOCH - _NTP_EPOCH).days * 24 * 3600
1616

1717

18+
class NtpError(Exception):
19+
"""Base class for ntp module errors."""
20+
21+
1822
def ntp_to_system_time(date):
1923
"""Convert a NTP time to system time.
2024
@@ -28,6 +32,9 @@ def system_time_to_ntp(date):
2832
2933
System time is reprensented by seconds since the epoch in UTC.
3034
"""
31-
ntp = date + _NTP_DELTA
35+
try:
36+
ntp = date + _NTP_DELTA
37+
except TypeError as ve:
38+
raise NtpError('Invalud date: {}'.format(ve))
3239
num_secs, fraction = str(ntp).split('.')
3340
return struct.pack('>I', int(num_secs)) + struct.pack('>I', int(fraction))

pythonosc/parsing/osc_types.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,4 +237,7 @@ def write_date(system_time):
237237
if system_time == IMMEDIATELY:
238238
return ntp.IMMEDIATELY
239239

240-
return ntp.system_time_to_ntp(system_time)
240+
try:
241+
return ntp.system_time_to_ntp(system_time)
242+
except ntp.NtpError as ntpe:
243+
raise BuildError(ntpe)

pythonosc/test/test_osc_bundle.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,12 @@
6767
b"\x00\x00\x00\x20"
6868
b"/SYNC\x00\x00\x00\x00")
6969

70+
_DGRAM_UNKNOWN_TYPE = (
71+
b"#bundle\x00"
72+
b"\x00\x00\x00\x00\x00\x00\x00\x01"
73+
b"\x00\x00\x00\x10"
74+
b"iamnotaslash")
75+
7076

7177
class TestOscBundle(unittest.TestCase):
7278

@@ -117,5 +123,8 @@ def test_raises_on_invalid_datagram(self):
117123
self.assertRaises(
118124
osc_bundle.ParseError, osc_bundle.OscBundle, _DGRAM_INVALID_INDEX)
119125

126+
def test_unknown_type(self):
127+
bundle = osc_bundle.OscBundle(_DGRAM_UNKNOWN_TYPE)
128+
120129
if __name__ == "__main__":
121130
unittest.main()

pythonosc/test/test_osc_bundle_builder.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ def test_raises_on_build(self):
1616
bundle.add_content(None)
1717
self.assertRaises(osc_bundle_builder.BuildError, bundle.build)
1818

19+
def test_raises_on_invalid_timestamp(self):
20+
bundle = osc_bundle_builder.OscBundleBuilder("I am not a timestamp")
21+
self.assertRaises(osc_bundle_builder.BuildError, bundle.build)
22+
1923
def test_build_complex_bundle(self):
2024
bundle = osc_bundle_builder.OscBundleBuilder(
2125
osc_bundle_builder.IMMEDIATELY)

0 commit comments

Comments
 (0)