From: Matt Haggard Date: Tue, 29 Dec 2015 23:12:39 +0000 (-0700) Subject: Give Cycler a next() method so that it works in python2 and 3 X-Git-Tag: 2.9~35^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=77781b93a54e6951b9566f8fca655c40542a3de9;p=thirdparty%2Fjinja.git Give Cycler a next() method so that it works in python2 and 3 --- diff --git a/jinja2/utils.py b/jinja2/utils.py index 612d5c3d..b5ea5a34 100644 --- a/jinja2/utils.py +++ b/jinja2/utils.py @@ -506,12 +506,14 @@ class Cycler(object): """Returns the current item.""" return self.items[self.pos] - def __next__(self): + def next(self): """Goes one item ahead and returns it.""" rv = self.current self.pos = (self.pos + 1) % len(self.items) return rv + __next__ = next + class Joiner(object): """A joining helper for templates.""" diff --git a/tests/test_api.py b/tests/test_api.py index 99d8dc1e..d44b6e3a 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -59,6 +59,17 @@ class TestExtendedAPI(): c.reset() assert c.current == 1 + def test_cycler_nextmethod(self, env): + items = 1, 2, 3 + c = Cycler(*items) + for item in items + items: + assert c.current == item + assert c.next() == item + c.next() + assert c.current == 2 + c.reset() + assert c.current == 1 + def test_expressions(self, env): expr = env.compile_expression("foo") assert expr() is None