Skip to content

Commit adb67ed

Browse files
graingertvstinner
andauthored
gh-131807: fix ResourceWarning in test_ucn.py (#131808)
Co-authored-by: Victor Stinner <[email protected]>
1 parent 27d1443 commit adb67ed

File tree

1 file changed

+18
-14
lines changed

1 file changed

+18
-14
lines changed

Lib/test/test_ucn.py

+18-14
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import ast
1111
import unittest
1212
import unicodedata
13+
import urllib.error
1314

1415
from test import support
1516
from http.client import HTTPException
@@ -181,20 +182,23 @@ def check_version(testfile):
181182
try:
182183
testdata = support.open_urlresource(url, encoding="utf-8",
183184
check=check_version)
184-
except (OSError, HTTPException):
185-
self.skipTest("Could not retrieve " + url)
186-
self.addCleanup(testdata.close)
187-
for line in testdata:
188-
line = line.strip()
189-
if not line or line.startswith('#'):
190-
continue
191-
seqname, codepoints = line.split(';')
192-
codepoints = ''.join(chr(int(cp, 16)) for cp in codepoints.split())
193-
self.assertEqual(unicodedata.lookup(seqname), codepoints)
194-
with self.assertRaises(SyntaxError):
195-
self.checkletter(seqname, None)
196-
with self.assertRaises(KeyError):
197-
unicodedata.ucd_3_2_0.lookup(seqname)
185+
except urllib.error.HTTPError as exc:
186+
exc.close()
187+
self.skipTest(f"Could not retrieve {url}: {exc!r}")
188+
except (OSError, HTTPException) as exc:
189+
self.skipTest(f"Could not retrieve {url}: {exc!r}")
190+
with testdata:
191+
for line in testdata:
192+
line = line.strip()
193+
if not line or line.startswith('#'):
194+
continue
195+
seqname, codepoints = line.split(';')
196+
codepoints = ''.join(chr(int(cp, 16)) for cp in codepoints.split())
197+
self.assertEqual(unicodedata.lookup(seqname), codepoints)
198+
with self.assertRaises(SyntaxError):
199+
self.checkletter(seqname, None)
200+
with self.assertRaises(KeyError):
201+
unicodedata.ucd_3_2_0.lookup(seqname)
198202

199203
def test_errors(self):
200204
self.assertRaises(TypeError, unicodedata.name)

0 commit comments

Comments
 (0)