def pytest_report_header(config): import sys s = "\nFull Python Version: \n{0}\n\n".format(sys.version) try: import warnings from astropy.utils.introspection import resolve_name except ImportError: return s for module_name in ('requests', 'numpy', 'astropy'): try: with warnings.catch_warnings(): warnings.simplefilter("ignore", DeprecationWarning) module = resolve_name(module_name) except ImportError: s += "{0}: not available\n".format(module_name) else: try: version = module.__version__ except AttributeError: version = 'unknown (no __version__ attribute)' s += "{0}: {1}\n".format(module_name, version) return s
def _read_table_iter(cls, f, format, **table_kw): names = f.tree["table_names"] # table type, for casting # so that QTableList can open a saved TableList correctly # TablesList specifies no type, so must rely on saved info if cls._types is None: table_type = f.tree["table_type"] if not isinstance(table_type, cabc.Sequence): table_type = [table_type] * len(names) ttypes = [resolve_name(t) for t in table_type] else: ttypes = [cls._types] * len(names) for i, name in enumerate(names): # order-preserving if isinstance(format, str): fmt = format else: fmt = format[i] # get kwargs for table writer # first get all general keys (by filtering) # then update with table-specific dictionary (if present) kw = { k: v for k, v in table_kw.items() if not k.startswith("table_") } kw.update(table_kw.get("table_" + name, {})) yield name, ttypes[i], fmt, kw
def _find_pkg_data_path(data_name, package=None): """ Look for data in the source-included data directories and return the path. """ if package is None: module = find_current_module( 1, finddiff=['astropy.utils.data', 'contextlib']) if module is None: # not called from inside an astropy package. So just pass name # through return data_name if not hasattr(module, '__package__') or not module.__package__: # The __package__ attribute may be missing or set to None; see # PEP-366, also astropy issue #1256 if '.' in module.__name__: package = module.__name__.rpartition('.')[0] else: package = module.__name__ else: package = module.__package__ else: module = resolve_name(package) rootpkgname = package.partition('.')[0] rootpkg = resolve_name(rootpkgname) module_path = os.path.dirname(module.__file__) path = os.path.join(module_path, data_name) root_dir = os.path.dirname(rootpkg.__file__) if not _is_inside(path, root_dir): raise RuntimeError("attempted to get a local data file outside " "of the {} tree.".format(rootpkgname)) return path
def _find_pkg_data_path(data_name, package=None): """ Look for data in the source-included data directories and return the path. """ if package is None: module = find_current_module(1, finddiff=['astropy.utils.data', 'contextlib']) if module is None: # not called from inside an astropy package. So just pass name # through return data_name if not hasattr(module, '__package__') or not module.__package__: # The __package__ attribute may be missing or set to None; see # PEP-366, also astropy issue #1256 if '.' in module.__name__: package = module.__name__.rpartition('.')[0] else: package = module.__name__ else: package = module.__package__ else: module = resolve_name(package) rootpkgname = package.partition('.')[0] rootpkg = resolve_name(rootpkgname) module_path = os.path.dirname(module.__file__) path = os.path.join(module_path, data_name) root_dir = os.path.dirname(rootpkg.__file__) if not _is_inside(path, root_dir): raise RuntimeError("attempted to get a local data file outside " "of the {} tree.".format(rootpkgname)) return path
def _infer_package( obj: T.Any, package: T.Union[ModuleType, str, None] = None, ) -> ModuleType: if inspect.ismodule(package): pass elif isinstance(package, str): package = resolve_name(package) elif package is None: # Need to get package from obj info = inspect.getmodule(obj) if info is None: # fails for c-compiled things package = obj.__class__.__module__ else: package = info.__package__ package = resolve_name(package.split(".")[0]) else: raise TypeError("package must be <module> or <str> or None.") return package
def test__infer_package(self): """Test method ``_infer_package``.""" # when key is None assert self.obj._infer_package(self.obj) == discO # when pass package # this overrides the package consideration assert self.obj._infer_package(None, package=pytest) == pytest # when pass package and it's a string # this overrides the package consideration assert self.obj._infer_package(None, package="pytest") == pytest # when package is None and object is c-compiled assert self.obj._infer_package(object(), package=None) == resolve_name( "builtins", ) # when fails with pytest.raises(TypeError): self.obj._infer_package(object(), package=TypeError)
def pytest_report_header(config): import sys import warnings from astropy.utils.introspection import resolve_name s = "\nFull Python Version: \n{0}\n\n".format(sys.version) for module_name in ('numpy', 'astropy', 'scipy', 'matplotlib', 'stsci.tools'): try: with warnings.catch_warnings(): warnings.simplefilter("ignore", DeprecationWarning) module = resolve_name(module_name) except ImportError: s += "{0}: not available\n".format(module_name) else: try: version = module.__version__ except AttributeError: version = 'unknown (no __version__ attribute)' s += "{0}: {1}\n".format(module_name, version) return s
def update_default_config(pkg, default_cfg_dir_or_fn, version=None): """ Checks if the configuration file for the specified package exists, and if not, copy over the default configuration. If the configuration file looks like it has already been edited, we do not write over it, but instead write a file alongside it named ``pkg.version.cfg`` as a "template" for the user. Parameters ---------- pkg : str The package to be updated. default_cfg_dir_or_fn : str The filename or directory name where the default configuration file is. If a directory name, ``'pkg.cfg'`` will be used in that directory. version : str, optional The current version of the given package. If not provided, it will be obtained from ``pkg.__version__``. Returns ------- updated : bool If the profile was updated, `True`, otherwise `False`. Raises ------ AttributeError If the version number of the package could not determined. """ if path.isdir(default_cfg_dir_or_fn): default_cfgfn = path.join(default_cfg_dir_or_fn, pkg + '.cfg') else: default_cfgfn = default_cfg_dir_or_fn if not path.isfile(default_cfgfn): # There is no template configuration file, which basically # means the affiliated package is not using the configuration # system, so just return. return False cfgfn = get_config(pkg).filename with open(default_cfgfn, 'rt', encoding='latin-1') as fr: template_content = fr.read() doupdate = False if cfgfn is not None: if path.exists(cfgfn): with open(cfgfn, 'rt', encoding='latin-1') as fd: content = fd.read() identical = (content == template_content) if not identical: doupdate = is_unedited_config_file(content, template_content) elif path.exists(path.dirname(cfgfn)): doupdate = True identical = False if version is None: version = resolve_name(pkg, '__version__') # Don't install template files for dev versions, or we'll end up # spamming `~/.astropy/config`. if 'dev' not in version and cfgfn is not None: template_path = path.join(get_config_dir(), f'{pkg}.{version}.cfg') needs_template = not path.exists(template_path) else: needs_template = False if doupdate or needs_template: if needs_template: with open(template_path, 'wt', encoding='latin-1') as fw: fw.write(template_content) # If we just installed a new template file and we can't # update the main configuration file because it has user # changes, display a warning. if not identical and not doupdate: warn( "The configuration options in {} {} may have changed, " "your configuration file was not updated in order to " "preserve local changes. A new configuration template " "has been saved to '{}'.".format(pkg, version, template_path), ConfigurationChangedWarning) if doupdate and not identical: with open(cfgfn, 'wt', encoding='latin-1') as fw: fw.write(template_content) return True return False
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 update_default_config(pkg, default_cfg_dir_or_fn, version=None): """ Checks if the configuration file for the specified package exists, and if not, copy over the default configuration. If the configuration file looks like it has already been edited, we do not write over it, but instead write a file alongside it named ``pkg.version.cfg`` as a "template" for the user. Parameters ---------- pkg : str The package to be updated. default_cfg_dir_or_fn : str The filename or directory name where the default configuration file is. If a directory name, ``'pkg.cfg'`` will be used in that directory. version : str, optional The current version of the given package. If not provided, it will be obtained from ``pkg.__version__``. Returns ------- updated : bool If the profile was updated, `True`, otherwise `False`. Raises ------ AttributeError If the version number of the package could not determined. """ if path.isdir(default_cfg_dir_or_fn): default_cfgfn = path.join(default_cfg_dir_or_fn, pkg + '.cfg') else: default_cfgfn = default_cfg_dir_or_fn if not path.isfile(default_cfgfn): # There is no template configuration file, which basically # means the affiliated package is not using the configuration # system, so just return. return False cfgfn = get_config(pkg).filename with open(default_cfgfn, 'rt', encoding='latin-1') as fr: template_content = fr.read() doupdate = False if cfgfn is not None: if path.exists(cfgfn): with open(cfgfn, 'rt', encoding='latin-1') as fd: content = fd.read() identical = (content == template_content) if not identical: doupdate = is_unedited_config_file( content, template_content) elif path.exists(path.dirname(cfgfn)): doupdate = True identical = False if version is None: version = resolve_name(pkg, '__version__') # Don't install template files for dev versions, or we'll end up # spamming `~/.astropy/config`. if 'dev' not in version and cfgfn is not None: template_path = path.join( get_config_dir(), '{0}.{1}.cfg'.format(pkg, version)) needs_template = not path.exists(template_path) else: needs_template = False if doupdate or needs_template: if needs_template: with open(template_path, 'wt', encoding='latin-1') as fw: fw.write(template_content) # If we just installed a new template file and we can't # update the main configuration file because it has user # changes, display a warning. if not identical and not doupdate: warn( "The configuration options in {0} {1} may have changed, " "your configuration file was not updated in order to " "preserve local changes. A new configuration template " "has been saved to '{2}'.".format( pkg, version, template_path), ConfigurationChangedWarning) if doupdate and not identical: with open(cfgfn, 'wt', encoding='latin-1') as fw: fw.write(template_content) return True return False
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 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