Skip to content

Commit

Permalink
Reapply #659 (#661)
Browse files Browse the repository at this point in the history
Signed-off-by: Ivan Santiago Paunovic <ivanpauno@ekumenlabs.com>
  • Loading branch information
ivanpauno authored Aug 18, 2021
1 parent 0102a4e commit 1f4a3aa
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
22 changes: 22 additions & 0 deletions ros2cli/ros2cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@
# limitations under the License.

import argparse
import builtins
import functools
import signal
import sys

from ros2cli.command import add_subparsers_on_demand

Expand All @@ -29,6 +32,14 @@ def main(*, script_name='ros2', argv=None, description=None, extension=None):
description=description,
formatter_class=argparse.RawDescriptionHelpFormatter
)
parser.add_argument(
'--use-python-default-buffering',
action='store_true',
default=False,
help=(
'Do not force line buffering in stdout and instead use the python default buffering, '
'which might be affected by PYTHONUNBUFFERED/-u and depends on whatever stdout is '
'interactive or not'))

# add arguments for command extension(s)
if extension:
Expand All @@ -53,6 +64,17 @@ def main(*, script_name='ros2', argv=None, description=None, extension=None):
# parse the command line arguments
args = parser.parse_args(args=argv)

if not args.use_python_default_buffering:
# Make the output always line buffered.
# TextIoWrapper has a reconfigure() method, call that if available.
# https://docs.python.org/3/library/io.html#io.TextIOWrapper.reconfigure
try:
sys.stdout.reconfigure(line_buffering=True)
except AttributeError:
# if stdout is not a TextIoWrapper instance, or we're using python older than 3.7,
# force line buffering by patching print
builtins.print = functools.partial(print, flush=True)

if extension is None:
# get extension identified by the passed command (if available)
extension = getattr(args, selected_extension_key, None)
Expand Down
4 changes: 2 additions & 2 deletions ros2param/test/test_verb_dump.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ def test_verb_dump_print(self):
expected_lines=[
"WARNING: '--print' is deprecated; this utility prints to stdout by default"
],
text=param_dump_command.output,
text=param_dump_command.stderr,
strict=True
)

Expand All @@ -250,7 +250,7 @@ def test_verb_dump_output(self):
expected_lines=[
"WARNING: '--output-dir' is deprecated; use redirection to save to a file"
],
text=param_dump_command.output,
text=param_dump_command.stderr,
strict=True
)
# Compare generated parameter file against expected
Expand Down

0 comments on commit 1f4a3aa

Please sign in to comment.