qemu-iotests: fix pylint 2.8 consider-using-with error

pylint 2.8 introduces consider-using-with error, suggesting
to use the 'with' block statement when possible.

Modify all subprocess.Popen call to use the 'with' statement,
except the one in __init__ of QemuIoInteractive class, since
it is assigned to a class field and used in other methods.

Signed-off-by: Emanuele Giuseppe Esposito <eesposit@redhat.com>
Message-Id: <20210510190449.65948-1-eesposit@redhat.com>
[mreitz: Disable bad-option-value warning in the iotests' pylintrc, so
         that disabling consider-using-with in QemuIoInteractive will
         not produce a warning in pre-2.8 pylint versions]
Signed-off-by: Max Reitz <mreitz@redhat.com>
This commit is contained in:
Emanuele Giuseppe Esposito 2021-05-10 21:04:49 +02:00 committed by Max Reitz
parent bcc8584c83
commit ac4e14f5dc
3 changed files with 46 additions and 42 deletions

View File

@ -112,15 +112,14 @@ def qemu_tool_pipe_and_status(tool: str, args: Sequence[str],
Run a tool and return both its output and its exit code Run a tool and return both its output and its exit code
""" """
stderr = subprocess.STDOUT if connect_stderr else None stderr = subprocess.STDOUT if connect_stderr else None
subp = subprocess.Popen(args, with subprocess.Popen(args, stdout=subprocess.PIPE,
stdout=subprocess.PIPE, stderr=stderr, universal_newlines=True) as subp:
stderr=stderr, output = subp.communicate()[0]
universal_newlines=True) if subp.returncode < 0:
output = subp.communicate()[0] cmd = ' '.join(args)
if subp.returncode < 0: sys.stderr.write(f'{tool} received signal \
cmd = ' '.join(args) {-subp.returncode}: {cmd}\n')
sys.stderr.write(f'{tool} received signal {-subp.returncode}: {cmd}\n') return (output, subp.returncode)
return (output, subp.returncode)
def qemu_img_pipe_and_status(*args: str) -> Tuple[str, int]: def qemu_img_pipe_and_status(*args: str) -> Tuple[str, int]:
""" """
@ -236,6 +235,9 @@ def qemu_io_silent_check(*args):
class QemuIoInteractive: class QemuIoInteractive:
def __init__(self, *args): def __init__(self, *args):
self.args = qemu_io_args_no_fmt + list(args) self.args = qemu_io_args_no_fmt + list(args)
# We need to keep the Popen objext around, and not
# close it immediately. Therefore, disable the pylint check:
# pylint: disable=consider-using-with
self._p = subprocess.Popen(self.args, stdin=subprocess.PIPE, self._p = subprocess.Popen(self.args, stdin=subprocess.PIPE,
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.STDOUT, stderr=subprocess.STDOUT,
@ -309,22 +311,22 @@ def qemu_nbd_popen(*args):
cmd.extend(args) cmd.extend(args)
log('Start NBD server') log('Start NBD server')
p = subprocess.Popen(cmd) with subprocess.Popen(cmd) as p:
try: try:
while not os.path.exists(pid_file): while not os.path.exists(pid_file):
if p.poll() is not None: if p.poll() is not None:
raise RuntimeError( raise RuntimeError(
"qemu-nbd terminated with exit code {}: {}" "qemu-nbd terminated with exit code {}: {}"
.format(p.returncode, ' '.join(cmd))) .format(p.returncode, ' '.join(cmd)))
time.sleep(0.01) time.sleep(0.01)
yield yield
finally: finally:
if os.path.exists(pid_file): if os.path.exists(pid_file):
os.remove(pid_file) os.remove(pid_file)
log('Kill NBD server') log('Kill NBD server')
p.kill() p.kill()
p.wait() p.wait()
def compare_images(img1, img2, fmt1=imgfmt, fmt2=imgfmt): def compare_images(img1, img2, fmt1=imgfmt, fmt2=imgfmt):
'''Return True if two image files are identical''' '''Return True if two image files are identical'''
@ -333,13 +335,12 @@ def compare_images(img1, img2, fmt1=imgfmt, fmt2=imgfmt):
def create_image(name, size): def create_image(name, size):
'''Create a fully-allocated raw image with sector markers''' '''Create a fully-allocated raw image with sector markers'''
file = open(name, 'wb') with open(name, 'wb') as file:
i = 0 i = 0
while i < size: while i < size:
sector = struct.pack('>l504xl', i // 512, i // 512) sector = struct.pack('>l504xl', i // 512, i // 512)
file.write(sector) file.write(sector)
i = i + 512 i = i + 512
file.close()
def image_size(img): def image_size(img):
'''Return image's virtual size''' '''Return image's virtual size'''

View File

@ -19,6 +19,9 @@ disable=invalid-name,
too-many-public-methods, too-many-public-methods,
# pylint warns about Optional[] etc. as unsubscriptable in 3.9 # pylint warns about Optional[] etc. as unsubscriptable in 3.9
unsubscriptable-object, unsubscriptable-object,
# Sometimes we need to disable a newly introduced pylint warning.
# Doing so should not produce a warning in older versions of pylint.
bad-option-value,
# These are temporary, and should be removed: # These are temporary, and should be removed:
missing-docstring, missing-docstring,
too-many-return-statements, too-many-return-statements,

View File

@ -246,17 +246,17 @@ class TestRunner(ContextManager['TestRunner']):
t0 = time.time() t0 = time.time()
with f_bad.open('w', encoding="utf-8") as f: with f_bad.open('w', encoding="utf-8") as f:
proc = subprocess.Popen(args, cwd=str(f_test.parent), env=env, with subprocess.Popen(args, cwd=str(f_test.parent), env=env,
stdout=f, stderr=subprocess.STDOUT) stdout=f, stderr=subprocess.STDOUT) as proc:
try: try:
proc.wait() proc.wait()
except KeyboardInterrupt: except KeyboardInterrupt:
proc.terminate() proc.terminate()
proc.wait() proc.wait()
return TestResult(status='not run', return TestResult(status='not run',
description='Interrupted by user', description='Interrupted by user',
interrupted=True) interrupted=True)
ret = proc.returncode ret = proc.returncode
elapsed = round(time.time() - t0, 1) elapsed = round(time.time() - t0, 1)