Mercurial > ~astiob > upreckon > hgweb
comparison 2.00/compat.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 | dc4be35d17e0 |
children | 8fec38b0dd6e |
comparison
equal
deleted
inserted
replaced
30:f17f19d9eb0a | 31:fe1463e7e24d |
---|---|
119 | 119 |
120 try: | 120 try: |
121 # Python 3 | 121 # Python 3 |
122 from itertools import zip_longest | 122 from itertools import zip_longest |
123 except ImportError: | 123 except ImportError: |
124 # Python 2.6/2.7 | 124 try: |
125 from itertools import izip_longest as zip_longest | 125 # Python 2.6/2.7 |
126 except ImportError: | 126 from itertools import izip_longest as zip_longest |
127 # Python 2.5 | 127 except ImportError: |
128 from itertools import chain, repeat | 128 # Python 2.5 |
129 # Adapted from the documentation of itertools.izip_longest | 129 from itertools import chain, repeat |
130 def zip_longest(*args, **kwargs): | 130 # Adapted from the documentation of itertools.izip_longest |
131 fillvalue = kwargs.get('fillvalue') | 131 def zip_longest(*args, **kwargs): |
132 def sentinel(counter=([fillvalue]*(len(args)-1)).pop): | 132 fillvalue = kwargs.get('fillvalue') |
133 yield counter() | 133 def sentinel(counter=([fillvalue]*(len(args)-1)).pop): |
134 fillers = repeat(fillvalue) | 134 yield counter() |
135 iters = [chain(it, sentinel(), fillers) for it in args] | 135 fillers = repeat(fillvalue) |
136 try: | 136 iters = [chain(it, sentinel(), fillers) for it in args] |
137 for tup in zip(*iters): | 137 try: |
138 yield tup | 138 for tup in zip(*iters): |
139 except IndexError: | 139 yield tup |
140 pass | 140 except IndexError: |
141 pass | |
141 | 142 |
142 # Automatically import * from this module into testconf.py's | 143 # Automatically import * from this module into testconf.py's |
143 class CompatBuiltins(object): | 144 class CompatBuiltins(object): |
144 __slots__ = 'originals' | 145 __slots__ = 'originals' |
145 def __init__(self): | 146 def __init__(self): |