From b91a2a499fd562011fd275238924baa6002fb1f8 Mon Sep 17 00:00:00 2001 From: Rigel Di Scala Date: Sat, 6 Jun 2015 20:24:02 +0100 Subject: [PATCH] Fixed #23190 -- Made Paginator.page_range an iterator --- AUTHORS | 1 + django/core/paginator.py | 2 +- docs/releases/1.9.txt | 13 +++++++++++++ docs/topics/pagination.txt | 10 ++++++++-- tests/pagination/tests.py | 6 ++++++ 5 files changed, 29 insertions(+), 3 deletions(-) diff --git a/AUTHORS b/AUTHORS index 0d0f454e25..05f568c1ba 100644 --- a/AUTHORS +++ b/AUTHORS @@ -600,6 +600,7 @@ answer newbie questions, and generally made Django that much better: Richard Davies Richard House Rick Wagner + Rigel Di Scala Robert Coup Robert Myers Roberto Aguilar diff --git a/django/core/paginator.py b/django/core/paginator.py index c72a6b0273..75292236fa 100644 --- a/django/core/paginator.py +++ b/django/core/paginator.py @@ -96,7 +96,7 @@ class Paginator(object): Returns a 1-based range of pages for iterating through within a template for loop. """ - return list(six.moves.range(1, self.num_pages + 1)) + return six.moves.range(1, self.num_pages + 1) page_range = property(_get_page_range) diff --git a/docs/releases/1.9.txt b/docs/releases/1.9.txt index 0b84b3c6d3..75c126c38b 100644 --- a/docs/releases/1.9.txt +++ b/docs/releases/1.9.txt @@ -770,6 +770,19 @@ To fix your ``simple_tag``\s, it is best to apply the following practices: Tags that follow these rules will be correct and safe whether they are run on Django 1.9+ or earlier. +``Paginator.page_range`` +~~~~~~~~~~~~~~~~~~~~~~~~ + +:attr:`Paginator.page_range ` is +now an iterator instead of a list. + +In versions of Django previous to 1.8, ``Paginator.page_range`` returned a +``list`` in Python 2 and a ``range`` in Python 3. Django 1.8 consistently +returned a list, but an iterator is more efficient. + +Existing code that depends on ``list`` specific features, such as indexing, +can be ported by converting the iterator into a ``list`` using ``list()``. + Miscellaneous ~~~~~~~~~~~~~ diff --git a/docs/topics/pagination.txt b/docs/topics/pagination.txt index 6ab12b2d1b..ee801ed620 100644 --- a/docs/topics/pagination.txt +++ b/docs/topics/pagination.txt @@ -24,8 +24,10 @@ page:: 4 >>> p.num_pages 2 + >>> type(p.page_range) # `` in Python 2. + >>> p.page_range - [1, 2] + range(1, 3) >>> page1 = p.page(1) >>> page1 @@ -191,8 +193,12 @@ Attributes .. attribute:: Paginator.page_range - A 1-based range of page numbers, e.g., ``[1, 2, 3, 4]``. + A 1-based range iterator of page numbers, e.g. yielding ``[1, 2, 3, 4]``. + .. versionchanged:: 1.9 + + In older versions, ``page_range`` returned a list instead of an + iterator. ``InvalidPage`` exceptions ========================== diff --git a/tests/pagination/tests.py b/tests/pagination/tests.py index 0757859628..31658810a2 100644 --- a/tests/pagination/tests.py +++ b/tests/pagination/tests.py @@ -233,6 +233,12 @@ class PaginationTests(unittest.TestCase): self.assertEqual(page2.previous_page_number(), 1) self.assertIsNone(page2.next_page_number()) + def test_page_range_iterator(self): + """ + Paginator.page_range should be an iterator. + """ + self.assertIsInstance(Paginator([1, 2, 3], 2).page_range, type(six.moves.range(0))) + class ModelPaginationTests(TestCase): """