]> git.ipfire.org Git - thirdparty/mkosi.git/commitdiff
Use with in spawn()
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Wed, 18 Oct 2023 10:01:26 +0000 (12:01 +0200)
committerDaan De Meyer <daan.j.demeyer@gmail.com>
Wed, 18 Oct 2023 10:01:26 +0000 (12:01 +0200)
If we yield the Popen, we yield a context manager from a context
manager, which becomes hard to follow, since we'll only enter the
outer context manager, and not the inner Popen context manager. To
make things simpler, let's enter the Popen context manager in
spawn() itself.

mkosi/run.py

index 3593a276bf2a8adfab42585ac69bfb88c1245e75..6eccccf759280a15382929481d7320a745a81258 100644 (file)
@@ -250,7 +250,7 @@ def spawn(
         stdout = sys.stderr
 
     try:
-        yield subprocess.Popen(
+        with subprocess.Popen(
             cmdline,
             stdin=stdin,
             stdout=stdout,
@@ -261,7 +261,8 @@ def spawn(
             pass_fds=pass_fds,
             env=env,
             preexec_fn=make_foreground_process if foreground else None,
-        )
+        ) as proc:
+            yield proc
     except FileNotFoundError:
         die(f"{cmdline[0]} not found in PATH.")
     except subprocess.CalledProcessError as e: