annotate Modules/selectmodule.c @ 7278:59d2761ba1cf legacy-trunk

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