dtoolutil: Let Python do Filename conversion to UTF-8

This commit is contained in:
rdb 2024-10-30 13:42:33 +01:00
parent 7560a1edd1
commit e58449cde7
1 changed files with 22 additions and 8 deletions

View File

@ -33,10 +33,17 @@ __init__(PyObject *path) {
Py_ssize_t length;
if (PyUnicode_CheckExact(path)) {
wchar_t *data;
data = PyUnicode_AsWideCharString(path, &length);
(*_this) = wstring(data, length);
PyMem_Free(data);
if (Filename::get_filesystem_encoding() == TextEncoder::E_utf8) {
const char *data = PyUnicode_AsUTF8AndSize(path, &length);
if (data != nullptr) {
(*_this) = string(data, length);
}
} else {
wchar_t *data;
data = PyUnicode_AsWideCharString(path, &length);
(*_this) = wstring(data, length);
PyMem_Free(data);
}
return;
}
@ -67,10 +74,17 @@ __init__(PyObject *path) {
}
if (PyUnicode_CheckExact(path_str)) {
wchar_t *data;
data = PyUnicode_AsWideCharString(path_str, &length);
(*_this) = Filename::from_os_specific_w(wstring(data, length));
PyMem_Free(data);
if (Filename::get_filesystem_encoding() == TextEncoder::E_utf8) {
const char *data = PyUnicode_AsUTF8AndSize(path_str, &length);
if (data != nullptr) {
(*_this) = Filename::from_os_specific(string(data, length));
}
} else {
wchar_t *data;
data = PyUnicode_AsWideCharString(path_str, &length);
(*_this) = Filename::from_os_specific_w(wstring(data, length));
PyMem_Free(data);
}
} else if (PyBytes_CheckExact(path_str)) {
char *data;