Ejemplo n.º 1
0
 def test_deferred_close_by_archive(self):
     """ Test archive deferred close without a stream. """
     z = ZipFile(self.f, 'r')
     self.assertIsNotNone(z._a)
     self.assertIsNone(z._stream)
     z.close()
     self.assertIsNone(z._a)
Ejemplo n.º 2
0
 def test_deferred_close_by_archive(self):
     """ Test archive deferred close without a stream. """
     f = file(ZIPPATH, mode='r')
     z = ZipFile(f, 'r')
     self.assertIsNotNone(z._a)
     self.assertIsNone(z._stream)
     z.close()
     self.assertIsNone(z._a)
Ejemplo n.º 3
0
 def test_deferred_close_by_stream(self):
     """ Ensure archive closes self if stream is closed first. """
     z = ZipFile(self.f, 'r')
     stream = z.readstream(FILENAMES[0])
     stream.close()
     # Make sure archive stays open after stream is closed.
     self.assertIsNotNone(z._a)
     self.assertIsNone(z._stream)
     z.close()
     self.assertIsNone(z._a)
     self.assertTrue(stream.closed)
Ejemplo n.º 4
0
 def test_deferred_close_by_stream(self):
     """ Ensure archive closes self if stream is closed first. """
     f = file(ZIPPATH, mode='r')
     z = ZipFile(f, 'r')
     stream = z.readstream(FILENAMES[0])
     stream.close()
     # Make sure archive stays open after stream is closed.
     self.assertIsNotNone(z._a)
     self.assertIsNone(z._stream)
     z.close()
     self.assertIsNone(z._a)
     self.assertTrue(stream.closed)
Ejemplo n.º 5
0
 def test_close_stream_first(self):
     """ Ensure that archive stays open after being closed if a stream is
     open. Further, ensure closing the stream closes the archive. """
     z = ZipFile(self.f, 'r')
     stream = z.readstream(FILENAMES[0])
     z.close()
     try:
         stream.read()
     except:
         self.fail("Reading stream from closed archive failed!")
     stream.close()
     # Now the archive should close.
     self.assertIsNone(z._a)
     self.assertTrue(stream.closed)
     self.assertIsNone(z._stream)
Ejemplo n.º 6
0
 def test_writestream_unbuffered(self):
     f = file(ZIPPATH, mode='w')
     z = ZipFile(f, 'w')
     for fname in FILENAMES:
         full_path = os.path.join(TMPDIR, fname)
         i = file(full_path)
         o = z.writestream(fname, os.path.getsize(full_path))
         while True:
             data = i.read(1)
             if not data:
                 break
             o.write(data)
         o.close()
         i.close()
     z.close()
Ejemplo n.º 7
0
 def test_writestream_unbuffered(self):
     f = file(ZIPPATH, mode='w')
     z = ZipFile(f, 'w')
     for fname in FILENAMES:
         full_path = os.path.join(TMPDIR, fname)
         i = file(full_path)
         o = z.writestream(fname, os.path.getsize(full_path))
         while True:
             data = i.read(1)
             if not data:
                 break
             o.write(data)
         o.close()
         i.close()
     z.close()
Ejemplo n.º 8
0
 def test_close_stream_first(self):
     """ Ensure that archive stays open after being closed if a stream is
     open. Further, ensure closing the stream closes the archive. """
     f = file(ZIPPATH, mode='r')
     z = ZipFile(f, 'r')
     stream = z.readstream(FILENAMES[0])
     z.close()
     try:
         stream.read()
     except:
         self.fail("Reading stream from closed archive failed!")
     stream.close()
     # Now the archive should close.
     self.assertIsNone(z._a)
     self.assertTrue(stream.closed)
     self.assertIsNone(z._stream)
Ejemplo n.º 9
0
 def test_filenames(self):
     f = file(ZIPPATH, mode='r')
     z = ZipFile(f, 'r')
     names = []
     for e in z:
         names.append(e.filename)
     self.assertEqual(names, FILENAMES, 'File names differ in archive.')
Ejemplo n.º 10
0
 def test_writestream_unbuffered(self):
     z = ZipFile(self.f, 'w')
     for fname in FILENAMES:
         full_path = os.path.join(TMPDIR, fname)
         i = open(full_path)
         o = z.writestream(fname, os.path.getsize(full_path))
         while True:
             data = i.read(1)
             if not data:
                 break
             if PY3:
                 o.write(data)
             else:
                 o.write(unicode(data))
         o.close()
         i.close()
     z.close()
