From cb6f3e11a05c15095bd5589dbb0b21c7548d1a55 Mon Sep 17 00:00:00 2001 From: Ronny Pfannschmidt Date: Mon, 16 Jan 2023 00:35:37 +0100 Subject: [PATCH] fixups ball of mud --- pyproject.toml | 2 +- src/execnet/gateway_base.py | 21 ++++++++++++--------- testing/test_xspec.py | 9 ++------- 3 files changed, 15 insertions(+), 17 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 9d85fc47..90c0b9d7 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -80,7 +80,7 @@ html = "sphinx-build -W -b html doc doc/_build" [tool.hatch.envs.pre-commit] detached = true -dependences = ["pre-commit>1.11.0"] +dependences = ["pre-commit>=2.20.0"] [tool.hatch.envs.pre-commit.scripts] all = "pre-commit run --all-files --show-diff-on-failure" diff --git a/src/execnet/gateway_base.py b/src/execnet/gateway_base.py index 11962db7..a53e5ec5 100644 --- a/src/execnet/gateway_base.py +++ b/src/execnet/gateway_base.py @@ -1,7 +1,7 @@ """ base execnet gateway code send to the other side for bootstrapping. -NOTE: aims to be compatible to Python 2.5-3.X, Jython and IronPython +NOTE: aims to be compatible to Python 3.8+ :copyright: 2004-2015 :authors: @@ -153,6 +153,8 @@ class Reply: through WorkerPool.spawn() """ + _exception: BaseException | None = None + def __init__(self, task, threadmodel): self.task = task self._result_ready = threadmodel.Event() @@ -165,10 +167,10 @@ def get(self, timeout=None): including its traceback. """ self.waitfinish(timeout) - try: + if self._exception is None: return self._result - except AttributeError: - reraise(*(self._excinfo[:3])) # noqa + else: + raise self._exception.with_traceback(self._exception.__traceback__) def waitfinish(self, timeout=None): if not self._result_ready.wait(timeout): @@ -179,10 +181,9 @@ def run(self): try: try: self._result = func(*args, **kwargs) - except: + except BaseException as e: # sys may be already None when shutting down the interpreter - if sys is not None: - self._excinfo = sys.exc_info() + self._exception = e finally: self._result_ready.set() self.running = False @@ -348,7 +349,9 @@ def __init__(self, outfile, infile, execmodel): except (AttributeError, OSError): pass self._read = getattr(infile, "buffer", infile).read - self._write = getattr(outfile, "buffer", outfile).write + _outfile = getattr(outfile, "buffer", outfile) + self._write = _outfile.write + self._flush = _outfile.flush self.execmodel = execmodel def read(self, numbytes): @@ -366,7 +369,7 @@ def write(self, data): """write out all data bytes.""" assert isinstance(data, bytes) self._write(data) - self.outfile.flush() + self._flush() def close_read(self): self.infile.close() diff --git a/testing/test_xspec.py b/testing/test_xspec.py index a841dfb6..64756a58 100644 --- a/testing/test_xspec.py +++ b/testing/test_xspec.py @@ -8,6 +8,7 @@ import execnet import pytest from execnet.gateway_io import popen_args +from execnet.gateway_io import popen_bootstrapline from execnet.gateway_io import ssh_args from execnet.gateway_io import vagrant_ssh_args @@ -77,13 +78,7 @@ def test_vagrant_options(self): def test_popen_with_sudo_python(self): spec = XSpec("popen//python=sudo python3") - assert popen_args(spec) == [ - "sudo", - "python3", - "-u", - "-c", - "import sys;exec(eval(sys.stdin.readline()))", - ] + assert popen_args(spec) == ["sudo", "python3", "-u", "-c", popen_bootstrapline] def test_env(self): xspec = XSpec("popen//env:NAME=value1")