Skip to content

Commit

Permalink
Add warning for out-of-bounds page range in pdfly cat command (py-pdf#58
Browse files Browse the repository at this point in the history
)
  • Loading branch information
Zingzy authored and ebotiab committed Oct 15, 2024
1 parent 8eb34ac commit 43e9803
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
15 changes: 14 additions & 1 deletion pdfly/cat.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,20 @@ def main(
in_fs[filename] = open(filename, "rb")

reader = PdfReader(in_fs[filename])

num_pages = len(reader.pages)
start, end, step = page_range.indices(num_pages)
if (
start < 0
or end < 0
or start >= num_pages
or end > num_pages
or start > end
):
print(
f"WARNING: Page range {page_range} is out of bounds",
file=sys.stderr,
)
if not use_complements:
for page_num in range(*page_range.indices(len(reader.pages))):
writer.add_page(reader.pages[page_num])
Expand All @@ -87,7 +101,6 @@ def main(
compl_page_nums = all_page_nums - page_nums
for page_num in compl_page_nums:
writer.add_page(reader.pages[page_num])

writer.write(output_fh)
except Exception:
print(traceback.format_exc(), file=sys.stderr)
Expand Down
3 changes: 1 addition & 2 deletions tests/test_cat.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ def test_cat_subset_invalid_args(capsys, tmp_path, page_range):
assert "Invalid file path or page range provided" in captured.err


@pytest.mark.skip(reason="This check is not implemented yet")
def test_cat_subset_warn_on_missing_pages(capsys, tmp_path):
with chdir(tmp_path):
exit_code = run_cli(
Expand All @@ -85,7 +84,7 @@ def test_cat_subset_warn_on_missing_pages(capsys, tmp_path):
)
captured = capsys.readouterr()
assert exit_code == 0, captured
assert "WARN" in captured.out
assert "WARN" in captured.err


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

0 comments on commit 43e9803

Please sign in to comment.