Skip to content

Commit db216f4

Browse files
committed
Extract 'make_iterable' for upload and register commands, avoiding masking loop input variable (B020).
1 parent a53e425 commit db216f4

File tree

2 files changed

+18
-11
lines changed

2 files changed

+18
-11
lines changed

distutils/command/register.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from distutils._log import log
1414
from warnings import warn
1515

16+
from .._itertools import always_iterable
1617
from ..core import PyPIRCCommand
1718

1819

@@ -273,12 +274,8 @@ def post_to_server(self, data, auth=None): # noqa: C901
273274
sep_boundary = '\n--' + boundary
274275
end_boundary = sep_boundary + '--'
275276
body = io.StringIO()
276-
for key, value in data.items():
277-
# handle multiple entries for the same name
278-
if type(value) not in (type([]), type(())):
279-
value = [value]
280-
for value in value:
281-
value = str(value)
277+
for key, values in data.items():
278+
for value in map(str, make_iterable(values)):
282279
body.write(sep_boundary)
283280
body.write('\nContent-Disposition: form-data; name="%s"' % key)
284281
body.write("\n\n")
@@ -318,3 +315,9 @@ def post_to_server(self, data, auth=None): # noqa: C901
318315
msg = '\n'.join(('-' * 75, data, '-' * 75))
319316
self.announce(msg, logging.INFO)
320317
return result
318+
319+
320+
def make_iterable(values):
321+
if values is None:
322+
return [None]
323+
return always_iterable(values)

distutils/command/upload.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from urllib.parse import urlparse
1414
from urllib.request import HTTPError, Request, urlopen
1515

16+
from .._itertools import always_iterable
1617
from ..core import PyPIRCCommand
1718
from ..errors import DistutilsError, DistutilsOptionError
1819
from ..spawn import spawn
@@ -151,12 +152,9 @@ def upload_file(self, command, pyversion, filename): # noqa: C901
151152
sep_boundary = b'\r\n--' + boundary.encode('ascii')
152153
end_boundary = sep_boundary + b'--\r\n'
153154
body = io.BytesIO()
154-
for key, value in data.items():
155+
for key, values in data.items():
155156
title = '\r\nContent-Disposition: form-data; name="%s"' % key
156-
# handle multiple entries for the same name
157-
if not isinstance(value, list):
158-
value = [value]
159-
for value in value:
157+
for value in make_iterable(values):
160158
if type(value) is tuple:
161159
title += '; filename="%s"' % value[0]
162160
value = value[1]
@@ -202,3 +200,9 @@ def upload_file(self, command, pyversion, filename): # noqa: C901
202200
msg = f'Upload failed ({status}): {reason}'
203201
self.announce(msg, logging.ERROR)
204202
raise DistutilsError(msg)
203+
204+
205+
def make_iterable(values):
206+
if values is None:
207+
return [None]
208+
return always_iterable(values, base_type=(bytes, str, tuple))

0 commit comments

Comments
 (0)