Fix get_ranges crash when content_length is None by bysiber · Pull Request #2077 · cherrypy/cherrypy

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