mirror of
https://github.com/opencv/opencv.git
synced 2025-06-07 17:44:04 +08:00
Merge pull request #19137 from VadimLevin:dev/vlevin/safe-string-conversion
This commit is contained in:
commit
b2ea15da35
@ -58,6 +58,12 @@ String dumpCString(const char* argument)
|
|||||||
return cv::format("String: %s", argument);
|
return cv::format("String: %s", argument);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
CV_WRAP static inline
|
||||||
|
String dumpString(const String& argument)
|
||||||
|
{
|
||||||
|
return cv::format("String: %s", argument.c_str());
|
||||||
|
}
|
||||||
|
|
||||||
CV_WRAP static inline
|
CV_WRAP static inline
|
||||||
AsyncArray testAsyncArray(InputArray argument)
|
AsyncArray testAsyncArray(InputArray argument)
|
||||||
{
|
{
|
||||||
|
@ -963,15 +963,31 @@ PyObject* pyopencv_from(const std::string& value)
|
|||||||
template<>
|
template<>
|
||||||
bool pyopencv_to(PyObject* obj, String &value, const ArgInfo& info)
|
bool pyopencv_to(PyObject* obj, String &value, const ArgInfo& info)
|
||||||
{
|
{
|
||||||
CV_UNUSED(info);
|
|
||||||
if(!obj || obj == Py_None)
|
if(!obj || obj == Py_None)
|
||||||
|
{
|
||||||
return true;
|
return true;
|
||||||
|
}
|
||||||
std::string str;
|
std::string str;
|
||||||
if (getUnicodeString(obj, str))
|
if (getUnicodeString(obj, str))
|
||||||
{
|
{
|
||||||
value = str;
|
value = str;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// If error hasn't been already set by Python conversion functions
|
||||||
|
if (!PyErr_Occurred())
|
||||||
|
{
|
||||||
|
// Direct access to underlying slots of PyObjectType is not allowed
|
||||||
|
// when limited API is enabled
|
||||||
|
#ifdef Py_LIMITED_API
|
||||||
|
failmsg("Can't convert object to 'str' for '%s'", info.name);
|
||||||
|
#else
|
||||||
|
failmsg("Can't convert object of type '%s' to 'str' for '%s'",
|
||||||
|
obj->ob_type->tp_name, info.name);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -314,7 +314,7 @@ class Arguments(NewOpenCVTests):
|
|||||||
|
|
||||||
def test_parse_to_cstring_convertible(self):
|
def test_parse_to_cstring_convertible(self):
|
||||||
try_to_convert = partial(self._try_to_convert, cv.utils.dumpCString)
|
try_to_convert = partial(self._try_to_convert, cv.utils.dumpCString)
|
||||||
for convertible in ('s', 'str', str(123), ('char'), np.str('test1'), np.str_('test2')):
|
for convertible in ('', 's', 'str', str(123), ('char'), np.str('test1'), np.str_('test2')):
|
||||||
expected = 'string: ' + convertible
|
expected = 'string: ' + convertible
|
||||||
actual = try_to_convert(convertible)
|
actual = try_to_convert(convertible)
|
||||||
self.assertEqual(expected, actual,
|
self.assertEqual(expected, actual,
|
||||||
@ -326,6 +326,20 @@ class Arguments(NewOpenCVTests):
|
|||||||
with self.assertRaises((TypeError), msg=get_no_exception_msg(not_convertible)):
|
with self.assertRaises((TypeError), msg=get_no_exception_msg(not_convertible)):
|
||||||
_ = cv.utils.dumpCString(not_convertible)
|
_ = cv.utils.dumpCString(not_convertible)
|
||||||
|
|
||||||
|
def test_parse_to_string_convertible(self):
|
||||||
|
try_to_convert = partial(self._try_to_convert, cv.utils.dumpString)
|
||||||
|
for convertible in (None, '', 's', 'str', str(123), np.str('test1'), np.str_('test2')):
|
||||||
|
expected = 'string: ' + (convertible if convertible else '')
|
||||||
|
actual = try_to_convert(convertible)
|
||||||
|
self.assertEqual(expected, actual,
|
||||||
|
msg=get_conversion_error_msg(convertible, expected, actual))
|
||||||
|
|
||||||
|
def test_parse_to_string_not_convertible(self):
|
||||||
|
for not_convertible in ((12,), ('t', 'e', 's', 't'), np.array(['123', ]),
|
||||||
|
np.array(['t', 'e', 's', 't']), 1, True, False):
|
||||||
|
with self.assertRaises((TypeError), msg=get_no_exception_msg(not_convertible)):
|
||||||
|
_ = cv.utils.dumpString(not_convertible)
|
||||||
|
|
||||||
|
|
||||||
class SamplesFindFile(NewOpenCVTests):
|
class SamplesFindFile(NewOpenCVTests):
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user