Skip to content

Commit

Permalink
improves handling of incorrect types passed to Document.__get_item().
Browse files Browse the repository at this point in the history
(FIXES: pymupdf#2961)
  • Loading branch information
bwagner committed Jan 4, 2024
1 parent b8879fe commit 9d2c486
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2546,6 +2546,7 @@ def __exit__(self, *args):
self.close()

def __getitem__(self, i: int =0):
assert isinstance(i, int) or (isinstance(i, tuple) and len(i) == 2 and all(isinstance(x, int) for x in i))
if i not in self:
raise IndexError(f"page {i} not in document")
return self.load_page(i)
Expand Down
1 change: 1 addition & 0 deletions src_classic/fitz_old.i
Original file line number Diff line number Diff line change
Expand Up @@ -4800,6 +4800,7 @@ if basestate:


def __getitem__(self, i: int =0)->"Page":
assert isinstance(i, int) or (isinstance(i, tuple) and len(i) == 2 and all(isinstance(x, int) for x in i))
if i not in self:
raise IndexError("page not in document")
return self.load_page(i)
Expand Down
8 changes: 8 additions & 0 deletions tests/test_pixmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import platform
import sys
import tempfile
import pytest

scriptdir = os.path.abspath(os.path.dirname(__file__))
epub = os.path.join(scriptdir, "resources", "Bezier.epub")
Expand Down Expand Up @@ -132,3 +133,10 @@ def test_2369():
img_bytes = img["image"]
fitz.Pixmap(img_bytes)

def test_page_idx_int():
doc = fitz.open(pdf)
with pytest.raises(AssertionError):
doc["0"]
assert doc[0]
assert doc[(0,0)]

0 comments on commit 9d2c486

Please sign in to comment.