bpo-44002: Switch to lru_cache in urllib.parse. (GH-25798)

Switch to lru_cache in urllib.parse.

urllib.parse now uses functool.lru_cache for its internal URL splitting and
quoting caches instead of rolling its own like its the 90s.

The undocumented internal Quoted class API is now deprecated
as it had no reason to be public and no existing OSS users were found.

The clear_cache() API remains undocumented but gets an explicit test as it
is used in a few projects' (twisted, gevent) tests as well as our own regrtest.
This commit is contained in:
Gregory P. Smith 2021-05-11 17:01:44 -07:00 committed by GitHub
parent e9d7f88d56
commit d597fdc5fd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 50 additions and 31 deletions

View file

@ -1044,16 +1044,24 @@ class UrlParseTestCase(unittest.TestCase):
self.assertEqual(p1.params, 'phone-context=+1-914-555')
def test_Quoter_repr(self):
quoter = urllib.parse.Quoter(urllib.parse._ALWAYS_SAFE)
quoter = urllib.parse._Quoter(urllib.parse._ALWAYS_SAFE)
self.assertIn('Quoter', repr(quoter))
def test_clear_cache_for_code_coverage(self):
urllib.parse.clear_cache()
def test_urllib_parse_getattr_failure(self):
"""Test that urllib.parse.__getattr__() fails correctly."""
with self.assertRaises(AttributeError):
unused = urllib.parse.this_does_not_exist
def test_all(self):
expected = []
undocumented = {
'splitattr', 'splithost', 'splitnport', 'splitpasswd',
'splitport', 'splitquery', 'splittag', 'splittype', 'splituser',
'splitvalue',
'Quoter', 'ResultBase', 'clear_cache', 'to_bytes', 'unwrap',
'ResultBase', 'clear_cache', 'to_bytes', 'unwrap',
}
for name in dir(urllib.parse):
if name.startswith('_') or name in undocumented:
@ -1245,6 +1253,12 @@ class Utility_Tests(unittest.TestCase):
class DeprecationTest(unittest.TestCase):
def test_Quoter_deprecation(self):
with self.assertWarns(DeprecationWarning) as cm:
old_class = urllib.parse.Quoter
self.assertIs(old_class, urllib.parse._Quoter)
self.assertIn('Quoter will be removed', str(cm.warning))
def test_splittype_deprecation(self):
with self.assertWarns(DeprecationWarning) as cm:
urllib.parse.splittype('')