<commit_before># Licensed under a 3-clause BSD style license - see PYFITS.rst

import errno
import gzip
import io
import mmap
import os
import pathlib
import shutil
import sys
import urllib.request
import zipfile
from unittest.mock import patch

import numpy as np
import pytest

from astropy.io import fits
from astropy.io.fits.convenience import _getext
from astropy.io.fits.diff import FITSDiff
from astropy.io.fits.file import GZIP_MAGIC, _File
from astropy.io.tests import safeio
from astropy.utils import data
# NOTE: Python can be built without bz2.
from astropy.utils.compat.optional_deps import HAS_BZ2
from astropy.utils.data import conf
from astropy.utils.exceptions import AstropyUserWarning
from astropy.utils.misc import _NOT_OVERWRITING_MSG_MATCH

from . import FitsTestCase

if HAS_BZ2:
    import bz2


class TestCore(FitsTestCase):

    def test_missing_file(self):
        with pytest.raises(OSError):
            fits.open(self.temp('does-not-exist.fits'))

    def test_naxisj_check(self):
        with fits.open(self.data('o4sp040b0_raw.fits')) as hdulist:
            hdulist[1].header['NAXIS3'] = 500

            assert 'NAXIS3' in hdulist[1].header
            hdulist.verify('silentfix')
            assert 'NAXIS3' not in hdulist[1].header

    def test_byteswap(self):
        p = fits.PrimaryHDU()
        lst = fits.HDUList()

        n = np.zeros(3, dtype='i2')
        n[0] = 1
        n[1] = 60000
        n[2] = 2

        c = fits.Column(name='foo', format='i2', bscale=1, bzero=32768,
                        array=n)
        t = fits.BinTableHDU.from_columns([c])

        lst.append(p)
        lst.append(t)

        lst.writeto(self.temp('test.fits'), overwrite=True)

        with fits.open(self.temp('test.fits')) as p:
            assert p[1].data[1]['foo'] == 60000.0

    def test_fits_file_path_object(self):
        """
        Testing when fits file is passed as pathlib.Path object #4412.
        """
        fpath = pathlib.Path(self.data('tdim.fits'))
        with fits.open(fpath) as hdulist:
            assert hdulist[0].filebytes() == 2880
            assert hdulist[1].filebytes() == 5760

            with fits.open(self.data('tdim.fits')) as hdulist2:
                assert FITSDiff(hdulist2, hdulist).identical is True

    def test_fits_pathlike_object(self):
        """
        Testing when fits file is passed as os.PathLike object #11579.
        """
        class TPath(os.PathLike):<commit_msg>Fix NumPy deprecation of setting `np.int16` with out-of-bound value<commit_after># Licensed under a 3-clause BSD style license - see PYFITS.rst

import errno
import gzip
import io
import mmap
import os
import pathlib
import shutil
import sys
import urllib.request
import zipfile
from unittest.mock import patch

import numpy as np
import pytest

from astropy.io import fits
from astropy.io.fits.convenience import _getext
from astropy.io.fits.diff import FITSDiff
from astropy.io.fits.file import GZIP_MAGIC, _File
from astropy.io.tests import safeio
from astropy.utils import data
# NOTE: Python can be built without bz2.
from astropy.utils.compat.optional_deps import HAS_BZ2
from astropy.utils.data import conf
from astropy.utils.exceptions import AstropyUserWarning
from astropy.utils.misc import _NOT_OVERWRITING_MSG_MATCH

from . import FitsTestCase

if HAS_BZ2:
    import bz2


class TestCore(FitsTestCase):

    def test_missing_file(self):
        with pytest.raises(OSError):
            fits.open(self.temp('does-not-exist.fits'))

    def test_naxisj_check(self):
        with fits.open(self.data('o4sp040b0_raw.fits')) as hdulist:
            hdulist[1].header['NAXIS3'] = 500

            assert 'NAXIS3' in hdulist[1].header
            hdulist.verify('silentfix')
            assert 'NAXIS3' not in hdulist[1].header

    def test_byteswap(self):
        p = fits.PrimaryHDU()
        lst = fits.HDUList()

        n = np.array([1, 60000, 0]).astype('i2')

        c = fits.Column(name='foo', format='i2', bscale=1, bzero=32768,
                        array=n)
        t = fits.BinTableHDU.from_columns([c])

        lst.append(p)
        lst.append(t)

        lst.writeto(self.temp('test.fits'), overwrite=True)

        with fits.open(self.temp('test.fits')) as p:
            assert p[1].data[1]['foo'] == 60000.0

    def test_fits_file_path_object(self):
        """
        Testing when fits file is passed as pathlib.Path object #4412.
        """
        fpath = pathlib.Path(self.data('tdim.fits'))
        with fits.open(fpath) as hdulist:
            assert hdulist[0].filebytes() == 2880
            assert hdulist[1].filebytes() == 5760

            with fits.open(self.data('tdim.fits')) as hdulist2:
                assert FITSDiff(hdulist2, hdulist).identical is True

    def test_fits_pathlike_object(self):
        """
        Testing when fits file is passed as os.PathLike object #11579.
        """
        class TPath(os.PathLike):
{'fraction_non_alphanumeric': 0.10356708552898257, 'fraction_numerical': 0.026753141467369273, 'mean_word_length': 2.7471526195899774, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, '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': '650010', 'n_tokens_mistral': 1775, 'n_tokens_neox': 1629, 'n_words': 404}