2013-10-28 04:26:51 +08:00
|
|
|
# -*- coding: utf-8 -*-
|
2013-07-30 01:19:04 +08:00
|
|
|
from __future__ import unicode_literals
|
2011-03-28 13:58:43 +08:00
|
|
|
|
2013-10-28 04:26:51 +08:00
|
|
|
from io import BytesIO
|
2013-06-20 18:55:27 +08:00
|
|
|
import os
|
2012-05-31 15:59:58 +08:00
|
|
|
import gzip
|
2011-03-28 13:58:43 +08:00
|
|
|
import tempfile
|
2013-07-01 20:22:27 +08:00
|
|
|
import unittest
|
2013-10-28 04:26:51 +08:00
|
|
|
import zlib
|
2010-09-13 04:03:55 +08:00
|
|
|
|
2011-03-28 13:58:43 +08:00
|
|
|
from django.core.files import File
|
2013-06-20 18:55:27 +08:00
|
|
|
from django.core.files.move import file_move_safe
|
2010-09-13 04:03:55 +08:00
|
|
|
from django.core.files.base import ContentFile
|
2013-10-28 04:26:51 +08:00
|
|
|
from django.core.files.uploadedfile import SimpleUploadedFile, UploadedFile
|
2013-09-07 02:23:50 +08:00
|
|
|
from django.core.files.temp import NamedTemporaryFile
|
2013-10-28 04:26:51 +08:00
|
|
|
from django.utils._os import upath
|
|
|
|
from django.utils import six
|
|
|
|
|
|
|
|
try:
|
2014-03-21 22:54:53 +08:00
|
|
|
from PIL import Image
|
|
|
|
except ImportError:
|
2013-10-28 04:26:51 +08:00
|
|
|
Image = None
|
2014-03-21 22:54:53 +08:00
|
|
|
else:
|
|
|
|
from django.core.files import images
|
2013-02-23 20:42:04 +08:00
|
|
|
|
|
|
|
|
2012-05-31 15:59:58 +08:00
|
|
|
class FileTests(unittest.TestCase):
|
2013-10-28 04:26:51 +08:00
|
|
|
def test_unicode_uploadedfile_name(self):
|
|
|
|
"""
|
|
|
|
Regression test for #8156: files with unicode names I can't quite figure
|
|
|
|
out the encoding situation between doctest and this file, but the actual
|
|
|
|
repr doesn't matter; it just shouldn't return a unicode object.
|
|
|
|
"""
|
|
|
|
uf = UploadedFile(name='¿Cómo?', content_type='text')
|
|
|
|
self.assertEqual(type(uf.__repr__()), str)
|
|
|
|
|
2011-03-28 13:58:43 +08:00
|
|
|
def test_context_manager(self):
|
|
|
|
orig_file = tempfile.TemporaryFile()
|
|
|
|
base_file = File(orig_file)
|
|
|
|
with base_file as f:
|
|
|
|
self.assertIs(base_file, f)
|
|
|
|
self.assertFalse(f.closed)
|
|
|
|
self.assertTrue(f.closed)
|
|
|
|
self.assertTrue(orig_file.closed)
|
2012-05-31 15:59:58 +08:00
|
|
|
|
2013-09-07 02:23:50 +08:00
|
|
|
def test_namedtemporaryfile_closes(self):
|
|
|
|
"""
|
|
|
|
The symbol django.core.files.NamedTemporaryFile is assigned as
|
|
|
|
a different class on different operating systems. In
|
|
|
|
any case, the result should minimally mock some of the API of
|
|
|
|
tempfile.NamedTemporaryFile from the Python standard library.
|
|
|
|
"""
|
|
|
|
tempfile = NamedTemporaryFile()
|
|
|
|
self.assertTrue(hasattr(tempfile, "closed"))
|
|
|
|
self.assertFalse(tempfile.closed)
|
|
|
|
|
|
|
|
tempfile.close()
|
|
|
|
self.assertTrue(tempfile.closed)
|
|
|
|
|
2012-05-31 15:59:58 +08:00
|
|
|
def test_file_mode(self):
|
|
|
|
# Should not set mode to None if it is not present.
|
|
|
|
# See #14681, stdlib gzip module crashes if mode is set to None
|
2012-08-12 04:29:18 +08:00
|
|
|
file = SimpleUploadedFile("mode_test.txt", b"content")
|
2012-05-31 15:59:58 +08:00
|
|
|
self.assertFalse(hasattr(file, 'mode'))
|
2013-10-19 20:31:38 +08:00
|
|
|
gzip.GzipFile(fileobj=file)
|
2013-06-20 18:55:27 +08:00
|
|
|
|
2014-02-21 02:13:25 +08:00
|
|
|
def test_file_iteration(self):
|
|
|
|
"""
|
|
|
|
File objects should yield lines when iterated over.
|
|
|
|
Refs #22107.
|
|
|
|
"""
|
|
|
|
file = File(BytesIO(b'one\ntwo\nthree'))
|
|
|
|
self.assertEqual(list(file), [b'one\n', b'two\n', b'three'])
|
|
|
|
|
2013-06-20 18:55:27 +08:00
|
|
|
|
2013-10-28 04:26:51 +08:00
|
|
|
class NoNameFileTestCase(unittest.TestCase):
|
|
|
|
"""
|
|
|
|
Other examples of unnamed files may be tempfile.SpooledTemporaryFile or
|
|
|
|
urllib.urlopen()
|
|
|
|
"""
|
|
|
|
def test_noname_file_default_name(self):
|
|
|
|
self.assertEqual(File(BytesIO(b'A file with no name')).name, None)
|
|
|
|
|
|
|
|
def test_noname_file_get_size(self):
|
|
|
|
self.assertEqual(File(BytesIO(b'A file with no name')).size, 19)
|
|
|
|
|
|
|
|
|
|
|
|
class ContentFileTestCase(unittest.TestCase):
|
|
|
|
def test_content_file_default_name(self):
|
|
|
|
self.assertEqual(ContentFile(b"content").name, None)
|
|
|
|
|
|
|
|
def test_content_file_custom_name(self):
|
|
|
|
"""
|
|
|
|
Test that the constructor of ContentFile accepts 'name' (#16590).
|
|
|
|
"""
|
|
|
|
name = "I can have a name too!"
|
|
|
|
self.assertEqual(ContentFile(b"content", name=name).name, name)
|
|
|
|
|
|
|
|
def test_content_file_input_type(self):
|
|
|
|
"""
|
|
|
|
Test that ContentFile can accept both bytes and unicode and that the
|
|
|
|
retrieved content is of the same type.
|
|
|
|
"""
|
|
|
|
self.assertIsInstance(ContentFile(b"content").read(), bytes)
|
|
|
|
if six.PY3:
|
|
|
|
self.assertIsInstance(ContentFile("español").read(), six.text_type)
|
|
|
|
else:
|
|
|
|
self.assertIsInstance(ContentFile("español").read(), bytes)
|
|
|
|
|
|
|
|
|
|
|
|
class DimensionClosingBug(unittest.TestCase):
|
|
|
|
"""
|
|
|
|
Test that get_image_dimensions() properly closes files (#8817)
|
|
|
|
"""
|
2014-03-21 22:54:53 +08:00
|
|
|
@unittest.skipUnless(Image, "Pillow not installed")
|
2013-10-28 04:26:51 +08:00
|
|
|
def test_not_closing_of_files(self):
|
|
|
|
"""
|
|
|
|
Open files passed into get_image_dimensions() should stay opened.
|
|
|
|
"""
|
|
|
|
empty_io = BytesIO()
|
|
|
|
try:
|
|
|
|
images.get_image_dimensions(empty_io)
|
|
|
|
finally:
|
|
|
|
self.assertTrue(not empty_io.closed)
|
|
|
|
|
2014-03-21 22:54:53 +08:00
|
|
|
@unittest.skipUnless(Image, "Pillow not installed")
|
2013-10-28 04:26:51 +08:00
|
|
|
def test_closing_of_filenames(self):
|
|
|
|
"""
|
|
|
|
get_image_dimensions() called with a filename should closed the file.
|
|
|
|
"""
|
|
|
|
# We need to inject a modified open() builtin into the images module
|
|
|
|
# that checks if the file was closed properly if the function is
|
|
|
|
# called with a filename instead of an file object.
|
|
|
|
# get_image_dimensions will call our catching_open instead of the
|
|
|
|
# regular builtin one.
|
|
|
|
|
|
|
|
class FileWrapper(object):
|
|
|
|
_closed = []
|
|
|
|
|
|
|
|
def __init__(self, f):
|
|
|
|
self.f = f
|
|
|
|
|
|
|
|
def __getattr__(self, name):
|
|
|
|
return getattr(self.f, name)
|
|
|
|
|
|
|
|
def close(self):
|
|
|
|
self._closed.append(True)
|
|
|
|
self.f.close()
|
|
|
|
|
|
|
|
def catching_open(*args):
|
|
|
|
return FileWrapper(open(*args))
|
|
|
|
|
|
|
|
images.open = catching_open
|
|
|
|
try:
|
|
|
|
images.get_image_dimensions(os.path.join(os.path.dirname(upath(__file__)), "test1.png"))
|
|
|
|
finally:
|
|
|
|
del images.open
|
|
|
|
self.assertTrue(FileWrapper._closed)
|
|
|
|
|
|
|
|
|
|
|
|
class InconsistentGetImageDimensionsBug(unittest.TestCase):
|
|
|
|
"""
|
|
|
|
Test that get_image_dimensions() works properly after various calls
|
|
|
|
using a file handler (#11158)
|
|
|
|
"""
|
2014-03-21 22:54:53 +08:00
|
|
|
@unittest.skipUnless(Image, "Pillow not installed")
|
2013-10-28 04:26:51 +08:00
|
|
|
def test_multiple_calls(self):
|
|
|
|
"""
|
|
|
|
Multiple calls of get_image_dimensions() should return the same size.
|
|
|
|
"""
|
|
|
|
img_path = os.path.join(os.path.dirname(upath(__file__)), "test.png")
|
2014-01-03 04:36:15 +08:00
|
|
|
with open(img_path, 'rb') as fh:
|
|
|
|
image = images.ImageFile(fh)
|
|
|
|
image_pil = Image.open(fh)
|
2013-10-28 04:26:51 +08:00
|
|
|
size_1 = images.get_image_dimensions(image)
|
|
|
|
size_2 = images.get_image_dimensions(image)
|
|
|
|
self.assertEqual(image_pil.size, size_1)
|
|
|
|
self.assertEqual(size_1, size_2)
|
|
|
|
|
2014-03-21 22:54:53 +08:00
|
|
|
@unittest.skipUnless(Image, "Pillow not installed")
|
2013-10-28 04:26:51 +08:00
|
|
|
def test_bug_19457(self):
|
|
|
|
"""
|
|
|
|
Regression test for #19457
|
|
|
|
get_image_dimensions fails on some pngs, while Image.size is working good on them
|
|
|
|
"""
|
|
|
|
img_path = os.path.join(os.path.dirname(upath(__file__)), "magic.png")
|
|
|
|
try:
|
|
|
|
size = images.get_image_dimensions(img_path)
|
|
|
|
except zlib.error:
|
|
|
|
self.fail("Exception raised from get_image_dimensions().")
|
2014-01-03 04:36:15 +08:00
|
|
|
with open(img_path, 'rb') as fh:
|
|
|
|
self.assertEqual(size, Image.open(fh).size)
|
2013-10-28 04:26:51 +08:00
|
|
|
|
|
|
|
|
2013-06-20 18:55:27 +08:00
|
|
|
class FileMoveSafeTests(unittest.TestCase):
|
|
|
|
def test_file_move_overwrite(self):
|
|
|
|
handle_a, self.file_a = tempfile.mkstemp(dir=os.environ['DJANGO_TEST_TEMP_DIR'])
|
|
|
|
handle_b, self.file_b = tempfile.mkstemp(dir=os.environ['DJANGO_TEST_TEMP_DIR'])
|
|
|
|
|
|
|
|
# file_move_safe should raise an IOError exception if destination file exists and allow_overwrite is False
|
|
|
|
self.assertRaises(IOError, lambda: file_move_safe(self.file_a, self.file_b, allow_overwrite=False))
|
|
|
|
|
|
|
|
# should allow it and continue on if allow_overwrite is True
|
|
|
|
self.assertIsNone(file_move_safe(self.file_a, self.file_b, allow_overwrite=True))
|
2013-09-20 05:02:49 +08:00
|
|
|
|
|
|
|
os.close(handle_a)
|
|
|
|
os.close(handle_b)
|