From 9d2c486462c6be520349094271be19196c3891d3 Mon Sep 17 00:00:00 2001 From: Bernhard Wagner Date: Wed, 3 Jan 2024 01:01:08 +0100 Subject: [PATCH] improves handling of incorrect types passed to Document.__get_item(). (FIXES: #2961) --- src/__init__.py | 1 + src_classic/fitz_old.i | 1 + tests/test_pixmap.py | 8 ++++++++ 3 files changed, 10 insertions(+) diff --git a/src/__init__.py b/src/__init__.py index eefe4f02a..762eb7367 100644 --- a/src/__init__.py +++ b/src/__init__.py @@ -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) diff --git a/src_classic/fitz_old.i b/src_classic/fitz_old.i index 15104bab9..a6caf965c 100644 --- a/src_classic/fitz_old.i +++ b/src_classic/fitz_old.i @@ -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) diff --git a/tests/test_pixmap.py b/tests/test_pixmap.py index f5ac30f66..168f08c20 100644 --- a/tests/test_pixmap.py +++ b/tests/test_pixmap.py @@ -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") @@ -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)] +