annotate Modules/selectmodule.c @ 13667:a6c7bfdd6777 legacy-trunk

Even more ANSIfication: fix as many function pointers and declarations as possible.
author Thomas Wouters <thomas@python.org>
date Sat, 22 Jul 2000 23:57:55 +0000
parents 99dd1c933161
children 59f8cedcf049
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
902
a4793ae26682 Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 /***********************************************************
13255
916fb024ce94 Change copyright notice.
Guido van Rossum <guido@python.org>
parents: 13079
diff changeset
2 Copyright (c) 2000, BeOpen.com.
916fb024ce94 Change copyright notice.
Guido van Rossum <guido@python.org>
parents: 13079
diff changeset
3 Copyright (c) 1995-2000, Corporation for National Research Initiatives.
916fb024ce94 Change copyright notice.
Guido van Rossum <guido@python.org>
parents: 13079
diff changeset
4 Copyright (c) 1990-1995, Stichting Mathematisch Centrum.
916fb024ce94 Change copyright notice.
Guido van Rossum <guido@python.org>
parents: 13079
diff changeset
5 All rights reserved.
902
a4793ae26682 Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff changeset
6
13255
916fb024ce94 Change copyright notice.
Guido van Rossum <guido@python.org>
parents: 13079
diff changeset
7 See the file "Misc/COPYRIGHT" for information on usage and
916fb024ce94 Change copyright notice.
Guido van Rossum <guido@python.org>
parents: 13079
diff changeset
8 redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
902
a4793ae26682 Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff changeset
9 ******************************************************************/
a4793ae26682 Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff changeset
10
3675
d617de15774b Yet another pass at fd2obj -- this time for Windows
Guido van Rossum <guido@python.org>
parents: 2611
diff changeset
11 /* select - Module containing unix select(2) call.
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
12 Under Unix, the file descriptors are small integers.
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
13 Under Win32, select only exists for sockets, and sockets may
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
14 have any value except INVALID_SOCKET.
8823
f2a4a2b60860 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8616
diff changeset
15 Under BeOS, we suffer the same dichotomy as Win32; sockets can be anything
f2a4a2b60860 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8616
diff changeset
16 >= 0.
3675
d617de15774b Yet another pass at fd2obj -- this time for Windows
Guido van Rossum <guido@python.org>
parents: 2611
diff changeset
17 */
902
a4793ae26682 Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff changeset
18
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
19 #include "Python.h"
902
a4793ae26682 Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff changeset
20
4600
d36250aefb82 Keep gcc -Wall happy.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
21 #ifdef HAVE_UNISTD_H
d36250aefb82 Keep gcc -Wall happy.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
22 #include <unistd.h>
d36250aefb82 Keep gcc -Wall happy.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
23 #endif
13550
95b9ddb00352 One more include of limits.h (possibly to be moved elsewhere when there is
Jack Jansen <jack.jansen@cwi.nl>
parents: 13502
diff changeset
24 #ifdef HAVE_LIMITS_H
95b9ddb00352 One more include of limits.h (possibly to be moved elsewhere when there is
Jack Jansen <jack.jansen@cwi.nl>
parents: 13502
diff changeset
25 #include <limits.h>
95b9ddb00352 One more include of limits.h (possibly to be moved elsewhere when there is
Jack Jansen <jack.jansen@cwi.nl>
parents: 13502
diff changeset
26 #endif
4600
d36250aefb82 Keep gcc -Wall happy.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
27
4621
a2f171ce25f8 Add ``extern void bzero();'' for SGI, to keep gcc -Wall happy.
Guido van Rossum <guido@python.org>
parents: 4600
diff changeset
28 #ifdef __sgi
a2f171ce25f8 Add ``extern void bzero();'' for SGI, to keep gcc -Wall happy.
Guido van Rossum <guido@python.org>
parents: 4600
diff changeset
29 /* This is missing from unistd.h */
13667
a6c7bfdd6777 Even more ANSIfication: fix as many function pointers and declarations as
Thomas Wouters <thomas@python.org>
parents: 13656
diff changeset
30 extern void bzero(void *, int);
4621
a2f171ce25f8 Add ``extern void bzero();'' for SGI, to keep gcc -Wall happy.
Guido van Rossum <guido@python.org>
parents: 4600
diff changeset
31 #endif
a2f171ce25f8 Add ``extern void bzero();'' for SGI, to keep gcc -Wall happy.
Guido van Rossum <guido@python.org>
parents: 4600
diff changeset
32
11243
6aac31cd2102 Patch by Mark Hammond to avoid certain header files on Windows/CE.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
33 #ifndef DONT_HAVE_SYS_TYPES_H
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1392
diff changeset
34 #include <sys/types.h>
11243
6aac31cd2102 Patch by Mark Hammond to avoid certain header files on Windows/CE.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
35 #endif
3675
d617de15774b Yet another pass at fd2obj -- this time for Windows
Guido van Rossum <guido@python.org>
parents: 2611
diff changeset
36
6626
5e31d381fa47 os2 patch by Jeff Rush
Guido van Rossum <guido@python.org>
parents: 6367
diff changeset
37 #if defined(PYOS_OS2)
5e31d381fa47 os2 patch by Jeff Rush
Guido van Rossum <guido@python.org>
parents: 6367
diff changeset
38 #include <sys/time.h>
5e31d381fa47 os2 patch by Jeff Rush
Guido van Rossum <guido@python.org>
parents: 6367
diff changeset
39 #include <utils.h>
5e31d381fa47 os2 patch by Jeff Rush
Guido van Rossum <guido@python.org>
parents: 6367
diff changeset
40 #endif
5e31d381fa47 os2 patch by Jeff Rush
Guido van Rossum <guido@python.org>
parents: 6367
diff changeset
41
3738
ce74bba8a6a6 Slightly different Windows ifdefs
Guido van Rossum <guido@python.org>
parents: 3675
diff changeset
42 #ifdef MS_WINDOWS
3675
d617de15774b Yet another pass at fd2obj -- this time for Windows
Guido van Rossum <guido@python.org>
parents: 2611
diff changeset
43 #include <winsock.h>
d617de15774b Yet another pass at fd2obj -- this time for Windows
Guido van Rossum <guido@python.org>
parents: 2611
diff changeset
44 #else
8823
f2a4a2b60860 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8616
diff changeset
45 #ifdef __BEOS__
f2a4a2b60860 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8616
diff changeset
46 #include <net/socket.h>
f2a4a2b60860 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8616
diff changeset
47 #define SOCKET int
f2a4a2b60860 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8616
diff changeset
48 #else
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1392
diff changeset
49 #include "myselect.h" /* Also includes mytime.h */
3675
d617de15774b Yet another pass at fd2obj -- this time for Windows
Guido van Rossum <guido@python.org>
parents: 2611
diff changeset
50 #define SOCKET int
d617de15774b Yet another pass at fd2obj -- this time for Windows
Guido van Rossum <guido@python.org>
parents: 2611
diff changeset
51 #endif
8823
f2a4a2b60860 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8616
diff changeset
52 #endif
902
a4793ae26682 Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff changeset
53
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
54 static PyObject *SelectError;
902
a4793ae26682 Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff changeset
55
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
56 /* list of Python objects and their file descriptor */
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
57 typedef struct {
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
58 PyObject *obj; /* owned reference */
3675
d617de15774b Yet another pass at fd2obj -- this time for Windows
Guido van Rossum <guido@python.org>
parents: 2611
diff changeset
59 SOCKET fd;
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
60 int sentinel; /* -1 == sentinel */
3675
d617de15774b Yet another pass at fd2obj -- this time for Windows
Guido van Rossum <guido@python.org>
parents: 2611
diff changeset
61 } pylist;
d617de15774b Yet another pass at fd2obj -- this time for Windows
Guido van Rossum <guido@python.org>
parents: 2611
diff changeset
62
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
63 static void
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13256
diff changeset
64 reap_obj(pylist fd2obj[FD_SETSIZE + 3])
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
65 {
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
66 int i;
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
67 for (i = 0; i < FD_SETSIZE + 3 && fd2obj[i].sentinel >= 0; i++) {
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
68 Py_XDECREF(fd2obj[i].obj);
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
69 fd2obj[i].obj = NULL;
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
70 }
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
71 fd2obj[0].sentinel = -1;
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
72 }
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
73
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
74
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
75 /* returns -1 and sets the Python exception if an error occurred, otherwise
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
76 returns a number >= 0
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
77 */
3675
d617de15774b Yet another pass at fd2obj -- this time for Windows
Guido van Rossum <guido@python.org>
parents: 2611
diff changeset
78 static int
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13256
diff changeset
79 list2set(PyObject *list, fd_set *set, pylist fd2obj[FD_SETSIZE + 3])
902
a4793ae26682 Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff changeset
80 {
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
81 int i;
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
82 int max = -1;
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
83 int index = 0;
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
84 int len = PyList_Size(list);
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
85 PyObject* o = NULL;
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
86
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
87 fd2obj[0].obj = (PyObject*)0; /* set list to zero size */
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
88 FD_ZERO(set);
2611
01687ce43708 fix subtle refcnt bug
Guido van Rossum <guido@python.org>
parents: 2061
diff changeset
89
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
90 for (i = 0; i < len; i++) {
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
91 SOCKET v;
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
92
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
93 /* any intervening fileno() calls could decr this refcnt */
4730
62cf7d589300 list2set(): PyList_GetItem could fail.
Barry Warsaw <barry@python.org>
parents: 4700
diff changeset
94 if (!(o = PyList_GetItem(list, i)))
4734
3db423515626 list2set(): correct return value (an int, not a PyObject*).
Barry Warsaw <barry@python.org>
parents: 4730
diff changeset
95 return -1;
4730
62cf7d589300 list2set(): PyList_GetItem could fail.
Barry Warsaw <barry@python.org>
parents: 4700
diff changeset
96
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
97 Py_INCREF(o);
13593
df64cc0e84e2 Use PyObject_AsFileDescriptor
Andrew M. Kuchling <amk@amk.ca>
parents: 13550
diff changeset
98 v = PyObject_AsFileDescriptor( o );
df64cc0e84e2 Use PyObject_AsFileDescriptor
Andrew M. Kuchling <amk@amk.ca>
parents: 13550
diff changeset
99 if (v == -1) goto finally;
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
100
11656
db756d31e727 According to Ron Bickers, and with apparent approval of Chris
Guido van Rossum <guido@python.org>
parents: 11243
diff changeset
101 #if defined(_MSC_VER)
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
102 max = 0; /* not used for Win32 */
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
103 #else /* !_MSC_VER */
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
104 if (v < 0 || v >= FD_SETSIZE) {
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
105 PyErr_SetString(PyExc_ValueError,
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
106 "filedescriptor out of range in select()");
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
107 goto finally;
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
108 }
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
109 if (v > max)
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
110 max = v;
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
111 #endif /* _MSC_VER */
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
112 FD_SET(v, set);
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
113
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
114 /* add object and its file descriptor to the list */
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
115 if (index >= FD_SETSIZE) {
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
116 PyErr_SetString(PyExc_ValueError,
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
117 "too many file descriptors in select()");
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
118 goto finally;
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
119 }
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
120 fd2obj[index].obj = o;
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
121 fd2obj[index].fd = v;
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
122 fd2obj[index].sentinel = 0;
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
123 fd2obj[++index].sentinel = -1;
902
a4793ae26682 Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff changeset
124 }
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
125 return max+1;
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
126
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
127 finally:
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
128 Py_XDECREF(o);
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
129 return -1;
902
a4793ae26682 Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff changeset
130 }
a4793ae26682 Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff changeset
131
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
132 /* returns NULL and sets the Python exception if an error occurred */
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
133 static PyObject *
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13256
diff changeset
134 set2list(fd_set *set, pylist fd2obj[FD_SETSIZE + 3])
902
a4793ae26682 Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff changeset
135 {
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
136 int i, j, count=0;
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
137 PyObject *list, *o;
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
138 SOCKET fd;
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
139
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
140 for (j = 0; fd2obj[j].sentinel >= 0; j++) {
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
141 if (FD_ISSET(fd2obj[j].fd, set))
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
142 count++;
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
143 }
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
144 list = PyList_New(count);
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
145 if (!list)
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
146 return NULL;
902
a4793ae26682 Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff changeset
147
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
148 i = 0;
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
149 for (j = 0; fd2obj[j].sentinel >= 0; j++) {
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
150 fd = fd2obj[j].fd;
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
151 if (FD_ISSET(fd, set)) {
3675
d617de15774b Yet another pass at fd2obj -- this time for Windows
Guido van Rossum <guido@python.org>
parents: 2611
diff changeset
152 #ifndef _MSC_VER
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
153 if (fd > FD_SETSIZE) {
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
154 PyErr_SetString(PyExc_SystemError,
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
155 "filedescriptor out of range returned in select()");
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
156 goto finally;
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
157 }
3675
d617de15774b Yet another pass at fd2obj -- this time for Windows
Guido van Rossum <guido@python.org>
parents: 2611
diff changeset
158 #endif
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
159 o = fd2obj[j].obj;
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
160 fd2obj[j].obj = NULL;
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
161 /* transfer ownership */
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
162 if (PyList_SetItem(list, i, o) < 0)
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
163 goto finally;
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
164
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
165 i++;
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
166 }
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
167 }
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
168 return list;
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
169 finally:
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
170 Py_DECREF(list);
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
171 return NULL;
902
a4793ae26682 Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff changeset
172 }
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
173
902
a4793ae26682 Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff changeset
174
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
175 static PyObject *
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13256
diff changeset
176 select_select(PyObject *self, PyObject *args)
902
a4793ae26682 Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff changeset
177 {
8616
2417f17fa3e5 On Windows, put the select file descriptor arrays on the heap.
Guido van Rossum <guido@python.org>
parents: 8570
diff changeset
178 #ifdef MS_WINDOWS
2417f17fa3e5 On Windows, put the select file descriptor arrays on the heap.
Guido van Rossum <guido@python.org>
parents: 8570
diff changeset
179 /* This would be an awful lot of stack space on Windows! */
2417f17fa3e5 On Windows, put the select file descriptor arrays on the heap.
Guido van Rossum <guido@python.org>
parents: 8570
diff changeset
180 pylist *rfd2obj, *wfd2obj, *efd2obj;
2417f17fa3e5 On Windows, put the select file descriptor arrays on the heap.
Guido van Rossum <guido@python.org>
parents: 8570
diff changeset
181 #else
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
182 pylist rfd2obj[FD_SETSIZE + 3];
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
183 pylist wfd2obj[FD_SETSIZE + 3];
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
184 pylist efd2obj[FD_SETSIZE + 3];
8616
2417f17fa3e5 On Windows, put the select file descriptor arrays on the heap.
Guido van Rossum <guido@python.org>
parents: 8570
diff changeset
185 #endif
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
186 PyObject *ifdlist, *ofdlist, *efdlist;
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
187 PyObject *ret = NULL;
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
188 PyObject *tout = Py_None;
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
189 fd_set ifdset, ofdset, efdset;
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
190 double timeout;
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
191 struct timeval tv, *tvp;
13079
185003d09e76 Trent Mick:
Guido van Rossum <guido@python.org>
parents: 12576
diff changeset
192 long seconds;
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
193 int imax, omax, emax, max;
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
194 int n;
902
a4793ae26682 Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff changeset
195
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
196 /* convert arguments */
11882
dc65c5f6ad7c Massive patch by Skip Montanaro to add ":name" to as many
Guido van Rossum <guido@python.org>
parents: 11656
diff changeset
197 if (!PyArg_ParseTuple(args, "OOO|O:select",
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
198 &ifdlist, &ofdlist, &efdlist, &tout))
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
199 return NULL;
902
a4793ae26682 Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff changeset
200
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
201 if (tout == Py_None)
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
202 tvp = (struct timeval *)0;
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
203 else if (!PyArg_Parse(tout, "d", &timeout)) {
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
204 PyErr_SetString(PyExc_TypeError,
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
205 "timeout must be a float or None");
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
206 return NULL;
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
207 }
1390
cffe8d6b2f09 * selectmodule.c (select_select): timeout argument may be None with same
Guido van Rossum <guido@python.org>
parents: 1284
diff changeset
208 else {
13079
185003d09e76 Trent Mick:
Guido van Rossum <guido@python.org>
parents: 12576
diff changeset
209 if (timeout > (double)LONG_MAX) {
185003d09e76 Trent Mick:
Guido van Rossum <guido@python.org>
parents: 12576
diff changeset
210 PyErr_SetString(PyExc_OverflowError, "timeout period too long");
185003d09e76 Trent Mick:
Guido van Rossum <guido@python.org>
parents: 12576
diff changeset
211 return NULL;
185003d09e76 Trent Mick:
Guido van Rossum <guido@python.org>
parents: 12576
diff changeset
212 }
185003d09e76 Trent Mick:
Guido van Rossum <guido@python.org>
parents: 12576
diff changeset
213 seconds = (long)timeout;
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
214 timeout = timeout - (double)seconds;
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
215 tv.tv_sec = seconds;
13079
185003d09e76 Trent Mick:
Guido van Rossum <guido@python.org>
parents: 12576
diff changeset
216 tv.tv_usec = (long)(timeout*1000000.0);
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
217 tvp = &tv;
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
218 }
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
219
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
220 /* sanity check first three arguments */
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
221 if (!PyList_Check(ifdlist) ||
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
222 !PyList_Check(ofdlist) ||
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
223 !PyList_Check(efdlist))
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
224 {
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
225 PyErr_SetString(PyExc_TypeError,
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
226 "arguments 1-3 must be lists");
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
227 return NULL;
1390
cffe8d6b2f09 * selectmodule.c (select_select): timeout argument may be None with same
Guido van Rossum <guido@python.org>
parents: 1284
diff changeset
228 }
902
a4793ae26682 Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff changeset
229
8616
2417f17fa3e5 On Windows, put the select file descriptor arrays on the heap.
Guido van Rossum <guido@python.org>
parents: 8570
diff changeset
230 #ifdef MS_WINDOWS
2417f17fa3e5 On Windows, put the select file descriptor arrays on the heap.
Guido van Rossum <guido@python.org>
parents: 8570
diff changeset
231 /* Allocate memory for the lists */
2417f17fa3e5 On Windows, put the select file descriptor arrays on the heap.
Guido van Rossum <guido@python.org>
parents: 8570
diff changeset
232 rfd2obj = PyMem_NEW(pylist, FD_SETSIZE + 3);
2417f17fa3e5 On Windows, put the select file descriptor arrays on the heap.
Guido van Rossum <guido@python.org>
parents: 8570
diff changeset
233 wfd2obj = PyMem_NEW(pylist, FD_SETSIZE + 3);
2417f17fa3e5 On Windows, put the select file descriptor arrays on the heap.
Guido van Rossum <guido@python.org>
parents: 8570
diff changeset
234 efd2obj = PyMem_NEW(pylist, FD_SETSIZE + 3);
2417f17fa3e5 On Windows, put the select file descriptor arrays on the heap.
Guido van Rossum <guido@python.org>
parents: 8570
diff changeset
235 if (rfd2obj == NULL || wfd2obj == NULL || efd2obj == NULL) {
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11882
diff changeset
236 if (rfd2obj) PyMem_DEL(rfd2obj);
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11882
diff changeset
237 if (wfd2obj) PyMem_DEL(wfd2obj);
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11882
diff changeset
238 if (efd2obj) PyMem_DEL(efd2obj);
8616
2417f17fa3e5 On Windows, put the select file descriptor arrays on the heap.
Guido van Rossum <guido@python.org>
parents: 8570
diff changeset
239 return NULL;
2417f17fa3e5 On Windows, put the select file descriptor arrays on the heap.
Guido van Rossum <guido@python.org>
parents: 8570
diff changeset
240 }
2417f17fa3e5 On Windows, put the select file descriptor arrays on the heap.
Guido van Rossum <guido@python.org>
parents: 8570
diff changeset
241 #endif
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
242 /* Convert lists to fd_sets, and get maximum fd number
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
243 * propagates the Python exception set in list2set()
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
244 */
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
245 rfd2obj[0].sentinel = -1;
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
246 wfd2obj[0].sentinel = -1;
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
247 efd2obj[0].sentinel = -1;
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
248 if ((imax=list2set(ifdlist, &ifdset, rfd2obj)) < 0)
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
249 goto finally;
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
250 if ((omax=list2set(ofdlist, &ofdset, wfd2obj)) < 0)
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
251 goto finally;
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
252 if ((emax=list2set(efdlist, &efdset, efd2obj)) < 0)
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
253 goto finally;
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
254 max = imax;
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
255 if (omax > max) max = omax;
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
256 if (emax > max) max = emax;
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
257
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
258 Py_BEGIN_ALLOW_THREADS
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
259 n = select(max, &ifdset, &ofdset, &efdset, tvp);
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
260 Py_END_ALLOW_THREADS
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
261
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
262 if (n < 0) {
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
263 PyErr_SetFromErrno(SelectError);
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
264 }
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
265 else if (n == 0) {
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
266 /* optimization */
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
267 ifdlist = PyList_New(0);
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
268 if (ifdlist) {
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
269 ret = Py_BuildValue("OOO", ifdlist, ifdlist, ifdlist);
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
270 Py_DECREF(ifdlist);
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
271 }
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
272 }
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
273 else {
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
274 /* any of these three calls can raise an exception. it's more
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
275 convenient to test for this after all three calls... but
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
276 is that acceptable?
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
277 */
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
278 ifdlist = set2list(&ifdset, rfd2obj);
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
279 ofdlist = set2list(&ofdset, wfd2obj);
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
280 efdlist = set2list(&efdset, efd2obj);
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
281 if (PyErr_Occurred())
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
282 ret = NULL;
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
283 else
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
284 ret = Py_BuildValue("OOO", ifdlist, ofdlist, efdlist);
902
a4793ae26682 Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff changeset
285
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
286 Py_DECREF(ifdlist);
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
287 Py_DECREF(ofdlist);
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
288 Py_DECREF(efdlist);
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
289 }
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
290
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
291 finally:
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
292 reap_obj(rfd2obj);
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
293 reap_obj(wfd2obj);
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
294 reap_obj(efd2obj);
8616
2417f17fa3e5 On Windows, put the select file descriptor arrays on the heap.
Guido van Rossum <guido@python.org>
parents: 8570
diff changeset
295 #ifdef MS_WINDOWS
2417f17fa3e5 On Windows, put the select file descriptor arrays on the heap.
Guido van Rossum <guido@python.org>
parents: 8570
diff changeset
296 PyMem_DEL(rfd2obj);
2417f17fa3e5 On Windows, put the select file descriptor arrays on the heap.
Guido van Rossum <guido@python.org>
parents: 8570
diff changeset
297 PyMem_DEL(wfd2obj);
2417f17fa3e5 On Windows, put the select file descriptor arrays on the heap.
Guido van Rossum <guido@python.org>
parents: 8570
diff changeset
298 PyMem_DEL(efd2obj);
2417f17fa3e5 On Windows, put the select file descriptor arrays on the heap.
Guido van Rossum <guido@python.org>
parents: 8570
diff changeset
299 #endif
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
300 return ret;
902
a4793ae26682 Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff changeset
301 }
a4793ae26682 Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff changeset
302
8570
bd695de18ecf Added doc strings.
Guido van Rossum <guido@python.org>
parents: 6626
diff changeset
303 static char select_doc[] =
bd695de18ecf Added doc strings.
Guido van Rossum <guido@python.org>
parents: 6626
diff changeset
304 "select(rlist, wlist, xlist[, timeout]) -> (rlist, wlist, xlist)\n\
bd695de18ecf Added doc strings.
Guido van Rossum <guido@python.org>
parents: 6626
diff changeset
305 \n\
bd695de18ecf Added doc strings.
Guido van Rossum <guido@python.org>
parents: 6626
diff changeset
306 Wait until one or more file descriptors are ready for some kind of I/O.\n\
bd695de18ecf Added doc strings.
Guido van Rossum <guido@python.org>
parents: 6626
diff changeset
307 The first three arguments are lists of file descriptors to be waited for:\n\
bd695de18ecf Added doc strings.
Guido van Rossum <guido@python.org>
parents: 6626
diff changeset
308 rlist -- wait until ready for reading\n\
bd695de18ecf Added doc strings.
Guido van Rossum <guido@python.org>
parents: 6626
diff changeset
309 wlist -- wait until ready for writing\n\
bd695de18ecf Added doc strings.
Guido van Rossum <guido@python.org>
parents: 6626
diff changeset
310 xlist -- wait for an ``exceptional condition''\n\
bd695de18ecf Added doc strings.
Guido van Rossum <guido@python.org>
parents: 6626
diff changeset
311 If only one kind of condition is required, pass [] for the other lists.\n\
bd695de18ecf Added doc strings.
Guido van Rossum <guido@python.org>
parents: 6626
diff changeset
312 A file descriptor is either a socket or file object, or a small integer\n\
bd695de18ecf Added doc strings.
Guido van Rossum <guido@python.org>
parents: 6626
diff changeset
313 gotten from a fileno() method call on one of those.\n\
bd695de18ecf Added doc strings.
Guido van Rossum <guido@python.org>
parents: 6626
diff changeset
314 \n\
bd695de18ecf Added doc strings.
Guido van Rossum <guido@python.org>
parents: 6626
diff changeset
315 The optional 4th argument specifies a timeout in seconds; it may be\n\
bd695de18ecf Added doc strings.
Guido van Rossum <guido@python.org>
parents: 6626
diff changeset
316 a floating point number to specify fractions of seconds. If it is absent\n\
bd695de18ecf Added doc strings.
Guido van Rossum <guido@python.org>
parents: 6626
diff changeset
317 or None, the call will never time out.\n\
bd695de18ecf Added doc strings.
Guido van Rossum <guido@python.org>
parents: 6626
diff changeset
318 \n\
bd695de18ecf Added doc strings.
Guido van Rossum <guido@python.org>
parents: 6626
diff changeset
319 The return value is a tuple of three lists corresponding to the first three\n\
bd695de18ecf Added doc strings.
Guido van Rossum <guido@python.org>
parents: 6626
diff changeset
320 arguments; each contains the subset of the corresponding file descriptors\n\
bd695de18ecf Added doc strings.
Guido van Rossum <guido@python.org>
parents: 6626
diff changeset
321 that are ready.\n\
bd695de18ecf Added doc strings.
Guido van Rossum <guido@python.org>
parents: 6626
diff changeset
322 \n\
bd695de18ecf Added doc strings.
Guido van Rossum <guido@python.org>
parents: 6626
diff changeset
323 *** IMPORTANT NOTICE ***\n\
bd695de18ecf Added doc strings.
Guido van Rossum <guido@python.org>
parents: 6626
diff changeset
324 On Windows, only sockets are supported; on Unix, all file descriptors.";
bd695de18ecf Added doc strings.
Guido van Rossum <guido@python.org>
parents: 6626
diff changeset
325
902
a4793ae26682 Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff changeset
326
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
327 static PyMethodDef select_methods[] = {
8570
bd695de18ecf Added doc strings.
Guido van Rossum <guido@python.org>
parents: 6626
diff changeset
328 {"select", select_select, 1, select_doc},
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
329 {0, 0}, /* sentinel */
902
a4793ae26682 Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff changeset
330 };
a4793ae26682 Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff changeset
331
8570
bd695de18ecf Added doc strings.
Guido van Rossum <guido@python.org>
parents: 6626
diff changeset
332 static char module_doc[] =
bd695de18ecf Added doc strings.
Guido van Rossum <guido@python.org>
parents: 6626
diff changeset
333 "This module supports asynchronous I/O on multiple file descriptors.\n\
bd695de18ecf Added doc strings.
Guido van Rossum <guido@python.org>
parents: 6626
diff changeset
334 \n\
bd695de18ecf Added doc strings.
Guido van Rossum <guido@python.org>
parents: 6626
diff changeset
335 *** IMPORTANT NOTICE ***\n\
bd695de18ecf Added doc strings.
Guido van Rossum <guido@python.org>
parents: 6626
diff changeset
336 On Windows, only sockets are supported; on Unix, all file descriptors.";
902
a4793ae26682 Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff changeset
337
9588
38023a425e48 Add DL_EXPORT() to all modules that could possibly be used
Guido van Rossum <guido@python.org>
parents: 8823
diff changeset
338 DL_EXPORT(void)
13651
8a063423e0b7 Bunch of minor ANSIfications: 'void initfunc()' -> 'void initfunc(void)',
Thomas Wouters <thomas@python.org>
parents: 13593
diff changeset
339 initselect(void)
902
a4793ae26682 Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff changeset
340 {
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
341 PyObject *m, *d;
8570
bd695de18ecf Added doc strings.
Guido van Rossum <guido@python.org>
parents: 6626
diff changeset
342 m = Py_InitModule3("select", select_methods, module_doc);
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
343 d = PyModule_GetDict(m);
6367
2c29b8982643 Apply two changes, systematically:
Guido van Rossum <guido@python.org>
parents: 4734
diff changeset
344 SelectError = PyErr_NewException("select.error", NULL, NULL);
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
345 PyDict_SetItemString(d, "error", SelectError);
902
a4793ae26682 Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff changeset
346 }