Skip to content

建议更新部分‘u'相关代码适应python3.12 #14

Description

@lkyxgh

waifu2x_py.cpp使用以下的代码:
PyArg_Parse(rel, "u", &path)
PyArg_ParseTuple(args, "|u", &modelPath)
PyArg_Parse(rel, "u#", &path)
由于python3.12已经完全弃'u',会造成编译的py312版本有bug。
经测试以下修改有用:
1、原来:
#if _WIN32
const wchar_t* path = NULL;
PyArg_Parse(rel, "u", &path);
改为:
#if _WIN32
const wchar_t* path = PyUnicode_AsWideCharString(rel, NULL);

2、原来:
#if _WIN32
const wchar_t* modelPath = 0;
if (!PyArg_ParseTuple(args, "|u", &modelPath))
Py_RETURN_NONE;
改为:
#if _WIN32
const wchar_t* modelPath = 0;
PyObject* pyModelPathObj = NULL; // 声明 PyObject 指针接收参数
if (!PyArg_ParseTuple(args, "|O", &pyModelPathObj)) // 使用 "O" 格式获取通用对象
Py_RETURN_NONE;

if (pyModelPathObj && pyModelPathObj != Py_None) {
    modelPath = PyUnicode_AsWideCharString(pyModelPathObj, NULL);  // 转换为 wchar_t*
    if (!modelPath) {
        Py_RETURN_NONE;
    }
}

3、原来:
#if _WIN32
const wchar_t* path = NULL;
PyArg_Parse(rel, "u#", &path);
改为:
#if _WIN32
PyObject* pyPathObj = NULL;
Py_ssize_t pathLength = 0;

    // 使用 "O" 格式获取通用对象,再通过 PyUnicode_AsWideCharString 显式转换
    if (!PyArg_Parse(rel, "O", &pyPathObj)) {
        // 错误处理
        return NULL;
    }

    const wchar_t* path = NULL;
    if (pyPathObj && pyPathObj != Py_None) {
        path = PyUnicode_AsWideCharString(pyPathObj, &pathLength);
        if (!path) {
            PyErr_SetString(PyExc_TypeError, "Failed to convert path to wide string");
            return NULL;
        }
    }

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions