10000 gh-134041: Avoid usage of unavailable windows path apis by maxbachmann · Pull Request #134042 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-134041: Avoid usage of unavailable windows path apis #134042

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 15, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
avoid unavailable win apis
  • Loading branch information
maxbachmann authored May 15, 2025
commit d36419b1e7781003130c768c003cc0d07dc4177d
13 changes: 13 additions & 0 deletions Modules/_winapi.c
66D1
Original file line number Diff line number Diff line change
Expand Up @@ -1573,6 +1573,7 @@ static PyObject *
_winapi_GetLongPathName_impl(PyObject *module, LPCWSTR path)
/*[clinic end generated code: output=c4774b080275a2d0 input=9872e211e3a4a88f]*/
{
#if defined(MS_WINDOWS_APP) || defined(MS_WINDOWS_SYSTEM)
DWORD cchBuffer;
PyObject *result = NULL;

Expand All @@ -1596,6 +1597,9 @@ _winapi_GetLongPathName_impl(PyObject *module, LPCWSTR path)
PyErr_SetFromWindowsErr(0);
}
return result;
#else
return PyUnicode_FromWideChar(path, wcslen(path));
#endif
}

/*[clinic input]
Expand Down Expand Up @@ -1649,6 +1653,7 @@ static PyObject *
_winapi_GetShortPathName_impl(PyObject *module, LPCWSTR path)
/*[clinic end generated code: output=dab6ae494c621e81 input=43fa349aaf2ac718]*/
{
#if defined(MS_WINDOWS_DESKTOP) || defined(MS_WINDOWS_SYSTEM)
DWORD cchBuffer;
PyObject *result = NULL;

Expand All @@ -1672,6 +1677,10 @@ _winapi_GetShortPathName_impl(PyObject *module, LPCWSTR path)
PyErr_SetFromWindowsErr(0);
}
return result;
#else
PyErr_SetString(PyExc_OSError, "GetShortPathName unavailable on this platform");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your call here, but if you put the ifdef around the entire function definition and regenerate the clinic headers (should be python Tools\clinic\clinic.py Modules\_winapi.c unless someone finally moved the clinic.py script) then it should entirely omit the function when you build.

Or maybe it'd make sense just to return the original string again? I'm not sure where this function gets used, but in general, unconditional errors are harder to detect than a missing function, and are less convenient than sensible-but-technically-incorrect behaviours. This is an internal module, so we only really need to consider stdlib uses, though of course we want to avoid breaking existing third-party code as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I can tell we don't use this anywhere

return NULL;
#endif
}

/*[clinic input]
Expand Down Expand Up @@ -2883,13 +2892,17 @@ _winapi_NeedCurrentDirectoryForExePath_impl(PyObject *module,
LPCWSTR exe_name)
/*[clinic end generated code: output=a65ec879502b58fc input=972aac88a1ec2f00]*/
{
#if defined(MS_WINDOWS_DESKTOP) || defined(MS_WINDOWS_SYSTEM)
BOOL result;

Py_BEGIN_ALLOW_THREADS
result = NeedCurrentDirectoryForExePathW(exe_name);
Py_END_ALLOW_THREADS

return result;
#else
return TRUE;
#endif
}


Expand Down
Loading
0