Skip to content

Return False from isValidFont for a missing font (#156)#161

Open
apoorvdarshan wants to merge 1 commit into
pwaller:mainfrom
apoorvdarshan:fix-isvalidfont-missing
Open

Return False from isValidFont for a missing font (#156)#161
apoorvdarshan wants to merge 1 commit into
pwaller:mainfrom
apoorvdarshan:fix-isvalidfont-missing

Conversation

@apoorvdarshan

Copy link
Copy Markdown

Fixes #156.

Problem

FigletFont.isValidFont(font) is a validity predicate, but it raises FileNotFoundError for a .flf/.tlf-named font that doesn't exist, instead of returning False:

>>> from pyfiglet import FigletFont
>>> FigletFont.isValidFont("slantd.flf")
Traceback (most recent call last):
  ...
  File ".../pyfiglet/__init__.py", line 182, in isValidFont
    f = importlib.resources.files('pyfiglet.fonts').joinpath(font).open('rb')
FileNotFoundError: [Errno 2] No such file or directory: '.../pyfiglet/fonts/slantd.flf'

When the font is neither a local file, nor in SHARED_DIRECTORY, nor a bundled package resource, the fallback ...joinpath(font).open('rb') raises. Callers reasonably expect isValidFont to simply return False for an unknown font.

Fix

Wrap the font-opening in try/except FileNotFoundError and return False when the font can't be found:

try:
    if os.path.isfile(font):
        f = open(font, 'rb')
    elif os.path.isfile(full_file):
        f = open(full_file, 'rb')
    else:
        f = importlib.resources.files('pyfiglet.fonts').joinpath(font).open('rb')
except FileNotFoundError:
    return False

Testing

  • Added test_is_valid_font_missing: isValidFont("does_not_exist.flf") and .tlf return False (no exception), and a real bundled font (standard.flf) is still recognized as valid. It fails on master (raises FileNotFoundError) and passes with this change.
  • The test suite passes (4 passed with the pyfiglet entry point on PATH; the CLI subprocess tests need it there, which is a pre-existing environment requirement unrelated to this change).

Disclosure: this change was prepared with the assistance of an AI tool (Claude Code). I reproduced the issue, implemented and verified the fix and test, and take responsibility for the contribution and will respond to review feedback personally.

isValidFont raised FileNotFoundError for a .flf/.tlf-named font that does
not exist (the fallback opens a package resource that may be absent),
instead of reporting the font as invalid. Catch FileNotFoundError and
return False so the validity check does not crash. Fixes pwaller#156.
@pwaller

pwaller commented Jul 4, 2026

Copy link
Copy Markdown
Owner

I just landed #158 which conflicts with the PR you sent. Can you ask your AI to rebase against it and fix up the conflicts?

@pwaller pwaller mentioned this pull request Jul 4, 2026
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.

Line 182 in __init__.py throws error

2 participants