@@@ lbuf needs test
[mLib-python] / mdup.pyx
index 7256211..12e727c 100644 (file)
--- a/mdup.pyx
+++ b/mdup.pyx
@@ -32,20 +32,23 @@ def mdup(v):
     with CUR reflecting the new file descriptors even on error.  Returns the
     same LIST on success.
   """
-  cdef mdup_fd *vv
+  cdef mdup_fd *vv = NULL
   cdef size_t n
   cdef int i
   cdef int rc
 
   n = len(v)
   vv = <mdup_fd *>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 --------------------------------------------------