# HG changeset patch # User Oleg Oshmyan # Date 1290643409 0 # Node ID fe1463e7e24d896a582024ed3e54173804a62151 # Parent f17f19d9eb0a28774e7ddc24d506209342a05d2e 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. diff -r f17f19d9eb0a -r fe1463e7e24d 2.00/compat.py --- 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): diff -r f17f19d9eb0a -r fe1463e7e24d 2.00/files.py --- 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