gh-90329: Add _winapi.GetLongPathName and GetShortPathName and use in venv to reduce warnings (GH-117817)

This commit is contained in:
Steve Dower 2024-04-15 15:36:06 +01:00 committed by GitHub
parent 64cd6fc9a6
commit 185999bb3a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 328 additions and 3 deletions

View file

@ -1517,6 +1517,49 @@ _winapi_GetLastError_impl(PyObject *module)
return GetLastError();
}
/*[clinic input]
_winapi.GetLongPathName
path: LPCWSTR
Return the long version of the provided path.
If the path is already in its long form, returns the same value.
The path must already be a 'str'. If the type is not known, use
os.fsdecode before calling this function.
[clinic start generated code]*/
static PyObject *
_winapi_GetLongPathName_impl(PyObject *module, LPCWSTR path)
/*[clinic end generated code: output=c4774b080275a2d0 input=9872e211e3a4a88f]*/
{
DWORD cchBuffer;
PyObject *result = NULL;
Py_BEGIN_ALLOW_THREADS
cchBuffer = GetLongPathNameW(path, NULL, 0);
Py_END_ALLOW_THREADS
if (cchBuffer) {
WCHAR *buffer = (WCHAR *)PyMem_Malloc(cchBuffer * sizeof(WCHAR));
if (buffer) {
Py_BEGIN_ALLOW_THREADS
cchBuffer = GetLongPathNameW(path, buffer, cchBuffer);
Py_END_ALLOW_THREADS
if (cchBuffer) {
result = PyUnicode_FromWideChar(buffer, cchBuffer);
} else {
PyErr_SetFromWindowsErr(0);
}
PyMem_Free((void *)buffer);
}
} else {
PyErr_SetFromWindowsErr(0);
}
return result;
}
/*[clinic input]
_winapi.GetModuleFileName
@ -1551,6 +1594,48 @@ _winapi_GetModuleFileName_impl(PyObject *module, HMODULE module_handle)
return PyUnicode_FromWideChar(filename, wcslen(filename));
}
/*[clinic input]
_winapi.GetShortPathName
path: LPCWSTR
Return the short version of the provided path.
If the path is already in its short form, returns the same value.
The path must already be a 'str'. If the type is not known, use
os.fsdecode before calling this function.
[clinic start generated code]*/
static PyObject *
_winapi_GetShortPathName_impl(PyObject *module, LPCWSTR path)
/*[clinic end generated code: output=dab6ae494c621e81 input=43fa349aaf2ac718]*/
{
DWORD cchBuffer;
PyObject *result = NULL;
Py_BEGIN_ALLOW_THREADS
cchBuffer = GetShortPathNameW(path, NULL, 0);
Py_END_ALLOW_THREADS
if (cchBuffer) {
WCHAR *buffer = (WCHAR *)PyMem_Malloc(cchBuffer * sizeof(WCHAR));
if (buffer) {
Py_BEGIN_ALLOW_THREADS
cchBuffer = GetShortPathNameW(path, buffer, cchBuffer);
Py_END_ALLOW_THREADS
if (cchBuffer) {
result = PyUnicode_FromWideChar(buffer, cchBuffer);
} else {
PyErr_SetFromWindowsErr(0);
}
PyMem_Free((void *)buffer);
}
} else {
PyErr_SetFromWindowsErr(0);
}
return result;
}
/*[clinic input]
_winapi.GetStdHandle -> HANDLE
@ -2846,7 +2931,9 @@ static PyMethodDef winapi_functions[] = {
_WINAPI_GETCURRENTPROCESS_METHODDEF
_WINAPI_GETEXITCODEPROCESS_METHODDEF
_WINAPI_GETLASTERROR_METHODDEF
_WINAPI_GETLONGPATHNAME_METHODDEF
_WINAPI_GETMODULEFILENAME_METHODDEF
_WINAPI_GETSHORTPATHNAME_METHODDEF
_WINAPI_GETSTDHANDLE_METHODDEF
_WINAPI_GETVERSION_METHODDEF
_WINAPI_MAPVIEWOFFILE_METHODDEF

View file

@ -741,6 +741,76 @@ exit:
return return_value;
}
PyDoc_STRVAR(_winapi_GetLongPathName__doc__,
"GetLongPathName($module, /, path)\n"
"--\n"
"\n"
"Return the long version of the provided path.\n"
"\n"
"If the path is already in its long form, returns the same value.\n"
"\n"
"The path must already be a \'str\'. If the type is not known, use\n"
"os.fsdecode before calling this function.");
#define _WINAPI_GETLONGPATHNAME_METHODDEF \
{"GetLongPathName", _PyCFunction_CAST(_winapi_GetLongPathName), METH_FASTCALL|METH_KEYWORDS, _winapi_GetLongPathName__doc__},
static PyObject *
_winapi_GetLongPathName_impl(PyObject *module, LPCWSTR path);
static PyObject *
_winapi_GetLongPathName(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
{
PyObject *return_value = NULL;
#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
#define NUM_KEYWORDS 1
static struct {
PyGC_Head _this_is_not_used;
PyObject_VAR_HEAD
PyObject *ob_item[NUM_KEYWORDS];
} _kwtuple = {
.ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
.ob_item = { &_Py_ID(path), },
};
#undef NUM_KEYWORDS
#define KWTUPLE (&_kwtuple.ob_base.ob_base)
#else // !Py_BUILD_CORE
# define KWTUPLE NULL
#endif // !Py_BUILD_CORE
static const char * const _keywords[] = {"path", NULL};
static _PyArg_Parser _parser = {
.keywords = _keywords,
.fname = "GetLongPathName",
.kwtuple = KWTUPLE,
};
#undef KWTUPLE
PyObject *argsbuf[1];
LPCWSTR path = NULL;
args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
if (!args) {
goto exit;
}
if (!PyUnicode_Check(args[0])) {
_PyArg_BadArgument("GetLongPathName", "argument 'path'", "str", args[0]);
goto exit;
}
path = PyUnicode_AsWideCharString(args[0], NULL);
if (path == NULL) {
goto exit;
}
return_value = _winapi_GetLongPathName_impl(module, path);
exit:
/* Cleanup for path */
PyMem_Free((void *)path);
return return_value;
}
PyDoc_STRVAR(_winapi_GetModuleFileName__doc__,
"GetModuleFileName($module, module_handle, /)\n"
"--\n"
@ -775,6 +845,76 @@ exit:
return return_value;
}
PyDoc_STRVAR(_winapi_GetShortPathName__doc__,
"GetShortPathName($module, /, path)\n"
"--\n"
"\n"
"Return the short version of the provided path.\n"
"\n"
"If the path is already in its short form, returns the same value.\n"
"\n"
"The path must already be a \'str\'. If the type is not known, use\n"
"os.fsdecode before calling this function.");
#define _WINAPI_GETSHORTPATHNAME_METHODDEF \
{"GetShortPathName", _PyCFunction_CAST(_winapi_GetShortPathName), METH_FASTCALL|METH_KEYWORDS, _winapi_GetShortPathName__doc__},
static PyObject *
_winapi_GetShortPathName_impl(PyObject *module, LPCWSTR path);
static PyObject *
_winapi_GetShortPathName(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
{
PyObject *return_value = NULL;
#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
#define NUM_KEYWORDS 1
static struct {
PyGC_Head _this_is_not_used;
PyObject_VAR_HEAD
PyObject *ob_item[NUM_KEYWORDS];
} _kwtuple = {
.ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
.ob_item = { &_Py_ID(path), },
};
#undef NUM_KEYWORDS
#define KWTUPLE (&_kwtuple.ob_base.ob_base)
#else // !Py_BUILD_CORE
# define KWTUPLE NULL
#endif // !Py_BUILD_CORE
static const char * const _keywords[] = {"path", NULL};
static _PyArg_Parser _parser = {
.keywords = _keywords,
.fname = "GetShortPathName",
.kwtuple = KWTUPLE,
};
#undef KWTUPLE
PyObject *argsbuf[1];
LPCWSTR path = NULL;
args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
if (!args) {
goto exit;
}
if (!PyUnicode_Check(args[0])) {
_PyArg_BadArgument("GetShortPathName", "argument 'path'", "str", args[0]);
goto exit;
}
path = PyUnicode_AsWideCharString(args[0], NULL);
if (path == NULL) {
goto exit;
}
return_value = _winapi_GetShortPathName_impl(module, path);
exit:
/* Cleanup for path */
PyMem_Free((void *)path);
return return_value;
}
PyDoc_STRVAR(_winapi_GetStdHandle__doc__,
"GetStdHandle($module, std_handle, /)\n"
"--\n"
@ -1978,4 +2118,4 @@ exit:
return return_value;
}
/*[clinic end generated code: output=1f5bbcfa8d1847c5 input=a9049054013a1b77]*/
/*[clinic end generated code: output=ed94a2482ede3744 input=a9049054013a1b77]*/