def test_save_backup(self): """Test for https://aeon.stsci.edu/ssb/trac/pyfits/ticket/121 Save backup of file before flushing changes. """ self.copy_file('scale.fits') with ignore_warnings(): with fits.open(self.temp('scale.fits'), mode='update', save_backup=True) as hdul: # Make some changes to the original file to force its header # and data to be rewritten hdul[0].header['TEST'] = 'TEST' hdul[0].data[0] = 0 assert os.path.exists(self.temp('scale.fits.bak')) with fits.open(self.data('scale.fits'), do_not_scale_image_data=True) as hdul1: with fits.open(self.temp('scale.fits.bak'), do_not_scale_image_data=True) as hdul2: assert hdul1[0].header == hdul2[0].header assert (hdul1[0].data == hdul2[0].data).all() with ignore_warnings(): with fits.open(self.temp('scale.fits'), mode='update', save_backup=True) as hdul: # One more time to see if multiple backups are made hdul[0].header['TEST2'] = 'TEST' hdul[0].data[0] = 1 assert os.path.exists(self.temp('scale.fits.bak')) assert os.path.exists(self.temp('scale.fits.bak.1'))
def test_uint_columns(self, utype): """Test basic functionality of tables with columns containing pseudo-unsigned integers. See https://github.com/astropy/astropy/pull/906 """ bits = 8 * int(utype[1]) if platform.architecture()[0] == '64bit' or bits != 64: bzero = self.utype_map[utype](2**(bits - 1)) one = self.utype_map[utype](1) u0 = np.arange(bits + 1, dtype=self.utype_map[utype]) u = 2**u0 - one if bits == 64: u[63] = bzero - one u[64] = u[63] + u[63] + one uu = (u - bzero).view(self.itype_map[utype]) # Construct a table from explicit column col = fits.Column(name=utype, array=u, format=self.format_map[utype], bzero=bzero) table = fits.BinTableHDU.from_columns([col]) assert (table.data[utype] == u).all() # This used to be table.data.base, but now after adding a table to # a BinTableHDU it gets stored as a view of the original table, # even if the original was already a FITS_rec. So now we need # table.data.base.base assert (table.data.base.base[utype] == uu).all() hdu0 = fits.PrimaryHDU() hdulist = fits.HDUList([hdu0, table]) with ignore_warnings(): hdulist.writeto(self.temp('tempfile.fits'), overwrite=True) # Test write of unsigned int del hdulist with fits.open(self.temp('tempfile.fits'), uint=True) as hdulist2: hdudata = hdulist2[1].data assert (hdudata[utype] == u).all() assert (hdudata[utype].dtype == self.utype_map[utype]) assert (hdudata.base[utype] == uu).all() # Construct recarray then write out that. v = u.view(dtype=[(utype, self.utype_map[utype])]) with ignore_warnings(): fits.writeto(self.temp('tempfile2.fits'), v, overwrite=True) with fits.open(self.temp('tempfile2.fits'), uint=True) as hdulist3: hdudata3 = hdulist3[1].data assert ( hdudata3.base[utype] == table.data.base.base[utype]).all() assert (hdudata3[utype] == table.data[utype]).all() assert (hdudata3[utype] == u).all()
def test_uint_columns(self, utype): """Test basic functionality of tables with columns containing pseudo-unsigned integers. See https://github.com/astropy/astropy/pull/906 """ bits = 8*int(utype[1]) if platform.architecture()[0] == '64bit' or bits != 64: bzero = self.utype_map[utype](2**(bits-1)) one = self.utype_map[utype](1) u0 = np.arange(bits+1, dtype=self.utype_map[utype]) u = 2**u0 - one if bits == 64: u[63] = bzero - one u[64] = u[63] + u[63] + one uu = (u - bzero).view(self.itype_map[utype]) # Construct a table from explicit column col = fits.Column(name=utype, array=u, format=self.format_map[utype], bzero=bzero) table = fits.BinTableHDU.from_columns([col]) assert (table.data[utype] == u).all() # This used to be table.data.base, but now after adding a table to # a BinTableHDU it gets stored as a view of the original table, # even if the original was already a FITS_rec. So now we need # table.data.base.base assert (table.data.base.base[utype] == uu).all() hdu0 = fits.PrimaryHDU() hdulist = fits.HDUList([hdu0, table]) with ignore_warnings(): hdulist.writeto(self.temp('tempfile.fits'), overwrite=True) # Test write of unsigned int del hdulist with fits.open(self.temp('tempfile.fits'), uint=True) as hdulist2: hdudata = hdulist2[1].data assert (hdudata[utype] == u).all() assert (hdudata[utype].dtype == self.utype_map[utype]) assert (hdudata.base[utype] == uu).all() # Construct recarray then write out that. v = u.view(dtype=[(utype, self.utype_map[utype])]) with ignore_warnings(): fits.writeto(self.temp('tempfile2.fits'), v, overwrite=True) with fits.open(self.temp('tempfile2.fits'), uint=True) as hdulist3: hdudata3 = hdulist3[1].data assert (hdudata3.base[utype] == table.data.base.base[utype]).all() assert (hdudata3[utype] == table.data[utype]).all() assert (hdudata3[utype] == u).all()
def test_add_del_columns2(self): hdulist = fits.open(self.data('tb.fits')) table = hdulist[1] assert table.data.dtype.names == ('c1', 'c2', 'c3', 'c4') assert table.columns.names == ['c1', 'c2', 'c3', 'c4'] table.columns.del_col(str('c1')) assert table.data.dtype.names == ('c2', 'c3', 'c4') assert table.columns.names == ['c2', 'c3', 'c4'] table.columns.del_col(str('c3')) assert table.data.dtype.names == ('c2', 'c4') assert table.columns.names == ['c2', 'c4'] table.columns.add_col(fits.Column(str('foo'), str('3J'))) assert table.data.dtype.names == ('c2', 'c4', 'foo') assert table.columns.names == ['c2', 'c4', 'foo'] hdulist.writeto(self.temp('test.fits'), overwrite=True) with ignore_warnings(): # TODO: The warning raised by this test is actually indication of a # bug and should *not* be ignored. But as it is a known issue we # hide it for now. See # https://github.com/spacetelescope/PyFITS/issues/44 with fits.open(self.temp('test.fits')) as hdulist: table = hdulist[1] assert table.data.dtype.names == ('c2', 'c4', 'foo') assert table.columns.names == ['c2', 'c4', 'foo']
def test_uint(self, utype, compressed): bits = 8 * int(utype[1]) if platform.architecture()[0] == '64bit' or bits != 64: if compressed: hdu = fits.CompImageHDU( np.array([-3, -2, -1, 0, 1, 2, 3], dtype=np.int64)) hdu_number = 1 else: hdu = fits.PrimaryHDU( np.array([-3, -2, -1, 0, 1, 2, 3], dtype=np.int64)) hdu_number = 0 hdu.scale(f'int{bits:d}', '', bzero=2**(bits - 1)) with ignore_warnings(): hdu.writeto(self.temp('tempfile.fits'), overwrite=True) with fits.open(self.temp('tempfile.fits'), uint=True) as hdul: assert hdul[hdu_number].data.dtype == self.utype_map[utype] assert (hdul[hdu_number].data == np.array( [(2**bits) - 3, (2**bits) - 2, (2**bits) - 1, 0, 1, 2, 3], dtype=self.utype_map[utype])).all() hdul.writeto(self.temp('tempfile1.fits')) with fits.open(self.temp('tempfile1.fits'), uint16=True) as hdul1: d1 = hdul[hdu_number].data d2 = hdul1[hdu_number].data assert (d1 == d2).all() if not compressed: # TODO: Enable these lines if CompImageHDUs ever grow # .section support sec = hdul[hdu_number].section[:1] assert sec.dtype.name == f'uint{bits}' assert (sec == d1[:1]).all()
def test_replace_mmap_data(self): """Regression test for https://github.com/spacetelescope/PyFITS/issues/25 Replacing the mmap'd data of one file with mmap'd data from a different file should work. """ arr_a = np.arange(10) arr_b = arr_a * 2 def test(mmap_a, mmap_b): hdu_a = fits.PrimaryHDU(data=arr_a) hdu_a.writeto(self.temp('test_a.fits'), overwrite=True) hdu_b = fits.PrimaryHDU(data=arr_b) hdu_b.writeto(self.temp('test_b.fits'), overwrite=True) with fits.open(self.temp('test_a.fits'), mode='update', memmap=mmap_a) as hdul_a: with fits.open(self.temp('test_b.fits'), memmap=mmap_b) as hdul_b: hdul_a[0].data = hdul_b[0].data with fits.open(self.temp('test_a.fits')) as hdul_a: assert np.all(hdul_a[0].data == arr_b) with ignore_warnings(): test(True, True) # Repeat the same test but this time don't mmap A test(False, True) # Finally, without mmaping B test(True, False)
def test_add_del_columns2(self): hdulist = fits.open(self.data('tb.fits')) table = hdulist[1] assert table.data.dtype.names == ('c1', 'c2', 'c3', 'c4') assert table.columns.names == ['c1', 'c2', 'c3', 'c4'] table.columns.del_col('c1') assert table.data.dtype.names == ('c2', 'c3', 'c4') assert table.columns.names == ['c2', 'c3', 'c4'] table.columns.del_col('c3') assert table.data.dtype.names == ('c2', 'c4') assert table.columns.names == ['c2', 'c4'] table.columns.add_col(fits.Column('foo', '3J')) assert table.data.dtype.names == ('c2', 'c4', 'foo') assert table.columns.names == ['c2', 'c4', 'foo'] hdulist.writeto(self.temp('test.fits'), overwrite=True) with ignore_warnings(): # TODO: The warning raised by this test is actually indication of a # bug and should *not* be ignored. But as it is a known issue we # hide it for now. See # https://github.com/spacetelescope/PyFITS/issues/44 with fits.open(self.temp('test.fits')) as hdulist: table = hdulist[1] assert table.data.dtype.names == ('c2', 'c4', 'foo') assert table.columns.names == ['c2', 'c4', 'foo']
def test_uint(self, utype, compressed): bits = 8*int(utype[1]) if platform.architecture()[0] == '64bit' or bits != 64: if compressed: hdu = fits.CompImageHDU(np.array([-3, -2, -1, 0, 1, 2, 3], dtype=np.int64)) hdu_number = 1 else: hdu = fits.PrimaryHDU(np.array([-3, -2, -1, 0, 1, 2, 3], dtype=np.int64)) hdu_number = 0 hdu.scale('int{0:d}'.format(bits), '', bzero=2 ** (bits-1)) with ignore_warnings(): hdu.writeto(self.temp('tempfile.fits'), overwrite=True) with fits.open(self.temp('tempfile.fits'), uint=True) as hdul: assert hdul[hdu_number].data.dtype == self.utype_map[utype] assert (hdul[hdu_number].data == np.array( [(2 ** bits) - 3, (2 ** bits) - 2, (2 ** bits) - 1, 0, 1, 2, 3], dtype=self.utype_map[utype])).all() hdul.writeto(self.temp('tempfile1.fits')) with fits.open(self.temp('tempfile1.fits'), uint16=True) as hdul1: d1 = hdul[hdu_number].data d2 = hdul1[hdu_number].data assert (d1 == d2).all() if not compressed: # TODO: Enable these lines if CompImageHDUs ever grow # .section support sec = hdul[hdu_number].section[:1] assert sec.dtype.name == 'uint{}'.format(bits) assert (sec == d1[:1]).all()
def test_open_zipped(self): zip_file = self._make_zip_file() with ignore_warnings(): with fits.open(zip_file) as fits_handle: assert fits_handle._file.compression == 'zip' assert len(fits_handle) == 5 with fits.open(zipfile.ZipFile(zip_file)) as fits_handle: assert fits_handle._file.compression == 'zip' assert len(fits_handle) == 5
def test_read_file_like_object(self): """Test reading a FITS file from a file-like object.""" filelike = io.BytesIO() with open(self.data('test0.fits'), 'rb') as f: filelike.write(f.read()) filelike.seek(0) with ignore_warnings(): assert len(fits.open(filelike)) == 5
def test_open_bzipped(self): bzip_file = self._make_bzip2_file() with ignore_warnings(): with fits.open(bzip_file) as fits_handle: assert fits_handle._file.compression == 'bzip2' assert len(fits_handle) == 5 with fits.open(bz2.BZ2File(bzip_file)) as fits_handle: assert fits_handle._file.compression == 'bzip2' assert len(fits_handle) == 5
def __init__(self, filename='$GAMMA_CAT/docs/data/gammacat.fits.gz'): filename = str(make_path(filename)) with ignore_warnings(): # ignore FITS units warnings table = Table.read(filename, hdu=1) self.filename = filename source_name_key = 'common_name' source_name_alias = ('other_names', 'gamma_names') super(SourceCatalogGammaCat, self).__init__( table=table, source_name_key=source_name_key, source_name_alias=source_name_alias, )
def test_open_file_with_end_padding(self): """Regression test for https://aeon.stsci.edu/ssb/trac/pyfits/ticket/106 Open files with end padding bytes. """ hdul = fits.open(self.data('test0.fits'), do_not_scale_image_data=True) info = hdul.info(output=False) hdul.writeto(self.temp('temp.fits')) with open(self.temp('temp.fits'), 'ab') as f: f.seek(0, os.SEEK_END) f.write(b'\0' * 2880) with ignore_warnings(): assert info == fits.info(self.temp('temp.fits'), output=False, do_not_scale_image_data=True)
def __init__(self, filename=None, hdu='HGPS_SOURCES'): if not filename: filename = Path(os.environ['HGPS_ANALYSIS'] ) / 'data/catalogs/HGPS3/release/HGPS_v0.4.fits' filename = str(make_path(filename)) with ignore_warnings(): # ignore FITS units warnings table = Table.read(filename, hdu=hdu) source_name_alias = ('Identified_Object', ) super(SourceCatalogHGPS, self).__init__( table=table, source_name_alias=source_name_alias, ) self.components = Table.read(filename, hdu='HGPS_GAUSS_COMPONENTS') self.associations = Table.read(filename, hdu='HGPS_ASSOCIATIONS') self.identifications = Table.read(filename, hdu='HGPS_IDENTIFICATIONS')
def test_replace_mmap_data_2(self): """Regression test for https://github.com/spacetelescope/PyFITS/issues/25 Replacing the mmap'd data of one file with mmap'd data from a different file should work. Like test_replace_mmap_data but with table data instead of image data. """ arr_a = np.arange(10) arr_b = arr_a * 2 def test(mmap_a, mmap_b): col_a = fits.Column(name='a', format='J', array=arr_a) col_b = fits.Column(name='b', format='J', array=arr_b) hdu_a = fits.BinTableHDU.from_columns([col_a]) hdu_a.writeto(self.temp('test_a.fits'), overwrite=True) hdu_b = fits.BinTableHDU.from_columns([col_b]) hdu_b.writeto(self.temp('test_b.fits'), overwrite=True) hdul_a = fits.open(self.temp('test_a.fits'), mode='update', memmap=mmap_a) hdul_b = fits.open(self.temp('test_b.fits'), memmap=mmap_b) hdul_a[1].data = hdul_b[1].data hdul_a.close() hdul_b.close() hdul_a = fits.open(self.temp('test_a.fits')) assert 'b' in hdul_a[1].columns.names assert 'a' not in hdul_a[1].columns.names assert np.all(hdul_a[1].data['b'] == arr_b) with ignore_warnings(): test(True, True) # Repeat the same test but this time don't mmap A test(False, True) # Finally, without mmaping B test(True, False)
def __init__(self, filename='$GAMMA_CAT/docs/data/gammacat.fits.gz'): filename = str(make_path(filename)) if 'GAMMA_CAT' not in os.environ: msg = 'The gamma-cat repo is not available. ' msg += 'You have to set the GAMMA_CAT environment variable ' msg += 'to point to the location for it to be found.' raise GammaCatNotFoundError(msg) with ignore_warnings(): # ignore FITS units warnings table = Table.read(filename, hdu=1) self.filename = filename source_name_key = 'common_name' source_name_alias = ('other_names', 'gamma_names') super(SourceCatalogGammaCat, self).__init__( table=table, source_name_key=source_name_key, source_name_alias=source_name_alias, )
def __init__( self, filename='$GAMMAPY_EXTRA/datasets/catalogs/fermi/gll_psch_v07.fit.gz' ): filename = str(make_path(filename)) with ignore_warnings(): # ignore FITS units warnings table = Table.read(filename, hdu='LAT_Point_Source_Catalog') table_standardise(table) source_name_key = 'Source_Name' source_name_alias = ('ASSOC1', 'ASSOC2', 'ASSOC_TEV', 'ASSOC_GAM') super(SourceCatalog1FHL, self).__init__( table=table, source_name_key=source_name_key, source_name_alias=source_name_alias, ) self.extended_sources_table = Table.read(filename, hdu='ExtendedSources')
def test_replace_mmap_data_2(self): """Regression test for https://github.com/spacetelescope/PyFITS/issues/25 Replacing the mmap'd data of one file with mmap'd data from a different file should work. Like test_replace_mmap_data but with table data instead of image data. """ arr_a = np.arange(10) arr_b = arr_a * 2 def test(mmap_a, mmap_b): col_a = fits.Column(name='a', format='J', array=arr_a) col_b = fits.Column(name='b', format='J', array=arr_b) hdu_a = fits.BinTableHDU.from_columns([col_a]) hdu_a.writeto(self.temp('test_a.fits'), overwrite=True) hdu_b = fits.BinTableHDU.from_columns([col_b]) hdu_b.writeto(self.temp('test_b.fits'), overwrite=True) with fits.open(self.temp('test_a.fits'), mode='update', memmap=mmap_a) as hdul_a: with fits.open(self.temp('test_b.fits'), memmap=mmap_b) as hdul_b: hdul_a[1].data = hdul_b[1].data with fits.open(self.temp('test_a.fits')) as hdul_a: assert 'b' in hdul_a[1].columns.names assert 'a' not in hdul_a[1].columns.names assert np.all(hdul_a[1].data['b'] == arr_b) with ignore_warnings(): test(True, True) # Repeat the same test but this time don't mmap A test(False, True) # Finally, without mmaping B test(True, False)
def __init__( self, filename='$GAMMAPY_EXTRA/datasets/catalogs/fermi/gll_psch_v08.fit.gz' ): filename = str(make_path(filename)) with ignore_warnings(): # ignore FITS units warnings table = Table.read(filename, hdu='2FHL Source Catalog') table_standardise(table) source_name_key = 'Source_Name' source_name_alias = ('ASSOC', '3FGL_Name', '1FHL_Name', 'TeVCat_Name') super(SourceCatalog2FHL, self).__init__( table=table, source_name_key=source_name_key, source_name_alias=source_name_alias, ) self.counts_image = SkyImage.read(filename, hdu='Count Map') self.extended_sources_table = Table.read(filename, hdu='Extended Sources') self.rois = Table.read(filename, hdu='ROIs')
def pytest_report_header(config): if not ASTROPY_INSTALLED: return # If the astropy display plugin is registered, we stop now and let it # handle the header. if ASTROPY_LT_40 and config.pluginmanager.hasplugin( 'astropy.tests.plugins.display'): return if not config.getoption("astropy_header") and not config.getini( "astropy_header"): return astropy_header_packages_option = config.getoption( "astropy_header_packages") astropy_header_packages_ini = config.getini("astropy_header_packages") if astropy_header_packages_option is not None: if isinstance(astropy_header_packages_option, str): astropy_header_packages_option = [ x.strip() for x in astropy_header_packages_option.split(',') ] packages_to_display = OrderedDict([ (x, x) for x in astropy_header_packages_option ]) elif len(astropy_header_packages_ini) > 0: if len(astropy_header_packages_ini) == 1: astropy_header_packages_ini = [ x.strip() for x in astropy_header_packages_ini[0].split(',') ] packages_to_display = OrderedDict([ (x, x) for x in astropy_header_packages_ini ]) else: packages_to_display = PYTEST_HEADER_MODULES try: stdoutencoding = sys.stdout.encoding or 'ascii' except AttributeError: stdoutencoding = 'ascii' args = config.args # TESTED_VERSIONS can contain the affiliated package version, too if len(TESTED_VERSIONS) > 1: for pkg, version in TESTED_VERSIONS.items(): if pkg not in ['Astropy', 'astropy_helpers']: s = "\nRunning tests with {} version {}.\n".format( pkg, version) else: s = "\nRunning tests with Astropy version {}.\n".format( TESTED_VERSIONS['Astropy']) # Per https://github.com/astropy/astropy/pull/4204, strip the rootdir from # each directory argument if hasattr(config, 'rootdir'): rootdir = str(config.rootdir) if not rootdir.endswith(os.sep): rootdir += os.sep dirs = [ arg[len(rootdir):] if arg.startswith(rootdir) else arg for arg in args ] else: dirs = args s += "Running tests in {}.\n\n".format(" ".join(dirs)) s += "Date: {}\n\n".format(datetime.datetime.now().isoformat()[:19]) from platform import platform plat = platform() if isinstance(plat, bytes): plat = plat.decode(stdoutencoding, 'replace') s += "Platform: {plat}\n\n".format(plat=plat) s += "Executable: {executable}\n\n".format(executable=sys.executable) s += "Full Python Version: \n{version}\n\n".format(version=sys.version) s += "encodings: sys: {}, locale: {}, filesystem: {}".format( sys.getdefaultencoding(), locale.getpreferredencoding(), sys.getfilesystemencoding()) s += '\n' s += "byteorder: {byteorder}\n".format(byteorder=sys.byteorder) s += "float info: dig: {0.dig}, mant_dig: {0.dig}\n\n".format( sys.float_info) s += "Package versions: \n" for module_display, module_name in packages_to_display.items(): try: with ignore_warnings(DeprecationWarning): module = resolve_name(module_name) except ImportError: s += "{module_display}: not available\n".format( module_display=module_display) else: try: version = module.__version__ except AttributeError: version = 'unknown (no __version__ attribute)' s += "{module_display}: {version}\n".format( module_display=module_display, version=version) # Helpers version if 'astropy_helpers' in TESTED_VERSIONS: astropy_helpers_version = TESTED_VERSIONS['astropy_helpers'] else: try: from astropy.version import astropy_helpers_version except ImportError: astropy_helpers_version = None if astropy_helpers_version: s += "astropy-helpers: {astropy_helpers_version}\n".format( astropy_helpers_version=astropy_helpers_version) s += "\n" special_opts = ["remote_data", "pep8"] opts = [] for op in special_opts: op_value = getattr(config.option, op, None) if op_value: if isinstance(op_value, str): op = ': '.join((op, op_value)) opts.append(op) if opts: s += "Using Astropy options: {}.\n".format(", ".join(opts)) return s
def test_detect_bzipped(self): """Test detection of a bzip2 file when the extension is not .bz2.""" with ignore_warnings(): with fits.open(self._make_bzip2_file('test0.xx')) as fits_handle: assert fits_handle._file.compression == 'bzip2' assert len(fits_handle) == 5
def test_detect_gzipped(self): """Test detection of a gzip file when the extension is not .gz.""" with ignore_warnings(): with fits.open(self._make_gzip_file('test0.fz')) as fits_handle: assert fits_handle._file.compression == 'gzip' assert len(fits_handle) == 5
def pytest_report_header(config): try: stdoutencoding = sys.stdout.encoding or 'ascii' except AttributeError: stdoutencoding = 'ascii' args = config.args # TESTED_VERSIONS can contain the affiliated package version, too if len(TESTED_VERSIONS) > 1: for pkg, version in TESTED_VERSIONS.items(): if pkg not in ['Astropy', 'astropy_helpers']: s = "\nRunning tests with {0} version {1}.\n".format( pkg, version) else: s = "\nRunning tests with Astropy version {0}.\n".format( TESTED_VERSIONS['Astropy']) # Per https://github.com/astropy/astropy/pull/4204, strip the rootdir from # each directory argument if hasattr(config, 'rootdir'): rootdir = str(config.rootdir) if not rootdir.endswith(os.sep): rootdir += os.sep dirs = [arg[len(rootdir):] if arg.startswith(rootdir) else arg for arg in args] else: dirs = args s += "Running tests in {0}.\n\n".format(" ".join(dirs)) s += "Date: {0}\n\n".format(datetime.datetime.now().isoformat()[:19]) from platform import platform plat = platform() if isinstance(plat, bytes): plat = plat.decode(stdoutencoding, 'replace') s += "Platform: {0}\n\n".format(plat) s += "Executable: {0}\n\n".format(sys.executable) s += "Full Python Version: \n{0}\n\n".format(sys.version) s += "encodings: sys: {0}, locale: {1}, filesystem: {2}".format( sys.getdefaultencoding(), locale.getpreferredencoding(), sys.getfilesystemencoding()) s += '\n' s += "byteorder: {0}\n".format(sys.byteorder) s += "float info: dig: {0.dig}, mant_dig: {0.dig}\n\n".format( sys.float_info) for module_display, module_name in PYTEST_HEADER_MODULES.items(): try: with ignore_warnings(DeprecationWarning): module = resolve_name(module_name) except ImportError: s += "{0}: not available\n".format(module_display) else: try: version = module.__version__ except AttributeError: version = 'unknown (no __version__ attribute)' s += "{0}: {1}\n".format(module_display, version) # Helpers version if 'astropy_helpers' in TESTED_VERSIONS: astropy_helpers_version = TESTED_VERSIONS['astropy_helpers'] else: try: from astropy.version import astropy_helpers_version except ImportError: astropy_helpers_version = None if astropy_helpers_version: s += "astropy_helpers: {0}\n".format(astropy_helpers_version) special_opts = ["remote_data", "pep8"] opts = [] for op in special_opts: op_value = getattr(config.option, op, None) if op_value: if isinstance(op_value, str): op = ': '.join((op, op_value)) opts.append(op) if opts: s += "Using Astropy options: {0}.\n".format(", ".join(opts)) return s
def test_detect_zipped(self): """Test detection of a zip file when the extension is not .zip.""" zf = self._make_zip_file(filename='test0.fz') with ignore_warnings(): assert len(fits.open(zf)) == 5
def pytest_report_header(config): try: stdoutencoding = sys.stdout.encoding or 'ascii' except AttributeError: stdoutencoding = 'ascii' args = config.args # TESTED_VERSIONS can contain the affiliated package version, too if len(TESTED_VERSIONS) > 1: for pkg, version in TESTED_VERSIONS.items(): if pkg not in ['Astropy', 'astropy_helpers']: s = "\nRunning tests with {0} version {1}.\n".format( pkg, version) else: s = "\nRunning tests with Astropy version {0}.\n".format( TESTED_VERSIONS['Astropy']) # Per https://github.com/astropy/astropy/pull/4204, strip the rootdir from # each directory argument if hasattr(config, 'rootdir'): rootdir = str(config.rootdir) if not rootdir.endswith(os.sep): rootdir += os.sep dirs = [ arg[len(rootdir):] if arg.startswith(rootdir) else arg for arg in args ] else: dirs = args s += "Running tests in {0}.\n\n".format(" ".join(dirs)) s += "Date: {0}\n\n".format(datetime.datetime.now().isoformat()[:19]) from platform import platform plat = platform() if isinstance(plat, bytes): plat = plat.decode(stdoutencoding, 'replace') s += "Platform: {0}\n\n".format(plat) s += "Executable: {0}\n\n".format(sys.executable) s += "Full Python Version: \n{0}\n\n".format(sys.version) s += "encodings: sys: {0}, locale: {1}, filesystem: {2}".format( sys.getdefaultencoding(), locale.getpreferredencoding(), sys.getfilesystemencoding()) s += '\n' s += "byteorder: {0}\n".format(sys.byteorder) s += "float info: dig: {0.dig}, mant_dig: {0.dig}\n\n".format( sys.float_info) for module_display, module_name in PYTEST_HEADER_MODULES.items(): try: with ignore_warnings(DeprecationWarning): module = resolve_name(module_name) except ImportError: s += "{0}: not available\n".format(module_display) else: try: version = module.__version__ except AttributeError: version = 'unknown (no __version__ attribute)' s += "{0}: {1}\n".format(module_display, version) # Helpers version if 'astropy_helpers' in TESTED_VERSIONS: astropy_helpers_version = TESTED_VERSIONS['astropy_helpers'] else: try: from astropy.version import astropy_helpers_version except ImportError: astropy_helpers_version = None if astropy_helpers_version: s += "astropy_helpers: {0}\n".format(astropy_helpers_version) special_opts = ["remote_data", "pep8"] opts = [] for op in special_opts: op_value = getattr(config.option, op, None) if op_value: if isinstance(op_value, str): op = ': '.join((op, op_value)) opts.append(op) if opts: s += "Using Astropy options: {0}.\n".format(", ".join(opts)) return s