def test_open_file_with_bad_header_padding(self): """ Regression test for #136; open files with nulls for header block padding instead of spaces. """ a = np.arange(100).reshape((10, 10)) hdu = pyfits.PrimaryHDU(data=a) hdu.writeto(self.temp('temp.fits')) # Figure out where the header padding begins and fill it with nulls end_card_pos = str(hdu.header).index('END' + ' ' * 77) padding_start = end_card_pos + 80 padding_len = 2880 - padding_start with open(self.temp('temp.fits'), 'r+b') as f: f.seek(padding_start) f.write('\0'.encode('ascii') * padding_len) with catch_warnings(record=True) as w: with pyfits.open(self.temp('temp.fits')) as hdul: assert_true('contains null bytes instead of spaces' in str(w[0].message)) assert_equal(len(w), 1) assert_equal(len(hdul), 1) assert_equal(str(hdul[0].header), str(hdu.header)) assert_true((hdul[0].data == a).all())
def test(): with catch_warnings(record=True) as w: pid = os.getpid() os.kill(pid, signal.SIGINT) # One more time, for good measure os.kill(pid, signal.SIGINT) assert len(w) == 2 assert (str(w[0].message) == 'KeyboardInterrupt ignored until test is ' 'complete!')
def test_verification_on_output(self): # verification on output # make a defect HDUList first err_text = "HDUList's 0th element is not a primary HDU." with catch_warnings(record=True) as w: x = pyfits.ImageHDU() # HDUList can take a list or one single HDU hdu = pyfits.HDUList(x) with CaptureStdio(): hdu.verify() assert_equal(len(w), 3) assert_true(err_text in str(w[1].message)) fix_text = err_text + " Fixed by inserting one as 0th HDU." with catch_warnings(record=True) as w: with CaptureStdio(): hdu.writeto(self.temp('test_new2.fits'), 'fix') assert_equal(len(w), 3) assert_true(fix_text in str(w[1].message))
def test_flush_readonly(self): """Test flushing changes to a file opened in a read only mode.""" oldmtime = os.stat(self.data('test0.fits')).st_mtime hdul = pyfits.open(self.data('test0.fits')) hdul[0].header['FOO'] = 'BAR' with catch_warnings(record=True) as w: hdul.flush() assert_equal(len(w), 1) assert_true('mode is not supported' in str(w[0].message)) assert_equal(oldmtime, os.stat(self.data('test0.fits')).st_mtime)
def test_ignore_verification_error(self): hdu = pyfits.ImageHDU() # The default here would be to issue a warning; ensure that no warnings # or exceptions are raised with catch_warnings(): warnings.simplefilter("error") del hdu.header["NAXIS"] try: hdu.verify("ignore") except Exception, e: self.fail("An exception occurred when the verification error " "should have been ignored: %s" % e)
def test_ignore_verification_error(self): hdu = fits.ImageHDU() # The default here would be to issue a warning; ensure that no warnings # or exceptions are raised with catch_warnings(): warnings.simplefilter('error') del hdu.header['NAXIS'] try: hdu.verify('ignore') except Exception, e: self.fail('An exception occurred when the verification error ' 'should have been ignored: %s' % e)
def test_disable_image_compression(self): with catch_warnings(): # No warnings should be displayed in this case warnings.simplefilter('error') with pyfits.open(self.data('comp.fits'), disable_image_compression=True) as hdul: # The compressed image HDU should show up as a BinTableHDU, but # *not* a CompImageHDU assert_true(isinstance(hdul[1], pyfits.BinTableHDU)) assert_false(isinstance(hdul[1], pyfits.CompImageHDU)) with pyfits.open(self.data('comp.fits')) as hdul: assert_true(isinstance(hdul[1], pyfits.CompImageHDU))
def test_section(self): # section testing fs = pyfits.open(self.data('arange.fits')) with catch_warnings(record=True) as w: assert_equal(fs[0].section[3, 2, 5], np.array([357])) assert_equal(len(w), 0)
def test_hdu_get_size(self): with catch_warnings(record=True) as w: t1 = pyfits.open(self.data('tb.fits')) assert_equal(len(w), 0)
def test_card_with_continue(self): h = pyfits.PrimaryHDU() with catch_warnings(record=True) as w: h.header['abc'] = 'abcdefg' * 20 assert_equal(len(w), 0)
def test_section(self): # section testing fs = fits.open(self.data('arange.fits')) with catch_warnings(record=True) as w: assert np.all(fs[0].section[3, 2, 5] == np.array([357])) assert len(w) == 0