Added a fallback for itertools.izip_longest to pycompat for python versions < 2.6

This commit is contained in:
Daniel Neuhäuser 2010-08-21 21:08:15 +02:00
parent 043579e468
commit bbd39e5533
2 changed files with 22 additions and 5 deletions

View File

@ -33,6 +33,27 @@ except ImportError: # python < 2.6
for prod in result:
yield tuple(prod)
try:
from itertools import izip_longest as zip_longest
except ImportError: # python > 2.6/2.7 or python < 2.6
try:
from itertools import zip_longest
except ImportError: # python < 2.6
# this code has been taken from the python documentation
from itertools import repeat, chain, izip
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 izip(*iters):
yield tup
except IndexError:
pass
# the ubiquitous "bytes" helper function
if sys.version_info >= (3, 0):

View File

@ -12,12 +12,8 @@
from uuid import uuid4
from operator import itemgetter
from collections import defaultdict
try:
from itertools import izip_longest as zip_longest
except ImportError:
from itertools import zip_longest
from sphinx.util.pycompat import product
from sphinx.util.pycompat import product, zip_longest
# anything below that ratio is considered equal/changed