Update python to 2.5.4 (#4408)
[openwrt/svn-archive/archive.git] / lang / python / patches / 050-add_signal_set_wakeup_fd.patch
1 diff -urN Python-2.5.1.orig/Include/pyerrors.h Python-2.5.1/Include/pyerrors.h
2 --- Python-2.5.1.orig/Include/pyerrors.h 2008-08-10 13:46:48.000000000 +0200
3 +++ Python-2.5.1/Include/pyerrors.h 2008-08-10 13:51:05.000000000 +0200
4 @@ -239,6 +239,9 @@
5 PyAPI_FUNC(int) PyErr_CheckSignals(void);
6 PyAPI_FUNC(void) PyErr_SetInterrupt(void);
7
8 +/* In signalmodule.c */
9 +int PySignal_SetWakeupFd(int fd);
10 +
11 /* Support for adding program text to SyntaxErrors */
12 PyAPI_FUNC(void) PyErr_SyntaxLocation(const char *, int);
13 PyAPI_FUNC(PyObject *) PyErr_ProgramText(const char *, int);
14 diff -urN Python-2.5.1.orig/Modules/signalmodule.c Python-2.5.1/Modules/signalmodule.c
15 --- Python-2.5.1.orig/Modules/signalmodule.c 2008-08-10 13:46:48.000000000 +0200
16 +++ Python-2.5.1/Modules/signalmodule.c 2008-08-10 13:50:02.000000000 +0200
17 @@ -12,6 +12,8 @@
18
19 #include <signal.h>
20
21 +#include <sys/stat.h>
22 +
23 #ifndef SIG_ERR
24 #define SIG_ERR ((PyOS_sighandler_t)(-1))
25 #endif
26 @@ -75,4 +77,6 @@
27 PyObject *func;
28 } Handlers[NSIG];
29
30 +static int wakeup_fd = -1;
31 +
32 /* Speed up sigcheck() when none tripped */
33 @@ -112,6 +116,7 @@
34 static void
35 signal_handler(int sig_num)
36 {
37 + const char dummy_byte = '\0';
38 #ifdef WITH_THREAD
39 #ifdef WITH_PTH
40 if (PyThread_get_thread_ident() != main_thread) {
41 @@ -125,6 +130,8 @@
42 is_tripped++;
43 Handlers[sig_num].tripped = 1;
44 Py_AddPendingCall(checksignals_witharg, NULL);
45 + if (wakeup_fd != -1)
46 + write(wakeup_fd, &dummy_byte, 1);
47 #ifdef WITH_THREAD
48 }
49 #endif
50 @@ -264,6 +271,50 @@
51 anything else -- the callable Python object used as a handler");
52
53
54 +static PyObject *
55 +signal_set_wakeup_fd(PyObject *self, PyObject *args)
56 +{
57 + struct stat buf;
58 + int fd, old_fd;
59 + if (!PyArg_ParseTuple(args, "i:set_wakeup_fd", &fd))
60 + return NULL;
61 +#ifdef WITH_THREAD
62 + if (PyThread_get_thread_ident() != main_thread) {
63 + PyErr_SetString(PyExc_ValueError,
64 + "set_wakeup_fd only works in main thread");
65 + return NULL;
66 + }
67 +#endif
68 + if (fd != -1 && fstat(fd, &buf) != 0) {
69 + PyErr_SetString(PyExc_ValueError, "invalid fd");
70 + return NULL;
71 + }
72 + old_fd = wakeup_fd;
73 + wakeup_fd = fd;
74 + return PyLong_FromLong(old_fd);
75 +}
76 +
77 +PyDoc_STRVAR(set_wakeup_fd_doc,
78 +"set_wakeup_fd(fd) -> fd\n\
79 +\n\
80 +Sets the fd to be written to (with '\\0') when a signal\n\
81 +comes in. A library can use this to wakeup select or poll.\n\
82 +The previous fd is returned.\n\
83 +\n\
84 +The fd must be non-blocking.");
85 +
86 +/* C API for the same, without all the error checking */
87 +int
88 +PySignal_SetWakeupFd(int fd)
89 +{
90 + int old_fd = wakeup_fd;
91 + if (fd < 0)
92 + fd = -1;
93 + wakeup_fd = fd;
94 + return old_fd;
95 +}
96 +
97 +
98 /* List of functions defined in the module */
99 static PyMethodDef signal_methods[] = {
100 #ifdef HAVE_ALARM
101 @@ -271,6 +322,7 @@
102 #endif
103 {"signal", signal_signal, METH_VARARGS, signal_doc},
104 {"getsignal", signal_getsignal, METH_VARARGS, getsignal_doc},
105 + {"set_wakeup_fd", signal_set_wakeup_fd, METH_VARARGS, set_wakeup_fd_doc},
106 #ifdef HAVE_PAUSE
107 {"pause", (PyCFunction)signal_pause,
108 METH_NOARGS,pause_doc},