Address ResourceWarning warnings

Signed-off-by: JoeLametta <JoeLametta@users.noreply.github.com>
This commit is contained in:
JoeLametta
2019-08-12 09:00:00 +00:00
parent fff3014e15
commit bb4c25df97
11 changed files with 49 additions and 47 deletions

View File

@@ -147,7 +147,8 @@ def _save_entry(raw_entry, path):
except OSError as e:
logger.error('could not save entry to %s: %s', path, e)
return
open(path, 'wb').write(raw_entry)
with open(path, 'wb') as f:
f.write(raw_entry)
def get_db_entry(path):
@@ -160,7 +161,8 @@ def get_db_entry(path):
cached_path = join(_CACHE_DIR, path)
if exists(cached_path):
logger.debug('found accuraterip entry at %s', cached_path)
raw_entry = open(cached_path, 'rb').read()
with open(cached_path, 'rb') as f:
raw_entry = f.read()
else:
raw_entry = _download_entry(path)
if raw_entry:

View File

@@ -98,17 +98,16 @@ class Persister:
if not os.path.exists(self._path):
return
handle = open(self._path, 'rb')
import pickle
try:
self.object = pickle.load(handle)
logger.debug('loaded persisted object from %r', self._path)
# FIXME: catching too general exception (Exception)
except Exception as e:
# can fail for various reasons; in that case, pretend we didn't
# load it
logger.debug(e)
with open(self._path, 'rb') as handle:
import pickle
try:
self.object = pickle.load(handle)
logger.debug('loaded persisted object from %r', self._path)
# FIXME: catching too general exception (Exception)
except Exception as e:
# can fail for various reasons; in that case, pretend we didn't
# load it
logger.debug(e)
def delete(self):
self.object = None

View File

@@ -317,11 +317,11 @@ class VersionGetter:
version = "(Unknown)"
try:
p = asyncsub.Popen(self._args,
stdin=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, close_fds=True)
p.wait()
output = asyncsub.recv_some(p, e=0, stderr=1).decode()
with asyncsub.Popen(self._args,
stdin=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, close_fds=True) as p:
p.wait()
output = asyncsub.recv_some(p, e=0, stderr=1).decode()
vre = self._regexp.search(output)
if vre:
version = self._expander % vre.groupdict()

View File

@@ -25,7 +25,6 @@ See http://digitalx.org/cuesheetsyntax.php
"""
import re
import codecs
from whipper.common import common
from whipper.image import table
@@ -86,9 +85,9 @@ class CueFile:
counter = 0
logger.info('parsing .cue file %r', self._path)
handle = codecs.open(self._path, 'r', 'utf-8')
for number, line in enumerate(handle.readlines()):
with open(self._path) as f:
content = f.readlines()
for number, line in enumerate(content):
line = line.rstrip()
m = _REM_RE.search(line)

View File

@@ -25,7 +25,6 @@ The .toc file format is described in the man page of cdrdao
"""
import re
import codecs
from whipper.common import common
from whipper.image import table
@@ -189,9 +188,9 @@ class TocFile:
# the first track's INDEX 1 can only be gotten from the .toc
# file once the first pregap is calculated; so we add INDEX 1
# at the end of each parsed TRACK record
handle = codecs.open(self._path, "r", "utf-8")
for number, line in enumerate(handle.readlines()):
with open(self._path) as f:
content = f.readlines()
for number, line in enumerate(content):
line = line.rstrip()
# look for CDTEXT stuff in either header or tracks

View File

@@ -70,7 +70,8 @@ class TestCase(unittest.TestCase):
version so we can use it in comparisons.
"""
cuefile = os.path.join(os.path.dirname(__file__), name)
ret = open(cuefile).read()
with open(cuefile) as f:
ret = f.read()
ret = re.sub(
'REM COMMENT "whipper.*',
'REM COMMENT "whipper %s"' % whipper.__version__,

View File

@@ -21,9 +21,8 @@ class TestAccurateRipResponse(TestCase):
@classmethod
def setUpClass(cls):
cls.path = 'c/1/2/dBAR-002-0000f21c-00027ef8-05021002.bin'
cls.entry = _split_responses(
open(join(dirname(__file__), cls.path[6:]), "rb").read()
)
with open(join(dirname(__file__), cls.path[6:]), 'rb') as f:
cls.entry = _split_responses(f.read())
cls.other_path = '4/8/2/dBAR-011-0010e284-009228a3-9809ff0b.bin'
def setUp(self):
@@ -100,9 +99,8 @@ class TestVerifyResult(TestCase):
@classmethod
def setUpClass(cls):
path = 'c/1/2/dBAR-002-0000f21c-00027ef8-05021002.bin'
cls.responses = _split_responses(
open(join(dirname(__file__), path[6:]), "rb").read()
)
with open(join(dirname(__file__), path[6:]), 'rb') as f:
cls.responses = _split_responses(f.read())
cls.checksums = {
'v1': ['284fc705', '9cc1f32e'],
'v2': ['dc77f9ab', 'dd97d2c3'],

View File

@@ -25,7 +25,8 @@ class RenameInFileTestcase(unittest.TestCase):
def testDo(self):
o = renamer.RenameInFile(self._path, 'is is a', 'at was some')
o.do()
output = open(self._path).read()
with open(self._path) as f:
output = f.read()
self.assertEqual(output, 'That was some test\nThat was somenother\n')
os.unlink(self._path)
@@ -34,7 +35,8 @@ class RenameInFileTestcase(unittest.TestCase):
data = o.serialize()
o2 = renamer.RenameInFile.deserialize(data)
o2.do()
output = open(self._path).read()
with open(self._path) as f:
output = f.read()
self.assertEqual(output, 'That was some test\nThat was somenother\n')
os.unlink(self._path)
@@ -66,7 +68,8 @@ class RenameFileTestcase(unittest.TestCase):
def testDo(self):
self._operation.do()
output = open(self._destination).read()
with open(self._destination) as f:
output = f.read()
self.assertEqual(output, 'This is a test\nThis is another\n')
os.unlink(self._destination)
@@ -74,7 +77,8 @@ class RenameFileTestcase(unittest.TestCase):
data = self._operation.serialize()
o = renamer.RenameFile.deserialize(data)
o.do()
output = open(self._destination).read()
with open(self._destination) as f:
output = f.read()
self.assertEqual(output, 'This is a test\nThis is another\n')
os.unlink(self._destination)

View File

@@ -361,10 +361,9 @@ class StrokesTestCase(common.TestCase):
self.assertEqual(i1.path, 'data.wav')
cue = self._filterCue(self.toc.table.cue())
ref = self._filterCue(
open(os.path.join(
os.path.dirname(__file__),
'strokes-someday.eac.cue')).read())
with open(os.path.join(os.path.dirname(__file__),
'strokes-someday.eac.cue')) as f:
ref = self._filterCue(f.read())
common.diffStrings(ref, cue)
@staticmethod

View File

@@ -27,7 +27,8 @@ class AudioLengthPathTestCase(tcommon.TestCase):
def _testSuffix(self, suffix):
fd, path = tempfile.mkstemp(suffix=suffix)
with os.fdopen(fd, "wb") as temptrack:
temptrack.write(open(base_track_file, "rb").read())
with open(base_track_file, "rb") as f:
temptrack.write(f.read())
t = AudioLengthTask(path)
runner = task.SyncRunner()

View File

@@ -131,20 +131,20 @@ class LoggerTestCase(unittest.TestCase):
logger = WhipperLogger()
actual = logger.log(ripResult)
actualLines = actual.splitlines()
expectedLines = open(
os.path.join(self.path, 'test_result_logger.log'), 'r'
).read().splitlines()
with open(os.path.join(self.path,
'test_result_logger.log'), 'r') as f:
expectedLines = f.read().splitlines()
# do not test on version line, date line, or SHA-256 hash line
self.assertListEqual(actualLines[2:-1], expectedLines[2:-1])
self.assertRegexpMatches(
self.assertRegex(
actualLines[0],
re.compile((
r'Log created by: whipper '
r'[\d]+\.[\d]+\.[\d]+\.dev[\w\.\+]+ \(internal logger\)'
))
)
self.assertRegexpMatches(
self.assertRegex(
actualLines[1],
re.compile((
r'Log creation date: '