|
11 | 11 |
|
12 | 12 | from ..verify import VerifyError |
13 | 13 | from ....extern.six.moves import range |
| 14 | +from ....extern import six |
14 | 15 | from ....io import fits |
15 | 16 | from ....tests.helper import raises, catch_warnings, ignore_warnings |
16 | 17 | from ....utils.exceptions import AstropyUserWarning, AstropyDeprecationWarning |
@@ -913,3 +914,53 @@ def test_proper_error_raised_on_non_fits_file_with_unicode(self): |
913 | 914 | # This should raise an IOError because there is no end card. |
914 | 915 | with pytest.raises(IOError): |
915 | 916 | fits.open(filename) |
| 917 | + |
| 918 | + @pytest.mark.skipif(six.PY2, |
| 919 | + reason='ResourceWarning is not created in Python 2') |
| 920 | + def test_no_resource_warning_raised_on_non_fits_file(self): |
| 921 | + """ |
| 922 | + Regression test for https://github.com/astropy/astropy/issues/6168 |
| 923 | +
|
| 924 | + The ResourceWarning shows up when (in python 3+) you try to |
| 925 | + open a non-FITS file when using a filename. |
| 926 | + """ |
| 927 | + |
| 928 | + # To avoid creating the file multiple times the tests are |
| 929 | + # all included in one test file. See the discussion to the |
| 930 | + # PR at https://github.com/astropy/astropy/issues/6168 |
| 931 | + # |
| 932 | + filename = self.temp('not-fits.fits') |
| 933 | + with open(filename, mode='w') as f: |
| 934 | + f.write('# header line\n') |
| 935 | + f.write('0.1 0.2\n') |
| 936 | + |
| 937 | + # Opening the file should raise an OSError however the file |
| 938 | + # is opened (there are two distinct code paths, depending on |
| 939 | + # whether ignore_missing_end is True or False). |
| 940 | + # |
| 941 | + # Explicit tests are added to make sure the file handle is not |
| 942 | + # closed when passed in to fits.open. In this case the ResourceWarning |
| 943 | + # was not raised, but a check is still included. |
| 944 | + # |
| 945 | + with catch_warnings(ResourceWarning) as ws: |
| 946 | + |
| 947 | + # Make sure that files opened by the user are not closed |
| 948 | + with open(filename, mode='rb') as f: |
| 949 | + with pytest.raises(OSError): |
| 950 | + fits.open(f, ignore_missing_end=False) |
| 951 | + |
| 952 | + assert not f.closed |
| 953 | + |
| 954 | + with open(filename, mode='rb') as f: |
| 955 | + with pytest.raises(OSError): |
| 956 | + fits.open(f, ignore_missing_end=True) |
| 957 | + |
| 958 | + assert not f.closed |
| 959 | + |
| 960 | + with pytest.raises(OSError): |
| 961 | + fits.open(filename, ignore_missing_end=False) |
| 962 | + |
| 963 | + with pytest.raises(OSError): |
| 964 | + fits.open(filename, ignore_missing_end=True) |
| 965 | + |
| 966 | + assert len(ws) == 0 |
0 commit comments