summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChris McDonough <chrism@plope.com>2014-06-05 22:47:35 -0400
committerChris McDonough <chrism@plope.com>2014-06-05 22:47:35 -0400
commit32602159eeef8595c7db6edbf1461c58e9ff0349 (patch)
tree340c5dfa5b2d76c087e319e922e38b9693ac26d8
parenta4959a3a69f3a8e2d6c666f589728fe4cc673012 (diff)
downloadpyramid-32602159eeef8595c7db6edbf1461c58e9ff0349.tar.gz
pyramid-32602159eeef8595c7db6edbf1461c58e9ff0349.tar.bz2
pyramid-32602159eeef8595c7db6edbf1461c58e9ff0349.zip
- Work around a bug introduced in Python 2.7.7 on Windows where
``mimetypes.guess_type`` returns Unicode rather than str for the content type, unlike any previous version of Python. See https://github.com/Pylons/pyramid/issues/1360 for more information. Closes #1360.
-rw-r--r--CHANGES.txt5
-rw-r--r--pyramid/response.py10
-rw-r--r--pyramid/tests/test_response.py21
3 files changed, 34 insertions, 2 deletions
diff --git a/CHANGES.txt b/CHANGES.txt
index e60c6efac..51af8ee01 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -20,6 +20,11 @@ Bug Fixes
- Fix an issue where a ``pyramid.response.FileResponse`` may apply a charset
where it does not belong. See https://github.com/Pylons/pyramid/pull/1251
+- Work around a bug introduced in Python 2.7.7 on Windows where
+ ``mimetypes.guess_type`` returns Unicode rather than str for the content
+ type, unlike any previous version of Python. See
+ https://github.com/Pylons/pyramid/issues/1360 for more information.
+
Docs
----
diff --git a/pyramid/response.py b/pyramid/response.py
index adc903b44..d11fd0123 100644
--- a/pyramid/response.py
+++ b/pyramid/response.py
@@ -53,10 +53,16 @@ class FileResponse(Response):
def __init__(self, path, request=None, cache_max_age=None,
content_type=None, content_encoding=None):
if content_type is None:
- content_type, content_encoding = (
- mimetypes.guess_type(path, strict=False))
+ content_type, content_encoding = mimetypes.guess_type(
+ path,
+ strict=False
+ )
if content_type is None:
content_type = 'application/octet-stream'
+ # str-ifying content_type is a workaround for a bug in Python 2.7.7
+ # on Windows where mimetypes.guess_type returns unicode for the
+ # content_type.
+ content_type = str(content_type)
super(FileResponse, self).__init__(
conditional_response=True,
content_type=content_type,
diff --git a/pyramid/tests/test_response.py b/pyramid/tests/test_response.py
index 8731fa764..a16eb8d33 100644
--- a/pyramid/tests/test_response.py
+++ b/pyramid/tests/test_response.py
@@ -62,6 +62,27 @@ class TestFileResponse(unittest.TestCase):
self.assertEqual(r.headers['content-type'], content_type)
r.app_iter.close()
+ def test_python_277_bug_15207(self):
+ # python 2.7.7 on windows has a bug where its mimetypes.guess_type
+ # function returns Unicode for the content_type, unlike any previous
+ # version of Python. See https://github.com/Pylons/pyramid/issues/1360
+ # for more information.
+ from pyramid.compat import text_
+ import mimetypes as old_mimetypes
+ from pyramid import response
+ class FakeMimetypesModule(object):
+ def guess_type(self, *arg, **kw):
+ return text_('foo/bar'), None
+ fake_mimetypes = FakeMimetypesModule()
+ try:
+ response.mimetypes = fake_mimetypes
+ path = self._getPath('xml')
+ r = self._makeOne(path)
+ self.assertEqual(r.content_type, 'foo/bar')
+ self.assertEqual(type(r.content_type), str)
+ finally:
+ response.mimetypes = old_mimetypes
+
class TestFileIter(unittest.TestCase):
def _makeOne(self, file, block_size):
from pyramid.response import FileIter