1515import sys
1616import termios
1717import tty
18+ from collections .abc import Mapping , Sequence
1819from contextlib import ExitStack
1920from selectors import EVENT_READ , DefaultSelector
2021from typing import Any , Protocol
@@ -53,15 +54,15 @@ def get_fno(obj: int | SupportsFileno | None) -> int | None:
5354
5455
5556def run (
56- cmd : list [str | bytes ] | str | bytes ,
57+ cmd : Sequence [str | bytes ] | str | bytes ,
5758 address : str | bytes | None = None ,
5859 service_type : str | bytes | None = None ,
5960 name : str | bytes | None = None ,
6061 user : str | bytes | None = None ,
6162 user_mode : bool = USER_MODE ,
6263 nice : int | None = None ,
6364 runtime_max_sec : int | float | None = None ,
64- env : dict [str | bytes , str | bytes ] | None = None ,
65+ env : Mapping [str , str | bytes ] | Mapping [ bytes , str | bytes ] | None = None ,
6566 extra : dict [bytes , Any ] | None = None ,
6667 cwd : str | bytes | None = None ,
6768 machine : str | bytes | None = None ,
@@ -78,10 +79,10 @@ def run(
7879 stderr : int | SupportsFileno | None = None ,
7980 _wait_polling : int | float | None = None ,
8081 slice_ : str | bytes | None = None ,
81- stop_cmd : list [str | bytes ] | str | bytes | None = None ,
82- stop_post_cmd : list [str | bytes ] | str | bytes | None = None ,
83- start_pre_cmd : list [str | bytes ] | str | bytes | None = None ,
84- start_post_cmd : list [str | bytes ] | str | bytes | None = None ,
82+ stop_cmd : Sequence [str | bytes ] | str | bytes | None = None ,
83+ stop_post_cmd : Sequence [str | bytes ] | str | bytes | None = None ,
84+ start_pre_cmd : Sequence [str | bytes ] | str | bytes | None = None ,
85+ start_post_cmd : Sequence [str | bytes ] | str | bytes | None = None ,
8586) -> Unit :
8687 """
8788 pystemd.run imitates systemd-run, but with a pythonic feel to it.
@@ -164,7 +165,9 @@ def bus_factory():
164165 runtime_max_usec = (runtime_max_sec or 0 ) * 10 ** 6 or runtime_max_sec
165166
166167 stdin , stdout , stderr = get_fno (stdin ), get_fno (stdout ), get_fno (stderr )
167- env = env or {}
168+ env_dict : dict [bytes , str | bytes ] = (
169+ {x2char_star (k ): v for k , v in env .items ()} if env else {}
170+ )
168171 unit_properties : dict [bytes , object ] = {}
169172
170173 extra = extra or {}
@@ -219,9 +222,9 @@ def bus_factory():
219222 sel .register (stdin , EVENT_READ )
220223
221224 if None not in (stdout , pty_master ):
222- if os .getenv ("TERM" ):
223- # pyrefly: ignore [missing-attribute]
224- env [b"TERM" ] = env .get (b"TERM" , os . getenv ( "TERM" ) .encode ())
225+ term = os .getenv ("TERM" )
226+ if term :
227+ env_dict [b"TERM" ] = env_dict .get (b"TERM" , term .encode ())
225228
226229 # pyrefly: ignore [bad-argument-type]
227230 sel .register (pty_master , EVENT_READ )
@@ -263,8 +266,8 @@ def bus_factory():
263266 b"Nice" : nice ,
264267 b"RuntimeMaxUSec" : runtime_max_usec ,
265268 b"Environment" : [
266- b"%s=%s" % (x2char_star ( key ) , x2char_star (value ))
267- for key , value in env .items ()
269+ b"%s=%s" % (key , x2char_star (value ))
270+ for key , value in env_dict .items ()
268271 ]
269272 or None ,
270273 }
0 commit comments