2
0
Эх сурвалжийг харах

express: allow using bytes() on Datagram

Closes: #297
rdb 7 жил өмнө
parent
commit
b4d29e6096

+ 12 - 0
panda/src/express/datagram.I

@@ -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.
  */

+ 1 - 0
panda/src/express/datagram.h

@@ -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;
 

+ 10 - 0
tests/putil/test_datagram.py

@@ -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."""