Skip to content
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
20 changes: 13 additions & 7 deletions reframe/utility/sanity.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,13 @@ def min(*args):


@deferrable
def print(*objects, sep=' ', end='\n', file=None, flush=False):
def print(obj, *, sep=' ', end='\n', file=None, flush=False):
'''Replacement for the built-in :func:`print() <python:print>` function.

The only difference is that this function returns the ``objects``, so that
you can use it transparently inside a complex sanity expression. For
example, you could write the following to print the matches returned from
the :func:`extractall()` function:
The only difference is that this function takes a *single* object argument
and it returns that, so that you can use it transparently inside a complex
sanity expression. For example, you could write the following to print the
matches returned from the :func:`extractall()` function:

.. code:: python

Expand All @@ -132,13 +132,19 @@ def print(*objects, sep=' ', end='\n', file=None, flush=False):
default. This would capture :attr:`sys.stdout` at the time this function
is defined and would prevent it from seeing changes to :attr:`sys.stdout`,
such as redirects, in the future.

.. versionchanged:: 3.4
This function accepts now a single object argument in contrast to the
built-in :func:`print() <python:print>` function, which accepts
multiple.

'''

if file is None:
file = sys.stdout

builtins.print(*objects, sep=sep, end=end, file=file, flush=flush)
return objects
builtins.print(obj, sep=sep, end=end, file=file, flush=flush)
return obj


@deferrable
Expand Down
28 changes: 12 additions & 16 deletions unittests/test_sanity_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,41 +148,37 @@ def test_min():
def test_print_stdout():
stdout = io.StringIO()
with contextlib.redirect_stdout(stdout):
x, y = sn.evaluate(sn.print(1, sn.defer(2)))
x = sn.evaluate(sn.print(sn.defer(2)))

assert stdout.getvalue() == '1 2\n'
assert x == 1
assert y == 2
assert stdout.getvalue() == '2\n'
assert x == 2


def test_print_stderr():
stderr = io.StringIO()
with contextlib.redirect_stderr(stderr):
x, y = sn.evaluate(sn.print(1, sn.defer(2), file=sys.stderr))
x = sn.evaluate(sn.print(sn.defer(2), file=sys.stderr))

assert stderr.getvalue() == '1 2\n'
assert x == 1
assert y == 2
assert stderr.getvalue() == '2\n'
assert x == 2


def test_print_separator():
stdout = io.StringIO()
with contextlib.redirect_stdout(stdout):
x, y = sn.evaluate(sn.print(1, sn.defer(2), sep='|'))
x = sn.evaluate(sn.print(sn.defer(2), sep='|'))

assert stdout.getvalue() == '1|2\n'
assert x == 1
assert y == 2
assert stdout.getvalue() == '2\n'
assert x == 2


def test_print_end():
stdout = io.StringIO()
with contextlib.redirect_stdout(stdout):
x, y = sn.evaluate(sn.print(1, sn.defer(2), end=''))
x = sn.evaluate(sn.print(sn.defer(2), end=''))

assert stdout.getvalue() == '1 2'
assert x == 1
assert y == 2
assert stdout.getvalue() == '2'
assert x == 2


def test_reversed():
Expand Down