Skip to content

Commit

Permalink
Message: Drop default code
Browse files Browse the repository at this point in the history
EMPTY is a bad default value because it is rarely needed in user code;
the new None allows for more convenient programming in the common
(client/server) cases.
  • Loading branch information
chrysn committed Nov 17, 2016
1 parent 4e4ff7c commit d0517c2
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
12 changes: 8 additions & 4 deletions aiocoap/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,19 @@ class Message(object):
resources are overhauled. Non-roundtrippable.
"""

def __init__(self, *, mtype=None, mid=None, code=EMPTY, payload=b'', token=b'', uri=None):
def __init__(self, *, mtype=None, mid=None, code=None, payload=b'', token=b'', uri=None):
self.version = 1
if mtype is None:
# leave it unspecified for convenience, sending functions will know what to do
self.mtype = None
else:
self.mtype = Type(mtype)
self.mid = mid
self.code = Code(code)
if code is None:
# as above with mtype
self.code = None
else:
self.code = Code(code)
self.token = token
self.payload = payload
self.opt = Options()
Expand Down Expand Up @@ -134,8 +138,8 @@ def decode(cls, rawdata, remote=None):

def encode(self):
"""Create binary representation of message from Message object."""
if self.mtype is None or self.mid is None:
raise TypeError("Fatal Error: Message Type and Message ID must not be None.")
if self.code is None or self.mtype is None or self.mid is None:
raise TypeError("Fatal Error: Code, Message Type and Message ID must not be None.")
rawdata = bytes([(self.version << 6) + ((self.mtype & 0x03) << 4) + (len(self.token) & 0x0F)])
rawdata += struct.pack('!BH', self.code, self.mid)
rawdata += self.token
Expand Down
8 changes: 4 additions & 4 deletions tests/encoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
class TestMessage(unittest.TestCase):

def test_encode(self):
msg1 = aiocoap.Message(mtype=aiocoap.CON, mid=0)
msg1 = aiocoap.Message(mtype=aiocoap.CON, mid=0, code=aiocoap.EMPTY)
binary1 = bytes((64,0,0,0))
self.assertEqual(msg1.encode(), binary1, "wrong encode operation for empty CON message")

Expand Down Expand Up @@ -45,18 +45,18 @@ def test_encode(self):
msg4 = aiocoap.Message(mtype=aiocoap.CON, mid=2<<16)
self.assertRaises(Exception, msg4.encode)

msg5 = aiocoap.Message(mtype=aiocoap.CON, mid=0)
msg5 = aiocoap.Message(mtype=aiocoap.CON, mid=0, code=aiocoap.EMPTY)
o = aiocoap.optiontypes.OpaqueOption(1234, value=b"abcd")
msg5.opt.add_option(o)
binary5 = binary1 + bytes((0xe4, 0x03, 0xc5)) + b"abcd"
self.assertEqual(msg5.encode(), binary5, "wrong encoding for high option numbers")

msg6 = aiocoap.Message(mtype=aiocoap.CON, mid=0)
msg6 = aiocoap.Message(mtype=aiocoap.CON, mid=0, code=aiocoap.EMPTY)
o = aiocoap.optiontypes.OpaqueOption(12345678, value=b"abcd")
msg6.opt.add_option(o)
self.assertRaises(ValueError, msg6.encode)

msg7 = aiocoap.Message(mtype=aiocoap.CON, mid=0)
msg7 = aiocoap.Message(mtype=aiocoap.CON, mid=0, code=aiocoap.EMPTY)
def set_unknown_opt():
msg7.opt.foobar = 42
self.assertRaises(AttributeError, set_unknown_opt)
Expand Down

0 comments on commit d0517c2

Please sign in to comment.