mirror of
https://github.com/xonsh/xonsh.git
synced 2025-03-04 08:24:40 +01:00
feat: Added environment variable to hide unwanted printing the stderr when using captured object. (#4221)
* Added XONSH_SUBPROC_CAPTURED_PRINT_STDERR * black Co-authored-by: a <a@a.a>
This commit is contained in:
parent
16884fc605
commit
e7b0977910
4 changed files with 53 additions and 6 deletions
23
news/subproc_captured_print_stderr.rst
Normal file
23
news/subproc_captured_print_stderr.rst
Normal file
|
@ -0,0 +1,23 @@
|
|||
**Added:**
|
||||
|
||||
* Added XONSH_SUBPROC_CAPTURED_PRINT_STDERR environment variable to hide unwanted printing the stderr when using captured object.
|
||||
|
||||
**Changed:**
|
||||
|
||||
* <news item>
|
||||
|
||||
**Deprecated:**
|
||||
|
||||
* <news item>
|
||||
|
||||
**Removed:**
|
||||
|
||||
* <news item>
|
||||
|
||||
**Fixed:**
|
||||
|
||||
* <news item>
|
||||
|
||||
**Security:**
|
||||
|
||||
* <news item>
|
|
@ -174,6 +174,22 @@ print(x.returncode)
|
|||
"hallo on err\n1\n",
|
||||
0,
|
||||
),
|
||||
# test captured streaming alias without stderr
|
||||
(
|
||||
"""
|
||||
def _test_stream(args, stdin, stdout, stderr):
|
||||
print('hallo on err', file=stderr)
|
||||
print('hallo on out', file=stdout)
|
||||
return 1
|
||||
|
||||
aliases['test-stream'] = _test_stream
|
||||
with __xonsh__.env.swap(XONSH_SUBPROC_CAPTURED_PRINT_STDERR=False):
|
||||
x = !(test-stream)
|
||||
print(x.returncode)
|
||||
""",
|
||||
"1\n",
|
||||
0,
|
||||
),
|
||||
# test piping aliases
|
||||
(
|
||||
"""
|
||||
|
|
|
@ -857,6 +857,10 @@ class GeneralSetting(Xettings):
|
|||
"should cause an end to execution. This is less useful at a terminal. "
|
||||
"The error that is raised is a ``subprocess.CalledProcessError``.",
|
||||
)
|
||||
XONSH_SUBPROC_CAPTURED_PRINT_STDERR = Var.with_default(
|
||||
True,
|
||||
"If ``True`` the stderr from captured subproc will be printed automatically.",
|
||||
)
|
||||
TERM = Var.no_default(
|
||||
"str",
|
||||
"TERM is sometimes set by the terminal emulator. This is used (when "
|
||||
|
|
|
@ -396,12 +396,16 @@ class CommandPipeline:
|
|||
if self.stderr_postfix:
|
||||
b += self.stderr_postfix
|
||||
stderr_has_buffer = hasattr(sys.stderr, "buffer")
|
||||
# write bytes to std stream
|
||||
if stderr_has_buffer:
|
||||
sys.stderr.buffer.write(b)
|
||||
else:
|
||||
sys.stderr.write(b.decode(encoding=enc, errors=err))
|
||||
sys.stderr.flush()
|
||||
show_stderr = self.captured != "object" or env.get(
|
||||
"XONSH_SUBPROC_CAPTURED_PRINT_STDERR", True
|
||||
)
|
||||
if show_stderr:
|
||||
# write bytes to std stream
|
||||
if stderr_has_buffer:
|
||||
sys.stderr.buffer.write(b)
|
||||
else:
|
||||
sys.stderr.write(b.decode(encoding=enc, errors=err))
|
||||
sys.stderr.flush()
|
||||
# save the raw bytes
|
||||
self._raw_error = b
|
||||
# do some munging of the line before we save it to the attr
|
||||
|
|
Loading…
Add table
Reference in a new issue