Skip to content

Don't install sage/tests/cmdline.py with meson #40252

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 25, 2025
Merged
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
2 changes: 1 addition & 1 deletion src/sage/repl/interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -722,7 +722,7 @@ def get_test_shell():

Check that :issue:`14070` has been resolved::

sage: from sage.tests.cmdline import test_executable
sage: from sage.tests import test_executable
sage: cmd = 'from sage.repl.interpreter import get_test_shell; shell = get_test_shell()'
sage: (out, err, ret) = test_executable(["sage", "-c", cmd])
sage: out + err
Expand Down
117 changes: 117 additions & 0 deletions src/sage/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,0 +1,117 @@
from subprocess import Popen, PIPE
import os
import sys
import select


def test_executable(args, input='', timeout=100.0, pydebug_ignore_warnings=False, **kwds):
r"""
Run the program defined by ``args`` using the string ``input`` on
the standard input.

INPUT:

- ``args`` -- list of program arguments, the first being the
executable

- ``input`` -- string serving as standard input; usually, this
should end with a newline

- ``timeout`` -- if the program produces no output for ``timeout``
seconds, a :exc:`RuntimeError` is raised

- ``pydebug_ignore_warnings`` -- boolean. Set the PYTHONWARNINGS environment variable to ignore
Python warnings when on a Python debug build (`--with-pydebug`, e.g. from building with
`SAGE_DEBUG=yes`). Debug builds do not install the default warning filters, which can break
some doctests. Unfortunately the environment variable does not support regex message filters,
so the filter will catch a bit more than the default filters. Hence we only enable it on debug
builds.

- ``**kwds`` -- additional keyword arguments passed to the
:class:`Popen` constructor

OUTPUT: a tuple ``(out, err, ret)`` with the standard output,
standard error and exitcode of the program run.

EXAMPLES::

sage: from sage.tests import test_executable
sage: (out, err, ret) = test_executable(["cat"], "Hello World!")
sage: out
'Hello World!'
sage: err
''
sage: ret
0

We test the timeout option::

sage: (out, err, ret) = test_executable(["sleep", "1"], timeout=0.1)
Traceback (most recent call last):
...
RuntimeError: timeout in test_executable()
"""
pexpect_env = dict(os.environ)
try:
del pexpect_env["TERM"]
except KeyError:
pass

__with_pydebug = hasattr(sys, 'gettotalrefcount') # This is a Python debug build (--with-pydebug)
if __with_pydebug and pydebug_ignore_warnings:
pexpect_env['PYTHONWARNINGS'] = ','.join([

Check warning on line 62 in src/sage/tests/__init__.py

View check run for this annotation

Codecov / codecov/patch

src/sage/tests/__init__.py#L62

Added line #L62 was not covered by tests
'ignore::DeprecationWarning',
])

kwds['encoding'] = kwds.pop('encoding', 'utf-8')

p = Popen(args, stdin=PIPE, stdout=PIPE, stderr=PIPE, env=pexpect_env,
**kwds)
if input:
p.stdin.write(input)

p.stdin.close()
fdout = p.stdout.fileno()
fderr = p.stderr.fileno()
out = []
err = []

while True:
# Try reading from fdout and fderr
rfd = []
if fdout:
rfd.append(fdout)
if fderr:
rfd.append(fderr)
if len(rfd) == 0:
break
timeout = float(timeout)
rlist = select.select(rfd, [], [], timeout)[0]

if len(rlist) == 0:
# Timeout!
p.terminate()
raise RuntimeError("timeout in test_executable()")
if fdout in rlist:
s = p.stdout.read(1024)
if not s:
fdout = None # EOF
p.stdout.close()
out.append(s)
if fderr in rlist:
s = p.stderr.read(1024)
if not s:
fderr = None # EOF
p.stderr.close()
err.append(s)

# In case out or err contains a quoted string, force the use of
# double quotes so that the output is enclosed in single
# quotes. This avoids some doctest failures with some versions of
# OS X and Xcode.
out = ''.join(out)
out = out.replace("'", '"')
err = ''.join(err)
err = err.replace("'", '"')

return (out, err, p.wait())
Loading
Loading