diff options
author | Vlastimil Zíma <vlastimil.zima@nic.cz> | 2018-05-22 09:46:00 +0200 |
---|---|---|
committer | Vlastimil Zíma <vlastimil.zima@nic.cz> | 2018-05-31 09:19:50 +0200 |
commit | 7e8fd5130d61ee581fd677204ee96c5f60612223 (patch) | |
tree | bf8363350e9b4119a9bf90e6d03daac75cda1d7f /openid | |
parent | c7cacf71077a358732f9695a49f78e0231ef03c4 (diff) | |
download | openid-7e8fd5130d61ee581fd677204ee96c5f60612223.tar.gz |
Clean unclosed file warnings
Diffstat (limited to 'openid')
-rw-r--r-- | openid/test/discoverdata.py | 3 | ||||
-rw-r--r-- | openid/test/test_accept.py | 8 | ||||
-rw-r--r-- | openid/test/test_discover.py | 3 | ||||
-rw-r--r-- | openid/test/test_etxrd.py | 12 | ||||
-rw-r--r-- | openid/test/test_fetchers.py | 42 |
5 files changed, 42 insertions, 26 deletions
diff --git a/openid/test/discoverdata.py b/openid/test/discoverdata.py index 1cc1484..4c8430f 100644 --- a/openid/test/discoverdata.py +++ b/openid/test/discoverdata.py @@ -59,7 +59,8 @@ discover_tests = {} def readTests(filename): - data = open(filename).read() + with open(filename) as data_file: + data = data_file.read() tests = {} for case in data.split('\f\n'): (name, content) = case.split('\n', 1) diff --git a/openid/test/test_accept.py b/openid/test/test_accept.py index 8acea20..b10934a 100644 --- a/openid/test/test_accept.py +++ b/openid/test/test_accept.py @@ -13,11 +13,9 @@ def getTestData(): () -> [(int, six.text_type)] """ filename = os.path.join(os.path.dirname(__file__), 'data', 'accept.txt') - i = 1 - lines = [] - for line in open(filename, 'rb'): - lines.append((i, line.decode('utf-8'))) - i += 1 + with open(filename, 'rb') as data_file: + content = data_file.read().decode('utf-8') + lines = enumerate(content.splitlines(), start=1) return lines diff --git a/openid/test/test_discover.py b/openid/test/test_discover.py index 5e56a77..75b7ab8 100644 --- a/openid/test/test_discover.py +++ b/openid/test/test_discover.py @@ -206,7 +206,8 @@ def readDataFile(filename): module_directory = os.path.dirname(os.path.abspath(__file__)) filename = os.path.join( module_directory, 'data', 'test_discover', filename) - return open(filename, 'rb').read() + with open(filename, 'rb') as data_file: + return data_file.read() class TestDiscovery(BaseTestDiscovery): diff --git a/openid/test/test_etxrd.py b/openid/test/test_etxrd.py index bc7d78f..c945583 100644 --- a/openid/test/test_etxrd.py +++ b/openid/test/test_etxrd.py @@ -88,7 +88,8 @@ class TestParseXRDS(unittest.TestCase): class TestServiceParser(unittest.TestCase): def setUp(self): - self.xmldoc = open(XRD_FILE, 'rb').read() + with open(XRD_FILE, 'rb') as xrd_file: + self.xmldoc = xrd_file.read() self.yadis_url = 'http://unittest.url/' def _getServices(self, flt=None): @@ -156,7 +157,8 @@ class TestServiceParser(unittest.TestCase): def testNoXRDS(self): """Make sure that we get an exception when an XRDS element is not present""" - self.xmldoc = open(NOXRDS_FILE, 'rb').read() + with open(NOXRDS_FILE, 'rb') as xml_file: + self.xmldoc = xml_file.read() self.assertRaises(etxrd.XRDSError, services.applyFilter, self.yadis_url, self.xmldoc, None) def testEmpty(self): @@ -168,7 +170,8 @@ class TestServiceParser(unittest.TestCase): def testNoXRD(self): """Make sure that we get an exception when there is no XRD element present.""" - self.xmldoc = open(NOXRD_FILE, 'rb').read() + with open(NOXRD_FILE, 'rb') as xml_file: + self.xmldoc = xml_file.read() self.assertRaises(etxrd.XRDSError, services.applyFilter, self.yadis_url, self.xmldoc, None) @@ -181,7 +184,8 @@ class TestCanonicalID(unittest.TestCase): filename = datapath(filename) def test(self): - xrds = etxrd.parseXRDS(open(filename, 'rb').read()) + with open(filename, 'rb') as xrds_file: + xrds = etxrd.parseXRDS(xrds_file.read()) self._getCanonicalID(iname, xrds, expectedID) return test diff --git a/openid/test/test_fetchers.py b/openid/test/test_fetchers.py index 44070ff..d7b03d1 100644 --- a/openid/test/test_fetchers.py +++ b/openid/test/test_fetchers.py @@ -176,21 +176,18 @@ class FetcherTestHandler(BaseHTTPRequestHandler): pass def do_GET(self): - if self.path == '/closed': - self.wfile.close() + try: + http_code, location = self.cases[self.path] + except KeyError: + self.errorResponse('Bad path') else: - try: - http_code, location = self.cases[self.path] - except KeyError: - self.errorResponse('Bad path') - else: - extra_headers = [('Content-type', 'text/plain')] - if location is not None: - host, port = self.server.server_address - base = ('http://%s:%s' % (socket.getfqdn(host), port,)) - location = base + location - extra_headers.append(('Location', location)) - self._respond(http_code, extra_headers, self.path) + extra_headers = [('Content-type', 'text/plain')] + if location is not None: + host, port = self.server.server_address + base = ('http://%s:%s' % (socket.getfqdn(host), port,)) + location = base + location + extra_headers.append(('Location', location)) + self._respond(http_code, extra_headers, self.path) def do_POST(self): try: @@ -227,7 +224,6 @@ class FetcherTestHandler(BaseHTTPRequestHandler): self.send_header(k, v) self.end_headers() self.wfile.write(body.encode('utf-8')) - self.wfile.close() def finish(self): if not self.wfile.closed: @@ -235,6 +231,22 @@ class FetcherTestHandler(BaseHTTPRequestHandler): self.wfile.close() self.rfile.close() + def parse_request(self): + """Contain a hook to simulate closed connection.""" + # Parse the request first + # BaseHTTPRequestHandler is old style class in 2.7 + if type(FetcherTestHandler) == type: + result = super(FetcherTestHandler, self).parse_request() + else: + result = BaseHTTPRequestHandler.parse_request(self) + # If the connection should be closed, do so. + if self.path == '/closed': + self.wfile.close() + return False + else: + # Otherwise continue as usual. + return result + class TestFetchers(unittest.TestCase): def test(self): |