Skip to content

Commit 1aefb3a

Browse files
authored
Add warning for out-of-bounds page range in pdfly cat command (#58)
1 parent 9e3523e commit 1aefb3a

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

pdfly/cat.py

+14
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
In case you don't want chapter 10 before chapter 2.
3838
3939
"""
40+
4041
# Copyright (c) 2014, Steve Witham <[email protected]>.
4142
# All rights reserved. This software is available under a BSD license;
4243
# see https://github.com/py-pdf/pypdf/LICENSE
@@ -73,6 +74,19 @@ def main(
7374
in_fs[filename] = open(filename, "rb")
7475

7576
reader = PdfReader(in_fs[filename])
77+
num_pages = len(reader.pages)
78+
start, end, step = page_range.indices(num_pages)
79+
if (
80+
start < 0
81+
or end < 0
82+
or start >= num_pages
83+
or end > num_pages
84+
or start > end
85+
):
86+
print(
87+
f"WARNING: Page range {page_range} is out of bounds",
88+
file=sys.stderr,
89+
)
7690
for page_num in range(*page_range.indices(len(reader.pages))):
7791
writer.add_page(reader.pages[page_num])
7892
writer.write(output_fh)

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)