python - 使用 Python C 扩展时如何修复 'UnicodeDecodeError: ' utf- 8' codec can' t decode byte'?

标签 python c++ c python-3.x encoding

给定以下文件 bug.txt:

event "øat" not handled

我在文件 fastfilewrapper.cpp 上编写了以下 Python C 扩展

#include <Python.h>
#include <cstdio>
#include <iostream>
#include <sstream>
#include <fstream>

static PyObject* hello_world(PyObject *self, PyObject *args) {
    printf("Hello, world!\n");
    std::string retval;
    std::ifstream fileifstream;

    fileifstream.open("./bug.txt");
    std::getline( fileifstream, retval );
    fileifstream.close();
    std::cout << "retval " << retval << std::endl;
    return Py_BuildValue( "s", retval.c_str() );
}

static PyMethodDef hello_methods[] = { {
        "hello_world", hello_world, METH_NOARGS,
        "Print 'hello world' from a method defined in a C extension."
    },
    {NULL, NULL, 0, NULL}
};

static struct PyModuleDef hello_definition = {
    PyModuleDef_HEAD_INIT,
    "hello", "A Python module that prints 'hello world' from C code.",
    -1, hello_methods
};

PyMODINIT_FUNC PyInit_fastfilepackage(void) {
    Py_Initialize();
    return PyModule_Create(&hello_definition);
}

我用 pip3 install 构建它。 使用这个 setup.py

from distutils.core import setup, Extension

# https://bugs.python.org/issue35893
from distutils.command import build_ext

def get_export_symbols(self, ext):
    parts = ext.name.split(".")
    if parts[-1] == "__init__":
        initfunc_name = "PyInit_" + parts[-2]
    else:
        initfunc_name = "PyInit_" + parts[-1]

build_ext.build_ext.get_export_symbols = get_export_symbols

setup(name='fastfilepackage', version='1.0',  \
      ext_modules=[Extension('fastfilepackage', ['fastfilewrapper.cpp'])])

然后,我使用这个 test.py 脚本:

import fastfilepackage

iterable = fastfilepackage.hello_world()
print('iterable', iterable)

但是当我运行 test.py Python 脚本时,Python 会抛出这个异常:

$ PYTHONIOENCODING=utf8 python3 test.py
Hello, world!
retval event "▒at" not handled
Traceback (most recent call last):
  File "test.py", line 3, in <module>
    iterable = fastfilepackage.hello_world()
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xf8 in position 7: invalid start byte

如何从无效的 Unicode 字符中恢复?

即,在绑定(bind) C 和 Python 时忽略这些错误。

当纯粹使用 Python 时,我可以使用这个:

file_in = open( './bug.txt', errors='replace' )
line = file_in.read()
print( "The input line was: {line}".format(line=line) )

Python C 扩展 绑定(bind)时,errors='replace' 的等效项是什么?

最佳答案

如果你想要“替换”错误处理语义,你应该像这样在 C 端执行它并将它返回到 python 端:

return PyUnicode_DecodeUTF8(retval.c_str(), retval.size(), "replace");

在我们的案例中,这将给出类似的东西:

Hello, world!
retval event "?at" not handled
iterable event "�at" not handled

关于python - 使用 Python C 扩展时如何修复 'UnicodeDecodeError: ' utf- 8' codec can' t decode byte'?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56204956/

相关文章:

c - 如何通过C编写的cgi-bin程序检索表单 "POST"数据

python加载在不同文件夹中找到的相同包结构的模块

c++ - Makefile 模式规则差异

python - 如何让 tkinter 显示这些 un​​icode 字符?

c++ - decltype(constexpr 变量)

c++ - 使用 Bind 创建处理程序函数以处理单击的菜单项 - wxWidgets 3.0

c - 在 C 中通过引用传递二维数组

c - 双字段的 calloc() 是否总是计算为 0.0?

python - 如何对列值在一定范围内的两个数据框进行外部合并?

python - 为什么在 Python OOP 中使用 Getters/Accessors 是(或不是)好的做法?