Ejemplo n.º 11
0
 def test_iterate(self):
     f = file(ZIPPATH, mode='r')
     z = ZipFile(f, 'r')
     count = 0
     for e in z:
         count += 1
     self.assertEqual(
         count, len(FILENAMES),
         'Did not enumerate correct number of items in archive.')
Ejemplo n.º 12
0
 def test_filenames(self):
     z = ZipFile(self.f, 'r')
     names = []
     for e in z:
         if PY3:
             names.append(e.filename)
         else:
             names.append(e.filename[0])
     self.assertEqual(names, FILENAMES, 'File names differ in archive.')
Ejemplo n.º 13
0
 def test_deferred_close_by_archive(self):
     """ Test archive deferred close without a stream. """
     f = file(ZIPPATH, mode='w')
     z = ZipFile(f, 'w')
     o = z.writestream(FILENAMES[0])
     z.close()
     self.assertIsNotNone(z._a)
     self.assertIsNotNone(z._stream)
     o.write('testdata')
     o.close()
     self.assertIsNone(z._a)
     self.assertIsNone(z._stream)
     z.close()
Ejemplo n.º 14
0
 def test_deferred_close_by_archive(self):
     """ Test archive deferred close without a stream. """
     z = ZipFile(self.f, 'w')
     o = z.writestream(FILENAMES[0])
     z.close()
     self.assertIsNotNone(z._a)
     self.assertIsNotNone(z._stream)
     if PY3:
         o.write('testdata')
     else:
         o.write(unicode('testdata'))
     o.close()
     self.assertIsNone(z._a)
     self.assertIsNone(z._stream)
     z.close()
Ejemplo n.º 15
0
 def test_deferred_close_by_archive(self):
     """ Test archive deferred close without a stream. """
     f = file(ZIPPATH, mode='w')
     z = ZipFile(f, 'w')
     o = z.writestream(FILENAMES[0])
     z.close()
     self.assertIsNotNone(z._a)
     self.assertIsNotNone(z._stream)
     o.write('testdata')
     o.close()
     self.assertIsNone(z._a)
     self.assertIsNone(z._stream)
     z.close()
Ejemplo n.º 16
0
 def test_writepath(self):
     f = file(ZIPPATH, mode='w')
     z = ZipFile(f, 'w')
     for fname in FILENAMES:
         z.writepath(file(os.path.join(TMPDIR, fname), 'r'))
     z.close()
Ejemplo n.º 17
0
    def test_writepath_directory(self):
        """ Test writing a directory. """
        z = ZipFile(self.f, 'w')
        z.writepath(None, pathname='/testdir', folder=True)
        z.writepath(None, pathname='/testdir/testinside', folder=True)
        z.close()
        self.f.close()

        f = open(ZIPPATH, mode='r')
        z = ZipFile(f, 'r')

        entries = z.infolist()

        assert len(entries) == 2
        assert entries[0].isdir()
        z.close()
        f.close()
Ejemplo n.º 18
0
 def test_writepath(self):
     z = ZipFile(self.f, 'w')
     for fname in FILENAMES:
         with open(os.path.join(TMPDIR, fname), 'r') as f:
             z.writepath(f)
     z.close()
Ejemplo n.º 19
0
 def test_writepath(self):
     f = file(ZIPPATH, mode='w')
     z = ZipFile(f, 'w')
     for fname in FILENAMES:
         z.writepath(file(os.path.join(TMPDIR, fname), 'r'))
     z.close()
Ejemplo n.º 20
0
    def test_writepath_directory(self):
        """ Test writing a directory. """

        f = file(ZIPPATH, mode='w')
        z = ZipFile(f, 'w')
        z.writepath(None, pathname='/testdir', folder=True)
        z.writepath(None, pathname='/testdir/testinside', folder=True)
        z.close()
        f.close()

        f = file(ZIPPATH, mode='r')
        z = ZipFile(f, 'r')

        entries = z.infolist()

        assert len(entries) == 2
        assert entries[0].isdir()
        z.close()
        f.close()