Skip to content

Commit

Permalink
pylint: adjust for consider-using-with
Browse files Browse the repository at this point in the history
Either apply suggestion, or disable warning, depending on context.
  • Loading branch information
marmarek committed Mar 18, 2022
1 parent ccaa123 commit 067b493
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 63 deletions.
1 change: 1 addition & 0 deletions qubes/backup.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ def set_ctty(ctty_fd, master_fd):
stderr=stderr,
preexec_fn=lambda: set_ctty(pty_slave, pty_master))
os.close(pty_slave)
# pylint: disable=consider-using-with
return p, open(pty_master, 'wb+', buffering=0)


Expand Down
12 changes: 6 additions & 6 deletions qubes/dochelpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,12 @@ def fetch_ticket_info(app, number):
:raises: urllib.error.HTTPError
"""

response = urllib.request.urlopen(urllib.request.Request(
app.config.ticket_base_uri.format(number=number),
headers={
'Accept': 'application/vnd.github.v3+json',
'User-agent': __name__}))
return GithubTicket(json.load(response))
with urllib.request.urlopen(urllib.request.Request(
app.config.ticket_base_uri.format(number=number),
headers={
'Accept': 'application/vnd.github.v3+json',
'User-agent': __name__})) as response:
return GithubTicket(json.load(response))


def ticket(name, rawtext, text, lineno, inliner, options=None, content=None):
Expand Down
6 changes: 3 additions & 3 deletions qubes/ext/pci.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,9 +258,9 @@ def on_device_pre_detached_pci(self, vm, event, device):
# qubes.DetachPciDevice, which unbinds driver, not to oops the kernel

device = _cache_get(device.backend_domain, device.ident)
p = subprocess.Popen(['xl', 'pci-list', str(vm.xid)],
stdout=subprocess.PIPE)
result = p.communicate()[0].decode()
with subprocess.Popen(['xl', 'pci-list', str(vm.xid)],
stdout=subprocess.PIPE) as p:
result = p.communicate()[0].decode()
m = re.search(r'^(\d+.\d+)\s+0000:{}$'.format(device.ident.replace(
'_', ':')),
result,
Expand Down
3 changes: 2 additions & 1 deletion qubes/rngdoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,8 @@ def write_rst_table(stream, itr, heads):


def main(filename, example):
schema = Schema(lxml.etree.parse(open(filename, 'rb')))
with open(filename, 'rb') as schema_f:
schema = Schema(lxml.etree.parse(schema_f))

sys.stdout.write(make_rst_section('Qubes XML specification', '='))
sys.stdout.write('''
Expand Down
6 changes: 3 additions & 3 deletions qubes/storage/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,9 +315,9 @@ def resize(self, size): # pylint: disable=invalid-overridden-method
with open(self.path, 'a+b') as fd:
fd.truncate(size)

p = subprocess.Popen(['losetup', '--associated', self.path],
stdout=subprocess.PIPE)
result = p.communicate()
with subprocess.Popen(['losetup', '--associated', self.path],
stdout=subprocess.PIPE) as p:
result = p.communicate()

m = re.match(r'^(/dev/loop\d+):\s', result[0].decode())
if m is not None:
Expand Down
6 changes: 3 additions & 3 deletions qubes/storage/lvm.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,9 +236,9 @@ def _parse_lvm_cache(lvm_output):
def init_cache(log=logging.getLogger('qubes.storage.lvm')):
cmd = _init_cache_cmd
environ={'LC_ALL': 'C.UTF-8', **os.environ}
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
close_fds=True, env=environ)
out, err = p.communicate()
with subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
close_fds=True, env=environ) as p:
out, err = p.communicate()
return_code = p.returncode
if return_code == 0 and err:
log.warning(err)
Expand Down
41 changes: 20 additions & 21 deletions qubes/tarwriter.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,28 +149,27 @@ def main(args=None):
parser.add_argument('output_file', default='-', nargs='?',
help='output file name')
args = parser.parse_args(args)
input_file = io.open(args.input_file, 'rb')
sparse_map = list(get_sparse_map(input_file))
header_name = args.input_file
if args.override_name:
header_name = args.override_name
tar_info = TarSparseInfo(header_name, sparse_map)
if args.output_file == '-':
output = io.open('/dev/stdout', 'wb')
else:
output = io.open(args.output_file, 'wb')
if args.use_compress_program:
compress = subprocess.Popen([args.use_compress_program],
stdin=subprocess.PIPE, stdout=output)
output = compress.stdin
else:
compress = None
output.write(tar_info.tobuf(tarfile.PAX_FORMAT))
copy_sparse_data(input_file, output, sparse_map)
finalize(output)
input_file.close()
output.close()
with io.open(args.input_file, 'rb') as input_file:
sparse_map = list(get_sparse_map(input_file))
header_name = args.input_file
if args.override_name:
header_name = args.override_name
tar_info = TarSparseInfo(header_name, sparse_map)
with io.open(('/dev/stdout' if args.output_file == '-'
else args.output_file),
'wb') as output:
if args.use_compress_program:
# pylint: disable=consider-using-with
compress = subprocess.Popen([args.use_compress_program],
stdin=subprocess.PIPE, stdout=output)
output = compress.stdin
else:
compress = None
output.write(tar_info.tobuf(tarfile.PAX_FORMAT))
copy_sparse_data(input_file, output, sparse_map)
finalize(output)
if compress is not None:
compress.stdin.close()
compress.wait()
return compress.returncode
return 0
Expand Down
12 changes: 6 additions & 6 deletions qubes/tools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -515,11 +515,11 @@ def print_table(table):

# for tests...
if sys.stdout != sys.__stdout__:
p = subprocess.Popen(cmd + ['-c', '80'], stdin=subprocess.PIPE,
stdout=subprocess.PIPE)
p.stdin.write(text_table.encode())
(out, _) = p.communicate()
with subprocess.Popen(cmd + ['-c', '80'], stdin=subprocess.PIPE,
stdout=subprocess.PIPE) as p:
p.stdin.write(text_table.encode())
(out, _) = p.communicate()
sys.stdout.write(out.decode())
else:
p = subprocess.Popen(cmd, stdin=subprocess.PIPE)
p.communicate(text_table.encode())
with subprocess.Popen(cmd, stdin=subprocess.PIPE) as p:
p.communicate(text_table.encode())
32 changes: 16 additions & 16 deletions qubes/tools/qmemmand.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ def domain_list_changed(self, refresh_only=False):
got_lock = False
if not refresh_only:
self.log.debug('acquiring global_lock')
# pylint: disable=consider-using-with
global_lock.acquire()
got_lock = True
self.log.debug('global_lock acquired')
Expand Down Expand Up @@ -148,23 +149,21 @@ def meminfo_changed(self, domain_id):
return

self.log.debug('acquiring global_lock')
global_lock.acquire()
self.log.debug('global_lock acquired')
try:
global force_refresh_domain_list
if force_refresh_domain_list:
self.domain_list_changed(refresh_only=True)
force_refresh_domain_list = False
if domain_id not in self.watch_token_dict:
# domain just destroyed
return
with global_lock:
self.log.debug('global_lock acquired')
try:
global force_refresh_domain_list
if force_refresh_domain_list:
self.domain_list_changed(refresh_only=True)
force_refresh_domain_list = False
if domain_id not in self.watch_token_dict:
# domain just destroyed
return

system_state.refresh_meminfo(domain_id, untrusted_meminfo_key)
except: # pylint: disable=bare-except
self.log.exception('Updating meminfo for %d failed', domain_id)
finally:
global_lock.release()
self.log.debug('global_lock released')
system_state.refresh_meminfo(domain_id, untrusted_meminfo_key)
except: # pylint: disable=bare-except
self.log.exception('Updating meminfo for %d failed', domain_id)
self.log.debug('global_lock released')

def watch_loop(self):
self.log.debug('watch_loop()')
Expand Down Expand Up @@ -206,6 +205,7 @@ def handle(self):
return

self.log.debug('acquiring global_lock')
# pylint: disable=consider-using-with
global_lock.acquire()
self.log.debug('global_lock acquired')

Expand Down
8 changes: 4 additions & 4 deletions qubes/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ def get_timezone():
if not tz_info:
return None
if tz_info.st_nlink > 1:
p = subprocess.Popen(['find', '/usr/share/zoneinfo',
'-inum', str(tz_info.st_ino), '-print', '-quit'],
stdout=subprocess.PIPE)
tz_path = p.communicate()[0].strip()
with subprocess.Popen(['find', '/usr/share/zoneinfo',
'-inum', str(tz_info.st_ino), '-print', '-quit'],
stdout=subprocess.PIPE) as p:
tz_path = p.communicate()[0].strip()
return tz_path.replace(b'/usr/share/zoneinfo/', b'')
return None

Expand Down

0 comments on commit 067b493

Please sign in to comment.