Skip to content

Fix get_ranges crash when content_length is None#2077

Open
bysiber wants to merge 1 commit intocherrypy:mainfrom
bysiber:fix-get-ranges-none-content-length
Open

Fix get_ranges crash when content_length is None#2077
bysiber wants to merge 1 commit intocherrypy:mainfrom
bysiber:fix-get-ranges-none-content-length

Conversation

@bysiber
Copy link
Copy Markdown

@bysiber bysiber commented Feb 22, 2026

When serving resources via serve_fileobj (e.g. zipapp resources loaded with importlib.resources), content_length can be None. This causes get_ranges() to crash with a TypeError when comparing integers against None.

This adds an early return of None when content_length is None, which is consistent with how the function already handles missing Range headers.

Includes regression tests for this case.

What kind of change does this PR introduce?

  • bug fix
  • feature
  • docs update
  • tests/coverage improvement
  • refactoring
  • other

What is the related issue number (starting with #)

Fixes #2024

What is the current behavior? (You can also link to an open issue here)

get_ranges() raises TypeError: '>=' not supported between instances of 'int' and 'NoneType' when content_length is None.

What is the new behavior (if this is a feature change)?

get_ranges() returns None when content_length is None, avoiding the crash.

Checklist:

  • I think the code is well written
  • I wrote good commit messages
  • I have squashed related commits together after the changes have been approved
  • Unit tests for the changes exist
  • Integration tests for the changes exist (if applicable)
  • I used the same coding conventions as the rest of the project
  • The new code doesn't generate linter offenses
  • Documentation reflects the changes
  • The PR relates to only one subject with a clear title
    and description in grammatically correct, complete sentences

When serving resources via serve_fileobj (e.g. zipapp resources),
content_length can be None. This caused get_ranges() to crash with
TypeError when comparing integers against None.

Return None early when content_length is None, consistent with how
the function already handles missing Range headers.

Added regression tests for this case.
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.

cherrypy.tools.static loses with a TypeError in httputil.get_ranges() when asked to serve a range from a fileobj representing a zipfile entry

1 participant