From 576f6df434814c017df93ecf7a2183423da26049 Mon Sep 17 00:00:00 2001 From: Bruno Alla Date: Mon, 13 Aug 2018 17:04:16 +0100 Subject: [PATCH 1/2] Test case for Pathlib compatibility --- tests/test_django_whitenoise.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tests/test_django_whitenoise.py b/tests/test_django_whitenoise.py index 8a4f0d0b..1f8b7ee9 100644 --- a/tests/test_django_whitenoise.py +++ b/tests/test_django_whitenoise.py @@ -1,5 +1,7 @@ from __future__ import unicode_literals +import unittest + try: from urllib.parse import urljoin, urlparse except ImportError: @@ -107,6 +109,17 @@ def test_get_nonascii_file(self): self.assertEqual(response.content, self.static_files.nonascii_content) +@override_settings() +class PathlibTest(SimpleTestCase): + + @unittest.skipIf(sys.version_info < (3, 4), "Pathlib was added in Python 3.4") + def test_pathlib_compat(self): + from pathlib import Path + settings.STATIC_ROOT = Path(tempfile.mkdtemp()) + app = get_wsgi_application() + self.assertNotEqual(app, None) + + @override_settings() class UseFindersTest(SimpleTestCase): From c3e1bd5396b65e82e3d5ccc3a1282350360d57ec Mon Sep 17 00:00:00 2001 From: Bruno Alla Date: Mon, 13 Aug 2018 17:21:09 +0100 Subject: [PATCH 2/2] Fix bug with file root being a pathlib.Path instance --- whitenoise/base.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/whitenoise/base.py b/whitenoise/base.py index 1e9bbb55..beb8fbb3 100644 --- a/whitenoise/base.py +++ b/whitenoise/base.py @@ -1,4 +1,5 @@ import os +import sys from posixpath import normpath import re import warnings @@ -11,6 +12,8 @@ from .string_utils import (decode_if_byte_string, decode_path_info, ensure_leading_trailing_slash) +TEXT_TYPE = str if sys.version_info[0] >= 3 else unicode + class WhiteNoise(object): @@ -89,6 +92,8 @@ def serve(static_file, environ, start_response): def add_files(self, root, prefix=None): root = decode_if_byte_string(root) + # `root` might be a pathlib.Path instance: cast it to a string + root = TEXT_TYPE(root) root = root.rstrip(os.path.sep) + os.path.sep prefix = decode_if_byte_string(prefix) prefix = ensure_leading_trailing_slash(prefix)