gh-131807: fix ResourceWarning in test_ucn.py (#131808)

Co-authored-by: Victor Stinner <vstinner@python.org>
This commit is contained in:
Thomas Grainger 2025-03-28 14:55:40 +00:00 committed by GitHub
parent 27d1443897
commit adb67ed7e4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -10,6 +10,7 @@ Modified for Python 2.0 by Fredrik Lundh (fredrik@pythonware.com)
import ast import ast
import unittest import unittest
import unicodedata import unicodedata
import urllib.error
from test import support from test import support
from http.client import HTTPException from http.client import HTTPException
@ -181,20 +182,23 @@ class UnicodeNamesTest(unittest.TestCase):
try: try:
testdata = support.open_urlresource(url, encoding="utf-8", testdata = support.open_urlresource(url, encoding="utf-8",
check=check_version) check=check_version)
except (OSError, HTTPException): except urllib.error.HTTPError as exc:
self.skipTest("Could not retrieve " + url) exc.close()
self.addCleanup(testdata.close) self.skipTest(f"Could not retrieve {url}: {exc!r}")
for line in testdata: except (OSError, HTTPException) as exc:
line = line.strip() self.skipTest(f"Could not retrieve {url}: {exc!r}")
if not line or line.startswith('#'): with testdata:
continue for line in testdata:
seqname, codepoints = line.split(';') line = line.strip()
codepoints = ''.join(chr(int(cp, 16)) for cp in codepoints.split()) if not line or line.startswith('#'):
self.assertEqual(unicodedata.lookup(seqname), codepoints) continue
with self.assertRaises(SyntaxError): seqname, codepoints = line.split(';')
self.checkletter(seqname, None) codepoints = ''.join(chr(int(cp, 16)) for cp in codepoints.split())
with self.assertRaises(KeyError): self.assertEqual(unicodedata.lookup(seqname), codepoints)
unicodedata.ucd_3_2_0.lookup(seqname) with self.assertRaises(SyntaxError):
self.checkletter(seqname, None)
with self.assertRaises(KeyError):
unicodedata.ucd_3_2_0.lookup(seqname)
def test_errors(self): def test_errors(self):
self.assertRaises(TypeError, unicodedata.name) self.assertRaises(TypeError, unicodedata.name)