Thanks to visit codestin.com
Credit goes to github.com

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

Conversation

maxbachmann
Copy link
Contributor

@maxbachmann maxbachmann commented May 15, 2025

This doesn't contain a doc entry yet, since I wasn't sure about some of the changes yet:
Right now I have the following changes:

  • _winapi.GetLongPathName: I believe in the gaming partition you simply don't have short name support and so returning the path should be fine
  • _winapi.GetShortPathName: throws an exception as unsupported
  • _winapi.NeedCurrentDirectoryForExePath: I have absolutely no clue whether this should be True or False. Really I don't usually start different executables on these targets. Even if I do, I would always use a full path or have the exe placed next to my main executable. So really no clue whether this should: return True, False, throw an exception or be unavailable

@maxbachmann
Copy link
Contributor Author

maxbachmann commented May 15, 2025

@zooba I found some more cases that I was working around by explicitly linking the kernel32 lib (which shouldn't be done).

It's the three done in here and the new os.get_blocking/os.set_blocking on windows.
That one is also weird because it looks like GetNamedPipeHandleStateW is unavailable but SetNamedPipeHandleState is available 😕

@@ -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

@zooba
Copy link
Member

zooba commented May 15, 2025

That one is also weird because it looks like GetNamedPipeHandleStateW is unavailable but SetNamedPipeHandleState is available

IIUC, the game API functions are basically decided by what is requested, so I guess someone needed to set the state but not read it (which seems reasonable - you'd normally set the state to what you need rather than what's "best").

If they're needed by other internal functions, we might just need to get the Get one directly from the DLL (which is fine as long as we safely handle the case where we can't get it). If you prefer to just omit the entire function then that's fine by me.

@maxbachmann
Copy link
Contributor Author

If they're needed by other internal functions, we might just need to get the Get one directly from the DLL (which is fine as long as we safely handle the case where we can't get it). If you prefer to just omit the entire function then that's fine by me.

Would be fine with me as long as I know which dll to retrieve it from. In my original xbox change (https://github.com/python/cpython/pull/102256/files#diff-27e9033ac552bd8208213a2d5a0a294312e3918cd81c4da822ab32c382e392c4) we did the same for PathCchSkipRoot and PathCchCombineEx by retrieving it from api-ms-win-core-path-l1-1-0.dll.

@zooba
Copy link
Member

zooba commented May 15, 2025

_winapi.NeedCurrentDirectoryForExePath: I have absolutely no clue whether this should be True or False.

I think TRUE is probably better. I'm thinking of cases where you might launch a script that runs a few processes, and is expecting to find tools in the script directory (probably CWD, likely not the "application" directory where python.exe is). But because it only affects the stdlib it probably only matters for shutil.which, so low impact in any case.

@zooba
Copy link
Member

zooba commented May 15, 2025

Would be fine with me as long as I know which dll to retrieve it from

Docs say kernel32.dll. The path APIs are a bit special, as I don't think they were ever in kernel32 to begin with (they were advapi32 or shell32, IIRC), but really core stuff has always been accessible from kernel32.

It might also be in an API set, but that shouldn't matter. This API isn't going to change at any rate we care about, and if the dynamic load is limited to the non-desktop partitions than devs have a much better chance of being able to manage breakage than regular users on desktop.

@zooba
Copy link
Member

zooba commented May 15, 2025

Skipping NEWS because it's an internal module and won't affect normal releases.

@zooba zooba enabled auto-merge (squash) May 15, 2025 11:38
@zooba zooba merged commit 1c4b34c into python:main May 15, 2025
40 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants