From bb4c25df978605a7c0fc054f3832c7b52b7df8e6 Mon Sep 17 00:00:00 2001 From: JoeLametta Date: Mon, 12 Aug 2019 09:00:00 +0000 Subject: [PATCH] Address ResourceWarning warnings Signed-off-by: JoeLametta --- whipper/common/accurip.py | 6 ++++-- whipper/common/cache.py | 21 ++++++++++----------- whipper/common/common.py | 10 +++++----- whipper/image/cue.py | 7 +++---- whipper/image/toc.py | 7 +++---- whipper/test/common.py | 3 ++- whipper/test/test_common_accurip.py | 10 ++++------ whipper/test/test_common_renamer.py | 12 ++++++++---- whipper/test/test_image_toc.py | 7 +++---- whipper/test/test_program_soxi.py | 3 ++- whipper/test/test_result_logger.py | 10 +++++----- 11 files changed, 49 insertions(+), 47 deletions(-) diff --git a/whipper/common/accurip.py b/whipper/common/accurip.py index 47b9932..146b3d8 100644 --- a/whipper/common/accurip.py +++ b/whipper/common/accurip.py @@ -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: diff --git a/whipper/common/cache.py b/whipper/common/cache.py index d413398..a3e32a6 100644 --- a/whipper/common/cache.py +++ b/whipper/common/cache.py @@ -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 diff --git a/whipper/common/common.py b/whipper/common/common.py index 6c2ead6..fd545f0 100644 --- a/whipper/common/common.py +++ b/whipper/common/common.py @@ -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() diff --git a/whipper/image/cue.py b/whipper/image/cue.py index 64b09a7..0bba8f4 100644 --- a/whipper/image/cue.py +++ b/whipper/image/cue.py @@ -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) diff --git a/whipper/image/toc.py b/whipper/image/toc.py index 9cf1f12..ed2e484 100644 --- a/whipper/image/toc.py +++ b/whipper/image/toc.py @@ -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 diff --git a/whipper/test/common.py b/whipper/test/common.py index a55ee14..45f83ad 100644 --- a/whipper/test/common.py +++ b/whipper/test/common.py @@ -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__, diff --git a/whipper/test/test_common_accurip.py b/whipper/test/test_common_accurip.py index 4b5378b..79c6f1c 100644 --- a/whipper/test/test_common_accurip.py +++ b/whipper/test/test_common_accurip.py @@ -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'], diff --git a/whipper/test/test_common_renamer.py b/whipper/test/test_common_renamer.py index c33ca07..d776e3f 100644 --- a/whipper/test/test_common_renamer.py +++ b/whipper/test/test_common_renamer.py @@ -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) diff --git a/whipper/test/test_image_toc.py b/whipper/test/test_image_toc.py index 04bf04a..79fcfc4 100644 --- a/whipper/test/test_image_toc.py +++ b/whipper/test/test_image_toc.py @@ -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 diff --git a/whipper/test/test_program_soxi.py b/whipper/test/test_program_soxi.py index 32f3e23..0bd0153 100644 --- a/whipper/test/test_program_soxi.py +++ b/whipper/test/test_program_soxi.py @@ -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() diff --git a/whipper/test/test_result_logger.py b/whipper/test/test_result_logger.py index 6e9d509..af8d520 100644 --- a/whipper/test/test_result_logger.py +++ b/whipper/test/test_result_logger.py @@ -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: '