diff options
author | Victor Stinner <victor.stinner@gmail.com> | 2014-03-17 21:38:41 (GMT) |
---|---|---|
committer | Victor Stinner <victor.stinner@gmail.com> | 2014-03-17 21:38:41 (GMT) |
commit | d6a91a7ab69fe449259d8719acf63cca9af45ba0 (patch) | |
tree | 417a68e33635f534e1f3d4cd4333de832622af3e /Lib/urllib | |
parent | 2a6053468ea318acc2b382808f858d853cf3a89a (diff) | |
download | cpython-d6a91a7ab69fe449259d8719acf63cca9af45ba0.zip cpython-d6a91a7ab69fe449259d8719acf63cca9af45ba0.tar.gz cpython-d6a91a7ab69fe449259d8719acf63cca9af45ba0.tar.bz2 |
Issue #20879: Delay the initialization of encoding and decoding tables for
base32, ascii85 and base85 codecs in the base64 module, and delay the
initialization of the unquote_to_bytes() table of the urllib.parse module, to
not waste memory if these modules are not used.
Diffstat (limited to 'Lib/urllib')
-rw-r--r-- | Lib/urllib/parse.py | 9 |
1 files changed, 7 insertions, 2 deletions
diff --git a/Lib/urllib/parse.py b/Lib/urllib/parse.py index 2ba3991..a2a912d 100644 --- a/Lib/urllib/parse.py +++ b/Lib/urllib/parse.py @@ -472,8 +472,7 @@ def urldefrag(url): return _coerce_result(DefragResult(defrag, frag)) _hexdig = '0123456789ABCDEFabcdef' -_hextobyte = {(a + b).encode(): bytes([int(a + b, 16)]) - for a in _hexdig for b in _hexdig} +_hextobyte = None def unquote_to_bytes(string): """unquote_to_bytes('abc%20def') -> b'abc def'.""" @@ -490,6 +489,12 @@ def unquote_to_bytes(string): return string res = [bits[0]] append = res.append + # Delay the initialization of the table to not waste memory + # if the function is never called + global _hextobyte + if _hextobyte is None: + _hextobyte = {(a + b).encode(): bytes([int(a + b, 16)]) + for a in _hexdig for b in _hexdig} for item in bits[1:]: try: append(_hextobyte[item[:2]]) |