如何查找PyImportModule导入失败的原因?

5 投票
1 回答
3644 浏览
提问于 2025-04-16 19:15

我在一个C语言的程序中嵌入了Python(版本2.7.1),代码如下:

{
PyObject *user_dict;
PyObject *user_func;
PyObject *result;
PyObject *header_tuple;
PyObject *original_recipients;
PyObject *working_recipients;

if (!Py_IsInitialized())
    {
    Py_Initialize();
    }

if (!expy_exim_dict)
    {
    PyObject *module = Py_InitModule(expy_exim_module, expy_exim_methods); /* Borrowed reference */
    Py_INCREF(module);                                 /* convert to New reference */
    expy_exim_dict = PyModule_GetDict(module);         /* Borrowed reference */
    Py_INCREF(expy_exim_dict);                         /* convert to New reference */
    }

if (!expy_user_module)
    {
    if (expy_path_add)
        {
        PyObject *sys_module;
        PyObject *sys_dict;
        PyObject *sys_path;
        PyObject *add_value;

        sys_module = PyImport_ImportModule("sys");  /* New Reference */
        if (!sys_module)
            {
            PyErr_Clear();
            *return_text = "Internal error, can't import Python sys module";
            log_write(0, LOG_REJECT, "Couldn't import Python 'sys' module");
            return PYTHON_FAILURE_RETURN;
            }

        sys_dict = PyModule_GetDict(sys_module);               /* Borrowed Reference, never fails */
        sys_path = PyMapping_GetItemString(sys_dict, "path");  /* New reference */

        if (!sys_path || (!PyList_Check(sys_path)))
            {
            PyErr_Clear();  /* in case sys_path was NULL, harmless otherwise */
            *return_text = "Internal error, sys.path doesn't exist or isn't a list";
            log_write(0, LOG_REJECT, "expy: Python sys.path doesn't exist or isn't a list");
            return PYTHON_FAILURE_RETURN;
            }

        add_value = PyString_FromString(expy_path_add);  /* New reference */
        if (!add_value)
            {
            PyErr_Clear();
            log_write(0, LOG_PANIC, "expy: Failed to create Python string from [%s]", expy_path_add);
            return PYTHON_FAILURE_RETURN;
            }

        if (PyList_Append(sys_path, add_value))
            {
            PyErr_Clear();
            log_write(0, LOG_PANIC, "expy: Failed to append [%s] to Python sys.path", expy_path_add);
            }

        Py_DECREF(add_value);
        Py_DECREF(sys_path);
        Py_DECREF(sys_module);
        }

    expy_user_module = PyImport_ImportModule(expy_scan_module);  /* New Reference */

    if (!expy_user_module)
        {
        PyErr_Clear();
        /* Handle error */
        }
    }

当使用PyImport_ImportModule导入模块失败时,它会返回NULL。那我该怎么知道它失败的原因是什么呢?(比如说,在嵌入的环境外面导入模块时是可以成功的)。

(这段代码是py-exim-localscan的一部分,我想在出现失败的少见情况下,增加一些关于失败原因的信息)。

1 个回答

6

你可以通过查看发生的异常来解决这个问题。目前你是把异常清除掉了(这就是PyErr_Clear()的作用)。不要这样做,而是应该打印出错误的追踪信息或者检查一下异常对象。想了解如何在C代码中做到这一点,可以查看这个链接,不过通常来说,最好的办法就是让异常继续传播下去。

撰写回答