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

Skip to content

Commit ac0ad88

Browse files
committed
Issue #17931: Resolve confusion on Windows between pids and process handles.
1 parent 448f1a8 commit ac0ad88

5 files changed

Lines changed: 29 additions & 23 deletions

File tree

Include/longobject.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,19 @@ PyAPI_FUNC(PyObject *) PyLong_GetInfo(void);
5252
#error "sizeof(pid_t) is neither sizeof(int), sizeof(long) or sizeof(long long)"
5353
#endif /* SIZEOF_PID_T */
5454

55+
#if SIZEOF_VOID_P == SIZEOF_INT
56+
# define _Py_PARSE_INTPTR "i"
57+
# define _Py_PARSE_UINTPTR "I"
58+
#elif SIZEOF_VOID_P == SIZEOF_LONG
59+
# define _Py_PARSE_INTPTR "l"
60+
# define _Py_PARSE_UINTPTR "k"
61+
#elif defined(SIZEOF_LONG_LONG) && SIZEOF_VOID_P == SIZEOF_LONG_LONG
62+
# define _Py_PARSE_INTPTR "L"
63+
# define _Py_PARSE_UINTPTR "K"
64+
#else
65+
# error "void* different in size from int, long and long long"
66+
#endif /* SIZEOF_VOID_P */
67+
5568
/* Used by Python/mystrtoul.c. */
5669
#ifndef Py_LIMITED_API
5770
PyAPI_DATA(unsigned char) _PyLong_DigitValue[256];

Misc/NEWS

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@ What's New in Python 3.4.0 Alpha 1?
1010
Core and Builtins
1111
-----------------
1212

13-
- Issue #17931: Fix PyLong_FromPid() on Windows 64-bit: processes are
14-
identified by their HANDLE which is a pointer (and not a long, which is
15-
smaller).
13+
- Issue #17931: Resolve confusion on Windows between pids and process
14+
handles.
1615

1716
- Tweak the exception message when the magic number or size value in a bytecode
1817
file is truncated.

Modules/posixmodule.c

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5014,11 +5014,7 @@ posix_spawnv(PyObject *self, PyObject *args)
50145014
if (spawnval == -1)
50155015
return posix_error();
50165016
else
5017-
#if SIZEOF_LONG == SIZEOF_VOID_P
5018-
return Py_BuildValue("l", (long) spawnval);
5019-
#else
5020-
return Py_BuildValue("L", (PY_LONG_LONG) spawnval);
5021-
#endif
5017+
return Py_BuildValue(_Py_PARSE_INTPTR, spawnval);
50225018
}
50235019

50245020

@@ -5104,11 +5100,7 @@ posix_spawnve(PyObject *self, PyObject *args)
51045100
if (spawnval == -1)
51055101
(void) posix_error();
51065102
else
5107-
#if SIZEOF_LONG == SIZEOF_VOID_P
5108-
res = Py_BuildValue("l", (long) spawnval);
5109-
#else
5110-
res = Py_BuildValue("L", (PY_LONG_LONG) spawnval);
5111-
#endif
5103+
res = Py_BuildValue(_Py_PARSE_INTPTR, spawnval);
51125104

51135105
while (--envc >= 0)
51145106
PyMem_DEL(envlist[envc]);
@@ -6178,16 +6170,17 @@ static PyObject *
61786170
win32_kill(PyObject *self, PyObject *args)
61796171
{
61806172
PyObject *result;
6181-
DWORD pid, sig, err;
6173+
pid_t pid;
6174+
DWORD sig, err;
61826175
HANDLE handle;
61836176

6184-
if (!PyArg_ParseTuple(args, "kk:kill", &pid, &sig))
6177+
if (!PyArg_ParseTuple(args, _Py_PARSE_PID "k:kill", &pid, &sig))
61856178
return NULL;
61866179

61876180
/* Console processes which share a common console can be sent CTRL+C or
61886181
CTRL+BREAK events, provided they handle said events. */
61896182
if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) {
6190-
if (GenerateConsoleCtrlEvent(sig, pid) == 0) {
6183+
if (GenerateConsoleCtrlEvent(sig, (DWORD)pid) == 0) {
61916184
err = GetLastError();
61926185
PyErr_SetFromWindowsErr(err);
61936186
}
@@ -6197,7 +6190,7 @@ win32_kill(PyObject *self, PyObject *args)
61976190

61986191
/* If the signal is outside of what GenerateConsoleCtrlEvent can use,
61996192
attempt to open and terminate the process. */
6200-
handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
6193+
handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, (DWORD)pid);
62016194
if (handle == NULL) {
62026195
err = GetLastError();
62036196
return PyErr_SetFromWindowsErr(err);
@@ -6603,7 +6596,7 @@ posix_waitpid(PyObject *self, PyObject *args)
66036596
Py_intptr_t pid;
66046597
int status, options;
66056598

6606-
if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options))
6599+
if (!PyArg_ParseTuple(args, _Py_PARSE_INTPTR "i:waitpid", &pid, &options))
66076600
return NULL;
66086601
Py_BEGIN_ALLOW_THREADS
66096602
pid = _cwait(&status, pid, options);
@@ -6612,7 +6605,7 @@ posix_waitpid(PyObject *self, PyObject *args)
66126605
return posix_error();
66136606

66146607
/* shift the status left a byte so this is more like the POSIX waitpid */
6615-
return Py_BuildValue("Ni", PyLong_FromPid(pid), status << 8);
6608+
return Py_BuildValue(_Py_PARSE_INTPTR "i", pid, status << 8);
66166609
}
66176610
#endif /* HAVE_WAITPID || HAVE_CWAIT */
66186611

PC/msvcrtmodule.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,12 @@ os.O_BINARY.");
113113
static PyObject *
114114
msvcrt_open_osfhandle(PyObject *self, PyObject *args)
115115
{
116-
long handle;
116+
Py_intptr_t handle;
117117
int flags;
118118
int fd;
119119

120-
if (!PyArg_ParseTuple(args, "li:open_osfhandle", &handle, &flags))
120+
if (!PyArg_ParseTuple(args, _Py_PARSE_INTPTR "i:open_osfhandle",
121+
&handle, &flags))
121122
return NULL;
122123

123124
fd = _open_osfhandle(handle, flags);

PC/pyconfig.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -723,8 +723,8 @@ Py_NO_ENABLE_SHARED to find out. Also support MS_NO_COREDLL for b/w compat */
723723
/* The size of `wchar_t', as computed by sizeof. */
724724
#define SIZEOF_WCHAR_T 2
725725

726-
/* The size of `pid_t' (HANDLE). */
727-
#define SIZEOF_PID_T SIZEOF_VOID_P
726+
/* The size of `pid_t', as computed by sizeof. */
727+
#define SIZEOF_PID_T SIZEOF_INT
728728

729729
/* Define if you have the dl library (-ldl). */
730730
/* #undef HAVE_LIBDL */

0 commit comments

Comments
 (0)