changeset 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 f17f19d9eb0a
children 3000bb94addb
files 2.00/compat.py 2.00/files.py
diffstat 2 files changed, 24 insertions(+), 23 deletions(-) [+]
line wrap: on
line diff
--- a/2.00/compat.py	Wed Nov 24 23:42:06 2010 +0000
+++ b/2.00/compat.py	Thu Nov 25 00:03:29 2010 +0000
@@ -121,23 +121,24 @@
 	# Python 3
 	from itertools import zip_longest
 except ImportError:
-	# Python 2.6/2.7
-	from itertools import izip_longest as zip_longest
-except ImportError:
-	# Python 2.5
-	from itertools import chain, repeat
-	# Adapted from the documentation of itertools.izip_longest
-	def zip_longest(*args, **kwargs):
-		fillvalue = kwargs.get('fillvalue')
-		def sentinel(counter=([fillvalue]*(len(args)-1)).pop):
-			yield counter()
-		fillers = repeat(fillvalue)
-		iters = [chain(it, sentinel(), fillers) for it in args]
-		try:
-			for tup in zip(*iters):
-				yield tup
-		except IndexError:
-			pass
+	try:
+		# Python 2.6/2.7
+		from itertools import izip_longest as zip_longest
+	except ImportError:
+		# Python 2.5
+		from itertools import chain, repeat
+		# Adapted from the documentation of itertools.izip_longest
+		def zip_longest(*args, **kwargs):
+			fillvalue = kwargs.get('fillvalue')
+			def sentinel(counter=([fillvalue]*(len(args)-1)).pop):
+				yield counter()
+			fillers = repeat(fillvalue)
+			iters = [chain(it, sentinel(), fillers) for it in args]
+			try:
+				for tup in zip(*iters):
+					yield tup
+			except IndexError:
+				pass
 
 # Automatically import * from this module into testconf.py's
 class CompatBuiltins(object):
--- a/2.00/files.py	Wed Nov 24 23:42:06 2010 +0000
+++ b/2.00/files.py	Thu Nov 25 00:03:29 2010 +0000
@@ -55,7 +55,9 @@
 
 try:
 	import tarfile
-	
+except ImportError:
+	TarArchive = None
+else:
 	class TarArchive(Archive):
 		__slots__ = '__namelist'
 		
@@ -99,12 +101,12 @@
 				self.file.closed = True
 	
 	formats['tar'] = formats['tgz'] = formats['tar.gz'] = formats['tbz2'] = formats['tar.bz2'] = TarArchive
-except ImportError:
-	TarArchive = None
 
 try:
 	import zipfile
-	
+except ImportError:
+	ZipArchive = None
+else:
 	class ZipArchive(Archive):
 		__slots__ = '__namelist'
 		
@@ -150,8 +152,6 @@
 				return self.file.close()
 	
 	formats['zip'] = ZipArchive
-except ImportError:
-	ZipArchive = None
 
 # Remove unsupported archive formats and replace full stops
 # with the platform-dependent file name extension separator