diff --git a/docs/changelog.rst b/docs/changelog.rst index 18dbd52b..de0c6297 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -4,6 +4,11 @@ Changelog * Support Django 4.2. +* Remove further support for byte strings from the ``root`` and ``prefix`` arguments to ``WhiteNoise``, and Django’s ``STATIC_ROOT`` setting. + Like in the previous release, this seems to be a remnant of Python 2 support. + Again, this change may be backwards incompatible for a small number of projects, but it’s unlikely. + Django does not support ``STATIC_ROOT`` being a byte string. + 6.3.0 (2023-01-03) ------------------ diff --git a/src/whitenoise/base.py b/src/whitenoise/base.py index d9063fa9..b310264a 100644 --- a/src/whitenoise/base.py +++ b/src/whitenoise/base.py @@ -13,7 +13,6 @@ from .responders import MissingFileError from .responders import Redirect from .responders import StaticFile -from .string_utils import decode_if_byte_string from .string_utils import decode_path_info from .string_utils import ensure_leading_trailing_slash @@ -96,10 +95,8 @@ def serve(static_file, environ, start_response): return [] def add_files(self, root, prefix=None): - root = decode_if_byte_string(root, force_text=True) root = os.path.abspath(root) root = root.rstrip(os.path.sep) + os.path.sep - prefix = decode_if_byte_string(prefix) prefix = ensure_leading_trailing_slash(prefix) if self.autorefresh: # Later calls to `add_files` overwrite earlier ones, hence we need diff --git a/src/whitenoise/middleware.py b/src/whitenoise/middleware.py index 23eb4382..3f5a8091 100644 --- a/src/whitenoise/middleware.py +++ b/src/whitenoise/middleware.py @@ -11,7 +11,6 @@ from django.urls import get_script_prefix from .base import WhiteNoise -from .string_utils import decode_if_byte_string from .string_utils import ensure_leading_trailing_slash __all__ = ["WhiteNoiseMiddleware"] @@ -101,7 +100,7 @@ def __init__(self, get_response=None, settings=settings): self.static_prefix = self.static_prefix[len(script_prefix) :] self.static_prefix = ensure_leading_trailing_slash(self.static_prefix) - self.static_root = decode_if_byte_string(settings.STATIC_ROOT) + self.static_root = settings.STATIC_ROOT if self.static_root: self.add_files(self.static_root, prefix=self.static_prefix) @@ -198,6 +197,6 @@ def get_name_without_hash(self, filename): def get_static_url(self, name): try: - return decode_if_byte_string(staticfiles_storage.url(name)) + return staticfiles_storage.url(name) except ValueError: return None diff --git a/src/whitenoise/string_utils.py b/src/whitenoise/string_utils.py index b56d2624..6be90620 100644 --- a/src/whitenoise/string_utils.py +++ b/src/whitenoise/string_utils.py @@ -1,14 +1,6 @@ from __future__ import annotations -def decode_if_byte_string(s, force_text=False): - if isinstance(s, bytes): - s = s.decode() - if force_text and not isinstance(s, str): - s = str(s) - return s - - # Follow Django in treating URLs as UTF-8 encoded (which requires undoing the # implicit ISO-8859-1 decoding applied in Python 3). Strictly speaking, URLs # should only be ASCII anyway, but UTF-8 can be found in the wild. diff --git a/tests/test_string_utils.py b/tests/test_string_utils.py index d5b0cf4a..3d56524e 100644 --- a/tests/test_string_utils.py +++ b/tests/test_string_utils.py @@ -1,24 +1,8 @@ from __future__ import annotations -from whitenoise.string_utils import decode_if_byte_string from whitenoise.string_utils import ensure_leading_trailing_slash -class DecodeIfByteStringTests: - def test_bytes(self): - assert decode_if_byte_string(b"abc") == "abc" - - def test_unforced(self): - x = object() - assert decode_if_byte_string(x) is x - - def test_forced(self): - x = object() - result = decode_if_byte_string(x, force_text=True) - assert isinstance(result, str) - assert result.startswith("