mirror of
https://github.com/opencv/opencv.git
synced 2026-12-02 13:16:32 -06:00
5.x port of #31296 (merged to 4.x), requested there. It is a clean cherry-pick of the same change. A Python str containing a lone surrogate, such as 'a\udcff', crashes the interpreter when it is passed to bindings that take a string, for example cv.putText, cv.imread or cv.imwrite. Python produces such strings for non-UTF-8 filenames on POSIX. getUnicodeString() in modules/python/src2/pycompat.hpp passed the NULL that PyUnicode_AsUTF8String() returns on failure to PyBytes_Check(). This PR adds the NULL check, so the pending UnicodeEncodeError is reported instead. Difference from 4.x: cv.getTextSize raises cv.error here, because 5.x has another overload; 4.x raises UnicodeEncodeError.