homepage

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.

classification
Title: 2to3 -d adds extra whitespace
Type: behavior Stage: resolved
Components: 2to3 (2.x to 3.x conversion tool) Versions: Python 3.8, Python 3.7, Python 2.7
process
Status: closed Resolution: wont fix
Dependencies: Superseder: Close 2to3 issues and list them here
View: 45544
Assigned To: Nosy List: Aaron.Meurer, VPeric, benjamin.peterson, xtreak
Priority: normal Keywords:

Created on 2011年08月17日 14:59 by VPeric, last changed 2022年04月11日 14:57 by admin. This issue is now closed.

Messages (3)
msg142281 - (view) Author: Vlada Peric (VPeric) Date: 2011年08月17日 14:59
When running 2to3 -d on this doctest (from this file[0] in SymPy):
 >>> class SzUpKet(Ket):
 ... def _represent_SzOp(self, basis, **options):
 ... return Matrix([1,0])
 ...
2to3 adds an extra space in the last line. This then raises an error for our automated whitespace tests (and is generally annoying). I haven't seen this happen anywhere else (and SymPy is a big codebase). 
It's really a minor issue, though (I can't set the priority myself, though).
[0] https://github.com/sympy/sympy/blob/master/sympy/physics/quantum/represent.py 
msg221914 - (view) Author: Mark Lawrence (BreamoreBoy) * Date: 2014年06月29日 22:56
Can we have a response to this please.
msg338345 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2019年03月19日 11:54
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
2022年04月11日 14:57:20adminsetgithub: 56980
2021年10月20日 23:07:56iritkatrielsetstatus: open -> closed
superseder: Close 2to3 issues and list them here
resolution: wont fix
stage: test needed -> resolved
2019年03月19日 11:54:10xtreaksetnosy: + xtreak

messages: + msg338345
versions: + Python 3.7, Python 3.8, - Python 3.4, Python 3.5
2019年03月16日 00:08:32BreamoreBoysetnosy: - BreamoreBoy
2014年06月29日 22:56:40BreamoreBoysetnosy: + BreamoreBoy

messages: + msg221914
versions: + Python 3.4, Python 3.5, - Python 3.2, Python 3.3
2011年11月29日 06:07:44ezio.melottisetnosy: + benjamin.peterson
stage: test needed
type: behavior

versions: + Python 3.3
2011年08月17日 14:59:47VPericcreate

AltStyle によって変換されたページ (->オリジナル) /