<commit_before>import os
import tempfile
import shutil
from django.db import models
from django.core.files.storage import FileSystemStorage
from django.core.files.base import ContentFile

# Test for correct behavior of width_field/height_field.
# Of course, we can't run this without PIL.

try:
    # Checking for the existence of Image is enough for CPython, but
    # for PyPy, you need to check for the underlying modules
    from PIL import Image, _imaging
except ImportError:
    Image = None

# If we have PIL, do these tests
if Image:
    temp_storage_dir = tempfile.mkdtemp()
    temp_storage = FileSystemStorage(temp_storage_dir)

    class Person(models.Model):
        name = models.CharField(max_length=50)
        mugshot = models.ImageField(storage=temp_storage, upload_to='tests',
                                    height_field='mug_height',
                                    width_field='mug_width')
        mug_height = models.PositiveSmallIntegerField()
        mug_width = models.PositiveSmallIntegerField()

    __test__ = {'API_TESTS': """
>>> from django.core.files import File
>>> image_data = open(os.path.join(os.path.dirname(__file__), "test.png"), 'rb').read()
>>> p = Person(name="Joe")
>>> p.mugshot.save("mug", ContentFile(image_data))
>>> p.mugshot.width
16
>>> p.mugshot.height
16
>>> p.mug_height
16
>>> p.mug_width
16

# Bug #9786: Ensure '==' and '!=' work correctly.
>>> image_data = open(os.path.join(os.path.dirname(__file__), "test1.png"), 'rb').read()
>>> p1 = Person(name="Bob")
>>> p1.mugshot.save("mug", ContentFile(image_data))
>>> p2 = Person.objects.get(name="Joe")
>>> p.mugshot == p2.mugshot
True
>>> p.mugshot != p2.mugshot
False
>>> p.mugshot != p1.mugshot
True

Bug #9508: Similarly to the previous test, make sure hash() works as expected
(equal items must hash to the same value).
>>> hash(p.mugshot) == hash(p2.mugshot)
True

# Bug #8175: correctly delete files that have been removed off the file system.
>>> import os
>>> p2 = Person(name="Fred")
>>> p2.mugshot.save("shot", ContentFile(image_data))
>>> os.remove(p2.mugshot.path)
>>> p2.delete()

# Bug #8534: FileField.size should not leave the file open.
>>> p3 = Person(name="Joan")
>>> p3.mugshot.save("shot", ContentFile(image_data))

# Get a "clean" model instance
>>> p3 = Person.objects.get(name="Joan")

# It won't have an opened file.
>>> p3.mugshot.closed
True

# After asking for the size, the file should still be closed.
>>> _ = p3.mugshot.size
>>> p3.mugshot.closed
True

# Make sure that wrapping the file in a file still works
>>> p3.mugshot.file.open()
>>> p = Person.objects.create(name="<NAME>", mugshot=File(p3.mugshot.file))
>>> p.save()

# Delete all test files
>>> shutil.rmtree(temp_storage_dir)
"""}<commit_msg>Fixed #11032: close() a file explictly open()'d in a test, so that deleting the file tree it is in doesn't fail on Windows.  Thanks bthomas for the patch.<commit_after>import os
import tempfile
import shutil
from django.db import models
from django.core.files.storage import FileSystemStorage
from django.core.files.base import ContentFile

# Test for correct behavior of width_field/height_field.
# Of course, we can't run this without PIL.

try:
    # Checking for the existence of Image is enough for CPython, but
    # for PyPy, you need to check for the underlying modules
    from PIL import Image, _imaging
except ImportError:
    Image = None

# If we have PIL, do these tests
if Image:
    temp_storage_dir = tempfile.mkdtemp()
    temp_storage = FileSystemStorage(temp_storage_dir)

    class Person(models.Model):
        name = models.CharField(max_length=50)
        mugshot = models.ImageField(storage=temp_storage, upload_to='tests',
                                    height_field='mug_height',
                                    width_field='mug_width')
        mug_height = models.PositiveSmallIntegerField()
        mug_width = models.PositiveSmallIntegerField()

    __test__ = {'API_TESTS': """
>>> from django.core.files import File
>>> image_data = open(os.path.join(os.path.dirname(__file__), "test.png"), 'rb').read()
>>> p = Person(name="Joe")
>>> p.mugshot.save("mug", ContentFile(image_data))
>>> p.mugshot.width
16
>>> p.mugshot.height
16
>>> p.mug_height
16
>>> p.mug_width
16

# Bug #9786: Ensure '==' and '!=' work correctly.
>>> image_data = open(os.path.join(os.path.dirname(__file__), "test1.png"), 'rb').read()
>>> p1 = Person(name="Bob")
>>> p1.mugshot.save("mug", ContentFile(image_data))
>>> p2 = Person.objects.get(name="Joe")
>>> p.mugshot == p2.mugshot
True
>>> p.mugshot != p2.mugshot
False
>>> p.mugshot != p1.mugshot
True

Bug #9508: Similarly to the previous test, make sure hash() works as expected
(equal items must hash to the same value).
>>> hash(p.mugshot) == hash(p2.mugshot)
True

# Bug #8175: correctly delete files that have been removed off the file system.
>>> import os
>>> p2 = Person(name="Fred")
>>> p2.mugshot.save("shot", ContentFile(image_data))
>>> os.remove(p2.mugshot.path)
>>> p2.delete()

# Bug #8534: FileField.size should not leave the file open.
>>> p3 = Person(name="Joan")
>>> p3.mugshot.save("shot", ContentFile(image_data))

# Get a "clean" model instance
>>> p3 = Person.objects.get(name="Joan")

# It won't have an opened file.
>>> p3.mugshot.closed
True

# After asking for the size, the file should still be closed.
>>> _ = p3.mugshot.size
>>> p3.mugshot.closed
True

# Make sure that wrapping the file in a file still works
>>> p3.mugshot.file.open()
>>> p = Person.objects.create(name="<NAME>", mugshot=File(p3.mugshot.file))
>>> p.save()
>>> p3.mugshot.file.close() 

# Delete all test files
>>> shutil.rmtree(temp_storage_dir)
"""}
{'fraction_non_alphanumeric': 0.14525238263325096, 'fraction_numerical': 0.01729615248852806, 'mean_word_length': 4.019486271036316, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 194, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7049629', 'n_tokens_mistral': 1950, 'n_tokens_neox': 1885, 'n_words': 516}