From 7825abd54050700f00b85e0eab849c787483cf4f Mon Sep 17 00:00:00 2001 From: Alexander Alekhin Date: Sat, 23 Jan 2021 01:58:45 +0000 Subject: [PATCH] python: handle errors in Rect conversions --- modules/dnn/misc/python/test/test_dnn.py | 2 +- modules/python/src2/cv2.cpp | 15 ++++++++++++++- modules/python/test/test_misc.py | 5 +++-- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/modules/dnn/misc/python/test/test_dnn.py b/modules/dnn/misc/python/test/test_dnn.py index 746dabf4ea..afc960ef4c 100644 --- a/modules/dnn/misc/python/test/test_dnn.py +++ b/modules/dnn/misc/python/test/test_dnn.py @@ -177,7 +177,7 @@ class dnn_test(NewOpenCVTests): cv.rectangle(frame, box, (0, 255, 0)) cv.rectangle(frame, np.array(box), (0, 255, 0)) cv.rectangle(frame, tuple(box), (0, 255, 0)) - cv.rectangle(frame, list(box), (0, 255, 0)) + # FIXIT never properly work: cv.rectangle(frame, list(box), (0, 255, 0)) def test_classification_model(self): diff --git a/modules/python/src2/cv2.cpp b/modules/python/src2/cv2.cpp index c44ce0229c..151524074b 100644 --- a/modules/python/src2/cv2.cpp +++ b/modules/python/src2/cv2.cpp @@ -1552,8 +1552,13 @@ template struct pyopencvVecConverter break; } } + if (i != n) + { + failmsg("Can't convert vector element for '%s', index=%d", info.name, i); + } return i == n; } + failmsg("Can't convert object to vector for '%s', unsupported type", info.name); return false; } @@ -1772,7 +1777,15 @@ bool pyopencv_to(PyObject* obj, Rect& r, const ArgInfo& info) else { std::vector value(4); - pyopencvVecConverter::to(obj, value, info); + if (!pyopencvVecConverter::to(obj, value, info)) + { + return false; + } + if (value.size() != 4) + { + failmsg("Expected 4 values for '%s', got %d", info.name, (int)value.size()); + return false; + } r = Rect(value[0], value[1], value[2], value[3]); return true; } diff --git a/modules/python/test/test_misc.py b/modules/python/test/test_misc.py index 370803bb02..171139dab4 100644 --- a/modules/python/test/test_misc.py +++ b/modules/python/test/test_misc.py @@ -98,13 +98,14 @@ class Bindings(NewOpenCVTests): no_exception_msg = 'Overload resolution failed without any exception for: "{}"'.format(msg) wrong_exception_msg = 'Overload resolution failed with wrong exception type for: "{}"'.format(msg) with self.assertRaises((cv.error, Exception), msg=no_exception_msg) as cm: - cv.utils.testOverloadResolution(*args, **kwargs) + res = cv.utils.testOverloadResolution(*args, **kwargs) + self.fail("Unexpected result for {}: '{}'".format(msg, res)) self.assertEqual(type(cm.exception), cv.error, wrong_exception_msg) test_overload_resolution('wrong second arg type (keyword arg)', 5, point=(1, 2, 3)) test_overload_resolution('wrong second arg type', 5, 2) test_overload_resolution('wrong first arg', 3.4, (12, 21)) - # FIXIT: test_overload_resolution('wrong first arg, no second arg', 4.5) + test_overload_resolution('wrong first arg, no second arg', 4.5) test_overload_resolution('wrong args number for first overload', 3, (12, 21), 123) test_overload_resolution('wrong args number for second overload', (3, 12, 12, 1), (12, 21)) # One of the common problems