Skip to content

Commit 7bf0a5c

Browse files
author
gregory.p.smith
committed
fix issue3120 - don't truncate handles on 64-bit Windows.
This is still messy, realistically PC/_subprocess.c should never cast pointers to python numbers and back at all. I don't have a 64-bit windows build environment because microsoft apparently thinks that should cost money. Time to watch the buildbots. It builds and passes tests on 32-bit windows. git-svn-id: http://svn.python.org/projects/python/trunk@65151 6015fed2-1504-0410-9fe1-9d1591cc4771
1 parent 6683b4b commit 7bf0a5c

File tree

2 files changed

+36
-23
lines changed

2 files changed

+36
-23
lines changed

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,8 @@ Library
141141
up in the child process to prevent deadlock and report proper thread counts
142142
if the new process uses the threading module.
143143

144+
- Issue #3120: On 64-bit Windows the subprocess module was truncating handles.
145+
144146
Tests
145147
-----
146148

PC/_subprocess.c

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,14 @@ sp_handle_new(HANDLE handle)
6969
return (PyObject*) self;
7070
}
7171

72+
#if defined(MS_WIN32) && !defined(MS_WIN64)
73+
#define HANDLE_TO_PYNUM(handle) PyInt_FromLong((long) handle)
74+
#define PY_HANDLE_PARAM "l"
75+
#else
76+
#define HANDLE_TO_PYNUM(handle) PyLong_FromLongLong((long long) handle)
77+
#define PY_HANDLE_PARAM "L"
78+
#endif
79+
7280
static PyObject*
7381
sp_handle_detach(sp_handle_object* self, PyObject* args)
7482
{
@@ -82,7 +90,7 @@ sp_handle_detach(sp_handle_object* self, PyObject* args)
8290
self->handle = NULL;
8391

8492
/* note: return the current handle, as an integer */
85-
return PyInt_FromLong((long) handle);
93+
return HANDLE_TO_PYNUM(handle);
8694
}
8795

8896
static PyObject*
@@ -122,7 +130,7 @@ sp_handle_getattr(sp_handle_object* self, char* name)
122130
static PyObject*
123131
sp_handle_as_int(sp_handle_object* self)
124132
{
125-
return PyInt_FromLong((long) self->handle);
133+
return HANDLE_TO_PYNUM(self->handle);
126134
}
127135

128136
static PyNumberMethods sp_handle_as_number;
@@ -168,7 +176,7 @@ sp_GetStdHandle(PyObject* self, PyObject* args)
168176
}
169177

170178
/* note: returns integer, not handle object */
171-
return PyInt_FromLong((long) handle);
179+
return HANDLE_TO_PYNUM(handle);
172180
}
173181

174182
static PyObject *
@@ -186,14 +194,16 @@ sp_DuplicateHandle(PyObject* self, PyObject* args)
186194
HANDLE target_handle;
187195
BOOL result;
188196

189-
long source_process_handle;
190-
long source_handle;
191-
long target_process_handle;
197+
HANDLE source_process_handle;
198+
HANDLE source_handle;
199+
HANDLE target_process_handle;
192200
int desired_access;
193201
int inherit_handle;
194202
int options = 0;
195203

196-
if (! PyArg_ParseTuple(args, "lllii|i:DuplicateHandle",
204+
if (! PyArg_ParseTuple(args,
205+
PY_HANDLE_PARAM PY_HANDLE_PARAM PY_HANDLE_PARAM
206+
"ii|i:DuplicateHandle",
197207
&source_process_handle,
198208
&source_handle,
199209
&target_process_handle,
@@ -204,9 +214,9 @@ sp_DuplicateHandle(PyObject* self, PyObject* args)
204214

205215
Py_BEGIN_ALLOW_THREADS
206216
result = DuplicateHandle(
207-
(HANDLE) source_process_handle,
208-
(HANDLE) source_handle,
209-
(HANDLE) target_process_handle,
217+
source_process_handle,
218+
source_handle,
219+
target_process_handle,
210220
&target_handle,
211221
desired_access,
212222
inherit_handle,
@@ -436,13 +446,13 @@ sp_TerminateProcess(PyObject* self, PyObject* args)
436446
{
437447
BOOL result;
438448

439-
long process;
449+
HANDLE process;
440450
int exit_code;
441-
if (! PyArg_ParseTuple(args, "li:TerminateProcess", &process,
442-
&exit_code))
451+
if (! PyArg_ParseTuple(args, PY_HANDLE_PARAM "i:TerminateProcess",
452+
&process, &exit_code))
443453
return NULL;
444454

445-
result = TerminateProcess((HANDLE) process, exit_code);
455+
result = TerminateProcess(process, exit_code);
446456

447457
if (! result)
448458
return PyErr_SetFromWindowsErr(GetLastError());
@@ -457,11 +467,11 @@ sp_GetExitCodeProcess(PyObject* self, PyObject* args)
457467
DWORD exit_code;
458468
BOOL result;
459469

460-
long process;
461-
if (! PyArg_ParseTuple(args, "l:GetExitCodeProcess", &process))
470+
HANDLE process;
471+
if (! PyArg_ParseTuple(args, PY_HANDLE_PARAM ":GetExitCodeProcess", &process))
462472
return NULL;
463473

464-
result = GetExitCodeProcess((HANDLE) process, &exit_code);
474+
result = GetExitCodeProcess(process, &exit_code);
465475

466476
if (! result)
467477
return PyErr_SetFromWindowsErr(GetLastError());
@@ -474,15 +484,15 @@ sp_WaitForSingleObject(PyObject* self, PyObject* args)
474484
{
475485
DWORD result;
476486

477-
long handle;
487+
HANDLE handle;
478488
int milliseconds;
479-
if (! PyArg_ParseTuple(args, "li:WaitForSingleObject",
489+
if (! PyArg_ParseTuple(args, PY_HANDLE_PARAM "i:WaitForSingleObject",
480490
&handle,
481491
&milliseconds))
482492
return NULL;
483493

484494
Py_BEGIN_ALLOW_THREADS
485-
result = WaitForSingleObject((HANDLE) handle, (DWORD) milliseconds);
495+
result = WaitForSingleObject(handle, (DWORD) milliseconds);
486496
Py_END_ALLOW_THREADS
487497

488498
if (result == WAIT_FAILED)
@@ -504,13 +514,14 @@ static PyObject *
504514
sp_GetModuleFileName(PyObject* self, PyObject* args)
505515
{
506516
BOOL result;
507-
long module;
517+
HMODULE module;
508518
TCHAR filename[MAX_PATH];
509519

510-
if (! PyArg_ParseTuple(args, "l:GetModuleFileName", &module))
520+
if (! PyArg_ParseTuple(args, PY_HANDLE_PARAM ":GetModuleFileName",
521+
&module))
511522
return NULL;
512523

513-
result = GetModuleFileName((HMODULE)module, filename, MAX_PATH);
524+
result = GetModuleFileName(module, filename, MAX_PATH);
514525
filename[MAX_PATH-1] = '\0';
515526

516527
if (! result)

0 commit comments

Comments
 (0)