summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMartin Braun <martin.braun@ettus.com>2018-06-25 18:15:06 -0700
committerMarcus Müller <marcus@hostalia.de>2018-06-29 00:15:40 +0200
commit1d3785f3f2d1619bbdf16c332653192f9e840b6a (patch)
treee68306b77f549cb97f454745d32b4aea68dc7aad
parent51c234b2103a3981482a66f3c20d45c7de72fb5c (diff)
pmt: Serialize PMTs to byte strings in Python (and vice versa)
-rw-r--r--gnuradio-runtime/python/pmt/qa_pmt.py2
-rw-r--r--gnuradio-runtime/swig/pmt_swig.i41
2 files changed, 42 insertions, 1 deletions
diff --git a/gnuradio-runtime/python/pmt/qa_pmt.py b/gnuradio-runtime/python/pmt/qa_pmt.py
index 0d87676a30..b6df648029 100644
--- a/gnuradio-runtime/python/pmt/qa_pmt.py
+++ b/gnuradio-runtime/python/pmt/qa_pmt.py
@@ -1,6 +1,6 @@
#!/usr/bin/env python
#
-# Copyright 2011 Free Software Foundation, Inc.
+# Copyright 2011,2018 Free Software Foundation, Inc.
#
# This file is part of GNU Radio
#
diff --git a/gnuradio-runtime/swig/pmt_swig.i b/gnuradio-runtime/swig/pmt_swig.i
index c627b7d3e6..f03ce1289a 100644
--- a/gnuradio-runtime/swig/pmt_swig.i
+++ b/gnuradio-runtime/swig/pmt_swig.i
@@ -40,6 +40,23 @@
#include <iosfwd>
#include <stdexcept>
#include <pmt/pmt.h>
+
+namespace pmt {
+ // Wrapper for serialize_str(), so we always have raw byte strings
+ std::vector<uint8_t> _serialize_str_u8(pmt_t obj)
+ {
+ std::string serialized_str(serialize_str(obj));
+ return std::vector<uint8_t>(serialized_str.begin(), serialized_str.end());
+ }
+
+ // Wrapper for deserialize_str(), so we always have raw byte strings
+ pmt_t _deserialize_str_u8(std::vector<uint8_t> py_str)
+ {
+ std::string cpp_str(py_str.begin(), py_str.end());
+ return deserialize_str(cpp_str);
+ }
+} /* namespace pmt */
+
%}
%feature("autodoc","1");
@@ -302,7 +319,31 @@ namespace pmt{
bool serialize(pmt_t obj, std::streambuf &sink);
pmt_t deserialize(std::streambuf &source);
void dump_sizeof();
+
+ std::vector<uint8_t> _serialize_str_u8(pmt_t obj);
+ pmt_t _deserialize_str_u8(std::vector<uint8_t> py_str);
+
+ %rename(_serialize_str) serialize_str;
std::string serialize_str(pmt_t obj);
+
+ %rename(_deserialize_str) deserialize_str;
pmt_t deserialize_str(std::string str);
} //namespace pmt
+
+%pythoncode %{
+ def serialize_str(pmt_obj):
+ import sys
+ if sys.version_info.major == 2:
+ return _serialize_str(pmt_obj)
+ import array
+ return array.array('B', _serialize_str_u8(pmt_obj)).tobytes()
+
+ def deserialize_str(pmt_str):
+ import sys
+ if sys.version_info.major == 2:
+ return _deserialize_str(pmt_str)
+ return _deserialize_str_u8(tuple(x for x in pmt_str))
+
+%}
+