Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Extension module used by multiprocessing package |
| 3 | * |
| 4 | * multiprocessing.c |
| 5 | * |
| 6 | * Copyright (c) 2006-2008, R Oudkerk --- see COPYING.txt |
| 7 | */ |
| 8 | |
| 9 | #include "multiprocessing.h" |
| 10 | |
Charles-François Natali | 10db4de | 2011-12-14 18:39:09 +0100 | [diff] [blame] | 11 | #if (defined(CMSG_LEN) && defined(SCM_RIGHTS)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 12 | #define HAVE_FD_TRANSFER 1 |
Benjamin Peterson | 965ce87 | 2009-04-05 21:24:58 +0000 | [diff] [blame] | 13 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 14 | #define HAVE_FD_TRANSFER 0 |
Benjamin Peterson | 965ce87 | 2009-04-05 21:24:58 +0000 | [diff] [blame] | 15 | #endif |
| 16 | |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 17 | PyObject *create_win32_namespace(void); |
| 18 | |
| 19 | PyObject *pickle_dumps, *pickle_loads, *pickle_protocol; |
| 20 | PyObject *ProcessError, *BufferTooShort; |
| 21 | |
| 22 | /* |
| 23 | * Function which raises exceptions based on error codes |
| 24 | */ |
| 25 | |
| 26 | PyObject * |
| 27 | mp_SetError(PyObject *Type, int num) |
| 28 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 29 | switch (num) { |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 30 | #ifdef MS_WINDOWS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 31 | case MP_STANDARD_ERROR: |
| 32 | if (Type == NULL) |
| 33 | Type = PyExc_WindowsError; |
| 34 | PyErr_SetExcFromWindowsErr(Type, 0); |
| 35 | break; |
| 36 | case MP_SOCKET_ERROR: |
| 37 | if (Type == NULL) |
| 38 | Type = PyExc_WindowsError; |
| 39 | PyErr_SetExcFromWindowsErr(Type, WSAGetLastError()); |
| 40 | break; |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 41 | #else /* !MS_WINDOWS */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 42 | case MP_STANDARD_ERROR: |
| 43 | case MP_SOCKET_ERROR: |
| 44 | if (Type == NULL) |
| 45 | Type = PyExc_OSError; |
| 46 | PyErr_SetFromErrno(Type); |
| 47 | break; |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 48 | #endif /* !MS_WINDOWS */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 49 | case MP_MEMORY_ERROR: |
| 50 | PyErr_NoMemory(); |
| 51 | break; |
| 52 | case MP_END_OF_FILE: |
| 53 | PyErr_SetNone(PyExc_EOFError); |
| 54 | break; |
| 55 | case MP_EARLY_END_OF_FILE: |
| 56 | PyErr_SetString(PyExc_IOError, |
| 57 | "got end of file during message"); |
| 58 | break; |
| 59 | case MP_BAD_MESSAGE_LENGTH: |
| 60 | PyErr_SetString(PyExc_IOError, "bad message length"); |
| 61 | break; |
| 62 | case MP_EXCEPTION_HAS_BEEN_SET: |
| 63 | break; |
| 64 | default: |
| 65 | PyErr_Format(PyExc_RuntimeError, |
| 66 | "unkown error number %d", num); |
| 67 | } |
| 68 | return NULL; |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | |
| 72 | /* |
| 73 | * Windows only |
| 74 | */ |
| 75 | |
| 76 | #ifdef MS_WINDOWS |
| 77 | |
| 78 | /* On Windows we set an event to signal Ctrl-C; compare with timemodule.c */ |
| 79 | |
| 80 | HANDLE sigint_event = NULL; |
| 81 | |
| 82 | static BOOL WINAPI |
| 83 | ProcessingCtrlHandler(DWORD dwCtrlType) |
| 84 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 85 | SetEvent(sigint_event); |
| 86 | return FALSE; |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | /* |
| 90 | * Unix only |
| 91 | */ |
| 92 | |
| 93 | #else /* !MS_WINDOWS */ |
| 94 | |
| 95 | #if HAVE_FD_TRANSFER |
| 96 | |
| 97 | /* Functions for transferring file descriptors between processes. |
| 98 | Reimplements some of the functionality of the fdcred |
| 99 | module at http://www.mca-ltd.com/resources/fdcred_1.tgz. */ |
Jesus Cea | ab7060e | 2011-09-10 04:04:09 +0200 | [diff] [blame] | 100 | /* Based in http://resin.csoft.net/cgi-bin/man.cgi?section=3&topic=CMSG_DATA */ |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 101 | |
| 102 | static PyObject * |
| 103 | multiprocessing_sendfd(PyObject *self, PyObject *args) |
| 104 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 105 | int conn, fd, res; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 106 | struct iovec dummy_iov; |
Jesus Cea | ab7060e | 2011-09-10 04:04:09 +0200 | [diff] [blame] | 107 | char dummy_char; |
| 108 | struct msghdr msg; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 109 | struct cmsghdr *cmsg; |
Jesus Cea | ab7060e | 2011-09-10 04:04:09 +0200 | [diff] [blame] | 110 | union { |
| 111 | struct cmsghdr hdr; |
| 112 | unsigned char buf[CMSG_SPACE(sizeof(int))]; |
| 113 | } cmsgbuf; |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 114 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 115 | if (!PyArg_ParseTuple(args, "ii", &conn, &fd)) |
| 116 | return NULL; |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 117 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 118 | dummy_iov.iov_base = &dummy_char; |
| 119 | dummy_iov.iov_len = 1; |
Jesus Cea | ab7060e | 2011-09-10 04:04:09 +0200 | [diff] [blame] | 120 | |
| 121 | memset(&msg, 0, sizeof(msg)); |
| 122 | msg.msg_control = &cmsgbuf.buf; |
| 123 | msg.msg_controllen = sizeof(cmsgbuf.buf); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 124 | msg.msg_iov = &dummy_iov; |
| 125 | msg.msg_iovlen = 1; |
Jesus Cea | ab7060e | 2011-09-10 04:04:09 +0200 | [diff] [blame] | 126 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 127 | cmsg = CMSG_FIRSTHDR(&msg); |
Jesus Cea | ab7060e | 2011-09-10 04:04:09 +0200 | [diff] [blame] | 128 | cmsg->cmsg_len = CMSG_LEN(sizeof(int)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 129 | cmsg->cmsg_level = SOL_SOCKET; |
| 130 | cmsg->cmsg_type = SCM_RIGHTS; |
Antoine Pitrou | bcb39d4 | 2011-08-23 19:46:22 +0200 | [diff] [blame] | 131 | * (int *) CMSG_DATA(cmsg) = fd; |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 132 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 133 | Py_BEGIN_ALLOW_THREADS |
| 134 | res = sendmsg(conn, &msg, 0); |
| 135 | Py_END_ALLOW_THREADS |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 136 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 137 | if (res < 0) |
| 138 | return PyErr_SetFromErrno(PyExc_OSError); |
| 139 | Py_RETURN_NONE; |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | static PyObject * |
| 143 | multiprocessing_recvfd(PyObject *self, PyObject *args) |
| 144 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 145 | int conn, fd, res; |
| 146 | char dummy_char; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 147 | struct iovec dummy_iov; |
Jesus Cea | ab7060e | 2011-09-10 04:04:09 +0200 | [diff] [blame] | 148 | struct msghdr msg = {0}; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 149 | struct cmsghdr *cmsg; |
Jesus Cea | ab7060e | 2011-09-10 04:04:09 +0200 | [diff] [blame] | 150 | union { |
| 151 | struct cmsghdr hdr; |
| 152 | unsigned char buf[CMSG_SPACE(sizeof(int))]; |
| 153 | } cmsgbuf; |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 154 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 155 | if (!PyArg_ParseTuple(args, "i", &conn)) |
| 156 | return NULL; |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 157 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 158 | dummy_iov.iov_base = &dummy_char; |
| 159 | dummy_iov.iov_len = 1; |
Jesus Cea | ab7060e | 2011-09-10 04:04:09 +0200 | [diff] [blame] | 160 | |
| 161 | memset(&msg, 0, sizeof(msg)); |
| 162 | msg.msg_control = &cmsgbuf.buf; |
| 163 | msg.msg_controllen = sizeof(cmsgbuf.buf); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 164 | msg.msg_iov = &dummy_iov; |
| 165 | msg.msg_iovlen = 1; |
Jesus Cea | ab7060e | 2011-09-10 04:04:09 +0200 | [diff] [blame] | 166 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 167 | cmsg = CMSG_FIRSTHDR(&msg); |
| 168 | cmsg->cmsg_level = SOL_SOCKET; |
| 169 | cmsg->cmsg_type = SCM_RIGHTS; |
| 170 | cmsg->cmsg_len = CMSG_LEN(sizeof(int)); |
| 171 | msg.msg_controllen = cmsg->cmsg_len; |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 172 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 173 | Py_BEGIN_ALLOW_THREADS |
| 174 | res = recvmsg(conn, &msg, 0); |
| 175 | Py_END_ALLOW_THREADS |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 176 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 177 | if (res < 0) |
| 178 | return PyErr_SetFromErrno(PyExc_OSError); |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 179 | |
Jesus Cea | 4507e64 | 2011-09-21 03:53:25 +0200 | [diff] [blame] | 180 | if (msg.msg_controllen < CMSG_LEN(sizeof(int)) || |
| 181 | (cmsg = CMSG_FIRSTHDR(&msg)) == NULL || |
| 182 | cmsg->cmsg_level != SOL_SOCKET || |
| 183 | cmsg->cmsg_type != SCM_RIGHTS || |
| 184 | cmsg->cmsg_len < CMSG_LEN(sizeof(int))) { |
| 185 | /* If at least one control message is present, there should be |
| 186 | no room for any further data in the buffer. */ |
| 187 | PyErr_SetString(PyExc_RuntimeError, "No file descriptor received"); |
| 188 | return NULL; |
| 189 | } |
| 190 | |
Antoine Pitrou | bcb39d4 | 2011-08-23 19:46:22 +0200 | [diff] [blame] | 191 | fd = * (int *) CMSG_DATA(cmsg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 192 | return Py_BuildValue("i", fd); |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | #endif /* HAVE_FD_TRANSFER */ |
| 196 | |
| 197 | #endif /* !MS_WINDOWS */ |
| 198 | |
| 199 | |
| 200 | /* |
| 201 | * All platforms |
| 202 | */ |
| 203 | |
| 204 | static PyObject* |
| 205 | multiprocessing_address_of_buffer(PyObject *self, PyObject *obj) |
| 206 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 207 | void *buffer; |
| 208 | Py_ssize_t buffer_len; |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 209 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 210 | if (PyObject_AsWriteBuffer(obj, &buffer, &buffer_len) < 0) |
| 211 | return NULL; |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 212 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 213 | return Py_BuildValue("N" F_PY_SSIZE_T, |
| 214 | PyLong_FromVoidPtr(buffer), buffer_len); |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | |
| 218 | /* |
| 219 | * Function table |
| 220 | */ |
| 221 | |
| 222 | static PyMethodDef module_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 223 | {"address_of_buffer", multiprocessing_address_of_buffer, METH_O, |
| 224 | "address_of_buffer(obj) -> int\n" |
| 225 | "Return address of obj assuming obj supports buffer inteface"}, |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 226 | #if HAVE_FD_TRANSFER |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 227 | {"sendfd", multiprocessing_sendfd, METH_VARARGS, |
| 228 | "sendfd(sockfd, fd) -> None\n" |
| 229 | "Send file descriptor given by fd over the unix domain socket\n" |
| 230 | "whose file decriptor is sockfd"}, |
| 231 | {"recvfd", multiprocessing_recvfd, METH_VARARGS, |
| 232 | "recvfd(sockfd) -> fd\n" |
| 233 | "Receive a file descriptor over a unix domain socket\n" |
| 234 | "whose file decriptor is sockfd"}, |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 235 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 236 | {NULL} |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 237 | }; |
| 238 | |
| 239 | |
| 240 | /* |
| 241 | * Initialize |
| 242 | */ |
| 243 | |
| 244 | static struct PyModuleDef multiprocessing_module = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 245 | PyModuleDef_HEAD_INIT, |
| 246 | "_multiprocessing", |
| 247 | NULL, |
| 248 | -1, |
| 249 | module_methods, |
| 250 | NULL, |
| 251 | NULL, |
| 252 | NULL, |
| 253 | NULL |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 254 | }; |
| 255 | |
| 256 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 257 | PyMODINIT_FUNC |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 258 | PyInit__multiprocessing(void) |
| 259 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 260 | PyObject *module, *temp, *value; |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 261 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 262 | /* Initialize module */ |
| 263 | module = PyModule_Create(&multiprocessing_module); |
| 264 | if (!module) |
| 265 | return NULL; |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 266 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 267 | /* Get copy of objects from pickle */ |
| 268 | temp = PyImport_ImportModule(PICKLE_MODULE); |
| 269 | if (!temp) |
| 270 | return NULL; |
| 271 | pickle_dumps = PyObject_GetAttrString(temp, "dumps"); |
| 272 | pickle_loads = PyObject_GetAttrString(temp, "loads"); |
| 273 | pickle_protocol = PyObject_GetAttrString(temp, "HIGHEST_PROTOCOL"); |
| 274 | Py_XDECREF(temp); |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 275 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 276 | /* Get copy of BufferTooShort */ |
| 277 | temp = PyImport_ImportModule("multiprocessing"); |
| 278 | if (!temp) |
| 279 | return NULL; |
| 280 | BufferTooShort = PyObject_GetAttrString(temp, "BufferTooShort"); |
| 281 | Py_XDECREF(temp); |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 282 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 283 | /* Add connection type to module */ |
| 284 | if (PyType_Ready(&ConnectionType) < 0) |
| 285 | return NULL; |
| 286 | Py_INCREF(&ConnectionType); |
| 287 | PyModule_AddObject(module, "Connection", (PyObject*)&ConnectionType); |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 288 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 289 | #if defined(MS_WINDOWS) || \ |
Mark Dickinson | a614f04 | 2009-11-28 12:48:43 +0000 | [diff] [blame] | 290 | (defined(HAVE_SEM_OPEN) && !defined(POSIX_SEMAPHORES_NOT_ENABLED)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 291 | /* Add SemLock type to module */ |
| 292 | if (PyType_Ready(&SemLockType) < 0) |
| 293 | return NULL; |
| 294 | Py_INCREF(&SemLockType); |
Gregory P. Smith | 68e0135 | 2010-10-17 02:14:36 +0000 | [diff] [blame] | 295 | { |
| 296 | PyObject *py_sem_value_max; |
| 297 | /* Some systems define SEM_VALUE_MAX as an unsigned value that |
| 298 | * causes it to be negative when used as an int (NetBSD). */ |
| 299 | if ((int)(SEM_VALUE_MAX) < 0) |
| 300 | py_sem_value_max = PyLong_FromLong(INT_MAX); |
| 301 | else |
| 302 | py_sem_value_max = PyLong_FromLong(SEM_VALUE_MAX); |
| 303 | if (py_sem_value_max == NULL) |
| 304 | return NULL; |
| 305 | PyDict_SetItemString(SemLockType.tp_dict, "SEM_VALUE_MAX", |
| 306 | py_sem_value_max); |
| 307 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 308 | PyModule_AddObject(module, "SemLock", (PyObject*)&SemLockType); |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 309 | #endif |
| 310 | |
| 311 | #ifdef MS_WINDOWS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 312 | /* Add PipeConnection to module */ |
| 313 | if (PyType_Ready(&PipeConnectionType) < 0) |
| 314 | return NULL; |
| 315 | Py_INCREF(&PipeConnectionType); |
| 316 | PyModule_AddObject(module, "PipeConnection", |
| 317 | (PyObject*)&PipeConnectionType); |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 318 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 319 | /* Initialize win32 class and add to multiprocessing */ |
| 320 | temp = create_win32_namespace(); |
| 321 | if (!temp) |
| 322 | return NULL; |
| 323 | PyModule_AddObject(module, "win32", temp); |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 324 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 325 | /* Initialize the event handle used to signal Ctrl-C */ |
| 326 | sigint_event = CreateEvent(NULL, TRUE, FALSE, NULL); |
| 327 | if (!sigint_event) { |
| 328 | PyErr_SetFromWindowsErr(0); |
| 329 | return NULL; |
| 330 | } |
| 331 | if (!SetConsoleCtrlHandler(ProcessingCtrlHandler, TRUE)) { |
| 332 | PyErr_SetFromWindowsErr(0); |
| 333 | return NULL; |
| 334 | } |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 335 | #endif |
| 336 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 337 | /* Add configuration macros */ |
| 338 | temp = PyDict_New(); |
| 339 | if (!temp) |
| 340 | return NULL; |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 341 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 342 | #define ADD_FLAG(name) \ |
| 343 | value = Py_BuildValue("i", name); \ |
| 344 | if (value == NULL) { Py_DECREF(temp); return NULL; } \ |
| 345 | if (PyDict_SetItemString(temp, #name, value) < 0) { \ |
| 346 | Py_DECREF(temp); Py_DECREF(value); return NULL; } \ |
| 347 | Py_DECREF(value) |
| 348 | |
Mark Dickinson | a614f04 | 2009-11-28 12:48:43 +0000 | [diff] [blame] | 349 | #if defined(HAVE_SEM_OPEN) && !defined(POSIX_SEMAPHORES_NOT_ENABLED) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 350 | ADD_FLAG(HAVE_SEM_OPEN); |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 351 | #endif |
| 352 | #ifdef HAVE_SEM_TIMEDWAIT |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 353 | ADD_FLAG(HAVE_SEM_TIMEDWAIT); |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 354 | #endif |
| 355 | #ifdef HAVE_FD_TRANSFER |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 356 | ADD_FLAG(HAVE_FD_TRANSFER); |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 357 | #endif |
| 358 | #ifdef HAVE_BROKEN_SEM_GETVALUE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 359 | ADD_FLAG(HAVE_BROKEN_SEM_GETVALUE); |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 360 | #endif |
| 361 | #ifdef HAVE_BROKEN_SEM_UNLINK |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 362 | ADD_FLAG(HAVE_BROKEN_SEM_UNLINK); |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 363 | #endif |
| 364 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 365 | if (PyModule_AddObject(module, "flags", temp) < 0) |
| 366 | return NULL; |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 367 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 368 | return module; |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 369 | } |