Commit | Line | Data |
---|---|---|
5b1830f3 MW |
1 | ### -*-pyrex-*- |
2 | ### | |
3 | ### File descriptor juggling | |
4 | ### | |
5 | ### (c) 2009 Straylight/Edgeware | |
6 | ### | |
ea2fc600 | 7 | |
5b1830f3 MW |
8 | ###----- Licensing notice --------------------------------------------------- |
9 | ### | |
10 | ### This file is part of the Python interface to mLib. | |
11 | ### | |
12 | ### mLib/Python is free software; you can redistribute it and/or modify | |
13 | ### it under the terms of the GNU General Public License as published by | |
14 | ### the Free Software Foundation; either version 2 of the License, or | |
15 | ### (at your option) any later version. | |
16 | ### | |
17 | ### mLib/Python is distributed in the hope that it will be useful, | |
18 | ### but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 | ### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
20 | ### GNU General Public License for more details. | |
21 | ### | |
22 | ### You should have received a copy of the GNU General Public License | |
23 | ### along with mLib/Python; if not, write to the Free Software Foundation, | |
24 | ### Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
ea2fc600 MW |
25 | |
26 | def mdup(v): | |
addc0c37 MW |
27 | """ |
28 | mdup(LIST) -> LIST: | |
29 | LIST is a list (mutable sequence) of pairs (CUR, WANT). Duplicate each | |
30 | CUR file descriptor as WANT (may be -1 to mean `don't care'), closing | |
31 | original CUR. Works even if there are cycles. LIST is updated in place | |
32 | with CUR reflecting the new file descriptors even on error. Returns the | |
33 | same LIST on success. | |
34 | """ | |
ea2fc600 MW |
35 | cdef mdup_fd *vv |
36 | cdef size_t n | |
37 | cdef int i | |
38 | cdef int rc | |
39 | ||
40 | n = len(v) | |
41 | vv = <mdup_fd *>xmalloc(n * PSIZEOF(vv)) | |
42 | for 0 <= i < n: | |
43 | vv[i].cur, vv[i].want = v[i] | |
44 | rc = _mdup(vv, n) | |
45 | for 0 <= i < n: | |
46 | v[i] = vv[i].cur, vv[i].want | |
47 | if rc < 0: | |
48 | _oserror() | |
49 | return v | |
50 | ||
5b1830f3 | 51 | ###----- That's all, folks -------------------------------------------------- |