From 0cbdd2131195b0d313762968f604e80a3e65ca9f Mon Sep 17 00:00:00 2001 From: Nikita Sobolev Date: Thu, 3 Feb 2022 12:20:08 +0300 Subject: bpo-46565: `del` loop vars that are leaking into module namespaces (GH-30993) --- Lib/_compat_pickle.py | 1 + Lib/email/contentmanager.py | 3 +++ Lib/email/quoprimime.py | 1 + Lib/http/cookiejar.py | 3 +-- Lib/inspect.py | 1 + Lib/json/encoder.py | 1 + Lib/lib2to3/pgen2/grammar.py | 1 + Lib/locale.py | 1 + Lib/multiprocessing/managers.py | 10 +++++----- Lib/multiprocessing/process.py | 1 + Lib/sysconfig.py | 1 + Lib/test/test_inspect.py | 2 +- Lib/tokenize.py | 2 ++ .../next/Library/2022-01-28-19-48-31.bpo-46565.bpZXO4.rst | 1 + 14 files changed, 21 insertions(+), 8 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2022-01-28-19-48-31.bpo-46565.bpZXO4.rst diff --git a/Lib/_compat_pickle.py b/Lib/_compat_pickle.py index f68496a..65a94b6 100644 --- a/Lib/_compat_pickle.py +++ b/Lib/_compat_pickle.py @@ -249,3 +249,4 @@ PYTHON3_IMPORTERROR_EXCEPTIONS = ( for excname in PYTHON3_IMPORTERROR_EXCEPTIONS: REVERSE_NAME_MAPPING[('builtins', excname)] = ('exceptions', 'ImportError') +del excname diff --git a/Lib/email/contentmanager.py b/Lib/email/contentmanager.py index fcf278d..b4f5830 100644 --- a/Lib/email/contentmanager.py +++ b/Lib/email/contentmanager.py @@ -72,12 +72,14 @@ def get_non_text_content(msg): return msg.get_payload(decode=True) for maintype in 'audio image video application'.split(): raw_data_manager.add_get_handler(maintype, get_non_text_content) +del maintype def get_message_content(msg): return msg.get_payload(0) for subtype in 'rfc822 external-body'.split(): raw_data_manager.add_get_handler('message/'+subtype, get_message_content) +del subtype def get_and_fixup_unknown_message_content(msg): @@ -246,3 +248,4 @@ def set_bytes_content(msg, data, maintype, subtype, cte='base64', _finalize_set(msg, disposition, filename, cid, params) for typ in (bytes, bytearray, memoryview): raw_data_manager.add_set_handler(typ, set_bytes_content) +del typ diff --git a/Lib/email/quoprimime.py b/Lib/email/quoprimime.py index 94534f7..27fcbb5 100644 --- a/Lib/email/quoprimime.py +++ b/Lib/email/quoprimime.py @@ -148,6 +148,7 @@ def header_encode(header_bytes, charset='iso-8859-1'): _QUOPRI_BODY_ENCODE_MAP = _QUOPRI_BODY_MAP[:] for c in b'\r\n': _QUOPRI_BODY_ENCODE_MAP[c] = chr(c) +del c def body_encode(body, maxlinelen=76, eol=NL): """Encode with quoted-printable, wrapping at maxlinelen characters. diff --git a/Lib/http/cookiejar.py b/Lib/http/cookiejar.py index eaa76c2..ee433c0 100644 --- a/Lib/http/cookiejar.py +++ b/Lib/http/cookiejar.py @@ -89,8 +89,7 @@ def _timegm(tt): DAYS = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"] MONTHS = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"] -MONTHS_LOWER = [] -for month in MONTHS: MONTHS_LOWER.append(month.lower()) +MONTHS_LOWER = [month.lower() for month in MONTHS] def time2isoz(t=None): """Return a string representing time in seconds since epoch, t. diff --git a/Lib/inspect.py b/Lib/inspect.py index d47f5b7..eb45f81 100644 --- a/Lib/inspect.py +++ b/Lib/inspect.py @@ -156,6 +156,7 @@ from collections import namedtuple, OrderedDict mod_dict = globals() for k, v in dis.COMPILER_FLAG_NAMES.items(): mod_dict["CO_" + v] = k +del k, v, mod_dict # See Include/object.h TPFLAGS_IS_ABSTRACT = 1 << 20 diff --git a/Lib/json/encoder.py b/Lib/json/encoder.py index 21bff2c..864f46d 100644 --- a/Lib/json/encoder.py +++ b/Lib/json/encoder.py @@ -30,6 +30,7 @@ ESCAPE_DCT = { for i in range(0x20): ESCAPE_DCT.setdefault(chr(i), '\\u{0:04x}'.format(i)) #ESCAPE_DCT.setdefault(chr(i), '\\u%04x' % (i,)) +del i INFINITY = float('inf') diff --git a/Lib/lib2to3/pgen2/grammar.py b/Lib/lib2to3/pgen2/grammar.py index 6a4d575..5d550ae 100644 --- a/Lib/lib2to3/pgen2/grammar.py +++ b/Lib/lib2to3/pgen2/grammar.py @@ -186,3 +186,4 @@ for line in opmap_raw.splitlines(): if line: op, name = line.split() opmap[op] = getattr(token, name) +del line, op, name diff --git a/Lib/locale.py b/Lib/locale.py index 6d4f519..4bd31c9 100644 --- a/Lib/locale.py +++ b/Lib/locale.py @@ -746,6 +746,7 @@ locale_encoding_alias = { for k, v in sorted(locale_encoding_alias.items()): k = k.replace('_', '') locale_encoding_alias.setdefault(k, v) +del k, v # # The locale_alias table maps lowercase alias names to C locale names diff --git a/Lib/multiprocessing/managers.py b/Lib/multiprocessing/managers.py index cf637c6..d973819 100644 --- a/Lib/multiprocessing/managers.py +++ b/Lib/multiprocessing/managers.py @@ -49,11 +49,11 @@ def reduce_array(a): reduction.register(array.array, reduce_array) view_types = [type(getattr({}, name)()) for name in ('items','keys','values')] -if view_types[0] is not list: # only needed in Py3.0 - def rebuild_as_list(obj): - return list, (list(obj),) - for view_type in view_types: - reduction.register(view_type, rebuild_as_list) +def rebuild_as_list(obj): + return list, (list(obj),) +for view_type in view_types: + reduction.register(view_type, rebuild_as_list) +del view_type, view_types # # Type for identifying shared objects diff --git a/Lib/multiprocessing/process.py b/Lib/multiprocessing/process.py index 0b2e0b4..3917d2e 100644 --- a/Lib/multiprocessing/process.py +++ b/Lib/multiprocessing/process.py @@ -427,6 +427,7 @@ _exitcode_to_name = {} for name, signum in list(signal.__dict__.items()): if name[:3]=='SIG' and '_' not in name: _exitcode_to_name[-signum] = f'-{name}' +del name, signum # For debug and leak testing _dangling = WeakSet() diff --git a/Lib/sysconfig.py b/Lib/sysconfig.py index ef335c6..d4a8a68 100644 --- a/Lib/sysconfig.py +++ b/Lib/sysconfig.py @@ -192,6 +192,7 @@ if _PYTHON_BUILD: scheme['headers'] = scheme['include'] scheme['include'] = '{srcdir}/Include' scheme['platinclude'] = '{projectbase}/.' + del scheme def _subst_vars(s, local_vars): diff --git a/Lib/test/test_inspect.py b/Lib/test/test_inspect.py index 76fa6f7..a553431 100644 --- a/Lib/test/test_inspect.py +++ b/Lib/test/test_inspect.py @@ -108,7 +108,7 @@ class IsTestBase(unittest.TestCase): self.assertFalse(other(obj), 'not %s(%s)' % (other.__name__, exp)) def test__all__(self): - support.check__all__(self, inspect, not_exported=("k", "v", "mod_dict", "modulesbyfile")) + support.check__all__(self, inspect, not_exported=("modulesbyfile",)) def generator_function_example(self): for i in range(2): diff --git a/Lib/tokenize.py b/Lib/tokenize.py index 0b9e238..46d2224 100644 --- a/Lib/tokenize.py +++ b/Lib/tokenize.py @@ -143,6 +143,7 @@ for _prefix in _all_string_prefixes(): endpats[_prefix + '"'] = Double endpats[_prefix + "'''"] = Single3 endpats[_prefix + '"""'] = Double3 +del _prefix # A set of all of the single and triple quoted string prefixes, # including the opening quotes. @@ -153,6 +154,7 @@ for t in _all_string_prefixes(): single_quoted.add(u) for u in (t + '"""', t + "'''"): triple_quoted.add(u) +del t, u tabsize = 8 diff --git a/Misc/NEWS.d/next/Library/2022-01-28-19-48-31.bpo-46565.bpZXO4.rst b/Misc/NEWS.d/next/Library/2022-01-28-19-48-31.bpo-46565.bpZXO4.rst new file mode 100644 index 0000000..9b0969e --- /dev/null +++ b/Misc/NEWS.d/next/Library/2022-01-28-19-48-31.bpo-46565.bpZXO4.rst @@ -0,0 +1 @@ +Remove loop variables that are leaking into modules' namespaces. -- cgit v0.12