diff options
author | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2023-09-01 08:41:36 +0200 |
---|---|---|
committer | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2023-09-05 11:17:14 +0200 |
commit | 45e39b3e69e370967be11a25d989e00d3bd54880 (patch) | |
tree | fadcb1facd4e9e5b7376f4ad75c9155986fe45e7 /sources/shiboken6/tests | |
parent | eceacdd605a10d282a9c3161430eeeaa6ef1b28f (diff) |
shiboken6: Add __int__, __float__
[ChangeLog][shiboken6] operator int() and/or operator double()
on classes are now used to provide the __int__ and/or __float__
special functions enabling the use of int() and float() for numerical
types.
Fixes: PYSIDE-2446
Change-Id: Iabb6392b5754d6e31d44209cfdd27d38c5055b2c
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Christian Tismer <tismer@stackless.com>
Diffstat (limited to 'sources/shiboken6/tests')
-rw-r--r-- | sources/shiboken6/tests/libsample/stdcomplex.cpp | 5 | ||||
-rw-r--r-- | sources/shiboken6/tests/libsample/stdcomplex.h | 3 | ||||
-rw-r--r-- | sources/shiboken6/tests/samplebinding/stdcomplex_test.py | 5 |
3 files changed, 13 insertions, 0 deletions
diff --git a/sources/shiboken6/tests/libsample/stdcomplex.cpp b/sources/shiboken6/tests/libsample/stdcomplex.cpp index c9937b9da..847174387 100644 --- a/sources/shiboken6/tests/libsample/stdcomplex.cpp +++ b/sources/shiboken6/tests/libsample/stdcomplex.cpp @@ -11,6 +11,11 @@ StdComplex::StdComplex(double re, double img) noexcept : m_impl(re, img) { } +StdComplex::operator int() const +{ + return std::lround(abs_value()); +} + StdComplex::StdComplex(const Impl &impl) noexcept : m_impl(impl) { } diff --git a/sources/shiboken6/tests/libsample/stdcomplex.h b/sources/shiboken6/tests/libsample/stdcomplex.h index 130ca6545..b39b80612 100644 --- a/sources/shiboken6/tests/libsample/stdcomplex.h +++ b/sources/shiboken6/tests/libsample/stdcomplex.h @@ -27,6 +27,9 @@ public: StdComplex pow(const StdComplex &exp) const; + operator double() const { return abs_value(); } + operator int() const; + friend inline bool operator==(const StdComplex &c1, const StdComplex &c2) noexcept { return c1.m_impl == c2.m_impl; } friend inline bool operator!=(const StdComplex &c1, const StdComplex &c2) noexcept diff --git a/sources/shiboken6/tests/samplebinding/stdcomplex_test.py b/sources/shiboken6/tests/samplebinding/stdcomplex_test.py index 539463508..0caa9764d 100644 --- a/sources/shiboken6/tests/samplebinding/stdcomplex_test.py +++ b/sources/shiboken6/tests/samplebinding/stdcomplex_test.py @@ -25,6 +25,11 @@ class StdComplexTest(unittest.TestCase): '''Test case for StdComplex class, exercising esoteric number protocols (Py_nb_). For standard number protocols, see Point.''' + def testConversion(self): + pt = StdComplex(REAL, IMAG) + self.assertEqual(int(pt), int(round(pt.abs_value()))) + self.assertEqual(float(pt), pt.abs_value()) + def testAbs(self): pt = StdComplex(REAL, IMAG) self.assertEqual(abs(pt), pt.abs_value()) |