diff options
author | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2023-05-11 09:21:37 +0200 |
---|---|---|
committer | Qt Cherry-pick Bot <cherrypick_bot@qt-project.org> | 2023-05-11 16:37:28 +0000 |
commit | 08164fff6d6269b01a5d5a80dc3ffd23d56b626c (patch) | |
tree | 016afb4a4df0ee24042a655f10dd8d959efb425c | |
parent | 58ec2ac3d01a8f01a951699ae7b1d2a64ffef845 (diff) |
Numpy support: Fix 64bit support and compiler warning about potentially uninitialized value
The size for long long was incorrect. Fix it and expand the test to
check values as well.
For really bizarre long types, the variable might be uninitialized,
causing
shiboken6/libshiboken/sbknumpyview.cpp:82:12: warning: type may be
used uninitialized in this function [-Wmaybe-uninitialized]
Fix by introducing a helper returning a std::optional.
Amends 499832abfdf13eac5aa35f84a62166fb5aa2e034.
Task-number: PYSIDE-2313
Change-Id: Ie7d22a728a42f644fa84cba811c4e35e7db7ebb5
Reviewed-by: Adrian Herrmann <adrian.herrmann@qt.io>
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Christian Tismer <tismer@stackless.com>
(cherry picked from commit b1d1161a814038adfe8b0a0dac24f101cf1e1db3)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
-rw-r--r-- | sources/pyside6/tests/QtCharts/qcharts_numpy_test.py | 12 | ||||
-rw-r--r-- | sources/shiboken6/libshiboken/sbknumpyview.cpp | 78 |
2 files changed, 49 insertions, 41 deletions
diff --git a/sources/pyside6/tests/QtCharts/qcharts_numpy_test.py b/sources/pyside6/tests/QtCharts/qcharts_numpy_test.py index 044fab34c..8154020c0 100644 --- a/sources/pyside6/tests/QtCharts/qcharts_numpy_test.py +++ b/sources/pyside6/tests/QtCharts/qcharts_numpy_test.py @@ -33,10 +33,16 @@ class QChartsNumpyTestCase(UsesQApplication): data_types = [np.short, np.ushort, np.int32, np.uint32, np.int64, np.uint64, np.float32, np.float64] for dt in data_types: + print("Testing ", dt) old_size = line_series.count() - arr = np.array([2], dtype=dt) - line_series.appendNp(arr, arr) - self.assertEqual(line_series.count(), old_size + 1) + x_arr = np.array([2], dtype=dt) + y_arr = np.array([3], dtype=dt) + line_series.appendNp(x_arr, y_arr) + size = line_series.count() + self.assertEqual(size, old_size + 1) + point = line_series.points()[size - 1] + self.assertEqual(point.x(), 2) + self.assertEqual(point.y(), 3) if __name__ == '__main__': diff --git a/sources/shiboken6/libshiboken/sbknumpyview.cpp b/sources/shiboken6/libshiboken/sbknumpyview.cpp index e83a5bda7..bafbf8038 100644 --- a/sources/shiboken6/libshiboken/sbknumpyview.cpp +++ b/sources/shiboken6/libshiboken/sbknumpyview.cpp @@ -6,70 +6,72 @@ #include "helper.h" #include <iostream> #include <iomanip> +#include <optional> #ifdef HAVE_NUMPY namespace Shiboken { namespace Numpy { -View View::fromPyObject(PyObject *pyIn) +static std::optional<View::Type> viewTypeFromNumPy(int npt) { - if (pyIn == nullptr || PyArray_Check(pyIn) == 0) - return {}; - auto *ar = reinterpret_cast<PyArrayObject *>(pyIn); - if ((PyArray_FLAGS(ar) & NPY_ARRAY_C_CONTIGUOUS) == 0) - return {}; - const int ndim = PyArray_NDIM(ar); - if (ndim > 2) - return {}; - - View::Type type; - switch (PyArray_TYPE(ar)) { + switch (npt) { case NPY_SHORT: - type = View::Int16; - break; + return View::Int16; case NPY_USHORT: - type = View::Unsigned16; - break; + return View::Unsigned16; case NPY_INT: - type = View::Int; - break; + return View::Int; case NPY_UINT: - type = View::Unsigned; - break; + return View::Unsigned; case NPY_LONG: - if constexpr (sizeof(long) == sizeof(int)) - type = View::Int; - else if constexpr (sizeof(long) == sizeof(int64_t)) - type = View::Int64; + if constexpr (sizeof(long) == sizeof(int)) + return View::Int; + if constexpr (sizeof(long) == sizeof(int64_t)) + return View::Int64; break; case NPY_ULONG: - if constexpr (sizeof(long) == sizeof(int)) - type = View::Unsigned; - else if constexpr (sizeof(long) == sizeof(int64_t)) - type = View::Unsigned64; + if constexpr (sizeof(long) == sizeof(int)) + return View::Unsigned; + if constexpr (sizeof(long) == sizeof(int64_t)) + return View::Unsigned64; break; case NPY_LONGLONG: - if constexpr (sizeof(long long) == 64) - type = View::Int64; + if constexpr (sizeof(long long) == 8) + return View::Int64; break; case NPY_ULONGLONG: - if constexpr (sizeof(long long) == 64) - type = View::Unsigned64; + if constexpr (sizeof(long long) == 8) + return View::Unsigned64; break; case NPY_FLOAT: - type = View::Float; - break; + return View::Float; case NPY_DOUBLE: - type = View::Double; - break; + return View::Double; default: - return {}; + break; } + return {}; +} + +View View::fromPyObject(PyObject *pyIn) +{ + if (pyIn == nullptr || PyArray_Check(pyIn) == 0) + return {}; + auto *ar = reinterpret_cast<PyArrayObject *>(pyIn); + if ((PyArray_FLAGS(ar) & NPY_ARRAY_C_CONTIGUOUS) == 0) + return {}; + const int ndim = PyArray_NDIM(ar); + if (ndim > 2) + return {}; + + const auto typeO = viewTypeFromNumPy(PyArray_TYPE(ar)); + if (!typeO.has_value()) + return {}; View result; result.ndim = ndim; - result.type = type; + result.type = typeO.value(); result.data = PyArray_DATA(ar); result.dimensions[0] = PyArray_DIMS(ar)[0]; result.stride[0] = PyArray_STRIDES(ar)[0]; |