Skip to content
Snippets Groups Projects
Commit e114af97 authored by Robert Luberda's avatar Robert Luberda
Browse files

Merge branch 'fix-resource-warnings-unclosed-file' into 'debian-sid'

Fix several ResourceWarning: unclosed file

See merge request !2
parents a5bff002 8a80edb7
No related branches found
No related tags found
1 merge request!2Fix several ResourceWarning: unclosed file
......@@ -66,17 +66,16 @@ def main(config):
sys.exit(0)
if frontend.needs_tty_stdin() and not sys.stdin.isatty():
try:
# Give any forked processes (eg. lynx) a normal stdin;
# See Debian Bug #343423. (Note: with $APT_HOOK_INFO_FD
# support introduced in version 3.2, stdin should point to
# a terminal already, so there should be no need to reopen it).
tty = open('/dev/tty', 'rb+', buffering=0)
os.close(0)
os.dup2(tty.fileno(), 0)
tty.close()
except Exception as ex:
ALCLog.warning(_("Cannot reopen /dev/tty for stdin: %s") % str(ex))
try:
# Give any forked processes (eg. lynx) a normal stdin;
# See Debian Bug #343423. (Note: with $APT_HOOK_INFO_FD
# support introduced in version 3.2, stdin should point to
# a terminal already, so there should be no need to reopen it).
with open('/dev/tty', 'rb+', buffering=0) as tty:
os.close(0)
os.dup2(tty.fileno(), 0)
except Exception as ex:
ALCLog.warning(_("Cannot reopen /dev/tty for stdin: %s") % str(ex))
status = None
if not config.show_all and config.since is None:
......
......@@ -53,14 +53,13 @@ class AptPipeline(object):
self._config = config
def read(self):
fd = self._open_apt_fd()
if self._config.debug:
ALCLog.debug(_("APT pipeline messages:"))
self._read_version(fd)
self._read_options(fd)
debs = self._read_packages(fd)
with self._open_apt_fd() as fd:
self._read_version(fd)
self._read_options(fd)
debs = self._read_packages(fd)
if self._config.debug:
ALCLog.debug(_("Packages list:"))
......
......@@ -113,7 +113,8 @@ class ControlParser:
def readfile(self, file):
try:
self.stanzas += [ControlStanza(x) for x in open(file, 'r', encoding='utf-8', errors='replace').read().split('\n\n') if x]
with open(file, encoding='utf-8', errors='replace') as f:
self.stanzas += [ControlStanza(x) for x in f.read().split('\n\n') if x]
except Exception as ex:
raise RuntimeError(_("Error processing '%(what)s': %(errmsg)s") %
{'what': file, 'errmsg': str(ex)}) from ex
......@@ -335,7 +336,8 @@ class Package:
if not fd:
return None
entries, urgency = ChangelogParser().parse(fd, since_version)
with fd:
entries, urgency = ChangelogParser().parse(fd, since_version)
if urgency is None:
return None # not a Debian changelog file
......
......@@ -277,11 +277,11 @@ class ttyconfirm(base):
if sys.stdin.isatty() and sys.stdout.isatty():
return input(prompt).rstrip()
tty = open('/dev/tty', 'rb+', buffering=0)
enc = ALChacks.system_encoding()
tty.write(enc.to_bytes(prompt))
tty.flush()
return enc.from_bytes(tty.readline()).rstrip()
with open('/dev/tty', 'rb+', buffering=0) as tty:
tty.write(enc.to_bytes(prompt))
tty.flush()
return enc.from_bytes(tty.readline()).rstrip()
def confirm(self):
response = self.ttyask('apt-listchanges: ' + _('Do you want to continue? [Y/n] '))
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment