From: Fredrik Tolf Date: Sat, 2 Feb 2013 05:29:04 +0000 (+0100) Subject: Merge branch 'master' into python3 X-Git-Url: http://git.dolda2000.com/gitweb/?a=commitdiff_plain;h=a7a0908019bb95bb2be1c01e97e88f344807ccec;p=wrw.git Merge branch 'master' into python3 Conflicts: wrw/dispatch.py --- a7a0908019bb95bb2be1c01e97e88f344807ccec diff --cc wrw/dispatch.py index 10cc2f6,4d22b72..768d3f4 --- a/wrw/dispatch.py +++ b/wrw/dispatch.py @@@ -16,34 -16,8 +16,8 @@@ def mangle(result) return result return [str(result)] - class iterproxy(object): - # Makes sure iter(real).next() is called immediately, in order to - # let generator code run. - def __init__(self, real): - self.bk = real - self.bki = iter(real) - self._next = [None] - self.__next__() - - def __iter__(self): - return self - - def __next__(self): - if self._next is None: - raise StopIteration() - ret = self._next[0] - try: - self._next[:] = [self.bki.__next__()] - except StopIteration: - self._next = None - return ret - - def close(self): - if hasattr(self.bk, "close"): - self.bk.close() - def defaulterror(req, excinfo): - import resp + from . import resp traceback.print_exception(*excinfo) raise resp.httperror(500) @@@ -60,11 -34,11 +34,11 @@@ def handle(req, startreq, handler) resp = [""] while True: try: - resp = iterproxy(handler(req)) + resp = handler(req) break - except restart, i: + except restart as i: handler = i.handle - except Exception, i: + except Exception as i: if eh is None: raise handler = wraphandler(eh, sys.exc_info()) diff --cc wrw/util.py index 5347ce4,55b4ee2..bf78f8e --- a/wrw/util.py +++ b/wrw/util.py @@@ -43,6 -34,37 +43,37 @@@ def persession(data = None) return wrapper return dec + class preiter(object): + __slots__ = ["bk", "bki", "_next"] + end = object() + def __init__(self, real): + self.bk = real + self.bki = iter(real) + self._next = None - self.next() ++ self.__next__() + + def __iter__(self): + return self + - def next(self): ++ def __next__(self): + if self._next is self.end: + raise StopIteration() + ret = self._next + try: + self._next = next(self.bki) + except StopIteration: + self._next = self.end + return ret + + def close(self): + if hasattr(self.bk, "close"): + self.bk.close() + + def pregen(callable): + def wrapper(*args, **kwargs): + return preiter(callable(*args, **kwargs)) + return wrapper + class sessiondata(object): @classmethod def get(cls, req, create = True):