Skip to content

Commit 0cbdd21

Browse files
authored
bpo-46565: del loop vars that are leaking into module namespaces (GH-30993)
1 parent 6394e98 commit 0cbdd21

14 files changed

+21
-8
lines changed

Lib/_compat_pickle.py

+1
Original file line numberDiff line numberDiff line change
@@ -249,3 +249,4 @@
249249

250250
for excname in PYTHON3_IMPORTERROR_EXCEPTIONS:
251251
REVERSE_NAME_MAPPING[('builtins', excname)] = ('exceptions', 'ImportError')
252+
del excname

Lib/email/contentmanager.py

+3
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,14 @@ def get_non_text_content(msg):
7272
return msg.get_payload(decode=True)
7373
for maintype in 'audio image video application'.split():
7474
raw_data_manager.add_get_handler(maintype, get_non_text_content)
75+
del maintype
7576

7677

7778
def get_message_content(msg):
7879
return msg.get_payload(0)
7980
for subtype in 'rfc822 external-body'.split():
8081
raw_data_manager.add_get_handler('message/'+subtype, get_message_content)
82+
del subtype
8183

8284

8385
def get_and_fixup_unknown_message_content(msg):
@@ -246,3 +248,4 @@ def set_bytes_content(msg, data, maintype, subtype, cte='base64',
246248
_finalize_set(msg, disposition, filename, cid, params)
247249
for typ in (bytes, bytearray, memoryview):
248250
raw_data_manager.add_set_handler(typ, set_bytes_content)
251+
del typ

Lib/email/quoprimime.py

+1
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ def header_encode(header_bytes, charset='iso-8859-1'):
148148
_QUOPRI_BODY_ENCODE_MAP = _QUOPRI_BODY_MAP[:]
149149
for c in b'\r\n':
150150
_QUOPRI_BODY_ENCODE_MAP[c] = chr(c)
151+
del c
151152

152153
def body_encode(body, maxlinelen=76, eol=NL):
153154
"""Encode with quoted-printable, wrapping at maxlinelen characters.

Lib/http/cookiejar.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,7 @@ def _timegm(tt):
8989
DAYS = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
9090
MONTHS = ["Jan", "Feb", "Mar", "Apr", "May", "Jun",
9191
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
92-
MONTHS_LOWER = []
93-
for month in MONTHS: MONTHS_LOWER.append(month.lower())
92+
MONTHS_LOWER = [month.lower() for month in MONTHS]
9493

9594
def time2isoz(t=None):
9695
"""Return a string representing time in seconds since epoch, t.

Lib/inspect.py

+1
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@
156156
mod_dict = globals()
157157
for k, v in dis.COMPILER_FLAG_NAMES.items():
158158
mod_dict["CO_" + v] = k
159+
del k, v, mod_dict
159160

160161
# See Include/object.h
161162
TPFLAGS_IS_ABSTRACT = 1 << 20

Lib/json/encoder.py

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
for i in range(0x20):
3131
ESCAPE_DCT.setdefault(chr(i), '\\u{0:04x}'.format(i))
3232
#ESCAPE_DCT.setdefault(chr(i), '\\u%04x' % (i,))
33+
del i
3334

3435
INFINITY = float('inf')
3536

Lib/lib2to3/pgen2/grammar.py

+1
Original file line numberDiff line numberDiff line change
@@ -186,3 +186,4 @@ def report(self):
186186
if line:
187187
op, name = line.split()
188188
opmap[op] = getattr(token, name)
189+
del line, op, name

Lib/locale.py

+1
Original file line numberDiff line numberDiff line change
@@ -746,6 +746,7 @@ def getpreferredencoding(do_setlocale=True):
746746
for k, v in sorted(locale_encoding_alias.items()):
747747
k = k.replace('_', '')
748748
locale_encoding_alias.setdefault(k, v)
749+
del k, v
749750

750751
#
751752
# The locale_alias table maps lowercase alias names to C locale names

Lib/multiprocessing/managers.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,11 @@ def reduce_array(a):
4949
reduction.register(array.array, reduce_array)
5050

5151
view_types = [type(getattr({}, name)()) for name in ('items','keys','values')]
52-
if view_types[0] is not list: # only needed in Py3.0
53-
def rebuild_as_list(obj):
54-
return list, (list(obj),)
55-
for view_type in view_types:
56-
reduction.register(view_type, rebuild_as_list)
52+
def rebuild_as_list(obj):
53+
return list, (list(obj),)
54+
for view_type in view_types:
55+
reduction.register(view_type, rebuild_as_list)
56+
del view_type, view_types
5757

5858
#
5959
# Type for identifying shared objects

Lib/multiprocessing/process.py

+1
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,7 @@ def close(self):
427427
for name, signum in list(signal.__dict__.items()):
428428
if name[:3]=='SIG' and '_' not in name:
429429
_exitcode_to_name[-signum] = f'-{name}'
430+
del name, signum
430431

431432
# For debug and leak testing
432433
_dangling = WeakSet()

Lib/sysconfig.py

+1
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ def is_python_build(check_home=False):
192192
scheme['headers'] = scheme['include']
193193
scheme['include'] = '{srcdir}/Include'
194194
scheme['platinclude'] = '{projectbase}/.'
195+
del scheme
195196

196197

197198
def _subst_vars(s, local_vars):

Lib/test/test_inspect.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ def istest(self, predicate, exp):
108108
self.assertFalse(other(obj), 'not %s(%s)' % (other.__name__, exp))
109109

110110
def test__all__(self):
111-
support.check__all__(self, inspect, not_exported=("k", "v", "mod_dict", "modulesbyfile"))
111+
support.check__all__(self, inspect, not_exported=("modulesbyfile",))
112112

113113
def generator_function_example(self):
114114
for i in range(2):

Lib/tokenize.py

+2
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ def _compile(expr):
143143
endpats[_prefix + '"'] = Double
144144
endpats[_prefix + "'''"] = Single3
145145
endpats[_prefix + '"""'] = Double3
146+
del _prefix
146147

147148
# A set of all of the single and triple quoted string prefixes,
148149
# including the opening quotes.
@@ -153,6 +154,7 @@ def _compile(expr):
153154
single_quoted.add(u)
154155
for u in (t + '"""', t + "'''"):
155156
triple_quoted.add(u)
157+
del t, u
156158

157159
tabsize = 8
158160

Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Remove loop variables that are leaking into modules' namespaces.

0 commit comments

Comments
 (0)