]> granicus.if.org Git - python/commitdiff
merge backout for #20621
authorBenjamin Peterson <benjamin@python.org>
Sun, 16 Feb 2014 19:17:28 +0000 (14:17 -0500)
committerBenjamin Peterson <benjamin@python.org>
Sun, 16 Feb 2014 19:17:28 +0000 (14:17 -0500)
1  2 
Lib/test/test_zipimport.py
Modules/zipimport.c

index bf058bcc5668f1afe460a499028b182eeaf29492,37603b9bcde3dcb50ef48e09b3b7318d5eef1978..1e351c8c8a16e30cfdc5e0ebb18e33f766b234ea
@@@ -54,44 -46,6 +54,23 @@@ pyc_file = importlib.util.cache_from_so
  pyc_ext = ('.pyc' if __debug__ else '.pyo')
  
  
- def _write_zip_package(zipname, files,
-                        data_to_prepend=b"", compression=ZIP_STORED):
-     z = ZipFile(zipname, "w")
-     try:
-         for name, (mtime, data) in files.items():
-             zinfo = ZipInfo(name, time.localtime(mtime))
-             zinfo.compress_type = compression
-             z.writestr(zinfo, data)
-     finally:
-         z.close()
-     if data_to_prepend:
-         # Prepend data to the start of the zipfile
-         with open(zipname, "rb") as f:
-             zip_data = f.read()
-         with open(zipname, "wb") as f:
-             f.write(data_to_prepend)
-             f.write(zip_data)
 +class ImportHooksBaseTestCase(unittest.TestCase):
 +
 +    def setUp(self):
 +        self.path = sys.path[:]
 +        self.meta_path = sys.meta_path[:]
 +        self.path_hooks = sys.path_hooks[:]
 +        sys.path_importer_cache.clear()
 +        self.modules_before = support.modules_setup()
 +
 +    def tearDown(self):
 +        sys.path[:] = self.path
 +        sys.meta_path[:] = self.meta_path
 +        sys.path_hooks[:] = self.path_hooks
 +        sys.path_importer_cache.clear()
 +        support.modules_cleanup(*self.modules_before)
 +
 +
  class UncompressedZipImportTestCase(ImportHooksBaseTestCase):
  
      compression = ZIP_STORED
index 9f662f528589e1a70b46612615962b7a80f722ee,2feb2a827c8b67dfd947d93153e289c869211bcc..8fe919539fdfffd7b1e13be5002c443cfdac4f8e
@@@ -594,8 -554,10 +560,7 @@@ zipimporter_get_data(PyObject *obj, PyO
  {
      ZipImporter *self = (ZipImporter *)obj;
      PyObject *path, *key;
-     FILE *fp;
-     PyObject *toc_entry, *data;
 -#ifdef ALTSEP
 -    _Py_IDENTIFIER(replace);
 -#endif
+     PyObject *toc_entry;
      Py_ssize_t path_start, path_len, len;
  
      if (!PyArg_ParseTuple(args, "U:zipimporter.get_data", &path))
@@@ -1046,8 -869,15 +873,15 @@@ read_directory(PyObject *archive
      const char *charset;
      int bootstrap;
  
-     assert(fp != NULL);
 -    fp = _Py_fopen(archive, "rb");
++    fp = _Py_fopen_obj(archive, "rb");
+     if (fp == NULL) {
+         if (!PyErr_Occurred())
+             PyErr_Format(ZipImportError, "can't open Zip file: %R", archive);
+         return NULL;
+     }
      if (fseek(fp, -22, SEEK_END) == -1) {
+         fclose(fp);
          PyErr_Format(ZipImportError, "can't read Zip file: %R", archive);
          return NULL;
      }
          PySys_FormatStderr("# zipimport: found %ld names in %R\n",
                             count, archive);
      return files;
 -fseek_error:
 +file_error:
+     fclose(fp);
      Py_XDECREF(files);
      Py_XDECREF(nameobj);
      PyErr_Format(ZipImportError, "can't read Zip file: %R", archive);
@@@ -1232,8 -1055,17 +1072,17 @@@ get_data(PyObject *archive, PyObject *t
          return NULL;
      }
  
 -    fp = _Py_fopen(archive, "rb");
++    fp = _Py_fopen_obj(archive, "rb");
+     if (!fp) {
+         if (!PyErr_Occurred())
+             PyErr_Format(PyExc_IOError,
+                "zipimport: can not open file %U", archive);
+         return NULL;
+     }
      /* Check to make sure the local file header is correct */
      if (fseek(fp, file_offset, 0) == -1) {
+         fclose(fp);
          PyErr_Format(ZipImportError, "can't read Zip file: %R", archive);
          return NULL;
      }
      l = PyMarshal_ReadLongFromFile(fp);
      if (l != 0x04034B50) {
          /* Bad: Local File Header */
 -        PyErr_Format(ZipImportError,
 -                     "bad local file header in %U",
 -                     archive);
 +        if (!PyErr_Occurred())
 +            PyErr_Format(ZipImportError,
 +                         "bad local file header in %U",
 +                         archive);
+         fclose(fp);
          return NULL;
      }
      if (fseek(fp, file_offset + 26, 0) == -1) {
  
      l = 30 + PyMarshal_ReadShortFromFile(fp) +
          PyMarshal_ReadShortFromFile(fp);        /* local header size */
 +    if (PyErr_Occurred()) {
++        fclose(fp);
 +        return NULL;
 +    }
      file_offset += l;           /* Start of file data */
  
      bytes_size = compress == 0 ? data_size : data_size + 1;