comparison 2.00/files.py @ 31:fe1463e7e24d

Clean up try-except clauses More try-except-else goodness (with the stress on else). Bug fix: compat.py now creates zip_longest in Python 2.5 instead of raising an exception.
author Oleg Oshmyan <chortos@inbox.lv>
date Thu, 25 Nov 2010 00:03:29 +0000
parents c1f52b5d80d6
children 8fec38b0dd6e
comparison
equal deleted inserted replaced
30:f17f19d9eb0a 31:fe1463e7e24d
53 def __del__(self): 53 def __del__(self):
54 del self.file 54 del self.file
55 55
56 try: 56 try:
57 import tarfile 57 import tarfile
58 58 except ImportError:
59 TarArchive = None
60 else:
59 class TarArchive(Archive): 61 class TarArchive(Archive):
60 __slots__ = '__namelist' 62 __slots__ = '__namelist'
61 63
62 def __init__(self, path): 64 def __init__(self, path):
63 self.file = tarfile.open(path) 65 self.file = tarfile.open(path)
97 if not self.file._extfileobj: 99 if not self.file._extfileobj:
98 self.file.fileobj.close() 100 self.file.fileobj.close()
99 self.file.closed = True 101 self.file.closed = True
100 102
101 formats['tar'] = formats['tgz'] = formats['tar.gz'] = formats['tbz2'] = formats['tar.bz2'] = TarArchive 103 formats['tar'] = formats['tgz'] = formats['tar.gz'] = formats['tbz2'] = formats['tar.bz2'] = TarArchive
102 except ImportError:
103 TarArchive = None
104 104
105 try: 105 try:
106 import zipfile 106 import zipfile
107 107 except ImportError:
108 ZipArchive = None
109 else:
108 class ZipArchive(Archive): 110 class ZipArchive(Archive):
109 __slots__ = '__namelist' 111 __slots__ = '__namelist'
110 112
111 def __init__(self, path): 113 def __init__(self, path):
112 self.file = zipfile.ZipFile(path) 114 self.file = zipfile.ZipFile(path)
148 return self.file.__exit__(exc_type, exc_value, traceback) 150 return self.file.__exit__(exc_type, exc_value, traceback)
149 else: 151 else:
150 return self.file.close() 152 return self.file.close()
151 153
152 formats['zip'] = ZipArchive 154 formats['zip'] = ZipArchive
153 except ImportError:
154 ZipArchive = None
155 155
156 # Remove unsupported archive formats and replace full stops 156 # Remove unsupported archive formats and replace full stops
157 # with the platform-dependent file name extension separator 157 # with the platform-dependent file name extension separator
158 def issupported(filename, formats=formats): 158 def issupported(filename, formats=formats):
159 ext = filename.partition('.')[2] 159 ext = filename.partition('.')[2]