1
0
mirror of https://github.com/spl0k/supysonic.git synced 2024-11-09 11:42:16 +00:00

Properly release resources

Hunting for the last ResourceWarnings
This commit is contained in:
Alban Féron 2020-11-22 18:09:15 +01:00
parent 9a3bdc30ac
commit 2c100a021a
No known key found for this signature in database
GPG Key ID: 8CE0313646D16165
2 changed files with 24 additions and 27 deletions

View File

@ -195,7 +195,9 @@ def stream_media():
raise raise
finally: finally:
if dec_proc != None: if dec_proc != None:
dec_proc.stdout.close()
dec_proc.wait() dec_proc.wait()
proc.stdout.close()
proc.wait() proc.wait()
resp_content = cache.set_generated(cache_key, handle_transcoding) resp_content = cache.set_generated(cache_key, handle_transcoding)
@ -303,19 +305,19 @@ def cover_art():
else: else:
return send_file(cover_path) return send_file(cover_path)
im = Image.open(cover_path) with Image.open(cover_path) as im:
mimetype = "image/{}".format(im.format.lower()) mimetype = "image/{}".format(im.format.lower())
if size > im.width and size > im.height: if size > im.width and size > im.height:
return send_file(cover_path, mimetype=mimetype) return send_file(cover_path, mimetype=mimetype)
cache_key = "{}-cover-{}".format(eid, size) cache_key = "{}-cover-{}".format(eid, size)
try: try:
return send_file(cache.get(cache_key), mimetype=mimetype) return send_file(cache.get(cache_key), mimetype=mimetype)
except CacheMiss: except CacheMiss:
im.thumbnail([size, size], Image.ANTIALIAS) im.thumbnail([size, size], Image.ANTIALIAS)
with cache.set_fileobj(cache_key) as fp: with cache.set_fileobj(cache_key) as fp:
im.save(fp, im.format) im.save(fp, im.format)
return send_file(cache.get(cache_key), mimetype=mimetype) return send_file(cache.get(cache_key), mimetype=mimetype)
@api.route("/getLyrics.view", methods=["GET", "POST"]) @api.route("/getLyrics.view", methods=["GET", "POST"])

View File

@ -135,6 +135,11 @@ class MediaTestCase(ApiTestBase):
self.assertEqual(rv.status_code, 200) self.assertEqual(rv.status_code, 200)
self.assertEqual(rv.mimetype, "application/zip") self.assertEqual(rv.mimetype, "application/zip")
def __assert_image_data(self, resp, format, size):
with Image.open(BytesIO(resp.data)) as im:
self.assertEqual(im.format, format)
self.assertEqual(im.size, (size, size))
def test_get_cover_art(self): def test_get_cover_art(self):
self._make_request("getCoverArt", error=10) self._make_request("getCoverArt", error=10)
self._make_request("getCoverArt", {"id": "string"}, error=0) self._make_request("getCoverArt", {"id": "string"}, error=0)
@ -150,9 +155,7 @@ class MediaTestCase(ApiTestBase):
) as rv: ) as rv:
self.assertEqual(rv.status_code, 200) self.assertEqual(rv.status_code, 200)
self.assertEqual(rv.mimetype, "image/jpeg") self.assertEqual(rv.mimetype, "image/jpeg")
im = Image.open(BytesIO(rv.data)) self.__assert_image_data(rv, "JPEG", 420)
self.assertEqual(im.format, "JPEG")
self.assertEqual(im.size, (420, 420))
args["size"] = 600 args["size"] = 600
with closing( with closing(
@ -160,9 +163,7 @@ class MediaTestCase(ApiTestBase):
) as rv: ) as rv:
self.assertEqual(rv.status_code, 200) self.assertEqual(rv.status_code, 200)
self.assertEqual(rv.mimetype, "image/jpeg") self.assertEqual(rv.mimetype, "image/jpeg")
im = Image.open(BytesIO(rv.data)) self.__assert_image_data(rv, "JPEG", 420)
self.assertEqual(im.format, "JPEG")
self.assertEqual(im.size, (420, 420))
args["size"] = 120 args["size"] = 120
with closing( with closing(
@ -170,9 +171,7 @@ class MediaTestCase(ApiTestBase):
) as rv: ) as rv:
self.assertEqual(rv.status_code, 200) self.assertEqual(rv.status_code, 200)
self.assertEqual(rv.mimetype, "image/jpeg") self.assertEqual(rv.mimetype, "image/jpeg")
im = Image.open(BytesIO(rv.data)) self.__assert_image_data(rv, "JPEG", 120)
self.assertEqual(im.format, "JPEG")
self.assertEqual(im.size, (120, 120))
# rerequest, just in case # rerequest, just in case
with closing( with closing(
@ -180,9 +179,7 @@ class MediaTestCase(ApiTestBase):
) as rv: ) as rv:
self.assertEqual(rv.status_code, 200) self.assertEqual(rv.status_code, 200)
self.assertEqual(rv.mimetype, "image/jpeg") self.assertEqual(rv.mimetype, "image/jpeg")
im = Image.open(BytesIO(rv.data)) self.__assert_image_data(rv, "JPEG", 120)
self.assertEqual(im.format, "JPEG")
self.assertEqual(im.size, (120, 120))
# TODO test non square covers # TODO test non square covers
@ -193,9 +190,7 @@ class MediaTestCase(ApiTestBase):
) as rv: ) as rv:
self.assertEqual(rv.status_code, 200) self.assertEqual(rv.status_code, 200)
self.assertEqual(rv.mimetype, "image/png") self.assertEqual(rv.mimetype, "image/png")
im = Image.open(BytesIO(rv.data)) self.__assert_image_data(rv, "PNG", 120)
self.assertEqual(im.format, "PNG")
self.assertEqual(im.size, (120, 120))
def test_get_avatar(self): def test_get_avatar(self):
self._make_request("getAvatar", error=0) self._make_request("getAvatar", error=0)