summaryrefslogtreecommitdiffstats
path: root/Lib/zipfile.py
diff options
context:
space:
mode:
authorGeorg Brandl <georg@python.org>2007-07-12 09:59:22 (GMT)
committerGeorg Brandl <georg@python.org>2007-07-12 09:59:22 (GMT)
commit4b3ab6fcc09d93cfe47fd0087fa694f1166a7b0b (patch)
treedfcfd35094d64d31ad1536f15831129be891cdf0 /Lib/zipfile.py
parent9467bc5ad1e886ecd0be84eddef35153ead936b1 (diff)
downloadcpython-4b3ab6fcc09d93cfe47fd0087fa694f1166a7b0b.zip
cpython-4b3ab6fcc09d93cfe47fd0087fa694f1166a7b0b.tar.gz
cpython-4b3ab6fcc09d93cfe47fd0087fa694f1166a7b0b.tar.bz2
Patch #1675424: Added tests for uncovered code in the zipfile module.
The KeyError raised by Zipfile.getinfo for nonexistent names now has a descriptive message.
Diffstat (limited to 'Lib/zipfile.py')
-rw-r--r--Lib/zipfile.py24
1 files changed, 21 insertions, 3 deletions
diff --git a/Lib/zipfile.py b/Lib/zipfile.py
index 67d2c5d..a53f8ed 100644
--- a/Lib/zipfile.py
+++ b/Lib/zipfile.py
@@ -568,8 +568,9 @@ class ZipFile:
def __init__(self, file, mode="r", compression=ZIP_STORED, allowZip64=False):
"""Open the ZIP file with mode read "r", write "w" or append "a"."""
- self._allowZip64 = allowZip64
- self._didModify = False
+ if mode not in ("r", "w", "a"):
+ raise RuntimeError('ZipFile() requires mode "r", "w", or "a"')
+
if compression == ZIP_STORED:
pass
elif compression == ZIP_DEFLATED:
@@ -578,6 +579,9 @@ class ZipFile:
"Compression requires the (missing) zlib module"
else:
raise RuntimeError, "That compression method is not supported"
+
+ self._allowZip64 = allowZip64
+ self._didModify = False
self.debug = 0 # Level of printing: 0 through 3
self.NameToInfo = {} # Find file info given name
self.filelist = [] # List of ZipInfo instances for archive
@@ -720,7 +724,12 @@ class ZipFile:
def getinfo(self, name):
"""Return the instance of ZipInfo given 'name'."""
- return self.NameToInfo[name]
+ info = self.NameToInfo.get(name)
+ if info is None:
+ raise KeyError(
+ 'There is no item named %r in the archive' % name)
+
+ return info
def setpassword(self, pwd):
"""Set default password for encrypted files."""
@@ -824,6 +833,10 @@ class ZipFile:
def write(self, filename, arcname=None, compress_type=None):
"""Put the bytes from filename into the archive under the name
arcname."""
+ if not self.fp:
+ raise RuntimeError(
+ "Attempt to write to ZIP archive that was already closed")
+
st = os.stat(filename)
mtime = time.localtime(st.st_mtime)
date_time = mtime[0:6]
@@ -896,6 +909,11 @@ class ZipFile:
zinfo.compress_type = self.compression
else:
zinfo = zinfo_or_arcname
+
+ if not self.fp:
+ raise RuntimeError(
+ "Attempt to write to ZIP archive that was already closed")
+
zinfo.file_size = len(bytes) # Uncompressed size
zinfo.header_offset = self.fp.tell() # Start of header bytes
self._writecheck(zinfo)