From 807ef4637998a19c5fc47255a5b5d6849323bf2b Mon Sep 17 00:00:00 2001 From: 0x5c Date: Tue, 31 Dec 2019 03:18:30 -0500 Subject: [PATCH] Fix unclosed files (BytesIO) Fixes #119 --- exts/qrz.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/exts/qrz.py b/exts/qrz.py index 27831a1..2d770c6 100644 --- a/exts/qrz.py +++ b/exts/qrz.py @@ -40,7 +40,8 @@ class QRZCog(commands.Cog): async with self.session.get(url) as resp: if resp.status != 200: raise ConnectionError(f'Unable to connect to QRZ (HTTP Error {resp.status})') - resp_xml = etree.parse(BytesIO(await resp.read())).getroot() + with BytesIO(await resp.read()) as resp_file: + resp_xml = etree.parse(resp_file).getroot() resp_xml_session = resp_xml.xpath('/x:QRZDatabase/x:Session', namespaces={'x': 'http://xmldata.qrz.com'}) @@ -99,7 +100,8 @@ async def qrz_login(user: str, passwd: str, session: aiohttp.ClientSession): async with session.get(url) as resp: if resp.status != 200: raise ConnectionError(f'Unable to connect to QRZ (HTTP Error {resp.status})') - resp_xml = etree.parse(BytesIO(await resp.read())).getroot() + with BytesIO(await resp.read()) as resp_file: + resp_xml = etree.parse(resp_file).getroot() resp_xml_session = resp_xml.xpath('/x:QRZDatabase/x:Session', namespaces={'x': 'http://xmldata.qrz.com'}) @@ -116,7 +118,8 @@ async def qrz_test_session(key: str, session: aiohttp.ClientSession): async with session.get(url) as resp: if resp.status != 200: raise ConnectionError(f'Unable to connect to QRZ (HTTP Error {resp.status})') - resp_xml = etree.parse(BytesIO(await resp.read())).getroot() + with BytesIO(await resp.read()) as resp_file: + resp_xml = etree.parse(resp_file).getroot() resp_xml_session = resp_xml.xpath('/x:QRZDatabase/x:Session', namespaces={'x': 'http://xmldata.qrz.com'})