diff --git a/src/sage/doctest/__main__.py b/src/sage/doctest/__main__.py index 42a80f78a1a..5b2014bfc92 100644 --- a/src/sage/doctest/__main__.py +++ b/src/sage/doctest/__main__.py @@ -3,6 +3,8 @@ import sys import shlex +import pytest + # Note: the DOT_SAGE and SAGE_STARTUP_FILE environment variables have already been set by sage-env DOT_SAGE = os.environ.get('DOT_SAGE', os.path.join(os.environ.get('HOME'), '.sage')) @@ -159,6 +161,9 @@ def __call__(self, parser, namespace, values, option_string=None): def main(): + from sage.doctest.control import DocTestController + from sage.env import SAGE_SRC + parser = _make_parser() # custom treatment to separate properly # one or several file names at the end @@ -191,44 +196,30 @@ def main(): os.environ["SAGE_NUM_THREADS"] = "2" - from sage.doctest.control import DocTestController DC = DocTestController(args, args.filenames) err = DC.run() - # Issue #33521: Do not run pytest if the pytest configuration is not available. - # This happens when the source tree is not available and SAGE_SRC falls back - # to SAGE_LIB. - from sage.env import SAGE_SRC - if not all(os.path.isfile(os.path.join(SAGE_SRC, f)) - for f in ["conftest.py", "tox.ini"]): - return err - - try: - exit_code_pytest = 0 - import pytest - pytest_options = [] - if args.verbose: - pytest_options.append("-v") - - # #35999: no filename in arguments defaults to "src" - if not args.filenames: - filenames = [SAGE_SRC] - else: - # #31924: Do not run pytest on individual Python files unless - # they match the pytest file pattern. However, pass names - # of directories. We use 'not os.path.isfile(f)' for this so that - # we do not silently hide typos. - filenames = [f for f in args.filenames - if f.endswith("_test.py") or not os.path.isfile(f)] - if filenames: - print(f"Running pytest on {filenames} with options {pytest_options}") - exit_code_pytest = pytest.main(filenames + pytest_options) - if exit_code_pytest == 5: - # Exit code 5 means there were no test files, pass in this case - exit_code_pytest = 0 - - except ModuleNotFoundError: - print("pytest is not installed in the venv, skip checking tests that rely on it") + exit_code_pytest = 0 + pytest_options = [] + if args.verbose: + pytest_options.append("-v") + + # #35999: no filename in arguments defaults to "src" + if not args.filenames: + filenames = [SAGE_SRC] + else: + # #31924: Do not run pytest on individual Python files unless + # they match the pytest file pattern. However, pass names + # of directories. We use 'not os.path.isfile(f)' for this so that + # we do not silently hide typos. + filenames = [f for f in args.filenames + if f.endswith("_test.py") or not os.path.isfile(f)] + if filenames: + print(f"Running pytest on {filenames} with options {pytest_options}") + exit_code_pytest = pytest.main(filenames + pytest_options) + if exit_code_pytest == 5: + # Exit code 5 means there were no test files, pass in this case + exit_code_pytest = 0 if err == 0: return exit_code_pytest diff --git a/src/sage/misc/dev_tools.py b/src/sage/misc/dev_tools.py index 90f41f8b0f4..31bc87bdb93 100644 --- a/src/sage/misc/dev_tools.py +++ b/src/sage/misc/dev_tools.py @@ -64,10 +64,11 @@ def runsnake(command): - :class:`Profiler` """ import cProfile - from sage.misc.temporary_file import tmp_filename + from sage.misc.misc import get_main_globals - from sage.repl.preparse import preparse from sage.misc.superseded import deprecation + from sage.misc.temporary_file import tmp_filename + from sage.repl.preparse import preparse deprecation(39274, "just use the runsnake program directly") @@ -176,9 +177,10 @@ def load_submodules(module=None, exclude_pattern=None): load sage.geometry.polyhedron.palp_database... succeeded load sage.geometry.polyhedron.ppl_lattice_polygon... succeeded """ - from .package_dir import walk_packages import importlib + from .package_dir import walk_packages + if module is None: import sage module = sage @@ -276,7 +278,7 @@ def find_objects_from_name(name, module_name=None, include_lazy_imports=False): from sage.misc.lazy_import import LazyImport obj = [] - for smodule_name, smodule in sys.modules.items(): + for smodule_name, smodule in list(sys.modules.items()): if module_name and not smodule_name.startswith(module_name): continue if hasattr(smodule, '__dict__') and name in smodule.__dict__: @@ -462,8 +464,9 @@ def import_statements(*objects, **kwds): detect deprecated stuff). So, if you use it, double check the answer and report weird behaviors. """ - import itertools import inspect + import itertools + from sage.misc.lazy_import import LazyImport answer = defaultdict(list)