Skip to content

Commit 43e9803

Browse files
Zingzyebotiab
authored andcommitted
Add warning for out-of-bounds page range in pdfly cat command (py-pdf#58)
1 parent 8eb34ac commit 43e9803

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

pdfly/cat.py

+14-1
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,20 @@ def main(
7878
in_fs[filename] = open(filename, "rb")
7979

8080
reader = PdfReader(in_fs[filename])
81+
82+
num_pages = len(reader.pages)
83+
start, end, step = page_range.indices(num_pages)
84+
if (
85+
start < 0
86+
or end < 0
87+
or start >= num_pages
88+
or end > num_pages
89+
or start > end
90+
):
91+
print(
92+
f"WARNING: Page range {page_range} is out of bounds",
93+
file=sys.stderr,
94+
)
8195
if not use_complements:
8296
for page_num in range(*page_range.indices(len(reader.pages))):
8397
writer.add_page(reader.pages[page_num])
@@ -87,7 +101,6 @@ def main(
87101
compl_page_nums = all_page_nums - page_nums
88102
for page_num in compl_page_nums:
89103
writer.add_page(reader.pages[page_num])
90-
91104
writer.write(output_fh)
92105
except Exception:
93106
print(traceback.format_exc(), file=sys.stderr)

tests/test_cat.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ def test_cat_subset_invalid_args(capsys, tmp_path, page_range):
7171
assert "Invalid file path or page range provided" in captured.err
7272

7373

74-
@pytest.mark.skip(reason="This check is not implemented yet")
7574
def test_cat_subset_warn_on_missing_pages(capsys, tmp_path):
7675
with chdir(tmp_path):
7776
exit_code = run_cli(
@@ -85,7 +84,7 @@ def test_cat_subset_warn_on_missing_pages(capsys, tmp_path):
8584
)
8685
captured = capsys.readouterr()
8786
assert exit_code == 0, captured
88-
assert "WARN" in captured.out
87+
assert "WARN" in captured.err
8988

9089

9190
@pytest.mark.xfail() # There is currently a bug there

0 commit comments

Comments
 (0)