gh-124213: Fix incorrect context manager use in in_systemd_nspawn_sync_suppressed() (#124892)

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.
This commit is contained in:
Michał Górny 2024-10-02 16:31:42 +02:00 committed by GitHub
parent c2ba931318
commit 8d7d257f6b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 3 additions and 2 deletions

View File

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