sanic-org / sanic

Accelerate your web app development | Build fast. Run fast.
https://sanic.dev
MIT License
18k stars 1.54k forks source link

Datetime issue with Python 3.11 (with fix) #2694

Closed stricaud closed 1 year ago

stricaud commented 1 year ago

Is there an existing issue for this?

Describe the bug

I am getting various exceptions like this:

[2023-02-24 22:41:25 +0100] [512643] [ERROR] Exception in static request handler: path=static, relative_url=fonts/general.woff2
Traceback (most recent call last):
  File "/home/sebastien/.local/lib/python3.11/site-packages/sanic/mixins/routes.py", line 848, in _static_request_handler
    response = await validate_file(request.headers, modified_since)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/sebastien/.local/lib/python3.11/site-packages/sanic/response/convenience.py", line 151, in validate_file
    if last_modified <= if_modified_since:
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: can't compare offset-naive and offset-aware datetimes

Which is fixed by ensuring last_modified and if_modified_since are converted to proper timestamps():

if last_modified.timestamp() <= if_modified_since.timestamp():

Code snippet

No response

Expected Behavior

No response

How do you run Sanic?

Sanic CLI

Operating System

Linux Debian

Sanic Version

Sanic 22.12.0; Routing 22.8.0

Additional context

No response

ahopkins commented 1 year ago

Excellent. Can you open a PR with this fix and a unit test?

stricaud commented 1 year ago

absolutely

stricaud commented 1 year ago

Done with PR #2697 2697