-/*\r
- * Extension module used by multiprocessing package\r
- *\r
- * multiprocessing.c\r
- *\r
- * Copyright (c) 2006-2008, R Oudkerk --- see COPYING.txt\r
- */\r
-\r
-#include "multiprocessing.h"\r
-\r
-PyObject *create_win32_namespace(void);\r
-\r
-PyObject *pickle_dumps, *pickle_loads, *pickle_protocol;\r
-PyObject *ProcessError, *BufferTooShort;\r
-\r
-/*\r
- * Function which raises exceptions based on error codes\r
- */\r
-\r
-PyObject *\r
-mp_SetError(PyObject *Type, int num)\r
-{\r
- switch (num) {\r
-#ifdef MS_WINDOWS\r
- case MP_STANDARD_ERROR: \r
- if (Type == NULL)\r
- Type = PyExc_WindowsError;\r
- PyErr_SetExcFromWindowsErr(Type, 0);\r
- break;\r
- case MP_SOCKET_ERROR:\r
- if (Type == NULL)\r
- Type = PyExc_WindowsError;\r
- PyErr_SetExcFromWindowsErr(Type, WSAGetLastError());\r
- break;\r
-#else /* !MS_WINDOWS */\r
- case MP_STANDARD_ERROR:\r
- case MP_SOCKET_ERROR:\r
- if (Type == NULL)\r
- Type = PyExc_OSError;\r
- PyErr_SetFromErrno(Type);\r
- break;\r
-#endif /* !MS_WINDOWS */\r
- case MP_MEMORY_ERROR:\r
- PyErr_NoMemory();\r
- break;\r
- case MP_END_OF_FILE:\r
- PyErr_SetNone(PyExc_EOFError);\r
- break;\r
- case MP_EARLY_END_OF_FILE:\r
- PyErr_SetString(PyExc_IOError,\r
- "got end of file during message");\r
- break;\r
- case MP_BAD_MESSAGE_LENGTH:\r
- PyErr_SetString(PyExc_IOError, "bad message length");\r
- break;\r
- case MP_EXCEPTION_HAS_BEEN_SET:\r
- break;\r
- default:\r
- PyErr_Format(PyExc_RuntimeError,\r
- "unkown error number %d", num);\r
- }\r
- return NULL;\r
-}\r
-\r
-\r
-/*\r
- * Windows only\r
- */\r
-\r
-#ifdef MS_WINDOWS\r
-\r
-/* On Windows we set an event to signal Ctrl-C; compare with timemodule.c */\r
-\r
-HANDLE sigint_event = NULL;\r
-\r
-static BOOL WINAPI\r
-ProcessingCtrlHandler(DWORD dwCtrlType)\r
-{\r
- SetEvent(sigint_event);\r
- return FALSE;\r
-}\r
-\r
-/*\r
- * Unix only\r
- */\r
-\r
-#else /* !MS_WINDOWS */\r
-\r
-#if HAVE_FD_TRANSFER\r
-\r
-/* Functions for transferring file descriptors between processes.\r
- Reimplements some of the functionality of the fdcred\r
- module at http://www.mca-ltd.com/resources/fdcred_1.tgz. */\r
-\r
-static PyObject *\r
-multiprocessing_sendfd(PyObject *self, PyObject *args)\r
-{\r
- int conn, fd, res;\r
- char dummy_char;\r
- char buf[CMSG_SPACE(sizeof(int))];\r
- struct msghdr msg = {0};\r
- struct iovec dummy_iov;\r
- struct cmsghdr *cmsg;\r
-\r
- if (!PyArg_ParseTuple(args, "ii", &conn, &fd))\r
- return NULL;\r
-\r
- dummy_iov.iov_base = &dummy_char;\r
- dummy_iov.iov_len = 1;\r
- msg.msg_control = buf;\r
- msg.msg_controllen = sizeof(buf);\r
- msg.msg_iov = &dummy_iov;\r
- msg.msg_iovlen = 1;\r
- cmsg = CMSG_FIRSTHDR(&msg);\r
- cmsg->cmsg_level = SOL_SOCKET;\r
- cmsg->cmsg_type = SCM_RIGHTS;\r
- cmsg->cmsg_len = CMSG_LEN(sizeof(int));\r
- msg.msg_controllen = cmsg->cmsg_len;\r
- *(int*)CMSG_DATA(cmsg) = fd;\r
-\r
- Py_BEGIN_ALLOW_THREADS\r
- res = sendmsg(conn, &msg, 0);\r
- Py_END_ALLOW_THREADS\r
-\r
- if (res < 0)\r
- return PyErr_SetFromErrno(PyExc_OSError);\r
- Py_RETURN_NONE;\r
-}\r
-\r
-static PyObject *\r
-multiprocessing_recvfd(PyObject *self, PyObject *args)\r
-{\r
- int conn, fd, res;\r
- char dummy_char;\r
- char buf[CMSG_SPACE(sizeof(int))];\r
- struct msghdr msg = {0};\r
- struct iovec dummy_iov;\r
- struct cmsghdr *cmsg;\r
-\r
- if (!PyArg_ParseTuple(args, "i", &conn))\r
- return NULL;\r
-\r
- dummy_iov.iov_base = &dummy_char;\r
- dummy_iov.iov_len = 1;\r
- msg.msg_control = buf;\r
- msg.msg_controllen = sizeof(buf);\r
- msg.msg_iov = &dummy_iov;\r
- msg.msg_iovlen = 1;\r
- cmsg = CMSG_FIRSTHDR(&msg);\r
- cmsg->cmsg_level = SOL_SOCKET;\r
- cmsg->cmsg_type = SCM_RIGHTS;\r
- cmsg->cmsg_len = CMSG_LEN(sizeof(int));\r
- msg.msg_controllen = cmsg->cmsg_len;\r
-\r
- Py_BEGIN_ALLOW_THREADS\r
- res = recvmsg(conn, &msg, 0);\r
- Py_END_ALLOW_THREADS\r
-\r
- if (res < 0)\r
- return PyErr_SetFromErrno(PyExc_OSError);\r
-\r
- fd = *(int*)CMSG_DATA(cmsg);\r
- return Py_BuildValue("i", fd);\r
-}\r
-\r
-#endif /* HAVE_FD_TRANSFER */\r
-\r
-#endif /* !MS_WINDOWS */\r
-\r
-\r
-/*\r
- * All platforms\r
- */\r
-\r
-static PyObject*\r
-multiprocessing_address_of_buffer(PyObject *self, PyObject *obj)\r
-{\r
- void *buffer;\r
- Py_ssize_t buffer_len;\r
-\r
- if (PyObject_AsWriteBuffer(obj, &buffer, &buffer_len) < 0)\r
- return NULL;\r
-\r
- return Py_BuildValue("N" F_PY_SSIZE_T, \r
- PyLong_FromVoidPtr(buffer), buffer_len);\r
-}\r
-\r
-\r
-/*\r
- * Function table\r
- */\r
-\r
-static PyMethodDef module_methods[] = {\r
- {"address_of_buffer", multiprocessing_address_of_buffer, METH_O, \r
- "address_of_buffer(obj) -> int\n" \r
- "Return address of obj assuming obj supports buffer inteface"},\r
-#if HAVE_FD_TRANSFER\r
- {"sendfd", multiprocessing_sendfd, METH_VARARGS, \r
- "sendfd(sockfd, fd) -> None\n"\r
- "Send file descriptor given by fd over the unix domain socket\n"\r
- "whose file decriptor is sockfd"},\r
- {"recvfd", multiprocessing_recvfd, METH_VARARGS,\r
- "recvfd(sockfd) -> fd\n"\r
- "Receive a file descriptor over a unix domain socket\n"\r
- "whose file decriptor is sockfd"},\r
-#endif\r
- {NULL}\r
-};\r
-\r
-\r
-/*\r
- * Initialize\r
- */\r
-\r
-static struct PyModuleDef multiprocessing_module = {\r
- PyModuleDef_HEAD_INIT,\r
- "_multiprocessing",\r
- NULL,\r
- -1,\r
- module_methods,\r
- NULL,\r
- NULL,\r
- NULL,\r
- NULL\r
-};\r
-\r
-\r
-PyMODINIT_FUNC \r
-PyInit__multiprocessing(void)\r
-{\r
- PyObject *module, *temp, *value;\r
-\r
- /* Initialize module */\r
- module = PyModule_Create(&multiprocessing_module);\r
- if (!module)\r
- return NULL;\r
-\r
- /* Get copy of objects from pickle */\r
- temp = PyImport_ImportModule(PICKLE_MODULE);\r
- if (!temp)\r
- return NULL;\r
- pickle_dumps = PyObject_GetAttrString(temp, "dumps");\r
- pickle_loads = PyObject_GetAttrString(temp, "loads");\r
- pickle_protocol = PyObject_GetAttrString(temp, "HIGHEST_PROTOCOL");\r
- Py_XDECREF(temp);\r
-\r
- /* Get copy of BufferTooShort */\r
- temp = PyImport_ImportModule("multiprocessing");\r
- if (!temp)\r
- return NULL;\r
- BufferTooShort = PyObject_GetAttrString(temp, "BufferTooShort");\r
- Py_XDECREF(temp);\r
-\r
- /* Add connection type to module */\r
- if (PyType_Ready(&ConnectionType) < 0)\r
- return NULL;\r
- Py_INCREF(&ConnectionType); \r
- PyModule_AddObject(module, "Connection", (PyObject*)&ConnectionType);\r
-\r
-#if defined(MS_WINDOWS) || HAVE_SEM_OPEN\r
- /* Add SemLock type to module */\r
- if (PyType_Ready(&SemLockType) < 0)\r
- return NULL;\r
- Py_INCREF(&SemLockType);\r
- PyDict_SetItemString(SemLockType.tp_dict, "SEM_VALUE_MAX", \r
- Py_BuildValue("i", SEM_VALUE_MAX));\r
- PyModule_AddObject(module, "SemLock", (PyObject*)&SemLockType); \r
-#endif\r
-\r
-#ifdef MS_WINDOWS\r
- /* Add PipeConnection to module */\r
- if (PyType_Ready(&PipeConnectionType) < 0)\r
- return NULL;\r
- Py_INCREF(&PipeConnectionType);\r
- PyModule_AddObject(module, "PipeConnection",\r
- (PyObject*)&PipeConnectionType);\r
-\r
- /* Initialize win32 class and add to multiprocessing */\r
- temp = create_win32_namespace();\r
- if (!temp)\r
- return NULL;\r
- PyModule_AddObject(module, "win32", temp);\r
-\r
- /* Initialize the event handle used to signal Ctrl-C */\r
- sigint_event = CreateEvent(NULL, TRUE, FALSE, NULL);\r
- if (!sigint_event) {\r
- PyErr_SetFromWindowsErr(0);\r
- return NULL;\r
- }\r
- if (!SetConsoleCtrlHandler(ProcessingCtrlHandler, TRUE)) {\r
- PyErr_SetFromWindowsErr(0);\r
- return NULL;\r
- }\r
-#endif\r
-\r
- /* Add configuration macros */\r
- temp = PyDict_New();\r
- if (!temp)\r
- return NULL;\r
-\r
-#define ADD_FLAG(name) \\r
- value = Py_BuildValue("i", name); \\r
- if (value == NULL) { Py_DECREF(temp); return NULL; } \\r
- if (PyDict_SetItemString(temp, #name, value) < 0) { \\r
- Py_DECREF(temp); Py_DECREF(value); return NULL; } \\r
- Py_DECREF(value)\r
- \r
-#ifdef HAVE_SEM_OPEN\r
- ADD_FLAG(HAVE_SEM_OPEN);\r
-#endif\r
-#ifdef HAVE_SEM_TIMEDWAIT\r
- ADD_FLAG(HAVE_SEM_TIMEDWAIT);\r
-#endif\r
-#ifdef HAVE_FD_TRANSFER\r
- ADD_FLAG(HAVE_FD_TRANSFER);\r
-#endif\r
-#ifdef HAVE_BROKEN_SEM_GETVALUE\r
- ADD_FLAG(HAVE_BROKEN_SEM_GETVALUE);\r
-#endif\r
-#ifdef HAVE_BROKEN_SEM_UNLINK\r
- ADD_FLAG(HAVE_BROKEN_SEM_UNLINK);\r
-#endif\r
-\r
- if (PyModule_AddObject(module, "flags", temp) < 0)\r
- return NULL;\r
-\r
- return module;\r
-}\r
+/*
+ * Extension module used by multiprocessing package
+ *
+ * multiprocessing.c
+ *
+ * Copyright (c) 2006-2008, R Oudkerk --- see COPYING.txt
+ */
+
+#include "multiprocessing.h"
+
+PyObject *create_win32_namespace(void);
+
+PyObject *pickle_dumps, *pickle_loads, *pickle_protocol;
+PyObject *ProcessError, *BufferTooShort;
+
+/*
+ * Function which raises exceptions based on error codes
+ */
+
+PyObject *
+mp_SetError(PyObject *Type, int num)
+{
+ switch (num) {
+#ifdef MS_WINDOWS
+ case MP_STANDARD_ERROR:
+ if (Type == NULL)
+ Type = PyExc_WindowsError;
+ PyErr_SetExcFromWindowsErr(Type, 0);
+ break;
+ case MP_SOCKET_ERROR:
+ if (Type == NULL)
+ Type = PyExc_WindowsError;
+ PyErr_SetExcFromWindowsErr(Type, WSAGetLastError());
+ break;
+#else /* !MS_WINDOWS */
+ case MP_STANDARD_ERROR:
+ case MP_SOCKET_ERROR:
+ if (Type == NULL)
+ Type = PyExc_OSError;
+ PyErr_SetFromErrno(Type);
+ break;
+#endif /* !MS_WINDOWS */
+ case MP_MEMORY_ERROR:
+ PyErr_NoMemory();
+ break;
+ case MP_END_OF_FILE:
+ PyErr_SetNone(PyExc_EOFError);
+ break;
+ case MP_EARLY_END_OF_FILE:
+ PyErr_SetString(PyExc_IOError,
+ "got end of file during message");
+ break;
+ case MP_BAD_MESSAGE_LENGTH:
+ PyErr_SetString(PyExc_IOError, "bad message length");
+ break;
+ case MP_EXCEPTION_HAS_BEEN_SET:
+ break;
+ default:
+ PyErr_Format(PyExc_RuntimeError,
+ "unkown error number %d", num);
+ }
+ return NULL;
+}
+
+
+/*
+ * Windows only
+ */
+
+#ifdef MS_WINDOWS
+
+/* On Windows we set an event to signal Ctrl-C; compare with timemodule.c */
+
+HANDLE sigint_event = NULL;
+
+static BOOL WINAPI
+ProcessingCtrlHandler(DWORD dwCtrlType)
+{
+ SetEvent(sigint_event);
+ return FALSE;
+}
+
+/*
+ * Unix only
+ */
+
+#else /* !MS_WINDOWS */
+
+#if HAVE_FD_TRANSFER
+
+/* Functions for transferring file descriptors between processes.
+ Reimplements some of the functionality of the fdcred
+ module at http://www.mca-ltd.com/resources/fdcred_1.tgz. */
+
+static PyObject *
+multiprocessing_sendfd(PyObject *self, PyObject *args)
+{
+ int conn, fd, res;
+ char dummy_char;
+ char buf[CMSG_SPACE(sizeof(int))];
+ struct msghdr msg = {0};
+ struct iovec dummy_iov;
+ struct cmsghdr *cmsg;
+
+ if (!PyArg_ParseTuple(args, "ii", &conn, &fd))
+ return NULL;
+
+ dummy_iov.iov_base = &dummy_char;
+ dummy_iov.iov_len = 1;
+ msg.msg_control = buf;
+ msg.msg_controllen = sizeof(buf);
+ msg.msg_iov = &dummy_iov;
+ msg.msg_iovlen = 1;
+ cmsg = CMSG_FIRSTHDR(&msg);
+ cmsg->cmsg_level = SOL_SOCKET;
+ cmsg->cmsg_type = SCM_RIGHTS;
+ cmsg->cmsg_len = CMSG_LEN(sizeof(int));
+ msg.msg_controllen = cmsg->cmsg_len;
+ *(int*)CMSG_DATA(cmsg) = fd;
+
+ Py_BEGIN_ALLOW_THREADS
+ res = sendmsg(conn, &msg, 0);
+ Py_END_ALLOW_THREADS
+
+ if (res < 0)
+ return PyErr_SetFromErrno(PyExc_OSError);
+ Py_RETURN_NONE;
+}
+
+static PyObject *
+multiprocessing_recvfd(PyObject *self, PyObject *args)
+{
+ int conn, fd, res;
+ char dummy_char;
+ char buf[CMSG_SPACE(sizeof(int))];
+ struct msghdr msg = {0};
+ struct iovec dummy_iov;
+ struct cmsghdr *cmsg;
+
+ if (!PyArg_ParseTuple(args, "i", &conn))
+ return NULL;
+
+ dummy_iov.iov_base = &dummy_char;
+ dummy_iov.iov_len = 1;
+ msg.msg_control = buf;
+ msg.msg_controllen = sizeof(buf);
+ msg.msg_iov = &dummy_iov;
+ msg.msg_iovlen = 1;
+ cmsg = CMSG_FIRSTHDR(&msg);
+ cmsg->cmsg_level = SOL_SOCKET;
+ cmsg->cmsg_type = SCM_RIGHTS;
+ cmsg->cmsg_len = CMSG_LEN(sizeof(int));
+ msg.msg_controllen = cmsg->cmsg_len;
+
+ Py_BEGIN_ALLOW_THREADS
+ res = recvmsg(conn, &msg, 0);
+ Py_END_ALLOW_THREADS
+
+ if (res < 0)
+ return PyErr_SetFromErrno(PyExc_OSError);
+
+ fd = *(int*)CMSG_DATA(cmsg);
+ return Py_BuildValue("i", fd);
+}
+
+#endif /* HAVE_FD_TRANSFER */
+
+#endif /* !MS_WINDOWS */
+
+
+/*
+ * All platforms
+ */
+
+static PyObject*
+multiprocessing_address_of_buffer(PyObject *self, PyObject *obj)
+{
+ void *buffer;
+ Py_ssize_t buffer_len;
+
+ if (PyObject_AsWriteBuffer(obj, &buffer, &buffer_len) < 0)
+ return NULL;
+
+ return Py_BuildValue("N" F_PY_SSIZE_T,
+ PyLong_FromVoidPtr(buffer), buffer_len);
+}
+
+
+/*
+ * Function table
+ */
+
+static PyMethodDef module_methods[] = {
+ {"address_of_buffer", multiprocessing_address_of_buffer, METH_O,
+ "address_of_buffer(obj) -> int\n"
+ "Return address of obj assuming obj supports buffer inteface"},
+#if HAVE_FD_TRANSFER
+ {"sendfd", multiprocessing_sendfd, METH_VARARGS,
+ "sendfd(sockfd, fd) -> None\n"
+ "Send file descriptor given by fd over the unix domain socket\n"
+ "whose file decriptor is sockfd"},
+ {"recvfd", multiprocessing_recvfd, METH_VARARGS,
+ "recvfd(sockfd) -> fd\n"
+ "Receive a file descriptor over a unix domain socket\n"
+ "whose file decriptor is sockfd"},
+#endif
+ {NULL}
+};
+
+
+/*
+ * Initialize
+ */
+
+static struct PyModuleDef multiprocessing_module = {
+ PyModuleDef_HEAD_INIT,
+ "_multiprocessing",
+ NULL,
+ -1,
+ module_methods,
+ NULL,
+ NULL,
+ NULL,
+ NULL
+};
+
+
+PyMODINIT_FUNC
+PyInit__multiprocessing(void)
+{
+ PyObject *module, *temp, *value;
+
+ /* Initialize module */
+ module = PyModule_Create(&multiprocessing_module);
+ if (!module)
+ return NULL;
+
+ /* Get copy of objects from pickle */
+ temp = PyImport_ImportModule(PICKLE_MODULE);
+ if (!temp)
+ return NULL;
+ pickle_dumps = PyObject_GetAttrString(temp, "dumps");
+ pickle_loads = PyObject_GetAttrString(temp, "loads");
+ pickle_protocol = PyObject_GetAttrString(temp, "HIGHEST_PROTOCOL");
+ Py_XDECREF(temp);
+
+ /* Get copy of BufferTooShort */
+ temp = PyImport_ImportModule("multiprocessing");
+ if (!temp)
+ return NULL;
+ BufferTooShort = PyObject_GetAttrString(temp, "BufferTooShort");
+ Py_XDECREF(temp);
+
+ /* Add connection type to module */
+ if (PyType_Ready(&ConnectionType) < 0)
+ return NULL;
+ Py_INCREF(&ConnectionType);
+ PyModule_AddObject(module, "Connection", (PyObject*)&ConnectionType);
+
+#if defined(MS_WINDOWS) || HAVE_SEM_OPEN
+ /* Add SemLock type to module */
+ if (PyType_Ready(&SemLockType) < 0)
+ return NULL;
+ Py_INCREF(&SemLockType);
+ PyDict_SetItemString(SemLockType.tp_dict, "SEM_VALUE_MAX",
+ Py_BuildValue("i", SEM_VALUE_MAX));
+ PyModule_AddObject(module, "SemLock", (PyObject*)&SemLockType);
+#endif
+
+#ifdef MS_WINDOWS
+ /* Add PipeConnection to module */
+ if (PyType_Ready(&PipeConnectionType) < 0)
+ return NULL;
+ Py_INCREF(&PipeConnectionType);
+ PyModule_AddObject(module, "PipeConnection",
+ (PyObject*)&PipeConnectionType);
+
+ /* Initialize win32 class and add to multiprocessing */
+ temp = create_win32_namespace();
+ if (!temp)
+ return NULL;
+ PyModule_AddObject(module, "win32", temp);
+
+ /* Initialize the event handle used to signal Ctrl-C */
+ sigint_event = CreateEvent(NULL, TRUE, FALSE, NULL);
+ if (!sigint_event) {
+ PyErr_SetFromWindowsErr(0);
+ return NULL;
+ }
+ if (!SetConsoleCtrlHandler(ProcessingCtrlHandler, TRUE)) {
+ PyErr_SetFromWindowsErr(0);
+ return NULL;
+ }
+#endif
+
+ /* Add configuration macros */
+ temp = PyDict_New();
+ if (!temp)
+ return NULL;
+
+#define ADD_FLAG(name) \
+ value = Py_BuildValue("i", name); \
+ if (value == NULL) { Py_DECREF(temp); return NULL; } \
+ if (PyDict_SetItemString(temp, #name, value) < 0) { \
+ Py_DECREF(temp); Py_DECREF(value); return NULL; } \
+ Py_DECREF(value)
+
+#ifdef HAVE_SEM_OPEN
+ ADD_FLAG(HAVE_SEM_OPEN);
+#endif
+#ifdef HAVE_SEM_TIMEDWAIT
+ ADD_FLAG(HAVE_SEM_TIMEDWAIT);
+#endif
+#ifdef HAVE_FD_TRANSFER
+ ADD_FLAG(HAVE_FD_TRANSFER);
+#endif
+#ifdef HAVE_BROKEN_SEM_GETVALUE
+ ADD_FLAG(HAVE_BROKEN_SEM_GETVALUE);
+#endif
+#ifdef HAVE_BROKEN_SEM_UNLINK
+ ADD_FLAG(HAVE_BROKEN_SEM_UNLINK);
+#endif
+
+ if (PyModule_AddObject(module, "flags", temp) < 0)
+ return NULL;
+
+ return module;
+}
-#ifndef MULTIPROCESSING_H\r
-#define MULTIPROCESSING_H\r
-\r
-#define PY_SSIZE_T_CLEAN\r
-\r
-#include "Python.h"\r
-#include "structmember.h"\r
-#include "pythread.h"\r
-\r
-/*\r
- * Platform includes and definitions\r
- */\r
-\r
-#ifdef MS_WINDOWS\r
-# define WIN32_LEAN_AND_MEAN\r
-# include <windows.h>\r
-# include <winsock2.h>\r
-# include <process.h> /* getpid() */\r
-# define SEM_HANDLE HANDLE\r
-# define SEM_VALUE_MAX LONG_MAX\r
-#else\r
-# include <fcntl.h> /* O_CREAT and O_EXCL */\r
-# include <sys/socket.h>\r
-# include <arpa/inet.h> /* htonl() and ntohl() */\r
-# if HAVE_SEM_OPEN\r
-# include <semaphore.h>\r
- typedef sem_t *SEM_HANDLE;\r
-# endif\r
-# define HANDLE int\r
-# define SOCKET int\r
-# define BOOL int\r
-# define UINT32 uint32_t\r
-# define INT32 int32_t\r
-# define TRUE 1\r
-# define FALSE 0\r
-# define INVALID_HANDLE_VALUE (-1)\r
-#endif\r
-\r
-/*\r
- * Make sure Py_ssize_t available\r
- */\r
-\r
-#if PY_VERSION_HEX < 0x02050000 && !defined(PY_SSIZE_T_MIN)\r
- typedef int Py_ssize_t;\r
-# define PY_SSIZE_T_MAX INT_MAX\r
-# define PY_SSIZE_T_MIN INT_MIN\r
-# define F_PY_SSIZE_T "i"\r
-# define PY_FORMAT_SIZE_T ""\r
-# define PyInt_FromSsize_t(n) PyInt_FromLong((long)n)\r
-#else\r
-# define F_PY_SSIZE_T "n"\r
-#endif\r
-\r
-/*\r
- * Format codes\r
- */\r
-\r
-#if SIZEOF_VOID_P == SIZEOF_LONG\r
-# define F_POINTER "k"\r
-# define T_POINTER T_ULONG\r
-#elif defined(HAVE_LONG_LONG) && (SIZEOF_VOID_P == SIZEOF_LONG_LONG)\r
-# define F_POINTER "K"\r
-# define T_POINTER T_ULONGLONG\r
-#else\r
-# error "can't find format code for unsigned integer of same size as void*"\r
-#endif\r
-\r
-#ifdef MS_WINDOWS\r
-# define F_HANDLE F_POINTER\r
-# define T_HANDLE T_POINTER\r
-# define F_SEM_HANDLE F_HANDLE\r
-# define T_SEM_HANDLE T_HANDLE\r
-# define F_DWORD "k"\r
-# define T_DWORD T_ULONG\r
-#else\r
-# define F_HANDLE "i"\r
-# define T_HANDLE T_INT\r
-# define F_SEM_HANDLE F_POINTER\r
-# define T_SEM_HANDLE T_POINTER\r
-#endif\r
-\r
-#if PY_VERSION_HEX >= 0x03000000\r
-# define F_RBUFFER "y"\r
-#else\r
-# define F_RBUFFER "s"\r
-#endif\r
-\r
-/*\r
- * Error codes which can be returned by functions called without GIL\r
- */\r
-\r
-#define MP_SUCCESS (0)\r
-#define MP_STANDARD_ERROR (-1)\r
-#define MP_MEMORY_ERROR (-1001)\r
-#define MP_END_OF_FILE (-1002)\r
-#define MP_EARLY_END_OF_FILE (-1003)\r
-#define MP_BAD_MESSAGE_LENGTH (-1004)\r
-#define MP_SOCKET_ERROR (-1005)\r
-#define MP_EXCEPTION_HAS_BEEN_SET (-1006)\r
-\r
-PyObject *mp_SetError(PyObject *Type, int num);\r
-\r
-/*\r
- * Externs - not all will really exist on all platforms\r
- */\r
-\r
-extern PyObject *pickle_dumps;\r
-extern PyObject *pickle_loads;\r
-extern PyObject *pickle_protocol;\r
-extern PyObject *BufferTooShort;\r
-extern PyTypeObject SemLockType;\r
-extern PyTypeObject ConnectionType;\r
-extern PyTypeObject PipeConnectionType;\r
-extern HANDLE sigint_event;\r
-\r
-/*\r
- * Py3k compatibility\r
- */\r
-\r
-#if PY_VERSION_HEX >= 0x03000000\r
-# define PICKLE_MODULE "pickle"\r
-# define FROM_FORMAT PyUnicode_FromFormat\r
-# define PyInt_FromLong PyLong_FromLong\r
-# define PyInt_FromSsize_t PyLong_FromSsize_t\r
-#else\r
-# define PICKLE_MODULE "cPickle"\r
-# define FROM_FORMAT PyString_FromFormat\r
-#endif\r
-\r
-#ifndef PyVarObject_HEAD_INIT\r
-# define PyVarObject_HEAD_INIT(type, size) PyObject_HEAD_INIT(type) size,\r
-#endif\r
-\r
-#ifndef Py_TPFLAGS_HAVE_WEAKREFS\r
-# define Py_TPFLAGS_HAVE_WEAKREFS 0\r
-#endif\r
-\r
-/*\r
- * Connection definition\r
- */\r
-\r
-#define CONNECTION_BUFFER_SIZE 1024\r
-\r
-typedef struct {\r
- PyObject_HEAD\r
- HANDLE handle;\r
- int flags;\r
- PyObject *weakreflist;\r
- char buffer[CONNECTION_BUFFER_SIZE];\r
-} ConnectionObject;\r
-\r
-/*\r
- * Miscellaneous\r
- */\r
-\r
-#define MAX_MESSAGE_LENGTH 0x7fffffff\r
-\r
-#ifndef MIN\r
-# define MIN(x, y) ((x) < (y) ? x : y)\r
-# define MAX(x, y) ((x) > (y) ? x : y)\r
-#endif\r
-\r
-#endif /* MULTIPROCESSING_H */\r
+#ifndef MULTIPROCESSING_H
+#define MULTIPROCESSING_H
+
+#define PY_SSIZE_T_CLEAN
+
+#include "Python.h"
+#include "structmember.h"
+#include "pythread.h"
+
+/*
+ * Platform includes and definitions
+ */
+
+#ifdef MS_WINDOWS
+# define WIN32_LEAN_AND_MEAN
+# include <windows.h>
+# include <winsock2.h>
+# include <process.h> /* getpid() */
+# define SEM_HANDLE HANDLE
+# define SEM_VALUE_MAX LONG_MAX
+#else
+# include <fcntl.h> /* O_CREAT and O_EXCL */
+# include <sys/socket.h>
+# include <arpa/inet.h> /* htonl() and ntohl() */
+# if HAVE_SEM_OPEN
+# include <semaphore.h>
+ typedef sem_t *SEM_HANDLE;
+# endif
+# define HANDLE int
+# define SOCKET int
+# define BOOL int
+# define UINT32 uint32_t
+# define INT32 int32_t
+# define TRUE 1
+# define FALSE 0
+# define INVALID_HANDLE_VALUE (-1)
+#endif
+
+/*
+ * Make sure Py_ssize_t available
+ */
+
+#if PY_VERSION_HEX < 0x02050000 && !defined(PY_SSIZE_T_MIN)
+ typedef int Py_ssize_t;
+# define PY_SSIZE_T_MAX INT_MAX
+# define PY_SSIZE_T_MIN INT_MIN
+# define F_PY_SSIZE_T "i"
+# define PY_FORMAT_SIZE_T ""
+# define PyInt_FromSsize_t(n) PyInt_FromLong((long)n)
+#else
+# define F_PY_SSIZE_T "n"
+#endif
+
+/*
+ * Format codes
+ */
+
+#if SIZEOF_VOID_P == SIZEOF_LONG
+# define F_POINTER "k"
+# define T_POINTER T_ULONG
+#elif defined(HAVE_LONG_LONG) && (SIZEOF_VOID_P == SIZEOF_LONG_LONG)
+# define F_POINTER "K"
+# define T_POINTER T_ULONGLONG
+#else
+# error "can't find format code for unsigned integer of same size as void*"
+#endif
+
+#ifdef MS_WINDOWS
+# define F_HANDLE F_POINTER
+# define T_HANDLE T_POINTER
+# define F_SEM_HANDLE F_HANDLE
+# define T_SEM_HANDLE T_HANDLE
+# define F_DWORD "k"
+# define T_DWORD T_ULONG
+#else
+# define F_HANDLE "i"
+# define T_HANDLE T_INT
+# define F_SEM_HANDLE F_POINTER
+# define T_SEM_HANDLE T_POINTER
+#endif
+
+#if PY_VERSION_HEX >= 0x03000000
+# define F_RBUFFER "y"
+#else
+# define F_RBUFFER "s"
+#endif
+
+/*
+ * Error codes which can be returned by functions called without GIL
+ */
+
+#define MP_SUCCESS (0)
+#define MP_STANDARD_ERROR (-1)
+#define MP_MEMORY_ERROR (-1001)
+#define MP_END_OF_FILE (-1002)
+#define MP_EARLY_END_OF_FILE (-1003)
+#define MP_BAD_MESSAGE_LENGTH (-1004)
+#define MP_SOCKET_ERROR (-1005)
+#define MP_EXCEPTION_HAS_BEEN_SET (-1006)
+
+PyObject *mp_SetError(PyObject *Type, int num);
+
+/*
+ * Externs - not all will really exist on all platforms
+ */
+
+extern PyObject *pickle_dumps;
+extern PyObject *pickle_loads;
+extern PyObject *pickle_protocol;
+extern PyObject *BufferTooShort;
+extern PyTypeObject SemLockType;
+extern PyTypeObject ConnectionType;
+extern PyTypeObject PipeConnectionType;
+extern HANDLE sigint_event;
+
+/*
+ * Py3k compatibility
+ */
+
+#if PY_VERSION_HEX >= 0x03000000
+# define PICKLE_MODULE "pickle"
+# define FROM_FORMAT PyUnicode_FromFormat
+# define PyInt_FromLong PyLong_FromLong
+# define PyInt_FromSsize_t PyLong_FromSsize_t
+#else
+# define PICKLE_MODULE "cPickle"
+# define FROM_FORMAT PyString_FromFormat
+#endif
+
+#ifndef PyVarObject_HEAD_INIT
+# define PyVarObject_HEAD_INIT(type, size) PyObject_HEAD_INIT(type) size,
+#endif
+
+#ifndef Py_TPFLAGS_HAVE_WEAKREFS
+# define Py_TPFLAGS_HAVE_WEAKREFS 0
+#endif
+
+/*
+ * Connection definition
+ */
+
+#define CONNECTION_BUFFER_SIZE 1024
+
+typedef struct {
+ PyObject_HEAD
+ HANDLE handle;
+ int flags;
+ PyObject *weakreflist;
+ char buffer[CONNECTION_BUFFER_SIZE];
+} ConnectionObject;
+
+/*
+ * Miscellaneous
+ */
+
+#define MAX_MESSAGE_LENGTH 0x7fffffff
+
+#ifndef MIN
+# define MIN(x, y) ((x) < (y) ? x : y)
+# define MAX(x, y) ((x) > (y) ? x : y)
+#endif
+
+#endif /* MULTIPROCESSING_H */
-/*\r
- * A type which wraps a pipe handle in message oriented mode\r
- *\r
- * pipe_connection.c\r
- *\r
- * Copyright (c) 2006-2008, R Oudkerk --- see COPYING.txt\r
- */\r
-\r
-#include "multiprocessing.h"\r
-\r
-#define CLOSE(h) CloseHandle(h)\r
-\r
-/*\r
- * Send string to the pipe; assumes in message oriented mode\r
- */\r
-\r
-static Py_ssize_t\r
-conn_send_string(ConnectionObject *conn, char *string, size_t length)\r
-{\r
- DWORD amount_written;\r
-\r
- return WriteFile(conn->handle, string, length, &amount_written, NULL)\r
- ? MP_SUCCESS : MP_STANDARD_ERROR;\r
-}\r
-\r
-/*\r
- * Attempts to read into buffer, or if buffer too small into *newbuffer.\r
- *\r
- * Returns number of bytes read. Assumes in message oriented mode.\r
- */\r
-\r
-static Py_ssize_t\r
-conn_recv_string(ConnectionObject *conn, char *buffer, \r
- size_t buflength, char **newbuffer, size_t maxlength)\r
-{\r
- DWORD left, length, full_length, err;\r
-\r
- *newbuffer = NULL;\r
-\r
- if (ReadFile(conn->handle, buffer, MIN(buflength, maxlength), \r
- &length, NULL))\r
- return length;\r
-\r
- err = GetLastError();\r
- if (err != ERROR_MORE_DATA) {\r
- if (err == ERROR_BROKEN_PIPE)\r
- return MP_END_OF_FILE;\r
- return MP_STANDARD_ERROR;\r
- }\r
-\r
- if (!PeekNamedPipe(conn->handle, NULL, 0, NULL, NULL, &left))\r
- return MP_STANDARD_ERROR;\r
-\r
- full_length = length + left;\r
- if (full_length > maxlength)\r
- return MP_BAD_MESSAGE_LENGTH;\r
-\r
- *newbuffer = PyMem_Malloc(full_length);\r
- if (*newbuffer == NULL)\r
- return MP_MEMORY_ERROR;\r
-\r
- memcpy(*newbuffer, buffer, length);\r
-\r
- if (ReadFile(conn->handle, *newbuffer+length, left, &length, NULL)) {\r
- assert(length == left);\r
- return full_length;\r
- } else {\r
- PyMem_Free(*newbuffer);\r
- return MP_STANDARD_ERROR;\r
- }\r
-}\r
-\r
-/*\r
- * Check whether any data is available for reading\r
- */\r
-\r
-#define conn_poll(conn, timeout) conn_poll_save(conn, timeout, _save)\r
-\r
-static int\r
-conn_poll_save(ConnectionObject *conn, double timeout, PyThreadState *_save)\r
-{\r
- DWORD bytes, deadline, delay;\r
- int difference, res;\r
- BOOL block = FALSE;\r
-\r
- if (!PeekNamedPipe(conn->handle, NULL, 0, NULL, &bytes, NULL))\r
- return MP_STANDARD_ERROR;\r
-\r
- if (timeout == 0.0)\r
- return bytes > 0;\r
-\r
- if (timeout < 0.0)\r
- block = TRUE;\r
- else\r
- /* XXX does not check for overflow */\r
- deadline = GetTickCount() + (DWORD)(1000 * timeout + 0.5);\r
-\r
- Sleep(0);\r
-\r
- for (delay = 1 ; ; delay += 1) {\r
- if (!PeekNamedPipe(conn->handle, NULL, 0, NULL, &bytes, NULL))\r
- return MP_STANDARD_ERROR;\r
- else if (bytes > 0)\r
- return TRUE;\r
-\r
- if (!block) {\r
- difference = deadline - GetTickCount();\r
- if (difference < 0)\r
- return FALSE;\r
- if ((int)delay > difference)\r
- delay = difference;\r
- }\r
-\r
- if (delay > 20)\r
- delay = 20;\r
-\r
- Sleep(delay);\r
-\r
- /* check for signals */\r
- Py_BLOCK_THREADS \r
- res = PyErr_CheckSignals();\r
- Py_UNBLOCK_THREADS\r
-\r
- if (res)\r
- return MP_EXCEPTION_HAS_BEEN_SET;\r
- }\r
-}\r
-\r
-/*\r
- * "connection.h" defines the PipeConnection type using the definitions above\r
- */\r
-\r
-#define CONNECTION_NAME "PipeConnection"\r
-#define CONNECTION_TYPE PipeConnectionType\r
-\r
-#include "connection.h"\r
+/*
+ * A type which wraps a pipe handle in message oriented mode
+ *
+ * pipe_connection.c
+ *
+ * Copyright (c) 2006-2008, R Oudkerk --- see COPYING.txt
+ */
+
+#include "multiprocessing.h"
+
+#define CLOSE(h) CloseHandle(h)
+
+/*
+ * Send string to the pipe; assumes in message oriented mode
+ */
+
+static Py_ssize_t
+conn_send_string(ConnectionObject *conn, char *string, size_t length)
+{
+ DWORD amount_written;
+
+ return WriteFile(conn->handle, string, length, &amount_written, NULL)
+ ? MP_SUCCESS : MP_STANDARD_ERROR;
+}
+
+/*
+ * Attempts to read into buffer, or if buffer too small into *newbuffer.
+ *
+ * Returns number of bytes read. Assumes in message oriented mode.
+ */
+
+static Py_ssize_t
+conn_recv_string(ConnectionObject *conn, char *buffer,
+ size_t buflength, char **newbuffer, size_t maxlength)
+{
+ DWORD left, length, full_length, err;
+
+ *newbuffer = NULL;
+
+ if (ReadFile(conn->handle, buffer, MIN(buflength, maxlength),
+ &length, NULL))
+ return length;
+
+ err = GetLastError();
+ if (err != ERROR_MORE_DATA) {
+ if (err == ERROR_BROKEN_PIPE)
+ return MP_END_OF_FILE;
+ return MP_STANDARD_ERROR;
+ }
+
+ if (!PeekNamedPipe(conn->handle, NULL, 0, NULL, NULL, &left))
+ return MP_STANDARD_ERROR;
+
+ full_length = length + left;
+ if (full_length > maxlength)
+ return MP_BAD_MESSAGE_LENGTH;
+
+ *newbuffer = PyMem_Malloc(full_length);
+ if (*newbuffer == NULL)
+ return MP_MEMORY_ERROR;
+
+ memcpy(*newbuffer, buffer, length);
+
+ if (ReadFile(conn->handle, *newbuffer+length, left, &length, NULL)) {
+ assert(length == left);
+ return full_length;
+ } else {
+ PyMem_Free(*newbuffer);
+ return MP_STANDARD_ERROR;
+ }
+}
+
+/*
+ * Check whether any data is available for reading
+ */
+
+#define conn_poll(conn, timeout) conn_poll_save(conn, timeout, _save)
+
+static int
+conn_poll_save(ConnectionObject *conn, double timeout, PyThreadState *_save)
+{
+ DWORD bytes, deadline, delay;
+ int difference, res;
+ BOOL block = FALSE;
+
+ if (!PeekNamedPipe(conn->handle, NULL, 0, NULL, &bytes, NULL))
+ return MP_STANDARD_ERROR;
+
+ if (timeout == 0.0)
+ return bytes > 0;
+
+ if (timeout < 0.0)
+ block = TRUE;
+ else
+ /* XXX does not check for overflow */
+ deadline = GetTickCount() + (DWORD)(1000 * timeout + 0.5);
+
+ Sleep(0);
+
+ for (delay = 1 ; ; delay += 1) {
+ if (!PeekNamedPipe(conn->handle, NULL, 0, NULL, &bytes, NULL))
+ return MP_STANDARD_ERROR;
+ else if (bytes > 0)
+ return TRUE;
+
+ if (!block) {
+ difference = deadline - GetTickCount();
+ if (difference < 0)
+ return FALSE;
+ if ((int)delay > difference)
+ delay = difference;
+ }
+
+ if (delay > 20)
+ delay = 20;
+
+ Sleep(delay);
+
+ /* check for signals */
+ Py_BLOCK_THREADS
+ res = PyErr_CheckSignals();
+ Py_UNBLOCK_THREADS
+
+ if (res)
+ return MP_EXCEPTION_HAS_BEEN_SET;
+ }
+}
+
+/*
+ * "connection.h" defines the PipeConnection type using the definitions above
+ */
+
+#define CONNECTION_NAME "PipeConnection"
+#define CONNECTION_TYPE PipeConnectionType
+
+#include "connection.h"
-/*\r
- * Win32 functions used by multiprocessing package\r
- *\r
- * win32_functions.c\r
- *\r
- * Copyright (c) 2006-2008, R Oudkerk --- see COPYING.txt\r
- */\r
-\r
-#include "multiprocessing.h"\r
-\r
-\r
-#define WIN32_FUNCTION(func) \\r
- {#func, (PyCFunction)win32_ ## func, METH_VARARGS | METH_STATIC, ""}\r
-\r
-#define WIN32_CONSTANT(fmt, con) \\r
- PyDict_SetItemString(Win32Type.tp_dict, #con, Py_BuildValue(fmt, con))\r
-\r
-\r
-static PyObject *\r
-win32_CloseHandle(PyObject *self, PyObject *args)\r
-{\r
- HANDLE hObject;\r
- BOOL success;\r
-\r
- if (!PyArg_ParseTuple(args, F_HANDLE, &hObject))\r
- return NULL;\r
-\r
- Py_BEGIN_ALLOW_THREADS\r
- success = CloseHandle(hObject); \r
- Py_END_ALLOW_THREADS\r
-\r
- if (!success)\r
- return PyErr_SetFromWindowsErr(0);\r
-\r
- Py_RETURN_NONE;\r
-}\r
-\r
-static PyObject *\r
-win32_ConnectNamedPipe(PyObject *self, PyObject *args)\r
-{\r
- HANDLE hNamedPipe;\r
- LPOVERLAPPED lpOverlapped;\r
- BOOL success;\r
-\r
- if (!PyArg_ParseTuple(args, F_HANDLE F_POINTER, \r
- &hNamedPipe, &lpOverlapped))\r
- return NULL;\r
-\r
- Py_BEGIN_ALLOW_THREADS\r
- success = ConnectNamedPipe(hNamedPipe, lpOverlapped);\r
- Py_END_ALLOW_THREADS\r
-\r
- if (!success)\r
- return PyErr_SetFromWindowsErr(0);\r
-\r
- Py_RETURN_NONE;\r
-}\r
-\r
-static PyObject *\r
-win32_CreateFile(PyObject *self, PyObject *args)\r
-{\r
- LPCTSTR lpFileName;\r
- DWORD dwDesiredAccess;\r
- DWORD dwShareMode;\r
- LPSECURITY_ATTRIBUTES lpSecurityAttributes;\r
- DWORD dwCreationDisposition;\r
- DWORD dwFlagsAndAttributes;\r
- HANDLE hTemplateFile;\r
- HANDLE handle;\r
-\r
- if (!PyArg_ParseTuple(args, "s" F_DWORD F_DWORD F_POINTER \r
- F_DWORD F_DWORD F_HANDLE,\r
- &lpFileName, &dwDesiredAccess, &dwShareMode, \r
- &lpSecurityAttributes, &dwCreationDisposition, \r
- &dwFlagsAndAttributes, &hTemplateFile))\r
- return NULL;\r
-\r
- Py_BEGIN_ALLOW_THREADS\r
- handle = CreateFile(lpFileName, dwDesiredAccess, \r
- dwShareMode, lpSecurityAttributes, \r
- dwCreationDisposition, \r
- dwFlagsAndAttributes, hTemplateFile);\r
- Py_END_ALLOW_THREADS\r
-\r
- if (handle == INVALID_HANDLE_VALUE)\r
- return PyErr_SetFromWindowsErr(0);\r
-\r
- return Py_BuildValue(F_HANDLE, handle);\r
-}\r
-\r
-static PyObject *\r
-win32_CreateNamedPipe(PyObject *self, PyObject *args)\r
-{\r
- LPCTSTR lpName;\r
- DWORD dwOpenMode;\r
- DWORD dwPipeMode;\r
- DWORD nMaxInstances;\r
- DWORD nOutBufferSize;\r
- DWORD nInBufferSize;\r
- DWORD nDefaultTimeOut;\r
- LPSECURITY_ATTRIBUTES lpSecurityAttributes;\r
- HANDLE handle;\r
-\r
- if (!PyArg_ParseTuple(args, "s" F_DWORD F_DWORD F_DWORD \r
- F_DWORD F_DWORD F_DWORD F_POINTER,\r
- &lpName, &dwOpenMode, &dwPipeMode, \r
- &nMaxInstances, &nOutBufferSize, \r
- &nInBufferSize, &nDefaultTimeOut,\r
- &lpSecurityAttributes))\r
- return NULL;\r
-\r
- Py_BEGIN_ALLOW_THREADS\r
- handle = CreateNamedPipe(lpName, dwOpenMode, dwPipeMode, \r
- nMaxInstances, nOutBufferSize, \r
- nInBufferSize, nDefaultTimeOut,\r
- lpSecurityAttributes);\r
- Py_END_ALLOW_THREADS\r
-\r
- if (handle == INVALID_HANDLE_VALUE)\r
- return PyErr_SetFromWindowsErr(0);\r
-\r
- return Py_BuildValue(F_HANDLE, handle);\r
-}\r
-\r
-static PyObject *\r
-win32_ExitProcess(PyObject *self, PyObject *args)\r
-{\r
- UINT uExitCode;\r
-\r
- if (!PyArg_ParseTuple(args, "I", &uExitCode))\r
- return NULL;\r
-\r
- ExitProcess(uExitCode);\r
-\r
- return NULL;\r
-}\r
-\r
-static PyObject *\r
-win32_GetLastError(PyObject *self, PyObject *args)\r
-{\r
- return Py_BuildValue(F_DWORD, GetLastError());\r
-}\r
-\r
-static PyObject *\r
-win32_OpenProcess(PyObject *self, PyObject *args)\r
-{\r
- DWORD dwDesiredAccess;\r
- BOOL bInheritHandle;\r
- DWORD dwProcessId;\r
- HANDLE handle;\r
-\r
- if (!PyArg_ParseTuple(args, F_DWORD "i" F_DWORD, \r
- &dwDesiredAccess, &bInheritHandle, &dwProcessId))\r
- return NULL;\r
-\r
- handle = OpenProcess(dwDesiredAccess, bInheritHandle, dwProcessId); \r
- if (handle == NULL)\r
- return PyErr_SetFromWindowsErr(0);\r
-\r
- return Py_BuildValue(F_HANDLE, handle);\r
-}\r
-\r
-static PyObject *\r
-win32_SetNamedPipeHandleState(PyObject *self, PyObject *args)\r
-{\r
- HANDLE hNamedPipe;\r
- PyObject *oArgs[3];\r
- DWORD dwArgs[3], *pArgs[3] = {NULL, NULL, NULL};\r
- int i;\r
-\r
- if (!PyArg_ParseTuple(args, F_HANDLE "OOO", \r
- &hNamedPipe, &oArgs[0], &oArgs[1], &oArgs[2]))\r
- return NULL;\r
-\r
- PyErr_Clear();\r
-\r
- for (i = 0 ; i < 3 ; i++) {\r
- if (oArgs[i] != Py_None) {\r
- dwArgs[i] = PyLong_AsUnsignedLongMask(oArgs[i]);\r
- if (PyErr_Occurred())\r
- return NULL;\r
- pArgs[i] = &dwArgs[i];\r
- }\r
- }\r
-\r
- if (!SetNamedPipeHandleState(hNamedPipe, pArgs[0], pArgs[1], pArgs[2]))\r
- return PyErr_SetFromWindowsErr(0);\r
-\r
- Py_RETURN_NONE;\r
-}\r
-\r
-static PyObject *\r
-win32_WaitNamedPipe(PyObject *self, PyObject *args)\r
-{\r
- LPCTSTR lpNamedPipeName;\r
- DWORD nTimeOut;\r
- BOOL success;\r
-\r
- if (!PyArg_ParseTuple(args, "s" F_DWORD, &lpNamedPipeName, &nTimeOut))\r
- return NULL;\r
-\r
- Py_BEGIN_ALLOW_THREADS\r
- success = WaitNamedPipe(lpNamedPipeName, nTimeOut);\r
- Py_END_ALLOW_THREADS\r
-\r
- if (!success)\r
- return PyErr_SetFromWindowsErr(0);\r
-\r
- Py_RETURN_NONE;\r
-}\r
-\r
-static PyMethodDef win32_methods[] = {\r
- WIN32_FUNCTION(CloseHandle),\r
- WIN32_FUNCTION(GetLastError),\r
- WIN32_FUNCTION(OpenProcess),\r
- WIN32_FUNCTION(ExitProcess),\r
- WIN32_FUNCTION(ConnectNamedPipe),\r
- WIN32_FUNCTION(CreateFile),\r
- WIN32_FUNCTION(CreateNamedPipe),\r
- WIN32_FUNCTION(SetNamedPipeHandleState),\r
- WIN32_FUNCTION(WaitNamedPipe),\r
- {NULL}\r
-};\r
-\r
-\r
-PyTypeObject Win32Type = {\r
- PyVarObject_HEAD_INIT(NULL, 0)\r
-};\r
-\r
-\r
-PyObject *\r
-create_win32_namespace(void)\r
-{\r
- Win32Type.tp_name = "_multiprocessing.win32";\r
- Win32Type.tp_methods = win32_methods;\r
- if (PyType_Ready(&Win32Type) < 0)\r
- return NULL;\r
- Py_INCREF(&Win32Type);\r
-\r
- WIN32_CONSTANT(F_DWORD, ERROR_ALREADY_EXISTS);\r
- WIN32_CONSTANT(F_DWORD, ERROR_PIPE_BUSY);\r
- WIN32_CONSTANT(F_DWORD, ERROR_PIPE_CONNECTED);\r
- WIN32_CONSTANT(F_DWORD, ERROR_SEM_TIMEOUT);\r
- WIN32_CONSTANT(F_DWORD, GENERIC_READ);\r
- WIN32_CONSTANT(F_DWORD, GENERIC_WRITE);\r
- WIN32_CONSTANT(F_DWORD, INFINITE);\r
- WIN32_CONSTANT(F_DWORD, NMPWAIT_WAIT_FOREVER);\r
- WIN32_CONSTANT(F_DWORD, OPEN_EXISTING);\r
- WIN32_CONSTANT(F_DWORD, PIPE_ACCESS_DUPLEX);\r
- WIN32_CONSTANT(F_DWORD, PIPE_ACCESS_INBOUND);\r
- WIN32_CONSTANT(F_DWORD, PIPE_READMODE_MESSAGE);\r
- WIN32_CONSTANT(F_DWORD, PIPE_TYPE_MESSAGE);\r
- WIN32_CONSTANT(F_DWORD, PIPE_UNLIMITED_INSTANCES);\r
- WIN32_CONSTANT(F_DWORD, PIPE_WAIT);\r
- WIN32_CONSTANT(F_DWORD, PROCESS_ALL_ACCESS);\r
-\r
- WIN32_CONSTANT("i", NULL);\r
-\r
- return (PyObject*)&Win32Type;\r
-}\r
+/*
+ * Win32 functions used by multiprocessing package
+ *
+ * win32_functions.c
+ *
+ * Copyright (c) 2006-2008, R Oudkerk --- see COPYING.txt
+ */
+
+#include "multiprocessing.h"
+
+
+#define WIN32_FUNCTION(func) \
+ {#func, (PyCFunction)win32_ ## func, METH_VARARGS | METH_STATIC, ""}
+
+#define WIN32_CONSTANT(fmt, con) \
+ PyDict_SetItemString(Win32Type.tp_dict, #con, Py_BuildValue(fmt, con))
+
+
+static PyObject *
+win32_CloseHandle(PyObject *self, PyObject *args)
+{
+ HANDLE hObject;
+ BOOL success;
+
+ if (!PyArg_ParseTuple(args, F_HANDLE, &hObject))
+ return NULL;
+
+ Py_BEGIN_ALLOW_THREADS
+ success = CloseHandle(hObject);
+ Py_END_ALLOW_THREADS
+
+ if (!success)
+ return PyErr_SetFromWindowsErr(0);
+
+ Py_RETURN_NONE;
+}
+
+static PyObject *
+win32_ConnectNamedPipe(PyObject *self, PyObject *args)
+{
+ HANDLE hNamedPipe;
+ LPOVERLAPPED lpOverlapped;
+ BOOL success;
+
+ if (!PyArg_ParseTuple(args, F_HANDLE F_POINTER,
+ &hNamedPipe, &lpOverlapped))
+ return NULL;
+
+ Py_BEGIN_ALLOW_THREADS
+ success = ConnectNamedPipe(hNamedPipe, lpOverlapped);
+ Py_END_ALLOW_THREADS
+
+ if (!success)
+ return PyErr_SetFromWindowsErr(0);
+
+ Py_RETURN_NONE;
+}
+
+static PyObject *
+win32_CreateFile(PyObject *self, PyObject *args)
+{
+ LPCTSTR lpFileName;
+ DWORD dwDesiredAccess;
+ DWORD dwShareMode;
+ LPSECURITY_ATTRIBUTES lpSecurityAttributes;
+ DWORD dwCreationDisposition;
+ DWORD dwFlagsAndAttributes;
+ HANDLE hTemplateFile;
+ HANDLE handle;
+
+ if (!PyArg_ParseTuple(args, "s" F_DWORD F_DWORD F_POINTER
+ F_DWORD F_DWORD F_HANDLE,
+ &lpFileName, &dwDesiredAccess, &dwShareMode,
+ &lpSecurityAttributes, &dwCreationDisposition,
+ &dwFlagsAndAttributes, &hTemplateFile))
+ return NULL;
+
+ Py_BEGIN_ALLOW_THREADS
+ handle = CreateFile(lpFileName, dwDesiredAccess,
+ dwShareMode, lpSecurityAttributes,
+ dwCreationDisposition,
+ dwFlagsAndAttributes, hTemplateFile);
+ Py_END_ALLOW_THREADS
+
+ if (handle == INVALID_HANDLE_VALUE)
+ return PyErr_SetFromWindowsErr(0);
+
+ return Py_BuildValue(F_HANDLE, handle);
+}
+
+static PyObject *
+win32_CreateNamedPipe(PyObject *self, PyObject *args)
+{
+ LPCTSTR lpName;
+ DWORD dwOpenMode;
+ DWORD dwPipeMode;
+ DWORD nMaxInstances;
+ DWORD nOutBufferSize;
+ DWORD nInBufferSize;
+ DWORD nDefaultTimeOut;
+ LPSECURITY_ATTRIBUTES lpSecurityAttributes;
+ HANDLE handle;
+
+ if (!PyArg_ParseTuple(args, "s" F_DWORD F_DWORD F_DWORD
+ F_DWORD F_DWORD F_DWORD F_POINTER,
+ &lpName, &dwOpenMode, &dwPipeMode,
+ &nMaxInstances, &nOutBufferSize,
+ &nInBufferSize, &nDefaultTimeOut,
+ &lpSecurityAttributes))
+ return NULL;
+
+ Py_BEGIN_ALLOW_THREADS
+ handle = CreateNamedPipe(lpName, dwOpenMode, dwPipeMode,
+ nMaxInstances, nOutBufferSize,
+ nInBufferSize, nDefaultTimeOut,
+ lpSecurityAttributes);
+ Py_END_ALLOW_THREADS
+
+ if (handle == INVALID_HANDLE_VALUE)
+ return PyErr_SetFromWindowsErr(0);
+
+ return Py_BuildValue(F_HANDLE, handle);
+}
+
+static PyObject *
+win32_ExitProcess(PyObject *self, PyObject *args)
+{
+ UINT uExitCode;
+
+ if (!PyArg_ParseTuple(args, "I", &uExitCode))
+ return NULL;
+
+ ExitProcess(uExitCode);
+
+ return NULL;
+}
+
+static PyObject *
+win32_GetLastError(PyObject *self, PyObject *args)
+{
+ return Py_BuildValue(F_DWORD, GetLastError());
+}
+
+static PyObject *
+win32_OpenProcess(PyObject *self, PyObject *args)
+{
+ DWORD dwDesiredAccess;
+ BOOL bInheritHandle;
+ DWORD dwProcessId;
+ HANDLE handle;
+
+ if (!PyArg_ParseTuple(args, F_DWORD "i" F_DWORD,
+ &dwDesiredAccess, &bInheritHandle, &dwProcessId))
+ return NULL;
+
+ handle = OpenProcess(dwDesiredAccess, bInheritHandle, dwProcessId);
+ if (handle == NULL)
+ return PyErr_SetFromWindowsErr(0);
+
+ return Py_BuildValue(F_HANDLE, handle);
+}
+
+static PyObject *
+win32_SetNamedPipeHandleState(PyObject *self, PyObject *args)
+{
+ HANDLE hNamedPipe;
+ PyObject *oArgs[3];
+ DWORD dwArgs[3], *pArgs[3] = {NULL, NULL, NULL};
+ int i;
+
+ if (!PyArg_ParseTuple(args, F_HANDLE "OOO",
+ &hNamedPipe, &oArgs[0], &oArgs[1], &oArgs[2]))
+ return NULL;
+
+ PyErr_Clear();
+
+ for (i = 0 ; i < 3 ; i++) {
+ if (oArgs[i] != Py_None) {
+ dwArgs[i] = PyLong_AsUnsignedLongMask(oArgs[i]);
+ if (PyErr_Occurred())
+ return NULL;
+ pArgs[i] = &dwArgs[i];
+ }
+ }
+
+ if (!SetNamedPipeHandleState(hNamedPipe, pArgs[0], pArgs[1], pArgs[2]))
+ return PyErr_SetFromWindowsErr(0);
+
+ Py_RETURN_NONE;
+}
+
+static PyObject *
+win32_WaitNamedPipe(PyObject *self, PyObject *args)
+{
+ LPCTSTR lpNamedPipeName;
+ DWORD nTimeOut;
+ BOOL success;
+
+ if (!PyArg_ParseTuple(args, "s" F_DWORD, &lpNamedPipeName, &nTimeOut))
+ return NULL;
+
+ Py_BEGIN_ALLOW_THREADS
+ success = WaitNamedPipe(lpNamedPipeName, nTimeOut);
+ Py_END_ALLOW_THREADS
+
+ if (!success)
+ return PyErr_SetFromWindowsErr(0);
+
+ Py_RETURN_NONE;
+}
+
+static PyMethodDef win32_methods[] = {
+ WIN32_FUNCTION(CloseHandle),
+ WIN32_FUNCTION(GetLastError),
+ WIN32_FUNCTION(OpenProcess),
+ WIN32_FUNCTION(ExitProcess),
+ WIN32_FUNCTION(ConnectNamedPipe),
+ WIN32_FUNCTION(CreateFile),
+ WIN32_FUNCTION(CreateNamedPipe),
+ WIN32_FUNCTION(SetNamedPipeHandleState),
+ WIN32_FUNCTION(WaitNamedPipe),
+ {NULL}
+};
+
+
+PyTypeObject Win32Type = {
+ PyVarObject_HEAD_INIT(NULL, 0)
+};
+
+
+PyObject *
+create_win32_namespace(void)
+{
+ Win32Type.tp_name = "_multiprocessing.win32";
+ Win32Type.tp_methods = win32_methods;
+ if (PyType_Ready(&Win32Type) < 0)
+ return NULL;
+ Py_INCREF(&Win32Type);
+
+ WIN32_CONSTANT(F_DWORD, ERROR_ALREADY_EXISTS);
+ WIN32_CONSTANT(F_DWORD, ERROR_PIPE_BUSY);
+ WIN32_CONSTANT(F_DWORD, ERROR_PIPE_CONNECTED);
+ WIN32_CONSTANT(F_DWORD, ERROR_SEM_TIMEOUT);
+ WIN32_CONSTANT(F_DWORD, GENERIC_READ);
+ WIN32_CONSTANT(F_DWORD, GENERIC_WRITE);
+ WIN32_CONSTANT(F_DWORD, INFINITE);
+ WIN32_CONSTANT(F_DWORD, NMPWAIT_WAIT_FOREVER);
+ WIN32_CONSTANT(F_DWORD, OPEN_EXISTING);
+ WIN32_CONSTANT(F_DWORD, PIPE_ACCESS_DUPLEX);
+ WIN32_CONSTANT(F_DWORD, PIPE_ACCESS_INBOUND);
+ WIN32_CONSTANT(F_DWORD, PIPE_READMODE_MESSAGE);
+ WIN32_CONSTANT(F_DWORD, PIPE_TYPE_MESSAGE);
+ WIN32_CONSTANT(F_DWORD, PIPE_UNLIMITED_INSTANCES);
+ WIN32_CONSTANT(F_DWORD, PIPE_WAIT);
+ WIN32_CONSTANT(F_DWORD, PROCESS_ALL_ACCESS);
+
+ WIN32_CONSTANT("i", NULL);
+
+ return (PyObject*)&Win32Type;
+}