【发布时间】:2019-10-22 06:44:58
【问题描述】:
给定一个带有无效 UTF8 的文件 /myfiles/file_with_invalid_encoding.txt:
parse this correctly
Føö»BÃ¥r
also parse this correctly
我正在使用 C API 中的内置 Python open 函数,如下最小示例(不包括 C Python 设置样板):
const char* filepath = "/myfiles/file_with_invalid_encoding.txt";
PyObject* iomodule = PyImport_ImportModule( "builtins" );
if( iomodule == NULL ) {
PyErr_PrintEx(100); return;
}
PyObject* openfunction = PyObject_GetAttrString( iomodule, "open" );
if( openfunction == NULL ) {
PyErr_PrintEx(100); return;
}
PyObject* openfile = PyObject_CallFunction( openfunction,
"s", filepath, "s", "r", "i", -1, "s", "UTF8", "s", "ignore" );
if( openfile == NULL ) {
PyErr_PrintEx(100); return;
}
PyObject* iterfunction = PyObject_GetAttrString( openfile, "__iter__" );
Py_DECREF( openfunction );
if( iterfunction == NULL ) {
PyErr_PrintEx(100); return;
}
PyObject* openfileresult = PyObject_CallObject( iterfunction, NULL );
Py_DECREF( iterfunction );
if( openfileresult == NULL ) {
PyErr_PrintEx(100); return;
}
PyObject* fileiterator = PyObject_GetAttrString( openfile, "__next__" );
Py_DECREF( openfileresult );
if( fileiterator == NULL ) {
PyErr_PrintEx(100); return;
}
PyObject* readline;
std::cout << "Here 1!" << std::endl;
while( ( readline = PyObject_CallObject( fileiterator, NULL ) ) != NULL ) {
std::cout << "Here 2!" << std::endl;
std::cout << PyUnicode_AsUTF8( readline ) << std::endl;
Py_DECREF( readline );
}
PyErr_PrintEx(100);
PyErr_Clear();
PyObject* closefunction = PyObject_GetAttrString( openfile, "close" );
if( closefunction == NULL ) {
PyErr_PrintEx(100); return;
}
PyObject* closefileresult = PyObject_CallObject( closefunction, NULL );
Py_DECREF( closefunction );
if( closefileresult == NULL ) {
PyErr_PrintEx(100); return;
}
Py_XDECREF( closefileresult );
Py_XDECREF( iomodule );
Py_XDECREF( openfile );
Py_XDECREF( fileiterator );
我正在调用 open 函数传递 ignore 参数以忽略编码错误,但 Python 忽略我并在发现无效 UTF8 字符时不断抛出编码异常:
Here 1!
Traceback (most recent call last):
File "/usr/lib/python3.6/codecs.py", line 321, in decode
(result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xbb in position 26: invalid start byte
正如你在上面和下面看到的,当我调用builtins.open() 函数时,我传递了ignore 参数,但它没有任何效果。我也尝试将ignore 更改为replace,但 C Python 始终抛出异常:
PyObject* openfile = PyObject_CallFunction( openfunction,
"s", filepath, "s", "r", "i", -1, "s", "UTF8", "s", "ignore" );
【问题讨论】:
-
我不确定这是您的only问题,但是在您通过调用
openfunction设置openfile的初始值之后,它似乎是错误的,它是后者的值,而不是前者,您测试为 null。 -
谢谢,我修复了它并重新测试了程序。但是编码问题仍然存在。我还添加了
"Here 1!"和"Here 2!",运行它时,只有Here 1!出现在堆栈跟踪之前。
标签: python c cpython python-c-api