Skip to content

Commit 8ebc645

Browse files
gongminminzooba
authored andcommitted
bpo-35890 : Fix some API calling consistency (pythonGH-11742)
Unicode version of Windows APIs are used in places, but not for GetVersionEx in Python/sysmodule.c The wcstok_s is called on Windows in Modules/main.c and PC/launcher.c, but not in Python/pathconfig.c
1 parent 4c70d9f commit 8ebc645

3 files changed

Lines changed: 13 additions & 6 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix API calling consistency of GetVersionEx and wcstok.

Python/pathconfig.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,12 @@ _PyPathConfig_ComputeArgv0(int argc, wchar_t **argv)
675675
}
676676

677677

678+
#ifdef MS_WINDOWS
679+
#define WCSTOK wcstok_s
680+
#else
681+
#define WCSTOK wcstok
682+
#endif
683+
678684
/* Search for a prefix value in an environment file (pyvenv.cfg).
679685
If found, copy it into the provided buffer. */
680686
int
@@ -705,11 +711,11 @@ _Py_FindEnvConfigValue(FILE *env_file, const wchar_t *key,
705711
wchar_t *tmpbuffer = _Py_DecodeUTF8_surrogateescape(buffer, n);
706712
if (tmpbuffer) {
707713
wchar_t * state;
708-
wchar_t * tok = wcstok(tmpbuffer, L" \t\r\n", &state);
714+
wchar_t * tok = WCSTOK(tmpbuffer, L" \t\r\n", &state);
709715
if ((tok != NULL) && !wcscmp(tok, key)) {
710-
tok = wcstok(NULL, L" \t", &state);
716+
tok = WCSTOK(NULL, L" \t", &state);
711717
if ((tok != NULL) && !wcscmp(tok, L"=")) {
712-
tok = wcstok(NULL, L"\r\n", &state);
718+
tok = WCSTOK(NULL, L"\r\n", &state);
713719
if (tok != NULL) {
714720
wcsncpy(value, tok, MAXPATHLEN);
715721
result = 1;

Python/sysmodule.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1126,15 +1126,15 @@ sys_getwindowsversion_impl(PyObject *module)
11261126
{
11271127
PyObject *version;
11281128
int pos = 0;
1129-
OSVERSIONINFOEX ver;
1129+
OSVERSIONINFOEXW ver;
11301130
DWORD realMajor, realMinor, realBuild;
11311131
HANDLE hKernel32;
11321132
wchar_t kernel32_path[MAX_PATH];
11331133
LPVOID verblock;
11341134
DWORD verblock_size;
11351135

11361136
ver.dwOSVersionInfoSize = sizeof(ver);
1137-
if (!GetVersionEx((OSVERSIONINFO*) &ver))
1137+
if (!GetVersionExW((OSVERSIONINFOW*) &ver))
11381138
return PyErr_SetFromWindowsErr(0);
11391139

11401140
version = PyStructSequence_New(&WindowsVersionType);
@@ -1145,7 +1145,7 @@ sys_getwindowsversion_impl(PyObject *module)
11451145
PyStructSequence_SET_ITEM(version, pos++, PyLong_FromLong(ver.dwMinorVersion));
11461146
PyStructSequence_SET_ITEM(version, pos++, PyLong_FromLong(ver.dwBuildNumber));
11471147
PyStructSequence_SET_ITEM(version, pos++, PyLong_FromLong(ver.dwPlatformId));
1148-
PyStructSequence_SET_ITEM(version, pos++, PyUnicode_FromString(ver.szCSDVersion));
1148+
PyStructSequence_SET_ITEM(version, pos++, PyUnicode_FromWideChar(ver.szCSDVersion, -1));
11491149
PyStructSequence_SET_ITEM(version, pos++, PyLong_FromLong(ver.wServicePackMajor));
11501150
PyStructSequence_SET_ITEM(version, pos++, PyLong_FromLong(ver.wServicePackMinor));
11511151
PyStructSequence_SET_ITEM(version, pos++, PyLong_FromLong(ver.wSuiteMask));

0 commit comments

Comments
 (0)