~pythonregexp2.7/python/issue2636-11

« back to all changes in this revision

Viewing changes to Modules/_multiprocessing/win32_functions.c

  • Committer: Jeffrey C. "The TimeHorse" Jacobs
  • Date: 2008-09-21 13:47:31 UTC
  • mfrom: (39021.1.404 Regexp-2.7)
  • mto: This revision was merged to the branch mainline in revision 39030.
  • Revision ID: darklord@timehorse.com-20080921134731-rudomuzeh1b2tz1y
Merged in changes from the latest python source snapshot.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Win32 functions used by multiprocessing package
 
3
 *
 
4
 * win32_functions.c
 
5
 *
 
6
 * Copyright (c) 2006-2008, R Oudkerk --- see COPYING.txt
 
7
 */
 
8
 
 
9
#include "multiprocessing.h"
 
10
 
 
11
 
 
12
#define WIN32_FUNCTION(func) \
 
13
    {#func, (PyCFunction)win32_ ## func, METH_VARARGS | METH_STATIC, ""}
 
14
 
 
15
#define WIN32_CONSTANT(fmt, con) \
 
16
    PyDict_SetItemString(Win32Type.tp_dict, #con, Py_BuildValue(fmt, con))
 
17
 
 
18
 
 
19
static PyObject *
 
20
win32_CloseHandle(PyObject *self, PyObject *args)
 
21
{
 
22
        HANDLE hObject;
 
23
        BOOL success;
 
24
 
 
25
        if (!PyArg_ParseTuple(args, F_HANDLE, &hObject))
 
26
                return NULL;
 
27
 
 
28
        Py_BEGIN_ALLOW_THREADS
 
29
        success = CloseHandle(hObject); 
 
30
        Py_END_ALLOW_THREADS
 
31
 
 
32
        if (!success)
 
33
                return PyErr_SetFromWindowsErr(0);
 
34
 
 
35
        Py_RETURN_NONE;
 
36
}
 
37
 
 
38
static PyObject *
 
39
win32_ConnectNamedPipe(PyObject *self, PyObject *args)
 
40
{
 
41
        HANDLE hNamedPipe;
 
42
        LPOVERLAPPED lpOverlapped;
 
43
        BOOL success;
 
44
 
 
45
        if (!PyArg_ParseTuple(args, F_HANDLE F_POINTER, 
 
46
                              &hNamedPipe, &lpOverlapped))
 
47
                return NULL;
 
48
 
 
49
        Py_BEGIN_ALLOW_THREADS
 
50
        success = ConnectNamedPipe(hNamedPipe, lpOverlapped);
 
51
        Py_END_ALLOW_THREADS
 
52
 
 
53
        if (!success)
 
54
                return PyErr_SetFromWindowsErr(0);
 
55
 
 
56
        Py_RETURN_NONE;
 
57
}
 
58
 
 
59
static PyObject *
 
60
win32_CreateFile(PyObject *self, PyObject *args)
 
61
{
 
62
        LPCTSTR lpFileName;
 
63
        DWORD dwDesiredAccess;
 
64
        DWORD dwShareMode;
 
65
        LPSECURITY_ATTRIBUTES lpSecurityAttributes;
 
66
        DWORD dwCreationDisposition;
 
67
        DWORD dwFlagsAndAttributes;
 
68
        HANDLE hTemplateFile;
 
69
        HANDLE handle;
 
70
 
 
71
        if (!PyArg_ParseTuple(args, "s" F_DWORD F_DWORD F_POINTER 
 
72
                              F_DWORD F_DWORD F_HANDLE,
 
73
                              &lpFileName, &dwDesiredAccess, &dwShareMode, 
 
74
                              &lpSecurityAttributes, &dwCreationDisposition, 
 
75
                              &dwFlagsAndAttributes, &hTemplateFile))
 
76
                return NULL;
 
77
 
 
78
        Py_BEGIN_ALLOW_THREADS
 
79
        handle = CreateFile(lpFileName, dwDesiredAccess, 
 
80
                            dwShareMode, lpSecurityAttributes, 
 
81
                            dwCreationDisposition, 
 
82
                            dwFlagsAndAttributes, hTemplateFile);
 
83
        Py_END_ALLOW_THREADS
 
84
 
 
85
        if (handle == INVALID_HANDLE_VALUE)
 
86
                return PyErr_SetFromWindowsErr(0);
 
87
 
 
88
        return Py_BuildValue(F_HANDLE, handle);
 
89
}
 
90
 
 
91
static PyObject *
 
92
win32_CreateNamedPipe(PyObject *self, PyObject *args)
 
93
{
 
94
        LPCTSTR lpName;
 
95
        DWORD dwOpenMode;
 
96
        DWORD dwPipeMode;
 
97
        DWORD nMaxInstances;
 
98
        DWORD nOutBufferSize;
 
99
        DWORD nInBufferSize;
 
100
        DWORD nDefaultTimeOut;
 
101
        LPSECURITY_ATTRIBUTES lpSecurityAttributes;
 
102
        HANDLE handle;
 
103
 
 
104
        if (!PyArg_ParseTuple(args, "s" F_DWORD F_DWORD F_DWORD 
 
105
                              F_DWORD F_DWORD F_DWORD F_POINTER,
 
106
                              &lpName, &dwOpenMode, &dwPipeMode, 
 
107
                              &nMaxInstances, &nOutBufferSize, 
 
108
                              &nInBufferSize, &nDefaultTimeOut,
 
109
                              &lpSecurityAttributes))
 
110
                return NULL;
 
111
 
 
112
        Py_BEGIN_ALLOW_THREADS
 
113
        handle = CreateNamedPipe(lpName, dwOpenMode, dwPipeMode, 
 
114
                                 nMaxInstances, nOutBufferSize, 
 
115
                                 nInBufferSize, nDefaultTimeOut,
 
116
                                 lpSecurityAttributes);
 
117
        Py_END_ALLOW_THREADS
 
118
 
 
119
        if (handle == INVALID_HANDLE_VALUE)
 
120
                return PyErr_SetFromWindowsErr(0);
 
121
 
 
122
        return Py_BuildValue(F_HANDLE, handle);
 
123
}
 
124
 
 
125
static PyObject *
 
126
win32_ExitProcess(PyObject *self, PyObject *args)
 
127
{
 
128
        UINT uExitCode;
 
129
 
 
130
        if (!PyArg_ParseTuple(args, "I", &uExitCode))
 
131
                return NULL;
 
132
 
 
133
        ExitProcess(uExitCode);
 
134
 
 
135
        return NULL;
 
136
}
 
137
 
 
138
static PyObject *
 
139
win32_GetLastError(PyObject *self, PyObject *args)
 
140
{
 
141
        return Py_BuildValue(F_DWORD, GetLastError());
 
142
}
 
143
 
 
144
static PyObject *
 
145
win32_OpenProcess(PyObject *self, PyObject *args)
 
146
{
 
147
        DWORD dwDesiredAccess;
 
148
        BOOL bInheritHandle;
 
149
        DWORD dwProcessId;
 
150
        HANDLE handle;
 
151
 
 
152
        if (!PyArg_ParseTuple(args, F_DWORD "i" F_DWORD, 
 
153
                              &dwDesiredAccess, &bInheritHandle, &dwProcessId))
 
154
                return NULL;
 
155
 
 
156
        handle = OpenProcess(dwDesiredAccess, bInheritHandle, dwProcessId);    
 
157
        if (handle == NULL)
 
158
                return PyErr_SetFromWindowsErr(0);
 
159
 
 
160
        return Py_BuildValue(F_HANDLE, handle);
 
161
}
 
162
 
 
163
static PyObject *
 
164
win32_SetNamedPipeHandleState(PyObject *self, PyObject *args)
 
165
{
 
166
        HANDLE hNamedPipe;
 
167
        PyObject *oArgs[3];
 
168
        DWORD dwArgs[3], *pArgs[3] = {NULL, NULL, NULL};
 
169
        int i;
 
170
 
 
171
        if (!PyArg_ParseTuple(args, F_HANDLE "OOO", 
 
172
                              &hNamedPipe, &oArgs[0], &oArgs[1], &oArgs[2]))
 
173
                return NULL;
 
174
 
 
175
        PyErr_Clear();
 
176
 
 
177
        for (i = 0 ; i < 3 ; i++) {
 
178
                if (oArgs[i] != Py_None) {
 
179
                        dwArgs[i] = PyInt_AsUnsignedLongMask(oArgs[i]);
 
180
                        if (PyErr_Occurred())
 
181
                                return NULL;
 
182
                        pArgs[i] = &dwArgs[i];
 
183
                }
 
184
        }
 
185
 
 
186
        if (!SetNamedPipeHandleState(hNamedPipe, pArgs[0], pArgs[1], pArgs[2]))
 
187
                return PyErr_SetFromWindowsErr(0);
 
188
 
 
189
        Py_RETURN_NONE;
 
190
}
 
191
 
 
192
static PyObject *
 
193
win32_WaitNamedPipe(PyObject *self, PyObject *args)
 
194
{
 
195
        LPCTSTR lpNamedPipeName;
 
196
        DWORD nTimeOut;
 
197
        BOOL success;
 
198
 
 
199
        if (!PyArg_ParseTuple(args, "s" F_DWORD, &lpNamedPipeName, &nTimeOut))
 
200
                return NULL;
 
201
 
 
202
        Py_BEGIN_ALLOW_THREADS
 
203
        success = WaitNamedPipe(lpNamedPipeName, nTimeOut);
 
204
        Py_END_ALLOW_THREADS
 
205
 
 
206
        if (!success)
 
207
                return PyErr_SetFromWindowsErr(0);
 
208
 
 
209
        Py_RETURN_NONE;
 
210
}
 
211
 
 
212
static PyMethodDef win32_methods[] = {
 
213
        WIN32_FUNCTION(CloseHandle),
 
214
        WIN32_FUNCTION(GetLastError),
 
215
        WIN32_FUNCTION(OpenProcess),
 
216
        WIN32_FUNCTION(ExitProcess),
 
217
        WIN32_FUNCTION(ConnectNamedPipe),
 
218
        WIN32_FUNCTION(CreateFile),
 
219
        WIN32_FUNCTION(CreateNamedPipe),
 
220
        WIN32_FUNCTION(SetNamedPipeHandleState),
 
221
        WIN32_FUNCTION(WaitNamedPipe),
 
222
        {NULL}
 
223
};
 
224
 
 
225
 
 
226
PyTypeObject Win32Type = {
 
227
        PyVarObject_HEAD_INIT(NULL, 0)
 
228
};
 
229
 
 
230
 
 
231
PyObject *
 
232
create_win32_namespace(void)
 
233
{
 
234
        Win32Type.tp_name = "_multiprocessing.win32";
 
235
        Win32Type.tp_methods = win32_methods;
 
236
        if (PyType_Ready(&Win32Type) < 0)
 
237
                return NULL;
 
238
        Py_INCREF(&Win32Type);
 
239
 
 
240
        WIN32_CONSTANT(F_DWORD, ERROR_ALREADY_EXISTS);
 
241
        WIN32_CONSTANT(F_DWORD, ERROR_PIPE_BUSY);
 
242
        WIN32_CONSTANT(F_DWORD, ERROR_PIPE_CONNECTED);
 
243
        WIN32_CONSTANT(F_DWORD, ERROR_SEM_TIMEOUT);
 
244
        WIN32_CONSTANT(F_DWORD, GENERIC_READ);
 
245
        WIN32_CONSTANT(F_DWORD, GENERIC_WRITE);
 
246
        WIN32_CONSTANT(F_DWORD, INFINITE);
 
247
        WIN32_CONSTANT(F_DWORD, NMPWAIT_WAIT_FOREVER);
 
248
        WIN32_CONSTANT(F_DWORD, OPEN_EXISTING);
 
249
        WIN32_CONSTANT(F_DWORD, PIPE_ACCESS_DUPLEX);
 
250
        WIN32_CONSTANT(F_DWORD, PIPE_ACCESS_INBOUND);
 
251
        WIN32_CONSTANT(F_DWORD, PIPE_READMODE_MESSAGE);
 
252
        WIN32_CONSTANT(F_DWORD, PIPE_TYPE_MESSAGE);
 
253
        WIN32_CONSTANT(F_DWORD, PIPE_UNLIMITED_INSTANCES);
 
254
        WIN32_CONSTANT(F_DWORD, PIPE_WAIT);
 
255
        WIN32_CONSTANT(F_DWORD, PROCESS_ALL_ACCESS);
 
256
 
 
257
        WIN32_CONSTANT("i", NULL);
 
258
 
 
259
        return (PyObject*)&Win32Type;
 
260
}