diff --git a/modules/python/src2/cv2.cpp b/modules/python/src2/cv2.cpp index ff57978dc5..67c4166799 100644 --- a/modules/python/src2/cv2.cpp +++ b/modules/python/src2/cv2.cpp @@ -1778,7 +1778,7 @@ private: const NPY_TYPES target_type = asNumpyType(); const int cols = DType::channels; - PyObject* array; + PyObject* array = NULL; if (cols == 1) { npy_intp dims = static_cast(value.size()); @@ -1796,14 +1796,16 @@ private: String shape; if (cols > 1) { - shape = cv::format("(%d x %d)", static_cast(value.size()), cols); + shape = format("(%d x %d)", static_cast(value.size()), cols); } else { - shape = cv::format("(%d)", static_cast(value.size())); + shape = format("(%d)", static_cast(value.size())); } - CV_Error_(Error::StsError, ("Can't allocate NumPy array for vector with dtype=%d shape=%s", - static_cast(target_type), static_cast(value.size()), shape.c_str())); + const String error_message = format("Can't allocate NumPy array for vector with dtype=%d and shape=%s", + static_cast(target_type), shape.c_str()); + emit_failmsg(PyExc_MemoryError, error_message.c_str()); + return array; } // Fill the array PyArrayObject* array_obj = reinterpret_cast(array);