Skip to content

Commit 9d2c486

Browse files
committed
improves handling of incorrect types passed to Document.__get_item().
(FIXES: #2961)
1 parent b8879fe commit 9d2c486

File tree

3 files changed

+10
-0
lines changed

3 files changed

+10
-0
lines changed

src/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -2546,6 +2546,7 @@ def __exit__(self, *args):
25462546
self.close()
25472547

25482548
def __getitem__(self, i: int =0):
2549+
assert isinstance(i, int) or (isinstance(i, tuple) and len(i) == 2 and all(isinstance(x, int) for x in i))
25492550
if i not in self:
25502551
raise IndexError(f"page {i} not in document")
25512552
return self.load_page(i)

src_classic/fitz_old.i

+1
Original file line numberDiff line numberDiff line change
@@ -4800,6 +4800,7 @@ if basestate:
48004800

48014801

48024802
def __getitem__(self, i: int =0)->"Page":
4803+
assert isinstance(i, int) or (isinstance(i, tuple) and len(i) == 2 and all(isinstance(x, int) for x in i))
48034804
if i not in self:
48044805
raise IndexError("page not in document")
48054806
return self.load_page(i)

tests/test_pixmap.py

+8
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import platform
1212
import sys
1313
import tempfile
14+
import pytest
1415

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

136+
def test_page_idx_int():
137+
doc = fitz.open(pdf)
138+
with pytest.raises(AssertionError):
139+
doc["0"]
140+
assert doc[0]
141+
assert doc[(0,0)]
142+

0 commit comments

Comments
 (0)