diff --git a/panda/src/express/datagram.I b/panda/src/express/datagram.I index 98b71820e5..8c555c777d 100644 --- a/panda/src/express/datagram.I +++ b/panda/src/express/datagram.I @@ -316,18 +316,6 @@ 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. */ diff --git a/panda/src/express/datagram.h b/panda/src/express/datagram.h index 69dce69e31..b2a3acb363 100644 --- a/panda/src/express/datagram.h +++ b/panda/src/express/datagram.h @@ -85,11 +85,16 @@ PUBLISHED: void append_data(const void *data, size_t size); INLINE void append_data(const vector_uchar &data); +public: void assign(const void *data, size_t size); INLINE std::string get_message() const; - INLINE vector_uchar __bytes__() const; INLINE const void *get_data() const; + +PUBLISHED: + EXTENSION(INLINE PyObject *get_message() const); + EXTENSION(INLINE PyObject *__bytes__() const); + INLINE size_t get_length() const; INLINE void set_array(PTA_uchar data); diff --git a/panda/src/express/datagram_ext.I b/panda/src/express/datagram_ext.I new file mode 100644 index 0000000000..a35c5410b2 --- /dev/null +++ b/panda/src/express/datagram_ext.I @@ -0,0 +1,35 @@ +/** + * PANDA 3D SOFTWARE + * Copyright (c) Carnegie Mellon University. All rights reserved. + * + * All use of this software is subject to the terms of the revised BSD + * license. You should have received a copy of this license along + * with this source code in a file named "LICENSE." + * + * @file datagram_ext.I + * @author rdb + * @date 2018-08-19 + */ + +/** + * Returns the datagram's data as a bytes object. + */ +INLINE PyObject *Extension:: +get_message() const { + const char *data = (const char *)_this->get_data(); + size_t size = _this->get_length(); + +#if PY_MAJOR_VERSION >= 3 + return PyBytes_FromStringAndSize((char *)data, size); +#else + return PyString_FromStringAndSize((char *)data, size); +#endif +} + +/** + * Returns the datagram's data as a bytes object. + */ +PyObject *Extension:: +__bytes__() const { + return get_message(); +} diff --git a/panda/src/express/datagram_ext.h b/panda/src/express/datagram_ext.h new file mode 100644 index 0000000000..35f2e32bc6 --- /dev/null +++ b/panda/src/express/datagram_ext.h @@ -0,0 +1,40 @@ +/** + * PANDA 3D SOFTWARE + * Copyright (c) Carnegie Mellon University. All rights reserved. + * + * All use of this software is subject to the terms of the revised BSD + * license. You should have received a copy of this license along + * with this source code in a file named "LICENSE." + * + * @file datagram_ext.h + * @author rdb + * @date 2018-08-19 + */ + +#ifndef DATAGRAM_EXT_H +#define DATAGRAM_EXT_H + +#include "dtoolbase.h" + +#ifdef HAVE_PYTHON + +#include "extension.h" +#include "datagram.h" +#include "py_panda.h" + +/** + * This class defines the extension methods for Datagram, which are called + * instead of any C++ methods with the same prototype. + */ +template<> +class Extension : public ExtensionBase { +public: + INLINE PyObject *get_message() const; + INLINE PyObject *__bytes__() const; +}; + +#include "datagram_ext.I" + +#endif // HAVE_PYTHON + +#endif // DATAGRAM_EXT_H