Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 26 additions & 35 deletions src/sage/doctest/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is cute but we should probably use os.path.isdir() or not os.path.exists().

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
Expand Down
13 changes: 8 additions & 5 deletions src/sage/misc/dev_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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__:
Expand Down Expand Up @@ -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)
Expand Down
Loading