[Numpy-svn] r8139 - in trunk/numpy/core/src: multiarray private

numpy-svn at scipy.org numpy-svn at scipy.org
Sat Feb 20 13:06:00 EST 2010


Author: ptvirtan
Date: 2010-02-20 12:06:00 -0600 (Sat, 20 Feb 2010)
New Revision: 8139

Modified:
   trunk/numpy/core/src/multiarray/methods.c
   trunk/numpy/core/src/multiarray/multiarraymodule.c
   trunk/numpy/core/src/private/npy_3kcompat.h
Log:
3K: BUG: fix tofile/fromfile on Py3 by dup-ing the file descriptor before fdopening -- works around issues in flushing data

Modified: trunk/numpy/core/src/multiarray/methods.c
===================================================================
--- trunk/numpy/core/src/multiarray/methods.c	2010-02-20 18:05:41 UTC (rev 8138)
+++ trunk/numpy/core/src/multiarray/methods.c	2010-02-20 18:06:00 UTC (rev 8139)
@@ -498,7 +498,11 @@
     else {
         Py_INCREF(file);
     }
-    fd = npy_PyFile_AsFile(file, "wb");
+#if defined(NPY_PY3K)
+    fd = npy_PyFile_Dup(file, "wb");
+#else
+    fd = PyFile_AsFile(file);
+#endif
     if (fd == NULL) {
         PyErr_SetString(PyExc_IOError, "first argument must be a " \
                         "string or open file");
@@ -506,6 +510,9 @@
         return NULL;
     }
     ret = PyArray_ToFile(self, fd, sep, format);
+#if defined(NPY_PY3K)
+    fclose(fd);
+#endif
     Py_DECREF(file);
     if (ret < 0) {
         return NULL;

Modified: trunk/numpy/core/src/multiarray/multiarraymodule.c
===================================================================
--- trunk/numpy/core/src/multiarray/multiarraymodule.c	2010-02-20 18:05:41 UTC (rev 8138)
+++ trunk/numpy/core/src/multiarray/multiarraymodule.c	2010-02-20 18:06:00 UTC (rev 8139)
@@ -1723,7 +1723,11 @@
     else {
         Py_INCREF(file);
     }
-    fp = npy_PyFile_AsFile(file, "rb");
+#if defined(NPY_PY3K)
+    fp = npy_PyFile_Dup(file, "rb");
+#else
+    fp = PyFile_AsFile(file);
+#endif
     if (fp == NULL) {
         PyErr_SetString(PyExc_IOError,
                 "first argument must be an open file");
@@ -1734,6 +1738,9 @@
         type = PyArray_DescrFromType(PyArray_DEFAULT);
     }
     ret = PyArray_FromFile(fp, type, (intp) nin, sep);
+#if defined(NPY_PY3K)
+    fclose(fp);
+#endif
     Py_DECREF(file);
     return ret;
 }

Modified: trunk/numpy/core/src/private/npy_3kcompat.h
===================================================================
--- trunk/numpy/core/src/private/npy_3kcompat.h	2010-02-20 18:05:41 UTC (rev 8138)
+++ trunk/numpy/core/src/private/npy_3kcompat.h	2010-02-20 18:06:00 UTC (rev 8139)
@@ -134,18 +134,27 @@
  */
 #if defined(NPY_PY3K)
 static NPY_INLINE FILE*
-npy_PyFile_AsFile(PyObject *file, char *mode)
+npy_PyFile_Dup(PyObject *file, char *mode)
 {
-    int fd;
+    int fd, fd2;
+    PyObject *ret, *os;
     fd = PyObject_AsFileDescriptor(file);
     if (fd == -1) {
         return NULL;
     }
-#warning XXX -- who releases the FILE* returned by fdopen? fclose() closes the file...
-    return fdopen(fd, mode);
+    os = PyImport_ImportModule("os");
+    if (os == NULL) {
+        return NULL;
+    }
+    ret = PyObject_CallMethod(os, "dup", "i", fd);
+    Py_DECREF(os);
+    if (ret == NULL) {
+        return NULL;
+    }
+    fd2 = PyNumber_AsSsize_t(ret, NULL);
+    Py_DECREF(ret);
+    return fdopen(fd2, mode);
 }
-#else
-#define npy_PyFile_AsFile(file, mode) PyFile_AsFile(file)
 #endif
 
 static NPY_INLINE PyObject*




More information about the Numpy-svn mailing list