Georg Brandl avatar Georg Brandl committed 57570eb

Supply substitute implementation of itertools.(i)zip_longest.

Comments (0)

Files changed (2)

sphinx/util/pycompat.py

 import sys
 import codecs
 import encodings
-import re
+
 
 try:
     from types import ClassType
     # Python 3
     class_types = (type,)
 
+
 try:
     from itertools import product
-except ImportError: # python < 2.6
-    # this code has been taken from the python documentation
+except ImportError: # Python < 2.6
+    # this code has been taken from the Python itertools documentation
     def product(*args, **kwargs):
         pools = map(tuple, args) * kwargs.get('repeat', 1)
         result = [[]]
             yield tuple(prod)
 
 
+try:
+    from itertools import izip_longest as zip_longest
+except ImportError: # Python < 2.6 or >= 3.0
+    try:
+        from itertools import zip_longest
+    except ImportError:
+        from itertools import izip, repeat, chain
+        # this code has been taken from the Python itertools documentation
+        def izip_longest(*args, **kwds):
+            # izip_longest('ABCD', 'xy', fillvalue='-') --> Ax By C- D-
+            fillvalue = kwds.get('fillvalue')
+            def sentinel(counter = ([fillvalue]*(len(args)-1)).pop):
+                yield counter()   # yields the fillvalue, or raises IndexError
+            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):
     def b(s):

sphinx/versioning.py

 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
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.