[Python-checkins] cpython (merge 3.2 -> default): (Merge 3.2) Handle correctly _Py_fopen() error: don't replace the exception

victor.stinner python-checkins at python.org
Sun Dec 18 21:04:48 CET 2011


http://hg.python.org/cpython/rev/f44ab94cd28b
changeset:   74060:f44ab94cd28b
parent:      74058:2a140b01d343
parent:      74059:586bcc05f88f
user:        Victor Stinner <victor.stinner at haypocalc.com>
date:        Sun Dec 18 21:05:22 2011 +0100
summary:
  (Merge 3.2) Handle correctly _Py_fopen() error: don't replace the exception

files:
  Modules/zipimport.c |   8 +++++---
  Python/import.c     |  30 +++++++++++++++++++++---------
  2 files changed, 26 insertions(+), 12 deletions(-)


diff --git a/Modules/zipimport.c b/Modules/zipimport.c
--- a/Modules/zipimport.c
+++ b/Modules/zipimport.c
@@ -742,7 +742,8 @@
 
     fp = _Py_fopen(archive, "rb");
     if (fp == NULL) {
-        PyErr_Format(ZipImportError, "can't open Zip file: %R", archive);
+        if (!PyErr_Occurred())
+            PyErr_Format(ZipImportError, "can't open Zip file: %R", archive);
         return NULL;
     }
     fseek(fp, -22, SEEK_END);
@@ -913,8 +914,9 @@
 
     fp = _Py_fopen(archive, "rb");
     if (!fp) {
-        PyErr_Format(PyExc_IOError,
-           "zipimport: can not open file %U", archive);
+        if (!PyErr_Occurred())
+            PyErr_Format(PyExc_IOError,
+               "zipimport: can not open file %U", archive);
         return NULL;
     }
 
diff --git a/Python/import.c b/Python/import.c
--- a/Python/import.c
+++ b/Python/import.c
@@ -3760,26 +3760,38 @@
         mode = "r" PY_STDIOTEXTMODE;
     if (fob == NULL) {
         fp = _Py_fopen(pathname, mode);
+        if (!fp) {
+            if (!PyErr_Occurred())
+                PyErr_SetFromErrno(PyExc_IOError);
+            return NULL;
+        }
+        return fp;
     }
     else {
         int fd = PyObject_AsFileDescriptor(fob);
         if (fd == -1)
             return NULL;
-        if (!_PyVerify_fd(fd))
-            goto error;
+        if (!_PyVerify_fd(fd)) {
+            PyErr_SetFromErrno(PyExc_IOError);
+            return NULL;
+        }
+
         /* the FILE struct gets a new fd, so that it can be closed
          * independently of the file descriptor given
          */
         fd = dup(fd);
-        if (fd == -1)
-            goto error;
+        if (fd == -1) {
+            PyErr_SetFromErrno(PyExc_IOError);
+            return NULL;
+        }
+
         fp = fdopen(fd, mode);
+        if (!fp) {
+            PyErr_SetFromErrno(PyExc_IOError);
+            return NULL;
+        }
+        return fp;
     }
-    if (fp)
-        return fp;
-error:
-    PyErr_SetFromErrno(PyExc_IOError);
-    return NULL;
 }
 
 static PyObject *

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list