express: allow using bytes() on Datagram

Closes: #297
This commit is contained in:
rdb 2018-04-06 17:41:12 +02:00
parent de6d753f79
commit b4d29e6096
3 changed files with 23 additions and 0 deletions

View File

@ -316,6 +316,18 @@ get_message() const {
}
}
/**
* Returns the datagram's data as a bytes object.
*/
INLINE vector_uchar Datagram::
__bytes__() const {
if (!_data.empty()) {
return vector_uchar(_data.v());
} else {
return vector_uchar();
}
}
/**
* Returns a pointer to the beginning of the datagram's data.
*/

View File

@ -84,6 +84,7 @@ PUBLISHED:
void assign(const void *data, size_t size);
INLINE string get_message() const;
INLINE vector_uchar __bytes__() const;
INLINE const void *get_data() const;
INLINE size_t get_length() const;

View File

@ -76,6 +76,16 @@ def datagram_large():
return dg, readback_function
def test_datagram_bytes():
"""Tests that we can put and get a bytes object on Datagram."""
dg = core.Datagram(b'abc\x00')
dg.append_data(b'\xff123')
assert bytes(dg) == b'abc\x00\xff123'
dgi = core.DatagramIterator(dg)
dgi.get_remaining_bytes() == b'abc\x00\xff123'
def test_iterator(datagram_small):
"""This tests Datagram/DatagramIterator, and sort of serves as a self-check
of the test fixtures too."""