mirror of
https://github.com/panda3d/panda3d.git
synced 2025-09-30 16:58:40 -04:00
express: make Datagram.get_message() return bytes in Python 3
This is done using a Python extension function, which also happens to make the call more efficient as this avoids an extra copy. The C++ version still returns std::string as there is still a lot of C++ code that relies on that. Fixes #297
This commit is contained in:
parent
97d6d84ade
commit
ba345d590f
@ -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.
|
||||
*/
|
||||
|
@ -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);
|
||||
|
35
panda/src/express/datagram_ext.I
Normal file
35
panda/src/express/datagram_ext.I
Normal file
@ -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<Datagram>::
|
||||
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<Datagram>::
|
||||
__bytes__() const {
|
||||
return get_message();
|
||||
}
|
40
panda/src/express/datagram_ext.h
Normal file
40
panda/src/express/datagram_ext.h
Normal file
@ -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<Datagram> : public ExtensionBase<Datagram> {
|
||||
public:
|
||||
INLINE PyObject *get_message() const;
|
||||
INLINE PyObject *__bytes__() const;
|
||||
};
|
||||
|
||||
#include "datagram_ext.I"
|
||||
|
||||
#endif // HAVE_PYTHON
|
||||
|
||||
#endif // DATAGRAM_EXT_H
|
Loading…
x
Reference in New Issue
Block a user