Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix incompatibility with Pathlib #193

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions tests/test_django_whitenoise.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from __future__ import unicode_literals

import unittest

try:
from urllib.parse import urljoin, urlparse
except ImportError:
Expand Down Expand Up @@ -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):

Expand Down
5 changes: 5 additions & 0 deletions whitenoise/base.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import sys
from posixpath import normpath
import re
import warnings
Expand All @@ -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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe you want move them to string_utils.py as BINARY_TYPE ?

if sys.version_info[0] >= 3:

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good suggestion!


class WhiteNoise(object):

Expand Down Expand Up @@ -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)
Expand Down