Skip to content

fix: catch OverflowError and TypeError in filesize._to_str()#601

Open
koteshyelamati wants to merge 1 commit into
PyFilesystem:masterfrom
koteshyelamati:patch-1
Open

fix: catch OverflowError and TypeError in filesize._to_str()#601
koteshyelamati wants to merge 1 commit into
PyFilesystem:masterfrom
koteshyelamati:patch-1

Conversation

@koteshyelamati

Copy link
Copy Markdown

Problem

_to_str() in fs/filesize.py only catches ValueError when converting the size argument to int, but two other exceptions can occur:

  • int(float('inf')) raises OverflowError, not ValueError
  • int(None) raises TypeError, not ValueError

This means callers passing float('inf') or None get an unexpected bare OverflowError/TypeError instead of the informative TypeError message the function is designed to produce.

>>> from fs.filesize import traditional
>>> traditional(float('inf'))
# OverflowError: cannot convert float infinity to integer
# (instead of: TypeError: filesize requires a numeric value, not inf)

Fix

Extend the except clause to also catch OverflowError and TypeError:

except (ValueError, TypeError, OverflowError):
    raise TypeError("filesize requires a numeric value, not {!r}".format(size))

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.

1 participant