Line data Source code
1 : /*
2 : * Extension module used by multiprocessing package
3 : *
4 : * multiprocessing.c
5 : *
6 : * Copyright (c) 2006-2008, R Oudkerk
7 : * Licensed to PSF under a Contributor Agreement.
8 : */
9 :
10 : #include "multiprocessing.h"
11 :
12 :
13 : PyObject *ProcessError, *BufferTooShort;
14 :
15 : /*
16 : * Function which raises exceptions based on error codes
17 : */
18 :
19 : PyObject *
20 0 : mp_SetError(PyObject *Type, int num)
21 : {
22 0 : switch (num) {
23 : #ifdef MS_WINDOWS
24 : case MP_STANDARD_ERROR:
25 : if (Type == NULL)
26 : Type = PyExc_WindowsError;
27 : PyErr_SetExcFromWindowsErr(Type, 0);
28 : break;
29 : case MP_SOCKET_ERROR:
30 : if (Type == NULL)
31 : Type = PyExc_WindowsError;
32 : PyErr_SetExcFromWindowsErr(Type, WSAGetLastError());
33 : break;
34 : #else /* !MS_WINDOWS */
35 : case MP_STANDARD_ERROR:
36 : case MP_SOCKET_ERROR:
37 0 : if (Type == NULL)
38 0 : Type = PyExc_OSError;
39 0 : PyErr_SetFromErrno(Type);
40 0 : break;
41 : #endif /* !MS_WINDOWS */
42 : case MP_MEMORY_ERROR:
43 0 : PyErr_NoMemory();
44 0 : break;
45 : case MP_EXCEPTION_HAS_BEEN_SET:
46 0 : break;
47 : default:
48 0 : PyErr_Format(PyExc_RuntimeError,
49 : "unkown error number %d", num);
50 : }
51 0 : return NULL;
52 : }
53 :
54 : #ifdef MS_WINDOWS
55 : static PyObject *
56 : multiprocessing_closesocket(PyObject *self, PyObject *args)
57 : {
58 : HANDLE handle;
59 : int ret;
60 :
61 : if (!PyArg_ParseTuple(args, F_HANDLE ":closesocket" , &handle))
62 : return NULL;
63 :
64 : Py_BEGIN_ALLOW_THREADS
65 : ret = closesocket((SOCKET) handle);
66 : Py_END_ALLOW_THREADS
67 :
68 : if (ret)
69 : return PyErr_SetExcFromWindowsErr(PyExc_IOError, WSAGetLastError());
70 : Py_RETURN_NONE;
71 : }
72 :
73 : static PyObject *
74 : multiprocessing_recv(PyObject *self, PyObject *args)
75 : {
76 : HANDLE handle;
77 : int size, nread;
78 : PyObject *buf;
79 :
80 : if (!PyArg_ParseTuple(args, F_HANDLE "i:recv" , &handle, &size))
81 : return NULL;
82 :
83 : buf = PyBytes_FromStringAndSize(NULL, size);
84 : if (!buf)
85 : return NULL;
86 :
87 : Py_BEGIN_ALLOW_THREADS
88 : nread = recv((SOCKET) handle, PyBytes_AS_STRING(buf), size, 0);
89 : Py_END_ALLOW_THREADS
90 :
91 : if (nread < 0) {
92 : Py_DECREF(buf);
93 : return PyErr_SetExcFromWindowsErr(PyExc_IOError, WSAGetLastError());
94 : }
95 : _PyBytes_Resize(&buf, nread);
96 : return buf;
97 : }
98 :
99 : static PyObject *
100 : multiprocessing_send(PyObject *self, PyObject *args)
101 : {
102 : HANDLE handle;
103 : Py_buffer buf;
104 : int ret;
105 :
106 : if (!PyArg_ParseTuple(args, F_HANDLE "y*:send" , &handle, &buf))
107 : return NULL;
108 :
109 : Py_BEGIN_ALLOW_THREADS
110 : ret = send((SOCKET) handle, buf.buf, buf.len, 0);
111 : Py_END_ALLOW_THREADS
112 :
113 : PyBuffer_Release(&buf);
114 : if (ret < 0)
115 : return PyErr_SetExcFromWindowsErr(PyExc_IOError, WSAGetLastError());
116 : return PyLong_FromLong(ret);
117 : }
118 :
119 : #endif
120 :
121 : /*
122 : * Function table
123 : */
124 :
125 : static PyMethodDef module_methods[] = {
126 : #ifdef MS_WINDOWS
127 : {"closesocket", multiprocessing_closesocket, METH_VARARGS, ""},
128 : {"recv", multiprocessing_recv, METH_VARARGS, ""},
129 : {"send", multiprocessing_send, METH_VARARGS, ""},
130 : #endif
131 : {NULL}
132 : };
133 :
134 :
135 : /*
136 : * Initialize
137 : */
138 :
139 : static struct PyModuleDef multiprocessing_module = {
140 : PyModuleDef_HEAD_INIT,
141 : "_multiprocessing",
142 : NULL,
143 : -1,
144 : module_methods,
145 : NULL,
146 : NULL,
147 : NULL,
148 : NULL
149 : };
150 :
151 :
152 : PyMODINIT_FUNC
153 0 : PyInit__multiprocessing(void)
154 : {
155 0 : PyObject *module, *temp, *value = NULL;
156 :
157 : /* Initialize module */
158 0 : module = PyModule_Create(&multiprocessing_module);
159 0 : if (!module)
160 0 : return NULL;
161 :
162 : /* Get copy of BufferTooShort */
163 0 : temp = PyImport_ImportModule("multiprocessing");
164 0 : if (!temp)
165 0 : return NULL;
166 0 : BufferTooShort = PyObject_GetAttrString(temp, "BufferTooShort");
167 0 : Py_XDECREF(temp);
168 :
169 : #if defined(MS_WINDOWS) || \
170 : (defined(HAVE_SEM_OPEN) && !defined(POSIX_SEMAPHORES_NOT_ENABLED))
171 : /* Add SemLock type to module */
172 0 : if (PyType_Ready(&SemLockType) < 0)
173 0 : return NULL;
174 0 : Py_INCREF(&SemLockType);
175 : {
176 : PyObject *py_sem_value_max;
177 : /* Some systems define SEM_VALUE_MAX as an unsigned value that
178 : * causes it to be negative when used as an int (NetBSD). */
179 : if ((int)(SEM_VALUE_MAX) < 0)
180 : py_sem_value_max = PyLong_FromLong(INT_MAX);
181 : else
182 0 : py_sem_value_max = PyLong_FromLong(SEM_VALUE_MAX);
183 0 : if (py_sem_value_max == NULL)
184 0 : return NULL;
185 0 : PyDict_SetItemString(SemLockType.tp_dict, "SEM_VALUE_MAX",
186 : py_sem_value_max);
187 : }
188 0 : PyModule_AddObject(module, "SemLock", (PyObject*)&SemLockType);
189 : #endif
190 :
191 : /* Add configuration macros */
192 0 : temp = PyDict_New();
193 0 : if (!temp)
194 0 : return NULL;
195 :
196 : #define ADD_FLAG(name) \
197 : value = Py_BuildValue("i", name); \
198 : if (value == NULL) { Py_DECREF(temp); return NULL; } \
199 : if (PyDict_SetItemString(temp, #name, value) < 0) { \
200 : Py_DECREF(temp); Py_DECREF(value); return NULL; } \
201 : Py_DECREF(value)
202 :
203 : #if defined(HAVE_SEM_OPEN) && !defined(POSIX_SEMAPHORES_NOT_ENABLED)
204 0 : ADD_FLAG(HAVE_SEM_OPEN);
205 : #endif
206 : #ifdef HAVE_SEM_TIMEDWAIT
207 0 : ADD_FLAG(HAVE_SEM_TIMEDWAIT);
208 : #endif
209 : #ifdef HAVE_BROKEN_SEM_GETVALUE
210 : ADD_FLAG(HAVE_BROKEN_SEM_GETVALUE);
211 : #endif
212 : #ifdef HAVE_BROKEN_SEM_UNLINK
213 : ADD_FLAG(HAVE_BROKEN_SEM_UNLINK);
214 : #endif
215 :
216 0 : if (PyModule_AddObject(module, "flags", temp) < 0)
217 0 : return NULL;
218 :
219 0 : return module;
220 : }
|