Skip to content

Commit

Permalink
move subprocess exec env to a re-usable function
Browse files Browse the repository at this point in the history
use it with opengl probe so it won't fail on weird / binary environment variables
  • Loading branch information
totaam committed Apr 30, 2024
1 parent f2e9e4b commit d567ce9
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 15 deletions.
17 changes: 4 additions & 13 deletions xpra/net/subprocess_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
from typing import Any

from xpra.gtk.signals import register_os_signals
from xpra.util.str_fn import csv, repr_ellipsized, bytestostr, hexstr
from xpra.util.env import envint, envbool
from xpra.util.str_fn import csv, repr_ellipsized, hexstr
from xpra.util.env import envint, envbool, get_exec_env
from xpra.net.bytestreams import TwoFileConnection
from xpra.net.common import ConnectionClosedException, PACKET_TYPES
from xpra.net.protocol.socket_handler import SocketProtocol
Expand Down Expand Up @@ -310,19 +310,10 @@ def exec_kwargs() -> dict[str, Any]:
return kwargs


def exec_env(blacklist=("LS_COLORS",)) -> dict[str, str]:
env = os.environ.copy()
def exec_env() -> dict[str, str]:
env = get_exec_env()
env["XPRA_SKIP_UI"] = "1"
env["XPRA_FORCE_COLOR_LOG"] = "1"
# let's make things more complicated than they should be:
# on win32, the environment can end up containing unicode, and subprocess chokes on it
for k, v in env.items():
if k in blacklist:
continue
try:
env[k] = bytestostr(v.encode("utf8"))
except Exception:
env[k] = bytestostr(v)
return env


Expand Down
5 changes: 3 additions & 2 deletions xpra/scripts/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from xpra.common import SocketState, noerr, noop
from xpra.util.objects import typedict
from xpra.util.str_fn import nonl, csv, print_nested_dict, pver, sorted_nicely, bytestostr
from xpra.util.env import envint, envbool, osexpand, save_env, OSEnvContext
from xpra.util.env import envint, envbool, osexpand, save_env, get_exec_env, OSEnvContext
from xpra.util.thread import set_main_thread
from xpra.exit_codes import ExitCode, ExitValue, RETRY_EXIT_CODES, exit_str
from xpra.os_util import (
Expand Down Expand Up @@ -1833,14 +1833,15 @@ def run_opengl_probe():
from xpra.platform.paths import get_nodock_command
log = Logger("opengl")
cmd = get_nodock_command() + ["opengl"]
env = os.environ.copy()
env = get_exec_env()
if is_debug_enabled("opengl"):
cmd += ["-d", "opengl"]
else:
env["NOTTY"] = "1"
env["XPRA_HIDE_DOCK"] = "1"
env["XPRA_REDIRECT_OUTPUT"] = "0"
start = monotonic()
log(f"run_opengl_probe() using cmd={cmd} with env={env}")
try:
proc = Popen(cmd, stdout=PIPE, stderr=PIPE, env=env, universal_newlines=True)
except Exception as e:
Expand Down
16 changes: 16 additions & 0 deletions xpra/util/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,22 @@ def get_saved_env_var(var, default=None):
return _saved_env.get(var, default)


def get_exec_env(remove=("LS_COLORS", "LESSOPEN", "HISTCONTROL", "HISTSIZE", )) -> dict[str, str]:
# let's make things more complicated than they should be:
# on win32, the environment can end up containing unicode, and subprocess chokes on it
env: dict[str, str] = {}
for k, v in os.environ.items():
if k in remove:
continue
try:
env[k] = v.encode("utf8").decode("latin1")
except UnicodeError:
env[k] = str(v)
env["XPRA_SKIP_UI"] = "1"
env["XPRA_FORCE_COLOR_LOG"] = "1"
return env


class SilenceWarningsContext(AbstractContextManager):

def __init__(self, *categories):
Expand Down

0 comments on commit d567ce9

Please sign in to comment.