<commit_before>from __future__ import division, with_statement  # confidence high

import contextlib
import sys
import warnings

import numpy as np

import pyfits as fits
from ..hdu.base import _ValidHDU
from . import PyfitsTestCase
from .test_table import comparerecords


class TestChecksumFunctions(PyfitsTestCase):

    # All checksums have been verified against CFITSIO
    def setup(self):
        super(TestChecksumFunctions, self).setup()
        self._oldfilters = warnings.filters[:]
        warnings.filterwarnings(
            'error',
            message='Checksum verification failed')
        warnings.filterwarnings(
            'error',
            message='Datasum verification failed')

        # Monkey-patch the _get_timestamp method so that the checksum
        # timestamps (and hence the checksum themselves) are always the same
        self._old_get_timestamp = _ValidHDU._get_timestamp
        _ValidHDU._get_timestamp = lambda self: '2013-12-20T13:36:10'

    def teardown(self):
        super(TestChecksumFunctions, self).teardown()
        warnings.filters = self._oldfilters
        _ValidHDU._get_timestamp = self._old_get_timestamp

    def test_sample_file(self):
        hdul = fits.open(self.data('checksum.fits'), checksum=True)
        hdul.close()

    def test_image_create(self):
        n = np.arange(100, dtype=np.int64)
        hdu = fits.PrimaryHDU(n)
        hdu.writeto(self.temp('tmp.fits'), clobber=True, checksum=True)
        with fits.open(self.temp('tmp.fits'), checksum=True) as hdul:
            assert (hdu.data == hdul[0].data).all()
            assert 'CHECKSUM' in hdul[0].header
            assert 'DATASUM' in hdul[0].header

            if not sys.platform.startswith('win32'):
                assert hdul[0].header['CHECKSUM'] == 'ZHMkeGKjZGKjbGKj'
                assert hdul[0].header['DATASUM'] == '4950'

    def test_nonstandard_checksum(self):
        hdu = fits.PrimaryHDU(np.arange(10.0 ** 6, dtype=np.float64))
        hdu.writeto(self.temp('tmp.fits'), clobber=True,
                    checksum='nonstandard')
        del hdu
        with fits.open(self.temp('tmp.fits'), checksum='nonstandard') as hdul:
            assert 'CHECKSUM' in hdul[0].header
            assert 'DATASUM' in hdul[0].header

            if not sys.platform.startswith('win32'):
                assert hdul[0].header['CHECKSUM'] == 'jD4Am942jC48j948'
                assert hdul[0].header['DATASUM'] == '4164005614'

    def test_scaled_data(self):
        with fits.open(self.data('scale.fits')) as hdul:
            orig_data = hdul[0].data.copy()
            hdul[0].scale('int16', 'old')
            hdul.writeto(self.temp('tmp.fits'), clobber=True, checksum=True)
            with fits.open(self.temp('tmp.fits'), checksum=True) as hdul1:
                assert (hdul1[0].data == orig_data).all()
                assert 'CHECKSUM' in hdul1[0].header
                assert hdul1[0].header['CHECKSUM'] == 'cUmaeUjZcUjacUjW'
                assert 'DATASUM' in hdul1[0].header
                assert hdul1[0].header['DATASUM'] == '1891563534'

    def test_scaled_data_auto_rescale(self):
        """
        Regression test for
        https://github.com/astropy/astropy/issues/3883#issuecomment-115122647

        Ensure that when scaled data is automatically rescaled on
        opening/writing a file that the checksum and datasum are computed for
        the rescaled array.
        """

        with fits.open(self.data('scale.fits')) as hdul:
            # Write out a copy of the data with the rescaling applied
            hdul.writeto(self.temp('rescaled.fits'))

        # Reopen the new file and save it back again with a checksum
        with fits.open(self.temp('rescaled.fits')) as hdul:
            hdul.writeto(self.temp('rescaled.fits'), clobber=True,
                         checksum=True)

        # Now do like in the first writeto but use checksum immediately
        with fits.open(self.data('scale.fits')) as hdul:
            hdul.writeto(self.temp('rescaled2.fits'), checksum=True)

        # Also don't rescale the data but add a checksum
        with fits.open(self.data('scale.fits'),
                       do_not_scale_image_data=True) as hdul:
            hdul.writeto(self.temp('scaled.fits'), checksum=True)

        # Must used nested with statements to support older Python versions
        # (but contextlib.nested is not available in newer Pythons :(
        with fits.open(self.temp('rescaled.fits')) as hdul1:
            with fits.open(self.temp('rescaled2.fits')) as hdul2:
                with fits.open(self.temp('scaled.fits')) as hdul3:
                    hdr1 = hdul1[0].header
                    hdr2 = hdul2[0].header
                    hdr3 = hdul3[0].header
                    assert hdr1['DATASUM'] == hdr2['DATASUM']
                    assert hdr1['CHECKSUM'] == hdr2['CHECKSUM']<commit_msg>Slightly rewrite this test to not run afoul of the bug on Windows that an HDUList can't overwrite the file it was read from while that file is still open for reading.<commit_after>from __future__ import division, with_statement  # confidence high

