]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
(Merge 3.2) Handle correctly _Py_fopen() error: don't replace the exception
authorVictor Stinner <victor.stinner@haypocalc.com>
Sun, 18 Dec 2011 20:05:22 +0000 (21:05 +0100)
committerVictor Stinner <victor.stinner@haypocalc.com>
Sun, 18 Dec 2011 20:05:22 +0000 (21:05 +0100)
1  2 
Modules/zipimport.c
Python/import.c

index ed835c9cd8e5f345ab14e1a643ac080fdfed2634,11c390452538b8f53cfac15e7a8bf3416f6de695..09bd83aed780e983636b178d272402629769cc78
@@@ -740,9 -727,17 +740,10 @@@ read_directory(PyObject *archive
      const char *charset;
      int bootstrap;
  
 -    if (PyUnicode_GET_SIZE(archive_obj) > MAXPATHLEN) {
 -        PyErr_SetString(PyExc_OverflowError,
 -                        "Zip path name is too long");
 -        return NULL;
 -    }
 -    Py_UNICODE_strcpy(path, PyUnicode_AS_UNICODE(archive_obj));
 -
 -    fp = _Py_fopen(archive_obj, "rb");
 +    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: '%U'", archive_obj);
++            PyErr_Format(ZipImportError, "can't open Zip file: %R", archive);
          return NULL;
      }
      fseek(fp, -22, SEEK_END);
diff --cc Python/import.c
index 34d1a26dc785b4a14f1fa6ed29b1f7a7f569ea70,e721498aaa38687730499be755ce9fafc2a6726e..c7541316b53e3baff99be21abbfdad0651a430b9
@@@ -3759,27 -3318,27 +3759,39 @@@ get_file(PyObject *pathname, PyObject *
      if (mode[0] == 'U')
          mode = "r" PY_STDIOTEXTMODE;
      if (fob == NULL) {
 -        fp = fopen(pathname, mode);
 +        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)
++        if (!fp) {
++            PyErr_SetFromErrno(PyExc_IOError);
++            return NULL;
++        }
          return fp;
--error:
--    PyErr_SetFromErrno(PyExc_IOError);
--    return NULL;
++    }
  }
  
  static PyObject *