X Tutup
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
13 changes: 11 additions & 2 deletions Lib/lib2to3/refactor.py
Original file line number Diff line number Diff line change
Expand Up @@ -592,12 +592,21 @@ def refactor_doctest(self, block, lineno, indent, filename):
return block
if self.refactor_tree(tree, filename):
new = str(tree).splitlines(keepends=True)
# Adjust lineno for lines inserted before
jmp = 0
for n in new:
Copy link
Copy Markdown
Member

@corona10 corona10 Aug 26, 2019

Choose a reason for hiding this comment

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

IMHO
This logic can be replaced by

jmp = max(new.index('\n'), 0)

if n == '\n': break
jmp += 1
pre = new[:jmp]
# Undo the adjustment of the line numbers in wrap_toks() below.
clipped, new = new[:lineno-1], new[lineno-1:]
clipped, new = new[jmp:lineno-1+jmp], new[lineno-1+jmp:]
assert clipped == ["\n"] * (lineno-1), clipped
if not new[-1].endswith("\n"):
new[-1] += "\n"
block = [indent + self.PS1 + new.pop(0)]
block = []
if pre:
block += [indent + self.PS1 + line for line in pre]
block += [indent + self.PS1 + new.pop(0)]
if new:
block += [indent + self.PS2 + line for line in new]
return block
Expand Down
24 changes: 24 additions & 0 deletions Lib/lib2to3/tests/test_fixers.py
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,30 @@ def test_unchanged(self):
s = "reduce()"
self.unchanged(s)

class Test_doctests(FixerTestCase):
fixer = "reduce"

def check(self, before, after, ignore_warnings=False):
before = support.reformat(before)
after = support.reformat(after)
output = self.refactor.refactor_docstring(before, self.filename)
self.assertEqual(after, output)

def test_reduce(self):
# See issue 12611
b = """\
'''
>>> reduce(lambda x, y: x + y, seq)
'''
"""
a = """\
'''
>>> from functools import reduce
>>> reduce(lambda x, y: x + y, seq)
'''
"""
self.check(b, a)

class Test_print(FixerTestCase):
fixer = "print"

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2to3 converting doctest using reduce(). Patch by Aldwin Pollefeyt.
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.

Suggested change
2to3 converting doctest using reduce(). Patch by Aldwin Pollefeyt.
Fix 2to3 crashing when run on a doctest that uses reduce(). Patch by Aldwin Pollefeyt.

X Tutup