|
55 | 55 | import importlib |
56 | 56 | import sys |
57 | 57 | import unittest |
| 58 | +import urllib.parse |
| 59 | +from typing import Callable |
58 | 60 | from unittest import mock |
59 | 61 |
|
60 | 62 | from tests._support import load_module, purge_modules, reimport_once_per_class |
|
106 | 108 | #: running the test until it stopped complaining, and that distinction is the |
107 | 109 | #: point of having it. Every import statement on a branch reachable at ``(3, 5)``: |
108 | 110 | #: :mod:`collections.abc` (line 34), :mod:`pathlib` (63), :mod:`threading` (67), |
109 | | -#: :mod:`typing` (4, 68, 126), :mod:`aenum` (143), :mod:`typing_extensions` (148, |
110 | | -#: 202), :mod:`contextlib` (153), :mod:`decimal` (154), :mod:`enum` (179). |
| 111 | +#: :mod:`typing` (4, 68, 126), :mod:`urllib.parse` (137, 138), :mod:`aenum` (173), |
| 112 | +#: :mod:`typing_extensions` (191, 245), :mod:`contextlib` (196), :mod:`decimal` |
| 113 | +#: (197), :mod:`enum` (222). |
111 | 114 | #: |
112 | 115 | #: Four of those never actually surface, and *why* is the point. Three -- |
113 | 116 | #: :mod:`collections.abc`, :mod:`contextlib` and :mod:`pathlib` -- are imported at |
|
118 | 121 | #: why the four are on the list despite costing a :data:`sys.modules` lookup each |
119 | 122 | #: and nothing else. Measured: with only the five that do surface warmed and |
120 | 123 | #: :mod:`pathlib` forced cold, the window leaks ``['pathlib', 'pathlib._os']``; |
121 | | -#: with the full list it is empty even with five of the nine forced cold. |
| 124 | +#: with the full list, then nine names, it is empty even with five of them forced |
| 125 | +#: cold. :mod:`urllib.parse`, added since for issue #1693, is warm before this |
| 126 | +#: module runs on 3.8 to 3.10, where :mod:`pathlib` imports it, and cold on 3.14 |
| 127 | +#: (both under ``python -S``). |
122 | 128 | WARM_BEFORE_FAKING = ('aenum', 'collections.abc', 'contextlib', 'decimal', 'enum', |
123 | | - 'pathlib', 'threading', 'typing', 'typing_extensions') |
| 129 | + 'pathlib', 'threading', 'typing', 'typing_extensions', 'urllib.parse') |
124 | 130 |
|
125 | 131 | #: Prefix of the names the loaders themselves write, which are excluded from the |
126 | 132 | #: "nothing was imported under the fake" check. :func:`tests._support.load_module` |
|
141 | 147 | #: assertion is aimed there. |
142 | 148 | OWN_MODULE_PREFIX = 'pcapkit' |
143 | 149 |
|
| 150 | +#: ``(value, encoding, errors, outcome)`` for ``compat.unquote``, each as Python |
| 151 | +#: 3.9's :func:`urllib.parse.unquote` has it: the text returned, or the exception |
| 152 | +#: type and message raised. ``None`` keywords leave the defaults in place. Before |
| 153 | +#: 3.9 every :obj:`bytes` row raised ``TypeError: Expected str, got bytes`` (#1693). |
| 154 | +UNQUOTED = ( |
| 155 | + (b'', 'utf-8', 'strict', ''), |
| 156 | + (b'a%2fb%2F', 'utf-8', 'strict', 'a/b/'), |
| 157 | + (b'a+b%20c', 'utf-8', 'strict', 'a+b c'), |
| 158 | + (b'%zz%', 'utf-8', 'strict', '%zz%'), |
| 159 | + (b'caf%E9', 'latin-1', 'strict', 'café'), |
| 160 | + (b'%FF%FEa%00', 'utf-16', 'strict', 'a'), |
| 161 | + (b'%E9', 'utf-8', 'ignore', ''), |
| 162 | + (b'%E9', None, None, '\ufffd'), |
| 163 | + (b'caf%C3%A9', None, None, 'café'), |
| 164 | + (b'%E9', 'utf-8', 'strict', |
| 165 | + (UnicodeDecodeError, "'utf-8' codec can't decode byte 0xe9 in position 0: unexpected end of data")), |
| 166 | + (b'\xff%41', 'utf-8', 'strict', |
| 167 | + (UnicodeDecodeError, "'utf-8' codec can't decode byte 0xff in position 0: invalid start byte")), |
| 168 | + ('a%20b', 'utf-8', 'strict', 'a b'), |
| 169 | + ('caf%E9', 'latin-1', 'strict', 'café'), |
| 170 | + ('%E9', None, None, '\ufffd'), |
| 171 | + (bytearray(b'a%20b'), 'utf-8', 'strict', (TypeError, "a bytes-like object is required, not 'str'")), |
| 172 | + (memoryview(b'a%20b'), 'utf-8', 'strict', (AttributeError, "'memoryview' object has no attribute 'split'")), |
| 173 | +) |
| 174 | + |
144 | 175 |
|
145 | 176 | def aenum_pyver_readings() -> 'dict[str, tuple[int, ...]]': |
146 | 177 | """``pyver`` as every imported :mod:`aenum` module currently sees it. |
@@ -286,6 +317,26 @@ def test_localcontext_applies_keyword_overrides(self) -> None: |
286 | 317 | self.assertEqual(decimal.getcontext().prec, 7) |
287 | 318 | self.assertEqual(decimal.getcontext().prec, original_prec) |
288 | 319 |
|
| 320 | + def assert_unquotes_as_python39(self, unquote: 'Callable[..., str]') -> None: |
| 321 | + """``unquote`` gives each :data:`UNQUOTED` row its outcome.""" |
| 322 | + for value, encoding, errors, outcome in UNQUOTED: |
| 323 | + kwargs = {} if encoding is None else {'encoding': encoding, 'errors': errors} |
| 324 | + with self.subTest(value=value, **kwargs): |
| 325 | + if isinstance(outcome, str): |
| 326 | + self.assertEqual(unquote(value, **kwargs), outcome) |
| 327 | + else: |
| 328 | + with self.assertRaises(outcome[0]) as raised: |
| 329 | + unquote(value, **kwargs) |
| 330 | + self.assertEqual(str(raised.exception), outcome[1]) |
| 331 | + |
| 332 | + def test_unquote_decodes_bytes_as_python39_does(self) -> None: |
| 333 | + """From 3.9 the name is :func:`urllib.parse.unquote` itself (#1693).""" |
| 334 | + if sys.version_info >= (3, 9): |
| 335 | + self.assertIs(self.compat.unquote, urllib.parse.unquote) |
| 336 | + else: |
| 337 | + self.assertIsNot(self.compat.unquote, urllib.parse.unquote) |
| 338 | + self.assert_unquotes_as_python39(self.compat.unquote) |
| 339 | + |
289 | 340 | def test_show_flag_values_returns_individual_bits(self) -> None: |
290 | 341 | class DemoFlag(enum.IntFlag): |
291 | 342 | READ = 1 |
@@ -409,6 +460,19 @@ class SlotDemo: |
409 | 460 | self.assertEqual(ctx.prec, 9) |
410 | 461 | self.assertEqual(decimal.getcontext().prec, original_prec) |
411 | 462 |
|
| 463 | + # The table again with the bytes rows kept from an unquote that rejects |
| 464 | + # them, as every interpreter's did before 3.9 (#1693). |
| 465 | + self.assertIsNot(compat.unquote, urllib.parse.unquote) |
| 466 | + self.assert_unquotes_as_python39(compat.unquote) |
| 467 | + |
| 468 | + def unquote_before_3_9(string: 'str', *args: 'str') -> 'str': |
| 469 | + if isinstance(string, bytes): |
| 470 | + raise TypeError('Expected str, got bytes') |
| 471 | + return urllib.parse.unquote(string, *args) |
| 472 | + |
| 473 | + with mock.patch.object(compat, '_unquote', unquote_before_3_9): |
| 474 | + self.assert_unquotes_as_python39(compat.unquote) |
| 475 | + |
412 | 476 | class OldFlag(enum.IntFlag): |
413 | 477 | READ = 1 |
414 | 478 | EXEC = 4 |
|
0 commit comments