diff --git a/src/execnet/gateway_base.py b/src/execnet/gateway_base.py index 9ba25b42..252bdbcb 100644 --- a/src/execnet/gateway_base.py +++ b/src/execnet/gateway_base.py @@ -207,6 +207,9 @@ def get_ident(self) -> int: def sleep(self, delay: float) -> None: import eventlet +# f = open("/tmp/execnet-%s" % os.getpid(), "w") +# def log_extra(*msg): +# f.write(" ".join([str(x) for x in msg]) + "\n") eventlet.sleep(delay) diff --git a/src/execnet/gateway_bootstrap.py b/src/execnet/gateway_bootstrap.py index e9d7efe1..d2cd5f1d 100644 --- a/src/execnet/gateway_bootstrap.py +++ b/src/execnet/gateway_bootstrap.py @@ -3,6 +3,7 @@ from __future__ import annotations import inspect +import json import os import execnet @@ -25,13 +26,13 @@ def bootstrap_import(io: IO, spec: XSpec) -> None: sendexec( io, "import sys", - "if %r not in sys.path:" % importdir, - " sys.path.insert(0, %r)" % importdir, + f"if {importdir!r} not in sys.path:", + f" sys.path.insert(0, {importdir!r})", "from execnet.gateway_base import serve, init_popen_io, get_execmodel", "sys.stdout.write('1')", "sys.stdout.flush()", - "execmodel = get_execmodel(%r)" % spec.execmodel, - "serve(init_popen_io(execmodel), id='%s-worker')" % spec.id, + f"execmodel = get_execmodel({spec.execmodel!r})", + f"serve(init_popen_io(execmodel), id='{spec.id}-worker')", ) s = io.read(1) assert s == b"1", repr(s) @@ -77,7 +78,8 @@ def bootstrap_socket(io: IO, id) -> None: def sendexec(io: IO, *sources: str) -> None: source = "\n".join(sources) - io.write((repr(source) + "\n").encode("utf-8")) + encoded = (json.dumps(source) + "\n").encode("utf-8") + io.write(encoded) def bootstrap(io: IO, spec: XSpec) -> execnet.Gateway: diff --git a/src/execnet/gateway_io.py b/src/execnet/gateway_io.py index 21285ab4..27cc8ddc 100644 --- a/src/execnet/gateway_io.py +++ b/src/execnet/gateway_io.py @@ -48,7 +48,7 @@ def kill(self) -> None: sys.stderr.flush() -popen_bootstrapline = "import sys;exec(eval(sys.stdin.readline()))" +popen_bootstrapline = "import sys;import json;exec(json.loads(sys.stdin.readline()))" def shell_split_path(path: str) -> list[str]: diff --git a/testing/test_basics.py b/testing/test_basics.py index 76d2aab7..11adeacd 100644 --- a/testing/test_basics.py +++ b/testing/test_basics.py @@ -2,6 +2,7 @@ from __future__ import annotations import inspect +import json import os import subprocess import sys @@ -93,7 +94,9 @@ def receive() -> str: try: source = inspect.getsource(read_write_loop) + "read_write_loop()" - send(repr(source) + "\n") + repr_source = json.dumps(source) + "\n" + sendline = repr_source + send(sendline) s = receive() assert s == "ok\n" send("hello\n")