pytest: close file handles after use, and two minor tidy-ups

Also:
- drop two unreachable return statements.
- test_17_ssl_use: avoid implicit string concatenations in lists.

Reported by GitHub CodeQL

Closes #21916
This commit is contained in:
Viktor Szakats 2026-06-08 23:27:32 +02:00
parent 2dfd265d66
commit 8145476d5d
No known key found for this signature in database
15 changed files with 149 additions and 84 deletions

View file

@ -104,8 +104,9 @@ class LocalClient:
log.warning(f'Timeout after {self._timeout}s: {args}')
exitcode = -1
exception = 'TimeoutExpired'
coutput = open(self._stdoutfile).readlines()
cerrput = open(self._stderrfile).readlines()
with open(self._stdoutfile) as fout, open(self._stderrfile) as ferr:
coutput = fout.readlines()
cerrput = ferr.readlines()
return ExecResult(args=myargs, exit_code=exitcode, exception=exception,
stdout=coutput, stderr=cerrput,
duration=datetime.now() - start)
@ -113,8 +114,10 @@ class LocalClient:
def dump_logs(self):
lines = []
lines.append('>>--stdout ----------------------------------------------\n')
lines.extend(open(self._stdoutfile).readlines())
with open(self._stdoutfile) as cstdout:
lines.extend(cstdout.readlines())
lines.append('>>--stderr ----------------------------------------------\n')
lines.extend(open(self._stderrfile).readlines())
with open(self._stderrfile) as cstderr:
lines.extend(cstderr.readlines())
lines.append('<<-------------------------------------------------------\n')
return ''.join(lines)

View file

@ -393,7 +393,7 @@ class ExecResult:
f'got {self.exit_code}\n{self.dump_logs()}'
elif code is False:
assert self.exit_code != 0, f'expected exit code {code}, '\
f'got {self.exit_code}\n{self.dump_logs()}'
f'got {self.exit_code}\n{self.dump_logs()}'
else:
assert self.exit_code == code, f'expected exit code {code}, '\
f'got {self.exit_code}\n{self.dump_logs()}'
@ -1072,8 +1072,9 @@ class CurlClient:
dtrace.finish()
if self._with_flame:
self._generate_flame(args, dtrace=dtrace, perf=perf)
coutput = open(self._stdoutfile).readlines()
cerrput = open(self._stderrfile).readlines()
with open(self._stdoutfile) as fout, open(self._stderrfile) as ferr:
coutput = fout.readlines()
cerrput = ferr.readlines()
return ExecResult(args=args, exit_code=exitcode, exception=exception,
stdout=coutput, stderr=cerrput,
duration=ended_at - started_at,
@ -1167,7 +1168,8 @@ class CurlClient:
return args
def _parse_headerfile(self, headerfile: str, r: Optional[ExecResult] = None) -> ExecResult:
lines = open(headerfile).readlines()
with open(headerfile) as fd:
lines = fd.readlines()
if r is None:
r = ExecResult(args=[], exit_code=0, stdout=[], stderr=[])

View file

@ -132,7 +132,8 @@ class Sshd:
self._host_key_files.append(key_file)
pub_file = f'{key_file}.pub'
self._host_pub_files.append(pub_file)
pubkey = open(pub_file).read()
with open(pub_file) as fp:
pubkey = fp.read()
# fd_known.write(f'[127.0.0.1]:{self.port} {pubkey}')
fd_known.write(f'[{self.env.domain1.lower()}]:{self.port} {pubkey}')
fd_unknown.write(f'dummy.invalid {pubkey}')
@ -159,7 +160,8 @@ class Sshd:
self._user_pub_files.append(f'{key_file}.pub')
with open(self._auth_keys, 'w') as fd:
os.chmod(self._auth_keys, stat.S_IRUSR | stat.S_IWUSR)
pubkey = open(self._user_pub_files[0]).read()
with open(self._user_pub_files[0]) as fp:
pubkey = fp.read()
fd.write(pubkey)
def clear_logs(self):