annotate Modules/selectmodule.c @ 5817:48b94de85bf3 legacy-trunk

Extensive changes by AMK.
author Guido van Rossum <guido@python.org>
date Thu, 17 Jul 1997 16:29:42 +0000
parents 3db423515626
children 2c29b8982643
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.
3675
d617de15774b Yet another pass at fd2obj -- this time for Windows
Guido van Rossum <guido@python.org>
parents: 2611
diff changeset
36 */
902
a4793ae26682 Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff changeset
37
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
38 #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
39
4600
d36250aefb82 Keep gcc -Wall happy.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
40 #ifdef HAVE_UNISTD_H
d36250aefb82 Keep gcc -Wall happy.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
41 #include <unistd.h>
d36250aefb82 Keep gcc -Wall happy.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
42 #endif
d36250aefb82 Keep gcc -Wall happy.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
43
4621
a2f171ce25f8 Add ``extern void bzero();'' for SGI, to keep gcc -Wall happy.
Guido van Rossum <guido@python.org>
parents: 4600
diff changeset
44 #ifdef __sgi
a2f171ce25f8 Add ``extern void bzero();'' for SGI, to keep gcc -Wall happy.
Guido van Rossum <guido@python.org>
parents: 4600
diff changeset
45 /* 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
46 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
47 #endif
a2f171ce25f8 Add ``extern void bzero();'' for SGI, to keep gcc -Wall happy.
Guido van Rossum <guido@python.org>
parents: 4600
diff changeset
48
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1392
diff changeset
49 #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
50
3738
ce74bba8a6a6 Slightly different Windows ifdefs
Guido van Rossum <guido@python.org>
parents: 3675
diff changeset
51 #ifdef MS_WINDOWS
3675
d617de15774b Yet another pass at fd2obj -- this time for Windows
Guido van Rossum <guido@python.org>
parents: 2611
diff changeset
52 #include <winsock.h>
d617de15774b Yet another pass at fd2obj -- this time for Windows
Guido van Rossum <guido@python.org>
parents: 2611
diff changeset
53 #else
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1392
diff changeset
54 #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
55 #define SOCKET int
d617de15774b Yet another pass at fd2obj -- this time for Windows
Guido van Rossum <guido@python.org>
parents: 2611
diff changeset
56 #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
57
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
58 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
59
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
60 /* 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
61 typedef struct {
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
62 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
63 SOCKET fd;
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
64 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
65 } pylist;
d617de15774b Yet another pass at fd2obj -- this time for Windows
Guido van Rossum <guido@python.org>
parents: 2611
diff changeset
66
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
67 static void
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
68 reap_obj(fd2obj)
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
69 pylist fd2obj[FD_SETSIZE + 3];
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
70 {
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
71 int i;
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
72 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
73 Py_XDECREF(fd2obj[i].obj);
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
74 fd2obj[i].obj = NULL;
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
75 }
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
76 fd2obj[0].sentinel = -1;
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
77 }
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
78
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
79
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
80 /* 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
81 returns a number >= 0
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
82 */
3675
d617de15774b Yet another pass at fd2obj -- this time for Windows
Guido van Rossum <guido@python.org>
parents: 2611
diff changeset
83 static int
922
c12b445cdace * myselect.h: bzero -> memset
Guido van Rossum <guido@python.org>
parents: 904
diff changeset
84 list2set(list, set, fd2obj)
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
85 PyObject *list;
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
86 fd_set *set;
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
87 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
88 {
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
89 int i;
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
90 int max = -1;
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
91 int index = 0;
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
92 int len = PyList_Size(list);
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
93 PyObject* o = NULL;
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
94
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
95 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
96 FD_ZERO(set);
2611
01687ce43708 fix subtle refcnt bug
Guido van Rossum <guido@python.org>
parents: 2061
diff changeset
97
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
98 for (i = 0; i < len; i++) {
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
99 PyObject *meth;
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
100 SOCKET v;
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
101
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
102 /* any intervening fileno() calls could decr this refcnt */
4730
62cf7d589300 list2set(): PyList_GetItem could fail.
Barry Warsaw <barry@python.org>
parents: 4700
diff changeset
103 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
104 return -1;
4730
62cf7d589300 list2set(): PyList_GetItem could fail.
Barry Warsaw <barry@python.org>
parents: 4700
diff changeset
105
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
106 Py_INCREF(o);
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
107
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
108 if (PyInt_Check(o)) {
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
109 v = PyInt_AsLong(o);
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
110 }
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
111 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
112 {
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
113 PyObject *fno = PyEval_CallObject(meth, NULL);
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
114 Py_DECREF(meth);
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
115 if (fno == NULL)
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
116 goto finally;
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
117
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
118 if (!PyInt_Check(fno)) {
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
119 PyErr_SetString(PyExc_TypeError,
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
120 "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
121 Py_DECREF(fno);
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
122 goto finally;
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
123 }
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
124 v = PyInt_AsLong(fno);
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
125 Py_DECREF(fno);
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
126 }
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
127 else {
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
128 PyErr_SetString(PyExc_TypeError,
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
129 "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
130 goto finally;
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
131 }
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
132 #ifdef _MSC_VER
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
133 max = 0; /* not used for Win32 */
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
134 #else /* !_MSC_VER */
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
135 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
136 PyErr_SetString(PyExc_ValueError,
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
137 "filedescriptor out of range in select()");
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
138 goto finally;
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
139 }
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
140 if (v > max)
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
141 max = v;
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
142 #endif /* _MSC_VER */
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
143 FD_SET(v, set);
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
144
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
145 /* 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
146 if (index >= FD_SETSIZE) {
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
147 PyErr_SetString(PyExc_ValueError,
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
148 "too many file descriptors in select()");
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
149 goto finally;
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
150 }
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
151 fd2obj[index].obj = o;
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
152 fd2obj[index].fd = v;
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
153 fd2obj[index].sentinel = 0;
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
154 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
155 }
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
156 return max+1;
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
157
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
158 finally:
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
159 Py_XDECREF(o);
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
160 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
161 }
a4793ae26682 Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff changeset
162
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
163 /* 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
164 static PyObject *
3675
d617de15774b Yet another pass at fd2obj -- this time for Windows
Guido van Rossum <guido@python.org>
parents: 2611
diff changeset
165 set2list(set, fd2obj)
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
166 fd_set *set;
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
167 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
168 {
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
169 int i, j, count=0;
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
170 PyObject *list, *o;
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
171 SOCKET fd;
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
172
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
173 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
174 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
175 count++;
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
176 }
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
177 list = PyList_New(count);
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
178 if (!list)
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
179 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
180
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
181 i = 0;
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
182 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
183 fd = fd2obj[j].fd;
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
184 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
185 #ifndef _MSC_VER
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
186 if (fd > FD_SETSIZE) {
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
187 PyErr_SetString(PyExc_SystemError,
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
188 "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
189 goto finally;
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
190 }
3675
d617de15774b Yet another pass at fd2obj -- this time for Windows
Guido van Rossum <guido@python.org>
parents: 2611
diff changeset
191 #endif
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
192 o = fd2obj[j].obj;
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
193 fd2obj[j].obj = NULL;
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
194 /* transfer ownership */
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
195 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
196 goto finally;
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
197
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
198 i++;
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
199 }
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
200 }
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
201 return list;
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
202 finally:
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
203 Py_DECREF(list);
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
204 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
205 }
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
206
902
a4793ae26682 Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff changeset
207
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
208 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
209 select_select(self, args)
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
210 PyObject *self;
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
211 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
212 {
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
213 pylist rfd2obj[FD_SETSIZE + 3];
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
214 pylist wfd2obj[FD_SETSIZE + 3];
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
215 pylist efd2obj[FD_SETSIZE + 3];
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
216 PyObject *ifdlist, *ofdlist, *efdlist;
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
217 PyObject *ret = NULL;
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
218 PyObject *tout = Py_None;
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
219 fd_set ifdset, ofdset, efdset;
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
220 double timeout;
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
221 struct timeval tv, *tvp;
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
222 int seconds;
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
223 int imax, omax, emax, max;
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
224 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
225
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
226 /* convert arguments */
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
227 if (!PyArg_ParseTuple(args, "OOO|O",
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
228 &ifdlist, &ofdlist, &efdlist, &tout))
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
229 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
230
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
231 if (tout == Py_None)
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
232 tvp = (struct timeval *)0;
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
233 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
234 PyErr_SetString(PyExc_TypeError,
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
235 "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
236 return NULL;
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
237 }
1390
cffe8d6b2f09 * selectmodule.c (select_select): timeout argument may be None with same
Guido van Rossum <guido@python.org>
parents: 1284
diff changeset
238 else {
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
239 seconds = (int)timeout;
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
240 timeout = timeout - (double)seconds;
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
241 tv.tv_sec = seconds;
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
242 tv.tv_usec = (int)(timeout*1000000.0);
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
243 tvp = &tv;
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
244 }
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
245
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
246 /* sanity check first three arguments */
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
247 if (!PyList_Check(ifdlist) ||
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
248 !PyList_Check(ofdlist) ||
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
249 !PyList_Check(efdlist))
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
250 {
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
251 PyErr_SetString(PyExc_TypeError,
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
252 "arguments 1-3 must be lists");
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
253 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
254 }
902
a4793ae26682 Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff changeset
255
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
256 /* 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
257 * propagates the Python exception set in list2set()
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
258 */
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
259 rfd2obj[0].sentinel = -1;
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
260 wfd2obj[0].sentinel = -1;
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
261 efd2obj[0].sentinel = -1;
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
262 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
263 goto finally;
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
264 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
265 goto finally;
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
266 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
267 goto finally;
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
268 max = imax;
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
269 if (omax > max) max = omax;
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
270 if (emax > max) max = emax;
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
271
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
272 Py_BEGIN_ALLOW_THREADS
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
273 n = select(max, &ifdset, &ofdset, &efdset, tvp);
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
274 Py_END_ALLOW_THREADS
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
275
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
276 if (n < 0) {
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
277 PyErr_SetFromErrno(SelectError);
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
278 }
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
279 else if (n == 0) {
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
280 /* optimization */
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
281 ifdlist = PyList_New(0);
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
282 if (ifdlist) {
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
283 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
284 Py_DECREF(ifdlist);
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
285 }
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
286 }
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
287 else {
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
288 /* 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
289 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
290 is that acceptable?
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
291 */
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
292 ifdlist = set2list(&ifdset, rfd2obj);
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
293 ofdlist = set2list(&ofdset, wfd2obj);
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
294 efdlist = set2list(&efdset, efd2obj);
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
295 if (PyErr_Occurred())
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
296 ret = NULL;
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
297 else
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
298 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
299
4700
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
300 Py_DECREF(ifdlist);
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
301 Py_DECREF(ofdlist);
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
302 Py_DECREF(efdlist);
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
303 }
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
304
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
305 finally:
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
306 reap_obj(rfd2obj);
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
307 reap_obj(wfd2obj);
825038d98ab6 Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents: 4692
diff changeset
308 reap_obj(efd2obj);
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
309 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
310 }
a4793ae26682 Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff changeset
311
a4793ae26682 Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff changeset
312
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
313 static PyMethodDef select_methods[] = {
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
314 {"select", select_select, 1},
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
315 {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
316 };
a4793ae26682 Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff changeset
317
a4793ae26682 Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff changeset
318
a4793ae26682 Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff changeset
319 void
a4793ae26682 Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff changeset
320 initselect()
a4793ae26682 Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff changeset
321 {
4692
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
322 PyObject *m, *d;
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
323 m = Py_InitModule("select", select_methods);
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
324 d = PyModule_GetDict(m);
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
325 SelectError = PyString_FromString("select.error");
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
326 PyDict_SetItemString(d, "error", SelectError);
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
327 if (PyErr_Occurred())
cb405b9696d4 Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents: 4621
diff changeset
328 Py_FatalError("Cannot initialize select module");
902
a4793ae26682 Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff changeset
329 }