我将C++代码包装在一个boost python模块中。
我的C++代码如下所示:
char s[2];
s[0] = (char) 160;
s[1] = '\0';
boost::python::str bs = boost::python::str(s);
即,它试图从包含不可打印字符(值160)的C字符串创建boost Python字符串。
当我从Python脚本运行这段代码时,我得到这个错误:
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xa0 in position 11: invalid start byte
其出现在值0xa0 (= 160)不能被解码时。
如何在C++代码中捕获此错误?
发布于 2021-01-20 20:34:41
如下所示:
boost::python::str bs;
try
{
bs = boost::python::str(s);
}
catch (const boost::python::::error_already_set&)
{
PyErr_Clear();
}
异常类型error_already_set
没有可用的信息-它以这种方式命名,因为这意味着错误是在Python解释器状态中设置的。
https://stackoverflow.com/questions/65809583
复制相似问题