Skip to content

Quality: Broad exception handling in fill_from_encoding#3771

Open
SlncTrZ wants to merge 1 commit into
py-pdf:mainfrom
SlncTrZ:CntrB/improve/quality/broad-exception-handling-in-fill-from-en
Open

Quality: Broad exception handling in fill_from_encoding#3771
SlncTrZ wants to merge 1 commit into
py-pdf:mainfrom
SlncTrZ:CntrB/improve/quality/broad-exception-handling-in-fill-from-en

Conversation

@SlncTrZ
Copy link
Copy Markdown

@SlncTrZ SlncTrZ commented May 11, 2026

Summary

Quality: Broad exception handling in fill_from_encoding

Problem

Severity: Low | File: pypdf/_codecs/__init__.py:L10

The function fill_from_encoding catches a generic Exception when decoding bytes to string. This can mask various errors (e.g., MemoryError, KeyboardInterrupt) and makes debugging difficult. It should catch specific exceptions like UnicodeDecodeError.

Solution

Replace except Exception: with except UnicodeDecodeError: to handle only expected decoding failures.

Changes

  • pypdf/_codecs/__init__.py (modified)

Testing

  • Existing tests pass
  • Manual review completed
  • No new warnings/errors introduced"

The function fill_from_encoding catches a generic Exception when decoding bytes to string. This can mask various errors (e.g., MemoryError, KeyboardInterrupt) and makes debugging difficult. It should catch specific exceptions like UnicodeDecodeError.

Affected files: __init__.py

Signed-off-by: Dinh Truong (SlncTrZ) <46520299+SlncTrZ@users.noreply.github.com>
@codecov
Copy link
Copy Markdown

codecov Bot commented May 11, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.62%. Comparing base (4c64630) to head (7297e8c).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #3771   +/-   ##
=======================================
  Coverage   97.62%   97.62%           
=======================================
  Files          55       55           
  Lines       10221    10221           
  Branches     1876     1876           
=======================================
  Hits         9978     9978           
  Misses        138      138           
  Partials      105      105           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Copy Markdown
Collaborator

@stefan6419846 stefan6419846 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR. Could you please have a look at the test failures?

Additionally: Did you run into specific issues where we could add a test for as well, or this is a generic finding?

@SlncTrZ
Copy link
Copy Markdown
Author

SlncTrZ commented May 11, 2026 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants