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 # Return when there are no foreground active task
if active_task is None: if active_task is None:
return last_task return last_task
obj = active_task["obj"] thread = active_task["obj"]
backgrounded = False backgrounded = False
try: try:
if obj.pid is None: if thread.pid is None:
# When the process stopped before os.waitpid it has no pid. # When the process stopped before os.waitpid it has no pid.
raise ChildProcessError("The process PID not found.") 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 except ChildProcessError as e: # No child processes
if return_error: if return_error:
return e return e
@ -283,11 +283,11 @@ else:
backgrounded = True backgrounded = True
elif os.WIFSIGNALED(wcode): elif os.WIFSIGNALED(wcode):
print() # get a newline because ^C will have been printed print() # get a newline because ^C will have been printed
obj.signal = (os.WTERMSIG(wcode), os.WCOREDUMP(wcode)) thread.signal = (os.WTERMSIG(wcode), os.WCOREDUMP(wcode))
obj.returncode = None thread.returncode = None
else: else:
obj.returncode = os.WEXITSTATUS(wcode) thread.returncode = os.WEXITSTATUS(wcode)
obj.signal = None thread.signal = None
return wait_for_active_job(last_task=active_task, backgrounded=backgrounded) 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.""" """Represents a subprocess-mode command pipeline."""
attrnames = ( attrnames = (
"stdin",
"stdout",
"stderr",
"pid",
"returncode", "returncode",
"pid",
"args", "args",
"alias", "alias",
"stdin_redirect",
"stdout_redirect",
"stderr_redirect",
"timestamps",
"executed_cmd", "executed_cmd",
"timestamps",
"input", "input",
"output", "output",
"errors", "errors",
) )
attrnames_ext = (
"stdin",
"stdout",
"stderr",
"stdin_redirect",
"stdout_redirect",
"stderr_redirect",
)
nonblocking = (io.BytesIO, NonBlockingFDReader, ConsoleParallelReader) nonblocking = (io.BytesIO, NonBlockingFDReader, ConsoleParallelReader)
def __init__(self, specs): def __init__(self, specs):
@ -183,8 +186,11 @@ class CommandPipeline:
self.proc = self.procs[-1] self.proc = self.procs[-1]
def __repr__(self): def __repr__(self):
attrs = self.attrnames + (
self.attrnames_ext if XSH.env.get("XONSH_DEBUG", False) else ()
)
s = self.__class__.__name__ + "(\n " 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)" s += "\n)"
return s return s

View file

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