This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author xtreak
Recipients Aaron.Meurer, VPeric, benjamin.peterson, xtreak
Date 2019-03-19.11:54:09
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1552996450.2.0.73758429208.issue12771@roundup.psfhosted.org>
In-reply-to
Content
PS2 = "... " is defined with a trailing space which is not stripped for empty lines with only PS2 in the doctest. A patch would be to strip the trailing space in PS2 for empty lines and a unittest would be as below. There are no test cases for this scenario asserting against exact format and hence the patch doesn't break any tests. Currently I don't see any doctest in the codebase having this leading whitespace. I can try converting this patch as PR if Benjamin is okay with the change.


diff --git a/Lib/lib2to3/refactor.py b/Lib/lib2to3/refactor.py
index 7841b99a5c..135678b46e 100644
--- a/Lib/lib2to3/refactor.py
+++ b/Lib/lib2to3/refactor.py
@@ -599,7 +599,11 @@ class RefactoringTool(object):
                 new[-1] += "\n"
             block = [indent + self.PS1 + new.pop(0)]
             if new:
-                block += [indent + self.PS2 + line for line in new]
+                for line in new:
+                    if not line.strip():
+                        block += [indent + self.PS2.strip() + line]
+                    else:
+                        block += [indent + self.PS2 + line]
         return block
 
     def summarize(self):
diff --git a/Lib/lib2to3/tests/test_refactor.py b/Lib/lib2to3/tests/test_refactor.py
index 9e3b8fbb90..f6a5e2d589 100644
--- a/Lib/lib2to3/tests/test_refactor.py
+++ b/Lib/lib2to3/tests/test_refactor.py
@@ -331,3 +331,22 @@ from __future__ import print_function"""
                 break
         else:
             self.fail("explicit fixer not loaded")
+
+    def test_refactor_doctest(self):
+        rt = self.rt()
+
+        expected = """
+>>> class Foo():
+...    def cheese(self):
+...        pass
+...
+"""
+
+        doc = """
+>>> class Foo():
+...    def parrot(self):
+...        pass
+...
+"""
+        out = rt.refactor_docstring(doc, "<test>")
+        self.assertEqual(out, expected)


Without patch this fails as below : 

$ ./python.exe -m unittest -v lib2to3.tests.test_refactor.TestRefactoringTool.test_refactor_doctest
test_refactor_doctest (lib2to3.tests.test_refactor.TestRefactoringTool) ... FAIL

======================================================================
FAIL: test_refactor_doctest (lib2to3.tests.test_refactor.TestRefactoringTool)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/karthikeyansingaravelan/stuff/python/cpython/Lib/lib2to3/tests/test_refactor.py", line 352, in test_refactor_doctest
    self.assertEqual(out, expected)
AssertionError: '\n>>> class Foo():\n...    def cheese(self):\n...        pass\n... \n' != '\n>>> class Foo():\n...    def cheese(self):\n...        pass\n...\n'

  >>> class Foo():
  ...    def cheese(self):
  ...        pass
- ...
?    -
+ ...


----------------------------------------------------------------------
Ran 1 test in 0.032s

FAILED (failures=1)
History
Date User Action Args
2019-03-19 11:54:10xtreaksetrecipients: + xtreak, benjamin.peterson, VPeric, Aaron.Meurer
2019-03-19 11:54:10xtreaksetmessageid: <1552996450.2.0.73758429208.issue12771@roundup.psfhosted.org>
2019-03-19 11:54:10xtreaklinkissue12771 messages
2019-03-19 11:54:09xtreakcreate