[Python-checkins] bpo-46565: `del` loop vars that are leaking into module namespaces (GH-30993)

serhiy-storchaka webhook-mailer at python.org
Thu Feb 3 04:20:23 EST 2022


https://github.com/python/cpython/commit/0cbdd2131195b0d313762968f604e80a3e65ca9f
commit: 0cbdd2131195b0d313762968f604e80a3e65ca9f
branch: main
author: Nikita Sobolev <mail at sobolevn.me>
committer: serhiy-storchaka <storchaka at gmail.com>
date: 2022-02-03T11:20:08+02:00
summary:

bpo-46565: `del` loop vars that are leaking into module namespaces (GH-30993)

files:
A Misc/NEWS.d/next/Library/2022-01-28-19-48-31.bpo-46565.bpZXO4.rst
M Lib/_compat_pickle.py
M Lib/email/contentmanager.py
M Lib/email/quoprimime.py
M Lib/http/cookiejar.py
M Lib/inspect.py
M Lib/json/encoder.py
M Lib/lib2to3/pgen2/grammar.py
M Lib/locale.py
M Lib/multiprocessing/managers.py
M Lib/multiprocessing/process.py
M Lib/sysconfig.py
M Lib/test/test_inspect.py
M Lib/tokenize.py

diff --git a/Lib/_compat_pickle.py b/Lib/_compat_pickle.py
index f68496ae639f5..65a94b6b1bdfd 100644
--- a/Lib/_compat_pickle.py
+++ b/Lib/_compat_pickle.py
@@ -249,3 +249,4 @@
 
 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 fcf278dbccbac..b4f5830beada4 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 94534f7ee1e33..27fcbb5a26e3a 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 eaa76c26b9c59..ee433c0f78f4a 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 d47f5b717471c..eb45f81aa2d95 100644
--- a/Lib/inspect.py
+++ b/Lib/inspect.py
@@ -156,6 +156,7 @@
 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 21bff2c1a1fca..864f46d9dbb72 100644
--- a/Lib/json/encoder.py
+++ b/Lib/json/encoder.py
@@ -30,6 +30,7 @@
 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 6a4d575ac2ccf..5d550aeb65e8d 100644
--- a/Lib/lib2to3/pgen2/grammar.py
+++ b/Lib/lib2to3/pgen2/grammar.py
@@ -186,3 +186,4 @@ def report(self):
     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 6d4f51929923f..4bd31c9fa2cdf 100644
--- a/Lib/locale.py
+++ b/Lib/locale.py
@@ -746,6 +746,7 @@ def getpreferredencoding(do_setlocale=True):
 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 cf637c6cbbe8d..d97381926d47b 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 0b2e0b45b2397..3917d2e4fa63e 100644
--- a/Lib/multiprocessing/process.py
+++ b/Lib/multiprocessing/process.py
@@ -427,6 +427,7 @@ def close(self):
 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 ef335c69421f5..d4a8a680286c1 100644
--- a/Lib/sysconfig.py
+++ b/Lib/sysconfig.py
@@ -192,6 +192,7 @@ def is_python_build(check_home=False):
         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 76fa6f7e2dab8..a553431bdccfb 100644
--- a/Lib/test/test_inspect.py
+++ b/Lib/test/test_inspect.py
@@ -108,7 +108,7 @@ def istest(self, predicate, exp):
             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 0b9e238310049..46d2224f5cc08 100644
--- a/Lib/tokenize.py
+++ b/Lib/tokenize.py
@@ -143,6 +143,7 @@ def _compile(expr):
     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 @@ def _compile(expr):
         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 0000000000000..9b0969ea5897b
--- /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.



More information about the Python-checkins mailing list