Browse Source

Fixed #23190 -- Made Paginator.page_range an iterator

Rigel Di Scala 9 years ago
parent
commit
b91a2a499f
5 changed files with 29 additions and 3 deletions
  1. 1 0
      AUTHORS
  2. 1 1
      django/core/paginator.py
  3. 13 0
      docs/releases/1.9.txt
  4. 8 2
      docs/topics/pagination.txt
  5. 6 0
      tests/pagination/tests.py

+ 1 - 0
AUTHORS

@@ -600,6 +600,7 @@ answer newbie questions, and generally made Django that much better:
     Richard Davies <richard.davies@elastichosts.com>
     Richard House <Richard.House@i-logue.com>
     Rick Wagner <rwagner@physics.ucsd.edu>
+    Rigel Di Scala <rigel.discala@propylon.com>
     Robert Coup
     Robert Myers <myer0052@gmail.com>
     Roberto Aguilar <roberto@baremetal.io>

+ 1 - 1
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)
 
 

+ 13 - 0
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 <django.core.paginator.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
 ~~~~~~~~~~~~~
 

+ 8 - 2
docs/topics/pagination.txt

@@ -24,8 +24,10 @@ page::
     4
     >>> p.num_pages
     2
+    >>> type(p.page_range)  # `<type 'rangeiterator'>` in Python 2.
+    <class 'range_iterator'>
     >>> 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
 ==========================

+ 6 - 0
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):
     """