diff options
Diffstat (limited to 'wsgitools/middlewares.py')
-rw-r--r-- | wsgitools/middlewares.py | 22 |
1 files changed, 15 insertions, 7 deletions
diff --git a/wsgitools/middlewares.py b/wsgitools/middlewares.py index 297e646..38e2e67 100644 --- a/wsgitools/middlewares.py +++ b/wsgitools/middlewares.py @@ -6,9 +6,17 @@ import cgitb import binascii from wsgitools.filters import CloseableList, CloseableIterator try: - import cStringIO as StringIO + import io except ImportError: - import StringIO + try: + import cStringIO as io + except ImportError: + import StringIO as io +try: + next +except NameError: + def next(it): + return it.next() __all__.append("SubdirMiddleware") class SubdirMiddleware: @@ -66,7 +74,7 @@ class NoWriteCallableMiddleware: todo.append(None) return start_response(status, headers, exc_info) else: - sio = StringIO.StringIO() + sio = io.StringIO() todo.append((status, headers, sio)) return sio.write @@ -92,7 +100,7 @@ class NoWriteCallableMiddleware: ret = iter(ret) stopped = False try: - first = ret.next() + first = next(ret) except StopIteration: stopped = True @@ -156,7 +164,7 @@ class ContentLengthMiddleware: data = CloseableList(getattr(ret, "close", None)) length = 0 try: - data.append(ret.next()) # fills todo + data.append(next(ret)) # fills todo length += len(data[-1]) except StopIteration: stopped = True @@ -165,7 +173,7 @@ class ContentLengthMiddleware: while (not stopped) and length < self.maxstore: try: - data.append(ret.next()) + data.append(next(ret)) length += len(data[-1]) except StopIteration: stopped = True @@ -354,7 +362,7 @@ class TracebackMiddleware: # exception there. ret = iter(ret) try: - first = ret.next() + first = next(ret) except StopIteration: return CloseableList(getattr(ret, "close", None), []) return CloseableIterator(getattr(ret, "close", None), [first], ret) |