]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-124213: Fix incorrect context manager use in in_systemd_nspawn_sync_suppressed...
authorMichał Górny <mgorny@gentoo.org>
Wed, 2 Oct 2024 14:31:42 +0000 (16:31 +0200)
committerGitHub <noreply@github.com>
Wed, 2 Oct 2024 14:31:42 +0000 (14:31 +0000)
Fix the incorrect use of `os.open()` result as a context manager,
while it is actually a numeric file descriptor.

I have missed the problem, because in the original version the
`os.open()` call would always fail, and I failed to test the final
version in all possible scenarios properly.

Lib/test/support/__init__.py

index 99cb10fc7b5f7b458d9a1fdc061f71c1fddaa01c..1a44cc638b5714ab2e7f2d92e54f6b21916d3de4 100644 (file)
@@ -2907,10 +2907,11 @@ def in_systemd_nspawn_sync_suppressed() -> bool:
     # trigger EINVAL.  Otherwise, ENOENT will be given instead.
     import errno
     try:
-        with os.open(__file__, os.O_RDONLY | os.O_SYNC):
-            pass
+        fd = os.open(__file__, os.O_RDONLY | os.O_SYNC)
     except OSError as err:
         if err.errno == errno.EINVAL:
             return True
+    else:
+        os.close(fd)
 
     return False