annotate Modules/selectmodule.c @ 10072:ecebcd84df37 legacy-trunk

put the preferences file in a folder called "Python" inside the prefs folder, just like the IDE does -- jvr
author Just van Rossum <just@lettererror.com>
date Tue, 02 Feb 1999 15:49:03 +0000
parents 38023a425e48
children 6aac31cd2102
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
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1392
diff changeset
51 #include <sys/types.h>
3675
d617de15774b Yet another pass at fd2obj -- this time for Windows
Guido van Rossum <guido@python.org>
parents: 2611
diff changeset
52
6626
5e31d381fa47 os2 patch by Jeff Rush
Guido van Rossum <guido@python.org>
parents: 6367
diff changeset
53 #if defined(PYOS_OS2)
5e31d381fa47 os2 patch by Jeff Rush
Guido van Rossum <guido@python.org>
parents: 6367
diff changeset
54 #include <sys/time.h>
5e31d381fa47 os2 patch by Jeff Rush
Guido van Rossum <guido@python.org>
parents: 6367
diff changeset
55 #include <utils.h>
5e31d381fa47 os2 patch by Jeff Rush
Guido van Rossum <guido@python.org>
parents: 6367
diff changeset
56 #endif
5e31d381fa47 os2 patch by Jeff Rush
Guido van Rossum <guido@python.org>
parents: 6367
diff changeset
57
3738
ce74bba8a6a6 Slightly different Windows ifdefs
Guido van Rossum <guido@python.org>
parents: 3675
diff changeset
58 #ifdef MS_WINDOWS
3675
d617de15774b Yet another pass at fd2obj -- this time for Windows
Guido van Rossum <guido@python.org>
parents: 2611
diff changeset
59 #include <winsock.h>
d617de15774b Yet another pass at fd2obj -- this time for Windows
Guido van Rossum <guido@python.org>
parents: 2611
diff changeset
60 #else
8823
f2a4a2b60860 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8616
diff changeset
61 #ifdef __BEOS__
f2a4a2b60860 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8616
diff changeset
62 #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
63 #define SOCKET int
f2a4a2b60860 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8616
diff changeset
64 #else
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1392
diff changeset
65 #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
66 #define SOCKET int
d617de15774b Yet another pass at fd2obj -- this time for Windows
Guido van Rossum <guido@python.org>
parents: 2611
diff changeset
67 #endif
8823
f2a4a2b60860 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8616
diff changeset
68 #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
69
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
70 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
71
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
72 /* 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
73 typedef struct {
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
74 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
75 SOCKET fd;
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
76 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
77 } pylist;
d617de15774b Yet another pass at fd2obj -- this time for Windows
Guido van Rossum <guido@python.org>
parents: 2611
diff changeset
78
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
79 static void
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
80 reap_obj(fd2obj)
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
81 pylist fd2obj[FD_SETSIZE + 3];
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
82 {
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
83 int i;
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
84 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
85 Py_XDECREF(fd2obj[i].obj);
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
86 fd2obj[i].obj = NULL;
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
87 }
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
88 fd2obj[0].sentinel = -1;
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
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
91
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
92 /* 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
93 returns a number >= 0
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
94 */
3675
d617de15774b Yet another pass at fd2obj -- this time for Windows
Guido van Rossum <guido@python.org>
parents: 2611
diff changeset
95 static int
922
c12b445cdace * myselect.h: bzero -> memset
Guido van Rossum <guido@python.org>
parents: 904
diff changeset
96 list2set(list, set, fd2obj)
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
97 PyObject *list;
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
98 fd_set *set;
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
99 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
100 {
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
101 int i;
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
102 int max = -1;
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
103 int index = 0;
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
104 int len = PyList_Size(list);
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
105 PyObject* o = NULL;
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
106
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
107 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
108 FD_ZERO(set);
2611
01687ce43708 fix subtle refcnt bug
Guido van Rossum <guido@python.org>
parents: 2061
diff changeset
109
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
110 for (i = 0; i < len; i++) {
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
111 PyObject *meth;
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
112 SOCKET v;
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
113
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
114 /* any intervening fileno() calls could decr this refcnt */
4730
62cf7d589300 list2set(): PyList_GetItem could fail.
Barry Warsaw <barry@python.org>
parents: 4700
diff changeset
115 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
116 return -1;
4730
62cf7d589300 list2set(): PyList_GetItem could fail.
Barry Warsaw <barry@python.org>
parents: 4700
diff changeset
117
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
118 Py_INCREF(o);
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
119
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
120 if (PyInt_Check(o)) {
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
121 v = PyInt_AsLong(o);
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
122 }
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
123 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
124 {
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
125 PyObject *fno = PyEval_CallObject(meth, NULL);
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
126 Py_DECREF(meth);
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
127 if (fno == NULL)
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
128 goto finally;
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
129
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
130 if (!PyInt_Check(fno)) {
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
131 PyErr_SetString(PyExc_TypeError,
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
132 "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
133 Py_DECREF(fno);
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
134 goto finally;
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
135 }
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
136 v = PyInt_AsLong(fno);
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
137 Py_DECREF(fno);
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
138 }
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
139 else {
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
140 PyErr_SetString(PyExc_TypeError,
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
141 "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
142 goto finally;
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
143 }
8823
f2a4a2b60860 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8616
diff changeset
144 #if defined(_MSC_VER) || defined(__BEOS__)
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
145 max = 0; /* not used for Win32 */
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
146 #else /* !_MSC_VER */
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
147 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
148 PyErr_SetString(PyExc_ValueError,
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
149 "filedescriptor out of range in select()");
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
150 goto finally;
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
151 }
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
152 if (v > max)
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
153 max = v;
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
154 #endif /* _MSC_VER */
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
155 FD_SET(v, set);
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
156
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
157 /* 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
158 if (index >= FD_SETSIZE) {
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
159 PyErr_SetString(PyExc_ValueError,
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
160 "too many file descriptors in select()");
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
161 goto finally;
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
162 }
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
163 fd2obj[index].obj = o;
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
164 fd2obj[index].fd = v;
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
165 fd2obj[index].sentinel = 0;
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
166 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
167 }
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
168 return max+1;
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
169
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
170 finally:
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
171 Py_XDECREF(o);
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
172 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
173 }
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 /* 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
176 static PyObject *
3675
d617de15774b Yet another pass at fd2obj -- this time for Windows
Guido van Rossum <guido@python.org>
parents: 2611
diff changeset
177 set2list(set, fd2obj)
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
178 fd_set *set;
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
179 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
180 {
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
181 int i, j, count=0;
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
182 PyObject *list, *o;
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
183 SOCKET fd;
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
184
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
185 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
186 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
187 count++;
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
188 }
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
189 list = PyList_New(count);
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
190 if (!list)
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
191 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
192
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
193 i = 0;
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
194 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
195 fd = fd2obj[j].fd;
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
196 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
197 #ifndef _MSC_VER
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
198 if (fd > FD_SETSIZE) {
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
199 PyErr_SetString(PyExc_SystemError,
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
200 "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
201 goto finally;
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
202 }
3675
d617de15774b Yet another pass at fd2obj -- this time for Windows
Guido van Rossum <guido@python.org>
parents: 2611
diff changeset
203 #endif
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
204 o = fd2obj[j].obj;
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
205 fd2obj[j].obj = NULL;
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
206 /* transfer ownership */
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
207 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
208 goto finally;
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
209
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
210 i++;
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
211 }
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
212 }
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
213 return list;
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
214 finally:
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
215 Py_DECREF(list);
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
216 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
217 }
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
218
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
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
220 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
221 select_select(self, args)
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
222 PyObject *self;
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
223 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
224 {
8616
2417f17fa3e5 On Windows, put the select file descriptor arrays on the heap.
Guido van Rossum <guido@python.org>
parents: 8570
diff changeset
225 #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
226 /* 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
227 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
228 #else
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
229 pylist rfd2obj[FD_SETSIZE + 3];
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
230 pylist wfd2obj[FD_SETSIZE + 3];
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
231 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
232 #endif
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
233 PyObject *ifdlist, *ofdlist, *efdlist;
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
234 PyObject *ret = NULL;
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
235 PyObject *tout = Py_None;
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
236 fd_set ifdset, ofdset, efdset;
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
237 double timeout;
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
238 struct timeval tv, *tvp;
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
239 int seconds;
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
240 int imax, omax, emax, max;
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
241 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
242
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
243 /* convert arguments */
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
244 if (!PyArg_ParseTuple(args, "OOO|O",
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
245 &ifdlist, &ofdlist, &efdlist, &tout))
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
246 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
247
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
248 if (tout == Py_None)
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
249 tvp = (struct timeval *)0;
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
250 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
251 PyErr_SetString(PyExc_TypeError,
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
252 "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
253 return NULL;
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
254 }
1390
cffe8d6b2f09 * selectmodule.c (select_select): timeout argument may be None with same
Guido van Rossum <guido@python.org>
parents: 1284
diff changeset
255 else {
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
256 seconds = (int)timeout;
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
257 timeout = timeout - (double)seconds;
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
258 tv.tv_sec = seconds;
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
259 tv.tv_usec = (int)(timeout*1000000.0);
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
260 tvp = &tv;
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
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
263 /* sanity check first three arguments */
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
264 if (!PyList_Check(ifdlist) ||
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
265 !PyList_Check(ofdlist) ||
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
266 !PyList_Check(efdlist))
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 PyErr_SetString(PyExc_TypeError,
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
269 "arguments 1-3 must be lists");
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
270 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
271 }
902
a4793ae26682 Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff changeset
272
8616
2417f17fa3e5 On Windows, put the select file descriptor arrays on the heap.
Guido van Rossum <guido@python.org>
parents: 8570
diff changeset
273 #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
274 /* 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
275 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
276 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
277 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
278 if (rfd2obj == NULL || wfd2obj == NULL || efd2obj == NULL) {
2417f17fa3e5 On Windows, put the select file descriptor arrays on the heap.
Guido van Rossum <guido@python.org>
parents: 8570
diff changeset
279 PyMem_XDEL(rfd2obj);
2417f17fa3e5 On Windows, put the select file descriptor arrays on the heap.
Guido van Rossum <guido@python.org>
parents: 8570
diff changeset
280 PyMem_XDEL(wfd2obj);
2417f17fa3e5 On Windows, put the select file descriptor arrays on the heap.
Guido van Rossum <guido@python.org>
parents: 8570
diff changeset
281 PyMem_XDEL(efd2obj);
2417f17fa3e5 On Windows, put the select file descriptor arrays on the heap.
Guido van Rossum <guido@python.org>
parents: 8570
diff changeset
282 return NULL;
2417f17fa3e5 On Windows, put the select file descriptor arrays on the heap.
Guido van Rossum <guido@python.org>
parents: 8570
diff changeset
283 }
2417f17fa3e5 On Windows, put the select file descriptor arrays on the heap.
Guido van Rossum <guido@python.org>
parents: 8570
diff changeset
284 #endif
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
285 /* 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
286 * propagates the Python exception set in list2set()
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
287 */
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
288 rfd2obj[0].sentinel = -1;
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
289 wfd2obj[0].sentinel = -1;
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
290 efd2obj[0].sentinel = -1;
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
291 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
292 goto finally;
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
293 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
294 goto finally;
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
295 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
296 goto finally;
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
297 max = imax;
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
298 if (omax > max) max = omax;
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
299 if (emax > max) max = emax;
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
300
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
301 Py_BEGIN_ALLOW_THREADS
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
302 n = select(max, &ifdset, &ofdset, &efdset, tvp);
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
303 Py_END_ALLOW_THREADS
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
304
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
305 if (n < 0) {
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
306 PyErr_SetFromErrno(SelectError);
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
307 }
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
308 else if (n == 0) {
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
309 /* optimization */
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
310 ifdlist = PyList_New(0);
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
311 if (ifdlist) {
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
312 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
313 Py_DECREF(ifdlist);
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
314 }
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
315 }
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
316 else {
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
317 /* 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
318 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
319 is that acceptable?
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
320 */
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
321 ifdlist = set2list(&ifdset, rfd2obj);
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
322 ofdlist = set2list(&ofdset, wfd2obj);
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
323 efdlist = set2list(&efdset, efd2obj);
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
324 if (PyErr_Occurred())
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
325 ret = NULL;
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
326 else
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
327 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
328
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
329 Py_DECREF(ifdlist);
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
330 Py_DECREF(ofdlist);
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
331 Py_DECREF(efdlist);
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
332 }
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
333
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
334 finally:
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
335 reap_obj(rfd2obj);
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
336 reap_obj(wfd2obj);
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
337 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
338 #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
339 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
340 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
341 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
342 #endif
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
343 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
344 }
a4793ae26682 Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff changeset
345
8570
bd695de18ecf Added doc strings.
Guido van Rossum <guido@python.org>
parents: 6626
diff changeset
346 static char select_doc[] =
bd695de18ecf Added doc strings.
Guido van Rossum <guido@python.org>
parents: 6626
diff changeset
347 "select(rlist, wlist, xlist[, timeout]) -> (rlist, wlist, xlist)\n\
bd695de18ecf Added doc strings.
Guido van Rossum <guido@python.org>
parents: 6626
diff changeset
348 \n\
bd695de18ecf Added doc strings.
Guido van Rossum <guido@python.org>
parents: 6626
diff changeset
349 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
350 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
351 rlist -- wait until ready for reading\n\
bd695de18ecf Added doc strings.
Guido van Rossum <guido@python.org>
parents: 6626
diff changeset
352 wlist -- wait until ready for writing\n\
bd695de18ecf Added doc strings.
Guido van Rossum <guido@python.org>
parents: 6626
diff changeset
353 xlist -- wait for an ``exceptional condition''\n\
bd695de18ecf Added doc strings.
Guido van Rossum <guido@python.org>
parents: 6626
diff changeset
354 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
355 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
356 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
357 \n\
bd695de18ecf Added doc strings.
Guido van Rossum <guido@python.org>
parents: 6626
diff changeset
358 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
359 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
360 or None, the call will never time out.\n\
bd695de18ecf Added doc strings.
Guido van Rossum <guido@python.org>
parents: 6626
diff changeset
361 \n\
bd695de18ecf Added doc strings.
Guido van Rossum <guido@python.org>
parents: 6626
diff changeset
362 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
363 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
364 that are ready.\n\
bd695de18ecf Added doc strings.
Guido van Rossum <guido@python.org>
parents: 6626
diff changeset
365 \n\
bd695de18ecf Added doc strings.
Guido van Rossum <guido@python.org>
parents: 6626
diff changeset
366 *** IMPORTANT NOTICE ***\n\
bd695de18ecf Added doc strings.
Guido van Rossum <guido@python.org>
parents: 6626
diff changeset
367 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
368
902
a4793ae26682 Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff changeset
369
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
370 static PyMethodDef select_methods[] = {
8570
bd695de18ecf Added doc strings.
Guido van Rossum <guido@python.org>
parents: 6626
diff changeset
371 {"select", select_select, 1, select_doc},
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
372 {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
373 };
a4793ae26682 Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff changeset
374
8570
bd695de18ecf Added doc strings.
Guido van Rossum <guido@python.org>
parents: 6626
diff changeset
375 static char module_doc[] =
bd695de18ecf Added doc strings.
Guido van Rossum <guido@python.org>
parents: 6626
diff changeset
376 "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
377 \n\
bd695de18ecf Added doc strings.
Guido van Rossum <guido@python.org>
parents: 6626
diff changeset
378 *** IMPORTANT NOTICE ***\n\
bd695de18ecf Added doc strings.
Guido van Rossum <guido@python.org>
parents: 6626
diff changeset
379 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
380
9588
38023a425e48 Add DL_EXPORT() to all modules that could possibly be used
Guido van Rossum <guido@python.org>
parents: 8823
diff changeset
381 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
382 initselect()
a4793ae26682 Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff changeset
383 {
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
384 PyObject *m, *d;
8570
bd695de18ecf Added doc strings.
Guido van Rossum <guido@python.org>
parents: 6626
diff changeset
385 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
386 d = PyModule_GetDict(m);
6367
2c29b8982643 Apply two changes, systematically:
Guido van Rossum <guido@python.org>
parents: 4734
diff changeset
387 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
388 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
389 }