Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions Lib/distutils/ccompiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,8 +341,7 @@ def _setup_compile(self, outdir, macros, incdirs, sources, depends,
pp_opts = gen_preprocess_options(macros, incdirs)

build = {}
for i in range(len(sources)):
src = sources[i]
for i, src in enumerate(sources):
Copy link
Copy Markdown
Contributor

@MSeifert04 MSeifert04 Sep 11, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This stell leaves the obj = objects[i], why not just for src, obj in zip(sources, objects):

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to stay close to the original code and do what was needed to replace range(len(sources)) with enumerate(sources).
Your suggestion could be done but it looks like the comments in the bug tracker would prefer no changes at all.

obj = objects[i]
ext = os.path.splitext(src)[1]
self.mkpath(os.path.dirname(obj))
Expand Down
14 changes: 7 additions & 7 deletions Lib/inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -795,14 +795,14 @@ def findsource(object):
# use the one with the least indentation, which is the one
# that's most probably not inside a function definition.
candidates = []
for i in range(len(lines)):
match = pat.match(lines[i])
for linenum, line in enumerate(lines):
match = pat.match(line)
if match:
# if it's at toplevel, it's already the best one
if lines[i][0] == 'c':
return lines, i
if line[0] == 'c':
return lines, linenum
# else add whitespace to candidate list
candidates.append((match.group(1), i))
candidates.append((match.group(1), linenum))
if candidates:
# this will sort by whitespace, and by line number,
# less whitespace first
Expand Down Expand Up @@ -1258,8 +1258,8 @@ def convert(name, locals=locals,
formatarg=formatarg, formatvalue=formatvalue):
return formatarg(name) + formatvalue(locals[name])
specs = []
for i in range(len(args)):
specs.append(convert(args[i]))
for i, arg in enumerate(args):
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason to keep the i at all here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This pattern can be seen in places in the repository.
For example, there is a similar pattern in the inspect.py module in the Signature class:
for idx, param in enumerate(parameters):
where idx is not subsequently used.

The original code also did not use a standard:
for i in args:

I focused on replacing range(len(args)) with enumerate(args).

specs.append(convert(arg))
if varargs:
specs.append(formatvarargs(varargs) + formatvalue(locals[varargs]))
if varkw:
Expand Down
4 changes: 2 additions & 2 deletions Lib/mimetypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,8 @@ def readfp(self, fp, strict=True):
if not line:
break
words = line.split()
for i in range(len(words)):
if words[i][0] == '#':
for i, word in enumerate(words):
if word[0] == '#':
del words[i:]
break
if not words:
Expand Down
6 changes: 3 additions & 3 deletions Lib/modulefinder.py
Original file line number Diff line number Diff line change
Expand Up @@ -554,9 +554,9 @@ def replace_paths_in_code(self, co):
self.processed_paths.append(original_filename)

consts = list(co.co_consts)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While we modify this function, I suggest to move type(co) out of the loop:

code_type = type(co)

for i in range(len(consts)):
if isinstance(consts[i], type(co)):
consts[i] = self.replace_paths_in_code(consts[i])
for i, const in enumerate(consts):
if isinstance(const, type(co)):
consts[i] = self.replace_paths_in_code(const)

return types.CodeType(co.co_argcount, co.co_kwonlyargcount,
co.co_nlocals, co.co_stacksize, co.co_flags,
Expand Down