import contextlib
import sys
import warnings

import numpy as np

import pyfits as fits
from ..hdu.base import _ValidHDU
from . import PyfitsTestCase
from .test_table import comparerecords


class TestChecksumFunctions(PyfitsTestCase):

    # All checksums have been verified against CFITSIO
    def setup(self):
        super(TestChecksumFunctions, self).setup()
        self._oldfilters = warnings.filters[:]
        warnings.filterwarnings(
            'error',
            message='Checksum verification failed')
        warnings.filterwarnings(
            'error',
            message='Datasum verification failed')

        # Monkey-patch the _get_timestamp method so that the checksum
        # timestamps (and hence the checksum themselves) are always the same
        self._old_get_timestamp = _ValidHDU._get_timestamp
        _ValidHDU._get_timestamp = lambda self: '2013-12-20T13:36:10'

    def teardown(self):
        super(TestChecksumFunctions, self).teardown()
        warnings.filters = self._oldfilters
        _ValidHDU._get_timestamp = self._old_get_timestamp

    def test_sample_file(self):
        hdul = fits.open(self.data('checksum.fits'), checksum=True)
        hdul.close()

    def test_image_create(self):
        n = np.arange(100, dtype=np.int64)
        hdu = fits.PrimaryHDU(n)
        hdu.writeto(self.temp('tmp.fits'), clobber=True, checksum=True)
        with fits.open(self.temp('tmp.fits'), checksum=True) as hdul:
            assert (hdu.data == hdul[0].data).all()
            assert 'CHECKSUM' in hdul[0].header
            assert 'DATASUM' in hdul[0].header

            if not sys.platform.startswith('win32'):
                assert hdul[0].header['CHECKSUM'] == 'ZHMkeGKjZGKjbGKj'
                assert hdul[0].header['DATASUM'] == '4950'

    def test_nonstandard_checksum(self):
        hdu = fits.PrimaryHDU(np.arange(10.0 ** 6, dtype=np.float64))
        hdu.writeto(self.temp('tmp.fits'), clobber=True,
                    checksum='nonstandard')
        del hdu
        with fits.open(self.temp('tmp.fits'), checksum='nonstandard') as hdul:
            assert 'CHECKSUM' in hdul[0].header
            assert 'DATASUM' in hdul[0].header

            if not sys.platform.startswith('win32'):
                assert hdul[0].header['CHECKSUM'] == 'jD4Am942jC48j948'
                assert hdul[0].header['DATASUM'] == '4164005614'

    def test_scaled_data(self):
        with fits.open(self.data('scale.fits')) as hdul:
            orig_data = hdul[0].data.copy()
            hdul[0].scale('int16', 'old')
            hdul.writeto(self.temp('tmp.fits'), clobber=True, checksum=True)
            with fits.open(self.temp('tmp.fits'), checksum=True) as hdul1:
                assert (hdul1[0].data == orig_data).all()
                assert 'CHECKSUM' in hdul1[0].header
                assert hdul1[0].header['CHECKSUM'] == 'cUmaeUjZcUjacUjW'
                assert 'DATASUM' in hdul1[0].header
                assert hdul1[0].header['DATASUM'] == '1891563534'

    def test_scaled_data_auto_rescale(self):
        """
        Regression test for
        https://github.com/astropy/astropy/issues/3883#issuecomment-115122647

        Ensure that when scaled data is automatically rescaled on
        opening/writing a file that the checksum and datasum are computed for
        the rescaled array.
        """

        with fits.open(self.data('scale.fits')) as hdul:
            # Write out a copy of the data with the rescaling applied
            hdul.writeto(self.temp('rescaled.fits'))

        # Reopen the new file and save it back again with a checksum
        with fits.open(self.temp('rescaled.fits')) as hdul:
            hdul.writeto(self.temp('rescaled2.fits'), clobber=True,
                         checksum=True)

        # Now do like in the first writeto but use checksum immediately
        with fits.open(self.data('scale.fits')) as hdul:
            hdul.writeto(self.temp('rescaled3.fits'), checksum=True)

        # Also don't rescale the data but add a checksum
        with fits.open(self.data('scale.fits'),
                       do_not_scale_image_data=True) as hdul:
            hdul.writeto(self.temp('scaled.fits'), checksum=True)

        # Must used nested with statements to support older Python versions
        # (but contextlib.nested is not available in newer Pythons :(
        with fits.open(self.temp('rescaled2.fits')) as hdul1:
            with fits.open(self.temp('rescaled3.fits')) as hdul2:
                with fits.open(self.temp('scaled.fits')) as hdul3:
                    hdr1 = hdul1[0].header
                    hdr2 = hdul2[0].header
                    hdr3 = hdul3[0].header
                    assert hdr1['DATASUM'] == hdr2['DATASUM']
                    assert hdr1['CHECKSUM'] == hdr2['CHECKSUM']
{'fraction_non_alphanumeric': 0.11007331525559907, 'fraction_numerical': 0.023701918248468415, 'mean_word_length': 2.409106470386854, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4414235', 'n_tokens_mistral': 3309, 'n_tokens_neox': 3060, 'n_words': 704}