aboutsummaryrefslogtreecommitdiffstats
path: root/sources/pyside6
diff options
context:
space:
mode:
authorFriedemann Kleint <Friedemann.Kleint@qt.io>2022-04-26 15:05:53 +0200
committerQt Cherry-pick Bot <cherrypick_bot@qt-project.org>2022-04-26 19:46:04 +0000
commitb69dd9e28d8aa729e854e9e7cedbef7d62080e9b (patch)
tree408421096d8074a67b447740fecab8f39987a5a4 /sources/pyside6
parent5fea94774123ba2cec71e29a7c93dcd2edfb7ab5 (diff)
libpyside: Add a debug operator for numpy arrays
Task-number: PYSIDE-1880 Change-Id: Ifa0f0cd2a170d75def73264d97c6a4d9cf9d34b3 Reviewed-by: Christian Tismer <tismer@stackless.com> (cherry picked from commit 8a4a6f3aec1b3f99ee902073fe8b0b3cecb91f3e) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
Diffstat (limited to 'sources/pyside6')
-rw-r--r--sources/pyside6/libpyside/pyside_numpy.cpp97
-rw-r--r--sources/pyside6/libpyside/pyside_numpy.h10
2 files changed, 107 insertions, 0 deletions
diff --git a/sources/pyside6/libpyside/pyside_numpy.cpp b/sources/pyside6/libpyside/pyside_numpy.cpp
index 344137b4c..460c8080f 100644
--- a/sources/pyside6/libpyside/pyside_numpy.cpp
+++ b/sources/pyside6/libpyside/pyside_numpy.cpp
@@ -42,6 +42,7 @@
// Include numpy first to get the proper PyArray_Check
# include <numpy/arrayobject.h>
# include "pyside_numpy.h"
+# include <QtCore/QDebug>
// Convert X,Y of type T data to a list of points (QPoint, PointF)
template <class T, class Point>
@@ -157,10 +158,100 @@ QList<QPoint> xyDataToQPointList(PyObject *pyXIn, PyObject *pyYIn)
return {};
}
+template <class T>
+static void debugArray(QDebug debug, const T *data, int n)
+{
+ static const int maxData = 10;
+ debug << " = ";
+ auto *end = data + qMin(n, maxData);
+ for (auto *d = data; d != end; ++d) {
+ if (d != data)
+ debug << ", ";
+ debug << *d;
+ }
+ if (n > maxData)
+ debug << "...";
+}
+
+QDebug operator<<(QDebug debug, const debugPyArrayObject &a)
+{
+ QDebugStateSaver saver(debug);
+ debug.noquote();
+ debug.nospace();
+
+ debug << "PyArrayObject(";
+ if (a.m_object == nullptr) {
+ debug << '0';
+ } else if (PyArray_Check(a.m_object) != 0) {
+ auto *ar = reinterpret_cast<PyArrayObject *>(a.m_object);
+ const int ndim = PyArray_NDIM(ar);
+ const int type = PyArray_TYPE(ar);
+ const int flags = PyArray_FLAGS(ar);
+ debug << "ndim=" << ndim << " [";
+ for (int d = 0; d < ndim; ++d) {
+ if (d)
+ debug << ", ";
+ debug << PyArray_DIMS(ar)[d];
+ }
+ debug << "], type=";
+ switch (type) {
+ case NPY_INT:
+ debug << "int";
+ break;
+ case NPY_UINT:
+ debug << "uint";
+ break;
+ case NPY_FLOAT:
+ debug << "float";
+ break;
+ case NPY_DOUBLE:
+ debug << "double";
+ break;
+ default:
+ debug << '(' << type << ')';
+ break;
+ }
+ debug << ", flags=0x" << Qt::hex << flags << Qt::dec;
+ if ((flags & NPY_ARRAY_C_CONTIGUOUS) != 0)
+ debug << " [C-contiguous]";
+ if ((flags & NPY_ARRAY_F_CONTIGUOUS) != 0)
+ debug << " [Fortran-contiguous]";
+ if ((flags & NPY_ARRAY_ALIGNED) != 0)
+ debug << " [aligned]";
+ if ((flags & NPY_ARRAY_OWNDATA) != 0)
+ debug << " [owndata]";
+ if ((flags & NPY_ARRAY_WRITEABLE) != 0)
+ debug << " [writeable]";
+
+ if (const int dim0 = PyArray_DIMS(ar)[0]) {
+ auto *data = PyArray_DATA(ar);
+ switch (type) {
+ case NPY_INT:
+ debugArray(debug, reinterpret_cast<const int *>(data), dim0);
+ break;
+ case NPY_UINT:
+ debugArray(debug, reinterpret_cast<const unsigned *>(data), dim0);
+ break;
+ case NPY_FLOAT:
+ debugArray(debug, reinterpret_cast<const float *>(data), dim0);
+ break;
+ case NPY_DOUBLE:
+ debugArray(debug, reinterpret_cast<const double *>(data), dim0);
+ break;
+ }
+ }
+ } else {
+ debug << "Invalid";
+ }
+ debug << ')';
+ return debug;
+}
+
} //namespace PySide::Numpy
#else // HAVE_NUMPY
# include "pyside_numpy.h"
+# include <QtCore/QDebug>
namespace PySide::Numpy
{
@@ -186,6 +277,12 @@ QList<QPoint> xyDataToQPointList(PyObject *, PyObject *)
return {};
}
+QDebug operator<<(QDebug debug, const debugPyArrayObject &)
+{
+ debug << "Unimplemented function " << __FUNCTION__ << ", (numpy was not found).";
+ return debug;
+}
+
} //namespace PySide::Numpy
#endif // !HAVE_NUMPY
diff --git a/sources/pyside6/libpyside/pyside_numpy.h b/sources/pyside6/libpyside/pyside_numpy.h
index 158865c1d..689d64cfa 100644
--- a/sources/pyside6/libpyside/pyside_numpy.h
+++ b/sources/pyside6/libpyside/pyside_numpy.h
@@ -48,6 +48,8 @@
#include <QtCore/QPoint>
#include <QtCore/QPointF>
+QT_FORWARD_DECLARE_CLASS(QDebug)
+
// This header provides a PyArray_Check() definition that can be used to avoid
// having to include the numpy headers. When using numpy headers, make sure
// to include this header after them to skip the definition. Also remember
@@ -79,6 +81,14 @@ PYSIDE_API QList<QPointF> xyDataToQPointFList(PyObject *pyXIn, PyObject *pyYIn);
PYSIDE_API QList<QPoint> xyDataToQPointList(PyObject *pyXIn, PyObject *pyYIn);
+struct debugPyArrayObject
+{
+ explicit debugPyArrayObject(PyObject *object) : m_object(object) {}
+
+ PyObject *m_object;
+};
+
+PYSIDE_API QDebug operator<<(QDebug debug, const debugPyArrayObject &a);
} //namespace PySide::Numpy