Skip to content

Commit 067b493

Browse files
committed
pylint: adjust for consider-using-with
Either apply suggestion, or disable warning, depending on context.
1 parent ccaa123 commit 067b493

10 files changed

Lines changed: 64 additions & 63 deletions

File tree

qubes/backup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ def set_ctty(ctty_fd, master_fd):
187187
stderr=stderr,
188188
preexec_fn=lambda: set_ctty(pty_slave, pty_master))
189189
os.close(pty_slave)
190+
# pylint: disable=consider-using-with
190191
return p, open(pty_master, 'wb+', buffering=0)
191192

192193

qubes/dochelpers.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,12 @@ def fetch_ticket_info(app, number):
7070
:raises: urllib.error.HTTPError
7171
"""
7272

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

8080

8181
def ticket(name, rawtext, text, lineno, inliner, options=None, content=None):

qubes/ext/pci.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -258,9 +258,9 @@ def on_device_pre_detached_pci(self, vm, event, device):
258258
# qubes.DetachPciDevice, which unbinds driver, not to oops the kernel
259259

260260
device = _cache_get(device.backend_domain, device.ident)
261-
p = subprocess.Popen(['xl', 'pci-list', str(vm.xid)],
262-
stdout=subprocess.PIPE)
263-
result = p.communicate()[0].decode()
261+
with subprocess.Popen(['xl', 'pci-list', str(vm.xid)],
262+
stdout=subprocess.PIPE) as p:
263+
result = p.communicate()[0].decode()
264264
m = re.search(r'^(\d+.\d+)\s+0000:{}$'.format(device.ident.replace(
265265
'_', ':')),
266266
result,

qubes/rngdoc.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,8 @@ def write_rst_table(stream, itr, heads):
192192

193193

194194
def main(filename, example):
195-
schema = Schema(lxml.etree.parse(open(filename, 'rb')))
195+
with open(filename, 'rb') as schema_f:
196+
schema = Schema(lxml.etree.parse(schema_f))
196197

197198
sys.stdout.write(make_rst_section('Qubes XML specification', '='))
198199
sys.stdout.write('''

qubes/storage/file.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -315,9 +315,9 @@ def resize(self, size): # pylint: disable=invalid-overridden-method
315315
with open(self.path, 'a+b') as fd:
316316
fd.truncate(size)
317317

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

322322
m = re.match(r'^(/dev/loop\d+):\s', result[0].decode())
323323
if m is not None:

qubes/storage/lvm.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -236,9 +236,9 @@ def _parse_lvm_cache(lvm_output):
236236
def init_cache(log=logging.getLogger('qubes.storage.lvm')):
237237
cmd = _init_cache_cmd
238238
environ={'LC_ALL': 'C.UTF-8', **os.environ}
239-
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
240-
close_fds=True, env=environ)
241-
out, err = p.communicate()
239+
with subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
240+
close_fds=True, env=environ) as p:
241+
out, err = p.communicate()
242242
return_code = p.returncode
243243
if return_code == 0 and err:
244244
log.warning(err)

qubes/tarwriter.py

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -149,28 +149,27 @@ def main(args=None):
149149
parser.add_argument('output_file', default='-', nargs='?',
150150
help='output file name')
151151
args = parser.parse_args(args)
152-
input_file = io.open(args.input_file, 'rb')
153-
sparse_map = list(get_sparse_map(input_file))
154-
header_name = args.input_file
155-
if args.override_name:
156-
header_name = args.override_name
157-
tar_info = TarSparseInfo(header_name, sparse_map)
158-
if args.output_file == '-':
159-
output = io.open('/dev/stdout', 'wb')
160-
else:
161-
output = io.open(args.output_file, 'wb')
162-
if args.use_compress_program:
163-
compress = subprocess.Popen([args.use_compress_program],
164-
stdin=subprocess.PIPE, stdout=output)
165-
output = compress.stdin
166-
else:
167-
compress = None
168-
output.write(tar_info.tobuf(tarfile.PAX_FORMAT))
169-
copy_sparse_data(input_file, output, sparse_map)
170-
finalize(output)
171-
input_file.close()
172-
output.close()
152+
with io.open(args.input_file, 'rb') as input_file:
153+
sparse_map = list(get_sparse_map(input_file))
154+
header_name = args.input_file
155+
if args.override_name:
156+
header_name = args.override_name
157+
tar_info = TarSparseInfo(header_name, sparse_map)
158+
with io.open(('/dev/stdout' if args.output_file == '-'
159+
else args.output_file),
160+
'wb') as output:
161+
if args.use_compress_program:
162+
# pylint: disable=consider-using-with
163+
compress = subprocess.Popen([args.use_compress_program],
164+
stdin=subprocess.PIPE, stdout=output)
165+
output = compress.stdin
166+
else:
167+
compress = None
168+
output.write(tar_info.tobuf(tarfile.PAX_FORMAT))
169+
copy_sparse_data(input_file, output, sparse_map)
170+
finalize(output)
173171
if compress is not None:
172+
compress.stdin.close()
174173
compress.wait()
175174
return compress.returncode
176175
return 0

qubes/tools/__init__.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -515,11 +515,11 @@ def print_table(table):
515515

516516
# for tests...
517517
if sys.stdout != sys.__stdout__:
518-
p = subprocess.Popen(cmd + ['-c', '80'], stdin=subprocess.PIPE,
519-
stdout=subprocess.PIPE)
520-
p.stdin.write(text_table.encode())
521-
(out, _) = p.communicate()
518+
with subprocess.Popen(cmd + ['-c', '80'], stdin=subprocess.PIPE,
519+
stdout=subprocess.PIPE) as p:
520+
p.stdin.write(text_table.encode())
521+
(out, _) = p.communicate()
522522
sys.stdout.write(out.decode())
523523
else:
524-
p = subprocess.Popen(cmd, stdin=subprocess.PIPE)
525-
p.communicate(text_table.encode())
524+
with subprocess.Popen(cmd, stdin=subprocess.PIPE) as p:
525+
p.communicate(text_table.encode())

qubes/tools/qmemmand.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ def domain_list_changed(self, refresh_only=False):
9494
got_lock = False
9595
if not refresh_only:
9696
self.log.debug('acquiring global_lock')
97+
# pylint: disable=consider-using-with
9798
global_lock.acquire()
9899
got_lock = True
99100
self.log.debug('global_lock acquired')
@@ -148,23 +149,21 @@ def meminfo_changed(self, domain_id):
148149
return
149150

150151
self.log.debug('acquiring global_lock')
151-
global_lock.acquire()
152-
self.log.debug('global_lock acquired')
153-
try:
154-
global force_refresh_domain_list
155-
if force_refresh_domain_list:
156-
self.domain_list_changed(refresh_only=True)
157-
force_refresh_domain_list = False
158-
if domain_id not in self.watch_token_dict:
159-
# domain just destroyed
160-
return
152+
with global_lock:
153+
self.log.debug('global_lock acquired')
154+
try:
155+
global force_refresh_domain_list
156+
if force_refresh_domain_list:
157+
self.domain_list_changed(refresh_only=True)
158+
force_refresh_domain_list = False
159+
if domain_id not in self.watch_token_dict:
160+
# domain just destroyed
161+
return
161162

162-
system_state.refresh_meminfo(domain_id, untrusted_meminfo_key)
163-
except: # pylint: disable=bare-except
164-
self.log.exception('Updating meminfo for %d failed', domain_id)
165-
finally:
166-
global_lock.release()
167-
self.log.debug('global_lock released')
163+
system_state.refresh_meminfo(domain_id, untrusted_meminfo_key)
164+
except: # pylint: disable=bare-except
165+
self.log.exception('Updating meminfo for %d failed', domain_id)
166+
self.log.debug('global_lock released')
168167

169168
def watch_loop(self):
170169
self.log.debug('watch_loop()')
@@ -206,6 +205,7 @@ def handle(self):
206205
return
207206

208207
self.log.debug('acquiring global_lock')
208+
# pylint: disable=consider-using-with
209209
global_lock.acquire()
210210
self.log.debug('global_lock acquired')
211211

qubes/utils.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,10 @@ def get_timezone():
5252
if not tz_info:
5353
return None
5454
if tz_info.st_nlink > 1:
55-
p = subprocess.Popen(['find', '/usr/share/zoneinfo',
56-
'-inum', str(tz_info.st_ino), '-print', '-quit'],
57-
stdout=subprocess.PIPE)
58-
tz_path = p.communicate()[0].strip()
55+
with subprocess.Popen(['find', '/usr/share/zoneinfo',
56+
'-inum', str(tz_info.st_ino), '-print', '-quit'],
57+
stdout=subprocess.PIPE) as p:
58+
tz_path = p.communicate()[0].strip()
5959
return tz_path.replace(b'/usr/share/zoneinfo/', b'')
6060
return None
6161

0 commit comments

Comments
 (0)