X-Git-Url: https://git.distorted.org.uk/~mdw/mLib-python/blobdiff_plain/ea2fc600626d9c59df58fd46a369eacea57f02fe..965caf5fc74fbbf2516eb1d098fac07cfbdb6820:/mdup.pyx diff --git a/mdup.pyx b/mdup.pyx index 24cd995..12e727c 100644 --- a/mdup.pyx +++ b/mdup.pyx @@ -1,45 +1,54 @@ -# -*-pyrex-*- -# -# $Id$ -# -# File descriptor juggling -# -# (c) 2009 Straylight/Edgeware -# +### -*-pyrex-*- +### +### File descriptor juggling +### +### (c) 2009 Straylight/Edgeware +### -#----- Licensing notice ----------------------------------------------------- -# -# This file is part of the Python interface to mLib. -# -# mLib/Python is free software; you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation; either version 2 of the License, or -# (at your option) any later version. -# -# mLib/Python is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with mLib/Python; if not, write to the Free Software Foundation, -# Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +###----- Licensing notice --------------------------------------------------- +### +### This file is part of the Python interface to mLib. +### +### mLib/Python is free software; you can redistribute it and/or modify +### it under the terms of the GNU General Public License as published by +### the Free Software Foundation; either version 2 of the License, or +### (at your option) any later version. +### +### mLib/Python is distributed in the hope that it will be useful, +### but WITHOUT ANY WARRANTY; without even the implied warranty of +### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +### GNU General Public License for more details. +### +### You should have received a copy of the GNU General Public License +### along with mLib/Python; if not, write to the Free Software Foundation, +### Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. def mdup(v): - cdef mdup_fd *vv + """ + mdup(LIST) -> LIST: + LIST is a list (mutable sequence) of pairs (CUR, WANT). Duplicate each + CUR file descriptor as WANT (may be -1 to mean `don't care'), closing + original CUR. Works even if there are cycles. LIST is updated in place + with CUR reflecting the new file descriptors even on error. Returns the + same LIST on success. + """ + cdef mdup_fd *vv = NULL cdef size_t n cdef int i cdef int rc n = len(v) vv = xmalloc(n * PSIZEOF(vv)) - for 0 <= i < n: - vv[i].cur, vv[i].want = v[i] - rc = _mdup(vv, n) - for 0 <= i < n: - v[i] = vv[i].cur, vv[i].want - if rc < 0: - _oserror() + try: + for 0 <= i < n: + vv[i].cur, vv[i].want = v[i] + rc = _mdup(vv, n) + for 0 <= i < n: + v[i] = vv[i].cur, vv[i].want + if rc < 0: + _oserror() + finally: + xfree(vv) return v -#----- That's all, folks ---------------------------------------------------- +###----- That's all, folks --------------------------------------------------