Skip to content

Commit bc38614

Browse files
Fix ZeroDivisionError when counting Pagination object pages
When ``per_page`` was set to ``0``, there was ZeroDivisionError raised in ``Pagination.pages`` property.
1 parent 1e536d9 commit bc38614

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

flask_sqlalchemy.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,11 @@ def __init__(self, query, page, per_page, total, items):
275275
@property
276276
def pages(self):
277277
"""The total number of pages"""
278-
return int(ceil(self.total / float(self.per_page)))
278+
if self.per_page == 0:
279+
pages = 0
280+
else:
281+
pages = int(ceil(self.total / float(self.per_page)))
282+
return pages
279283

280284
def prev(self, error_out=False):
281285
"""Returns a :class:`Pagination` object for the previous page."""

test_sqlalchemy.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,10 @@ def test_basic_pagination(self):
189189
self.assertEqual(list(p.iter_pages()),
190190
[1, 2, None, 8, 9, 10, 11, 12, 13, 14, None, 24, 25])
191191

192+
def test_pagination_pages_when_0_items_per_page(self):
193+
p = sqlalchemy.Pagination(None, 1, 0, 500, [])
194+
self.assertEqual(p.pages, 0)
195+
192196

193197
class BindsTestCase(unittest.TestCase):
194198

0 commit comments

Comments
 (0)