annotate Modules/selectmodule.c @ 13184:efecbbf72765 legacy-trunk

Describe a bit about writing test cases for Python...
author Skip Montanaro <skip@pobox.com>
date Fri, 30 Jun 2000 06:08:35 +0000
parents 185003d09e76
children 916fb024ce94
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 /***********************************************************
2061
172ba04a5191 Added 1995 to copyright message.
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
172ba04a5191 Added 1995 to copyright message.
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
3 The Netherlands.
902
a4793ae26682 Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff changeset
4
a4793ae26682 Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff changeset
5 All Rights Reserved
a4793ae26682 Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff changeset
6
4503
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3738
diff changeset
7 Permission to use, copy, modify, and distribute this software and its
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3738
diff changeset
8 documentation for any purpose and without fee is hereby granted,
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 provided that the above copyright notice appear in all copies and that
4503
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3738
diff changeset
10 both that copyright notice and this permission notice appear in
902
a4793ae26682 Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff changeset
11 supporting documentation, and that the names of Stichting Mathematisch
4503
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3738
diff changeset
12 Centrum or CWI or Corporation for National Research Initiatives or
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3738
diff changeset
13 CNRI not be used in advertising or publicity pertaining to
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3738
diff changeset
14 distribution of the software without specific, written prior
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3738
diff changeset
15 permission.
902
a4793ae26682 Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff changeset
16
4503
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3738
diff changeset
17 While CWI is the initial source for this software, a modified version
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3738
diff changeset
18 is made available by the Corporation for National Research Initiatives
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3738
diff changeset
19 (CNRI) at the Internet address ftp://ftp.python.org.
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3738
diff changeset
20
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3738
diff changeset
21 STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3738
diff changeset
22 REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3738
diff changeset
23 MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3738
diff changeset
24 CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3738
diff changeset
25 DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3738
diff changeset
26 PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3738
diff changeset
27 TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3738
diff changeset
28 PERFORMANCE OF THIS SOFTWARE.
902
a4793ae26682 Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff changeset
29
a4793ae26682 Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff changeset
30 ******************************************************************/
a4793ae26682 Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff changeset
31
3675
d617de15774b Yet another pass at fd2obj -- this time for Windows
Guido van Rossum <guido@python.org>
parents: 2611
diff changeset
32 /* 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
33 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
34 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
35 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
36 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
37 >= 0.
3675
d617de15774b Yet another pass at fd2obj -- this time for Windows
Guido van Rossum <guido@python.org>
parents: 2611
diff changeset
38 */
902
a4793ae26682 Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff changeset
39
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
40 #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
41
4600
d36250aefb82 Keep gcc -Wall happy.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
42 #ifdef HAVE_UNISTD_H
d36250aefb82 Keep gcc -Wall happy.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
43 #include <unistd.h>
d36250aefb82 Keep gcc -Wall happy.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
44 #endif
d36250aefb82 Keep gcc -Wall happy.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
45
4621
a2f171ce25f8 Add ``extern void bzero();'' for SGI, to keep gcc -Wall happy.
Guido van Rossum <guido@python.org>
parents: 4600
diff changeset
46 #ifdef __sgi
a2f171ce25f8 Add ``extern void bzero();'' for SGI, to keep gcc -Wall happy.
Guido van Rossum <guido@python.org>
parents: 4600
diff changeset
47 /* This is missing from unistd.h */
a2f171ce25f8 Add ``extern void bzero();'' for SGI, to keep gcc -Wall happy.
Guido van Rossum <guido@python.org>
parents: 4600
diff changeset
48 extern void bzero();
a2f171ce25f8 Add ``extern void bzero();'' for SGI, to keep gcc -Wall happy.
Guido van Rossum <guido@python.org>
parents: 4600
diff changeset
49 #endif
a2f171ce25f8 Add ``extern void bzero();'' for SGI, to keep gcc -Wall happy.
Guido van Rossum <guido@python.org>
parents: 4600
diff changeset
50
11243
6aac31cd2102 Patch by Mark Hammond to avoid certain header files on Windows/CE.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
51 #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
52 #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
53 #endif
3675
d617de15774b Yet another pass at fd2obj -- this time for Windows
Guido van Rossum <guido@python.org>
parents: 2611
diff changeset
54
6626
5e31d381fa47 os2 patch by Jeff Rush
Guido van Rossum <guido@python.org>
parents: 6367
diff changeset
55 #if defined(PYOS_OS2)
5e31d381fa47 os2 patch by Jeff Rush
Guido van Rossum <guido@python.org>
parents: 6367
diff changeset
56 #include <sys/time.h>
5e31d381fa47 os2 patch by Jeff Rush
Guido van Rossum <guido@python.org>
parents: 6367
diff changeset
57 #include <utils.h>
5e31d381fa47 os2 patch by Jeff Rush
Guido van Rossum <guido@python.org>
parents: 6367
diff changeset
58 #endif
5e31d381fa47 os2 patch by Jeff Rush
Guido van Rossum <guido@python.org>
parents: 6367
diff changeset
59
3738
ce74bba8a6a6 Slightly different Windows ifdefs
Guido van Rossum <guido@python.org>
parents: 3675
diff changeset
60 #ifdef MS_WINDOWS
3675
d617de15774b Yet another pass at fd2obj -- this time for Windows
Guido van Rossum <guido@python.org>
parents: 2611
diff changeset
61 #include <winsock.h>
d617de15774b Yet another pass at fd2obj -- this time for Windows
Guido van Rossum <guido@python.org>
parents: 2611
diff changeset
62 #else
8823
f2a4a2b60860 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8616
diff changeset
63 #ifdef __BEOS__
f2a4a2b60860 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8616
diff changeset
64 #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
65 #define SOCKET int
f2a4a2b60860 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8616
diff changeset
66 #else
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1392
diff changeset
67 #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
68 #define SOCKET int
d617de15774b Yet another pass at fd2obj -- this time for Windows
Guido van Rossum <guido@python.org>
parents: 2611
diff changeset
69 #endif
8823
f2a4a2b60860 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8616
diff changeset
70 #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
71
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
72 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
73
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
74 /* 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
75 typedef struct {
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
76 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
77 SOCKET fd;
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
78 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
79 } pylist;
d617de15774b Yet another pass at fd2obj -- this time for Windows
Guido van Rossum <guido@python.org>
parents: 2611
diff changeset
80
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
81 static void
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
82 reap_obj(fd2obj)
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
83 pylist fd2obj[FD_SETSIZE + 3];
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
84 {
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
85 int i;
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
86 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
87 Py_XDECREF(fd2obj[i].obj);
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
88 fd2obj[i].obj = NULL;
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
89 }
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
90 fd2obj[0].sentinel = -1;
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
91 }
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
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
94 /* 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
95 returns a number >= 0
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
96 */
3675
d617de15774b Yet another pass at fd2obj -- this time for Windows
Guido van Rossum <guido@python.org>
parents: 2611
diff changeset
97 static int
922
c12b445cdace * myselect.h: bzero -> memset
Guido van Rossum <guido@python.org>
parents: 904
diff changeset
98 list2set(list, set, fd2obj)
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
99 PyObject *list;
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
100 fd_set *set;
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
101 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
102 {
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
103 int i;
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
104 int max = -1;
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
105 int index = 0;
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
106 int len = PyList_Size(list);
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
107 PyObject* o = NULL;
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
108
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
109 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
110 FD_ZERO(set);
2611
01687ce43708 fix subtle refcnt bug
Guido van Rossum <guido@python.org>
parents: 2061
diff changeset
111
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
112 for (i = 0; i < len; i++) {
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
113 PyObject *meth;
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
114 SOCKET v;
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
115
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
116 /* any intervening fileno() calls could decr this refcnt */
4730
62cf7d589300 list2set(): PyList_GetItem could fail.
Barry Warsaw <barry@python.org>
parents: 4700
diff changeset
117 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
118 return -1;
4730
62cf7d589300 list2set(): PyList_GetItem could fail.
Barry Warsaw <barry@python.org>
parents: 4700
diff changeset
119
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
120 Py_INCREF(o);
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
121
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
122 if (PyInt_Check(o)) {
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
123 v = PyInt_AsLong(o);
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
124 }
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
125 else if ((meth = PyObject_GetAttrString(o, "fileno")) != NULL)
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
126 {
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
127 PyObject *fno = PyEval_CallObject(meth, NULL);
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
128 Py_DECREF(meth);
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
129 if (fno == NULL)
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
130 goto finally;
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
131
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
132 if (!PyInt_Check(fno)) {
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
133 PyErr_SetString(PyExc_TypeError,
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
134 "fileno method returned a non-integer");
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
135 Py_DECREF(fno);
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
136 goto finally;
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
137 }
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
138 v = PyInt_AsLong(fno);
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
139 Py_DECREF(fno);
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
140 }
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
141 else {
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
142 PyErr_SetString(PyExc_TypeError,
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
143 "argument must be an int, or have a fileno() method.");
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
144 goto finally;
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
145 }
11656
db756d31e727 According to Ron Bickers, and with apparent approval of Chris
Guido van Rossum <guido@python.org>
parents: 11243
diff changeset
146 #if defined(_MSC_VER)
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
147 max = 0; /* not used for Win32 */
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
148 #else /* !_MSC_VER */
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
149 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
150 PyErr_SetString(PyExc_ValueError,
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
151 "filedescriptor out of range in select()");
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
152 goto finally;
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
153 }
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
154 if (v > max)
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
155 max = v;
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
156 #endif /* _MSC_VER */
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
157 FD_SET(v, set);
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
158
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
159 /* 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
160 if (index >= FD_SETSIZE) {
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
161 PyErr_SetString(PyExc_ValueError,
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
162 "too many file descriptors in select()");
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
163 goto finally;
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
164 }
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
165 fd2obj[index].obj = o;
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
166 fd2obj[index].fd = v;
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
167 fd2obj[index].sentinel = 0;
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
168 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
169 }
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
170 return max+1;
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
171
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
172 finally:
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
173 Py_XDECREF(o);
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
174 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
175 }
a4793ae26682 Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff changeset
176
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
177 /* 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
178 static PyObject *
3675
d617de15774b Yet another pass at fd2obj -- this time for Windows
Guido van Rossum <guido@python.org>
parents: 2611
diff changeset
179 set2list(set, fd2obj)
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
180 fd_set *set;
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
181 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
182 {
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
183 int i, j, count=0;
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
184 PyObject *list, *o;
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
185 SOCKET fd;
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
186
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
187 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
188 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
189 count++;
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
190 }
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
191 list = PyList_New(count);
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
192 if (!list)
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
193 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
194
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
195 i = 0;
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
196 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
197 fd = fd2obj[j].fd;
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
198 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
199 #ifndef _MSC_VER
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
200 if (fd > FD_SETSIZE) {
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
201 PyErr_SetString(PyExc_SystemError,
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
202 "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
203 goto finally;
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
204 }
3675
d617de15774b Yet another pass at fd2obj -- this time for Windows
Guido van Rossum <guido@python.org>
parents: 2611
diff changeset
205 #endif
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
206 o = fd2obj[j].obj;
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
207 fd2obj[j].obj = NULL;
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
208 /* transfer ownership */
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
209 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
210 goto finally;
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
211
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
212 i++;
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
213 }
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
214 }
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
215 return list;
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
216 finally:
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
217 Py_DECREF(list);
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
218 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
219 }
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
220
902
a4793ae26682 Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff changeset
221
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
222 static PyObject *
902
a4793ae26682 Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff changeset
223 select_select(self, args)
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
224 PyObject *self;
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
225 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
226 {
8616
2417f17fa3e5 On Windows, put the select file descriptor arrays on the heap.
Guido van Rossum <guido@python.org>
parents: 8570
diff changeset
227 #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
228 /* 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
229 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
230 #else
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
231 pylist rfd2obj[FD_SETSIZE + 3];
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
232 pylist wfd2obj[FD_SETSIZE + 3];
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
233 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
234 #endif
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
235 PyObject *ifdlist, *ofdlist, *efdlist;
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
236 PyObject *ret = NULL;
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
237 PyObject *tout = Py_None;
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
238 fd_set ifdset, ofdset, efdset;
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
239 double timeout;
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
240 struct timeval tv, *tvp;
13079
185003d09e76 Trent Mick:
Guido van Rossum <guido@python.org>
parents: 12576
diff changeset
241 long seconds;
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
242 int imax, omax, emax, max;
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
243 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
244
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
245 /* 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
246 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
247 &ifdlist, &ofdlist, &efdlist, &tout))
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
248 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
249
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
250 if (tout == Py_None)
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
251 tvp = (struct timeval *)0;
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
252 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
253 PyErr_SetString(PyExc_TypeError,
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
254 "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
255 return NULL;
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
256 }
1390
cffe8d6b2f09 * selectmodule.c (select_select): timeout argument may be None with same
Guido van Rossum <guido@python.org>
parents: 1284
diff changeset
257 else {
13079
185003d09e76 Trent Mick:
Guido van Rossum <guido@python.org>
parents: 12576
diff changeset
258 if (timeout > (double)LONG_MAX) {
185003d09e76 Trent Mick:
Guido van Rossum <guido@python.org>
parents: 12576
diff changeset
259 PyErr_SetString(PyExc_OverflowError, "timeout period too long");
185003d09e76 Trent Mick:
Guido van Rossum <guido@python.org>
parents: 12576
diff changeset
260 return NULL;
185003d09e76 Trent Mick:
Guido van Rossum <guido@python.org>
parents: 12576
diff changeset
261 }
185003d09e76 Trent Mick:
Guido van Rossum <guido@python.org>
parents: 12576
diff changeset
262 seconds = (long)timeout;
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
263 timeout = timeout - (double)seconds;
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
264 tv.tv_sec = seconds;
13079
185003d09e76 Trent Mick:
Guido van Rossum <guido@python.org>
parents: 12576
diff changeset
265 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
266 tvp = &tv;
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
267 }
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
268
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
269 /* sanity check first three arguments */
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
270 if (!PyList_Check(ifdlist) ||
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
271 !PyList_Check(ofdlist) ||
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
272 !PyList_Check(efdlist))
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
273 {
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
274 PyErr_SetString(PyExc_TypeError,
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
275 "arguments 1-3 must be lists");
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
276 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
277 }
902
a4793ae26682 Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff changeset
278
8616
2417f17fa3e5 On Windows, put the select file descriptor arrays on the heap.
Guido van Rossum <guido@python.org>
parents: 8570
diff changeset
279 #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
280 /* 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
281 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
282 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
283 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
284 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
285 if (rfd2obj) PyMem_DEL(rfd2obj);
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11882
diff changeset
286 if (wfd2obj) PyMem_DEL(wfd2obj);
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11882
diff changeset
287 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
288 return NULL;
2417f17fa3e5 On Windows, put the select file descriptor arrays on the heap.
Guido van Rossum <guido@python.org>
parents: 8570
diff changeset
289 }
2417f17fa3e5 On Windows, put the select file descriptor arrays on the heap.
Guido van Rossum <guido@python.org>
parents: 8570
diff changeset
290 #endif
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
291 /* 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
292 * propagates the Python exception set in list2set()
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
293 */
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
294 rfd2obj[0].sentinel = -1;
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
295 wfd2obj[0].sentinel = -1;
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
296 efd2obj[0].sentinel = -1;
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
297 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
298 goto finally;
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
299 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
300 goto finally;
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
301 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
302 goto finally;
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
303 max = imax;
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
304 if (omax > max) max = omax;
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
305 if (emax > max) max = emax;
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
306
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
307 Py_BEGIN_ALLOW_THREADS
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
308 n = select(max, &ifdset, &ofdset, &efdset, tvp);
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
309 Py_END_ALLOW_THREADS
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
310
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
311 if (n < 0) {
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
312 PyErr_SetFromErrno(SelectError);
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
313 }
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
314 else if (n == 0) {
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
315 /* optimization */
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
316 ifdlist = PyList_New(0);
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
317 if (ifdlist) {
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
318 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
319 Py_DECREF(ifdlist);
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
320 }
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
321 }
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
322 else {
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
323 /* 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
324 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
325 is that acceptable?
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
326 */
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
327 ifdlist = set2list(&ifdset, rfd2obj);
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
328 ofdlist = set2list(&ofdset, wfd2obj);
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
329 efdlist = set2list(&efdset, efd2obj);
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
330 if (PyErr_Occurred())
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
331 ret = NULL;
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
332 else
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
333 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
334
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
335 Py_DECREF(ifdlist);
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
336 Py_DECREF(ofdlist);
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
337 Py_DECREF(efdlist);
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
338 }
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
339
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
340 finally:
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
341 reap_obj(rfd2obj);
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
342 reap_obj(wfd2obj);
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
343 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
344 #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
345 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
346 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
347 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
348 #endif
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
349 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
350 }
a4793ae26682 Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff changeset
351
8570
bd695de18ecf Added doc strings.
Guido van Rossum <guido@python.org>
parents: 6626
diff changeset
352 static char select_doc[] =
bd695de18ecf Added doc strings.
Guido van Rossum <guido@python.org>
parents: 6626
diff changeset
353 "select(rlist, wlist, xlist[, timeout]) -> (rlist, wlist, xlist)\n\
bd695de18ecf Added doc strings.
Guido van Rossum <guido@python.org>
parents: 6626
diff changeset
354 \n\
bd695de18ecf Added doc strings.
Guido van Rossum <guido@python.org>
parents: 6626
diff changeset
355 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
356 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
357 rlist -- wait until ready for reading\n\
bd695de18ecf Added doc strings.
Guido van Rossum <guido@python.org>
parents: 6626
diff changeset
358 wlist -- wait until ready for writing\n\
bd695de18ecf Added doc strings.
Guido van Rossum <guido@python.org>
parents: 6626
diff changeset
359 xlist -- wait for an ``exceptional condition''\n\
bd695de18ecf Added doc strings.
Guido van Rossum <guido@python.org>
parents: 6626
diff changeset
360 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
361 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
362 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
363 \n\
bd695de18ecf Added doc strings.
Guido van Rossum <guido@python.org>
parents: 6626
diff changeset
364 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
365 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
366 or None, the call will never time out.\n\
bd695de18ecf Added doc strings.
Guido van Rossum <guido@python.org>
parents: 6626
diff changeset
367 \n\
bd695de18ecf Added doc strings.
Guido van Rossum <guido@python.org>
parents: 6626
diff changeset
368 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
369 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
370 that are ready.\n\
bd695de18ecf Added doc strings.
Guido van Rossum <guido@python.org>
parents: 6626
diff changeset
371 \n\
bd695de18ecf Added doc strings.
Guido van Rossum <guido@python.org>
parents: 6626
diff changeset
372 *** IMPORTANT NOTICE ***\n\
bd695de18ecf Added doc strings.
Guido van Rossum <guido@python.org>
parents: 6626
diff changeset
373 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
374
902
a4793ae26682 Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff changeset
375
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
376 static PyMethodDef select_methods[] = {
8570
bd695de18ecf Added doc strings.
Guido van Rossum <guido@python.org>
parents: 6626
diff changeset
377 {"select", select_select, 1, select_doc},
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
378 {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
379 };
a4793ae26682 Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff changeset
380
8570
bd695de18ecf Added doc strings.
Guido van Rossum <guido@python.org>
parents: 6626
diff changeset
381 static char module_doc[] =
bd695de18ecf Added doc strings.
Guido van Rossum <guido@python.org>
parents: 6626
diff changeset
382 "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
383 \n\
bd695de18ecf Added doc strings.
Guido van Rossum <guido@python.org>
parents: 6626
diff changeset
384 *** IMPORTANT NOTICE ***\n\
bd695de18ecf Added doc strings.
Guido van Rossum <guido@python.org>
parents: 6626
diff changeset
385 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
386
9588
38023a425e48 Add DL_EXPORT() to all modules that could possibly be used
Guido van Rossum <guido@python.org>
parents: 8823
diff changeset
387 DL_EXPORT(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
388 initselect()
a4793ae26682 Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff changeset
389 {
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
390 PyObject *m, *d;
8570
bd695de18ecf Added doc strings.
Guido van Rossum <guido@python.org>
parents: 6626
diff changeset
391 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
392 d = PyModule_GetDict(m);
6367
2c29b8982643 Apply two changes, systematically:
Guido van Rossum <guido@python.org>
parents: 4734
diff changeset
393 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
394 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
395 }