]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.12] gh-131807: fix ResourceWarning in test_ucn.py (GH-131808) (#131846)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Fri, 28 Mar 2025 15:12:59 +0000 (16:12 +0100)
committerGitHub <noreply@github.com>
Fri, 28 Mar 2025 15:12:59 +0000 (15:12 +0000)
gh-131807: fix ResourceWarning in test_ucn.py (GH-131808)
(cherry picked from commit adb67ed7e465410829ac0b1f903ec5678e0e51cc)

Co-authored-by: Thomas Grainger <tagrain@gmail.com>
Co-authored-by: Victor Stinner <vstinner@python.org>
Lib/test/test_ucn.py

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