Skip to content

Commit

Permalink
middleware fixes (warnings) & python 3.13 (#126)
Browse files Browse the repository at this point in the history
* fix DeprecationWarning
- fix warnings with compression middleware when no compression is used
* add python 3.13 to tested versions as the local tests were working under
python 3.13 and the server_error fix is only required for python 3.13
  • Loading branch information
devkral authored Jan 2, 2025
1 parent 104d2d6 commit 4e742e5
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test-suite.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
runs-on: "ubuntu-latest"
strategy:
matrix:
python-version: ["3.9", "3.10", "3.11", "3.12"]
python-version: ["3.9", "3.10", "3.11", "3.12", "3.13"]

services:
postgres:
Expand Down
15 changes: 11 additions & 4 deletions lilya/middleware/compression.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import gzip
import io
from functools import cached_property
from typing import NoReturn

from lilya.datastructures import Header
Expand Down Expand Up @@ -79,9 +80,10 @@ def __init__(self, app: ASGIApp, minimum_size: int, compresslevel: int = 9) -> N
self.started = False
self.content_encoding_set = False
self.gzip_buffer = io.BytesIO()
self.gzip_file = gzip.GzipFile(
mode="wb", fileobj=self.gzip_buffer, compresslevel=self.compresslevel
)

@cached_property
def gzip_file(self) -> gzip.GzipFile:
return gzip.GzipFile(mode="wb", fileobj=self.gzip_buffer, compresslevel=self.compresslevel)

async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
"""
Expand All @@ -93,7 +95,12 @@ async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
send (Send): The send channel.
"""
self.send = send
await self.app(scope, receive, self.send_with_gzip)
try:
await self.app(scope, receive, self.send_with_gzip)
finally:
# ensure cleanup if file is initialized
if "gzip_file" in self.__dict__ and not self.gzip_file.closed:
self.gzip_file.close()

async def send_with_gzip(self, message: Message) -> None:
"""
Expand Down
10 changes: 6 additions & 4 deletions lilya/middleware/server_error.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,10 +195,12 @@ def generate_html(self, exc: Exception) -> str:
is_collapsed = True

# escape error class and text
error = (
f"{html.escape(traceback_obj.exc_type.__name__)}: "
f"{html.escape(str(traceback_obj))}"
)
try:
exc_type_str = traceback_obj.exc_type_str
except Exception:
# for older python versions < 3.13
exc_type_str = traceback_obj.exc_type.__name__
error = f"{html.escape(exc_type_str)}: " f"{html.escape(str(traceback_obj))}"

template = get_template_errors()
return template.format(styles=get_css_style(), js=get_js(), error=error, exc_html=exc_html)
Expand Down

0 comments on commit 4e742e5

Please sign in to comment.