Skip to content

Commit

Permalink
Remove decode_if_byte_string (#469)
Browse files Browse the repository at this point in the history
  • Loading branch information
adamchainz authored Jan 29, 2023
1 parent e28f386 commit 1c44b8b
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 30 deletions.
5 changes: 5 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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)
------------------

Expand Down
3 changes: 0 additions & 3 deletions src/whitenoise/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions src/whitenoise/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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
8 changes: 0 additions & 8 deletions src/whitenoise/string_utils.py
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
16 changes: 0 additions & 16 deletions tests/test_string_utils.py
Original file line number Diff line number Diff line change
@@ -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("<object object at ")


class EnsureLeadingTrailingSlashTests:
def test_none(self):
assert ensure_leading_trailing_slash(None) == "/"
Expand Down

0 comments on commit 1c44b8b

Please sign in to comment.