Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 62dba4c

Browse files
committed
select.select() now accepts a sequence (as defined by PySequence_Fast()) for
its first three arguments. Closes RFE #798046 .
1 parent fd4fa88 commit 62dba4c

3 files changed

Lines changed: 25 additions & 23 deletions

File tree

Doc/lib/libselect.tex

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@ \section{\module{select} ---
3232

3333
\begin{funcdesc}{select}{iwtd, owtd, ewtd\optional{, timeout}}
3434
This is a straightforward interface to the \UNIX{} \cfunction{select()}
35-
system call. The first three arguments are lists of `waitable
35+
system call. The first three arguments are sequences of `waitable
3636
objects': either integers representing file descriptors or
3737
objects with a parameterless method named \method{fileno()} returning
38-
such an integer. The three lists of waitable objects are for input,
39-
output and `exceptional conditions', respectively. Empty lists are
40-
allowed, but acceptance of three empty lists is platform-dependent.
38+
such an integer. The three sequences of waitable objects are for input,
39+
output and `exceptional conditions', respectively. Empty sequences are
40+
allowed, but acceptance of three empty sequences is platform-dependent.
4141
(It is known to work on \UNIX{} but not on Windows.) The optional
4242
\var{timeout} argument specifies a time-out as a floating point number
4343
in seconds. When the \var{timeout} argument is omitted the function
@@ -49,7 +49,7 @@ \section{\module{select} ---
4949
without a file descriptor becoming ready, three empty lists are
5050
returned.
5151

52-
Among the acceptable object types in the lists are Python file
52+
Among the acceptable object types in the sequences are Python file
5353
objects (e.g. \code{sys.stdin}, or objects returned by
5454
\function{open()} or \function{os.popen()}), socket objects
5555
returned by \function{socket.socket()}.%

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ Core and builtins
2424
Extension modules
2525
-----------------
2626

27+
- select.select() now accepts sequences for its first three arguments.
28+
2729
- cStringIO now supports the f.closed attribute.
2830

2931
- The signal module now exposes SIGRTMIN and SIGRTMAX (if available).

Modules/selectmodule.c

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -75,22 +75,29 @@ reap_obj(pylist fd2obj[FD_SETSIZE + 1])
7575
returns a number >= 0
7676
*/
7777
static int
78-
list2set(PyObject *list, fd_set *set, pylist fd2obj[FD_SETSIZE + 1])
78+
seq2set(PyObject *seq, fd_set *set, pylist fd2obj[FD_SETSIZE + 1])
7979
{
8080
int i;
8181
int max = -1;
8282
int index = 0;
83-
int len = PyList_Size(list);
83+
int len = -1;
84+
PyObject* fast_seq = NULL;
8485
PyObject* o = NULL;
8586

8687
fd2obj[0].obj = (PyObject*)0; /* set list to zero size */
8788
FD_ZERO(set);
8889

90+
fast_seq=PySequence_Fast(seq, "arguments 1-3 must be sequences");
91+
if (!fast_seq)
92+
return -1;
93+
94+
len = PySequence_Fast_GET_SIZE(fast_seq);
95+
8996
for (i = 0; i < len; i++) {
9097
SOCKET v;
9198

9299
/* any intervening fileno() calls could decr this refcnt */
93-
if (!(o = PyList_GetItem(list, i)))
100+
if (!(o = PySequence_Fast_GET_ITEM(fast_seq, i)))
94101
return -1;
95102

96103
Py_INCREF(o);
@@ -121,10 +128,12 @@ list2set(PyObject *list, fd_set *set, pylist fd2obj[FD_SETSIZE + 1])
121128
fd2obj[index].sentinel = 0;
122129
fd2obj[++index].sentinel = -1;
123130
}
131+
Py_DECREF(fast_seq);
124132
return max+1;
125133

126134
finally:
127135
Py_XDECREF(o);
136+
Py_DECREF(fast_seq);
128137
return -1;
129138
}
130139

@@ -229,15 +238,6 @@ select_select(PyObject *self, PyObject *args)
229238
tvp = &tv;
230239
}
231240

232-
/* sanity check first three arguments */
233-
if (!PyList_Check(ifdlist) ||
234-
!PyList_Check(ofdlist) ||
235-
!PyList_Check(efdlist))
236-
{
237-
PyErr_SetString(PyExc_TypeError,
238-
"arguments 1-3 must be lists");
239-
return NULL;
240-
}
241241

242242
#ifdef SELECT_USES_HEAP
243243
/* Allocate memory for the lists */
@@ -251,17 +251,17 @@ select_select(PyObject *self, PyObject *args)
251251
return PyErr_NoMemory();
252252
}
253253
#endif /* SELECT_USES_HEAP */
254-
/* Convert lists to fd_sets, and get maximum fd number
255-
* propagates the Python exception set in list2set()
254+
/* Convert sequences to fd_sets, and get maximum fd number
255+
* propagates the Python exception set in seq2set()
256256
*/
257257
rfd2obj[0].sentinel = -1;
258258
wfd2obj[0].sentinel = -1;
259259
efd2obj[0].sentinel = -1;
260-
if ((imax=list2set(ifdlist, &ifdset, rfd2obj)) < 0)
260+
if ((imax=seq2set(ifdlist, &ifdset, rfd2obj)) < 0)
261261
goto finally;
262-
if ((omax=list2set(ofdlist, &ofdset, wfd2obj)) < 0)
262+
if ((omax=seq2set(ofdlist, &ofdset, wfd2obj)) < 0)
263263
goto finally;
264-
if ((emax=list2set(efdlist, &efdset, efd2obj)) < 0)
264+
if ((emax=seq2set(efdlist, &efdset, efd2obj)) < 0)
265265
goto finally;
266266
max = imax;
267267
if (omax > max) max = omax;
@@ -618,7 +618,7 @@ PyDoc_STRVAR(select_doc,
618618
"select(rlist, wlist, xlist[, timeout]) -> (rlist, wlist, xlist)\n\
619619
\n\
620620
Wait until one or more file descriptors are ready for some kind of I/O.\n\
621-
The first three arguments are lists of file descriptors to be waited for:\n\
621+
The first three arguments are sequences of file descriptors to be waited for:\n\
622622
rlist -- wait until ready for reading\n\
623623
wlist -- wait until ready for writing\n\
624624
xlist -- wait for an ``exceptional condition''\n\

0 commit comments

Comments
 (0)