CommandPipeline: clean repr + a bit of code cleaning (#5369)

* XONSH_TRACE_SUBPROC returns more useful details.

* update

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Name obj to thread because it's thread!

* update

* wip

* wip

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: a <1@1.1>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
Andy Kipp 2024-05-02 20:39:31 +02:00 committed by GitHub
parent c4c7d1cbd7
commit a5f0308a5a
Failed to generate hash of commit
4 changed files with 48 additions and 25 deletions

23
news/mini_refactor.rst Normal file
View file

@ -0,0 +1,23 @@
**Added:**
* <news item>
**Changed:**
* The CommandPipeline repr will not show descriptors by default. Use XONSH_DEBUG mode to see them.
**Deprecated:**
* <news item>
**Removed:**
* <news item>
**Fixed:**
* <news item>
**Security:**
* <news item>

View file

@ -264,13 +264,13 @@ else:
# Return when there are no foreground active task
if active_task is None:
return last_task
obj = active_task["obj"]
thread = active_task["obj"]
backgrounded = False
try:
if obj.pid is None:
if thread.pid is None:
# When the process stopped before os.waitpid it has no pid.
raise ChildProcessError("The process PID not found.")
_, wcode = os.waitpid(obj.pid, os.WUNTRACED)
_, wcode = os.waitpid(thread.pid, os.WUNTRACED)
except ChildProcessError as e: # No child processes
if return_error:
return e
@ -283,11 +283,11 @@ else:
backgrounded = True
elif os.WIFSIGNALED(wcode):
print() # get a newline because ^C will have been printed
obj.signal = (os.WTERMSIG(wcode), os.WCOREDUMP(wcode))
obj.returncode = None
thread.signal = (os.WTERMSIG(wcode), os.WCOREDUMP(wcode))
thread.returncode = None
else:
obj.returncode = os.WEXITSTATUS(wcode)
obj.signal = None
thread.returncode = os.WEXITSTATUS(wcode)
thread.signal = None
return wait_for_active_job(last_task=active_task, backgrounded=backgrounded)

View file

@ -93,23 +93,26 @@ class CommandPipeline:
"""Represents a subprocess-mode command pipeline."""
attrnames = (
"stdin",
"stdout",
"stderr",
"pid",
"returncode",
"pid",
"args",
"alias",
"stdin_redirect",
"stdout_redirect",
"stderr_redirect",
"timestamps",
"executed_cmd",
"timestamps",
"input",
"output",
"errors",
)
attrnames_ext = (
"stdin",
"stdout",
"stderr",
"stdin_redirect",
"stdout_redirect",
"stderr_redirect",
)
nonblocking = (io.BytesIO, NonBlockingFDReader, ConsoleParallelReader)
def __init__(self, specs):
@ -183,8 +186,11 @@ class CommandPipeline:
self.proc = self.procs[-1]
def __repr__(self):
attrs = self.attrnames + (
self.attrnames_ext if XSH.env.get("XONSH_DEBUG", False) else ()
)
s = self.__class__.__name__ + "(\n "
s += ",\n ".join(a + "=" + repr(getattr(self, a)) for a in self.attrnames)
s += ",\n ".join(a + "=" + repr(getattr(self, a)) for a in attrs)
s += "\n)"
return s

View file

@ -591,7 +591,6 @@ class SubprocSpec:
spec.resolve_args_list()
# perform initial redirects
spec.resolve_redirects()
# apply aliases
spec.resolve_alias()
spec.resolve_binary_loc()
spec.resolve_auto_cd()
@ -691,16 +690,11 @@ class SubprocSpec:
if not callable(alias):
return
self.is_proxy = True
env = XSH.env
thable = env.get("THREAD_SUBPROCS") and getattr(
self.threadable = XSH.env.get("THREAD_SUBPROCS") and getattr(
alias, "__xonsh_threadable__", True
)
cls = ProcProxyThread if thable else ProcProxy
self.cls = cls
self.threadable = thable
# also check capturability, while we are here
cpable = getattr(alias, "__xonsh_capturable__", self.captured)
self.captured = cpable
self.cls = ProcProxyThread if self.threadable else ProcProxy
self.captured = getattr(alias, "__xonsh_capturable__", self.captured)
def resolve_stack(self):
"""Computes the stack for a callable alias's call-site, if needed."""