Skip to content

Commit dffc579

Browse files
author
Andrew Bjonnes
committed
Fix bug in fix_print.py fixer
When the print ends with a non-space whitespace character, an extra space character should not be printed at the end.
1 parent c66309c commit dffc579

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

src/libfuturize/fixes/fix_print.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,16 @@ def transform(self, node, results):
5757
if args and args[-1] == Comma():
5858
args = args[:-1]
5959
end = " "
60+
61+
# try to determine if the string ends in a non-space whitespace character, in which
62+
# case there should be no space at the end of the conversion
63+
string_leaves = [leaf for leaf in args[-1].leaves() if leaf.type == token.STRING]
64+
if (
65+
string_leaves
66+
and string_leaves[-1].value[0] != "r" # "raw" string
67+
and string_leaves[-1].value[-3:-1] in (r"\t", r"\n", r"\r")
68+
):
69+
end = ""
6070
if args and args[0] == pytree.Leaf(token.RIGHTSHIFT, u">>"):
6171
assert len(args) >= 2
6272
file = args[1].clone()

tests/test_future/test_libfuturize_fixers.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,37 @@ def test_trailing_comma_3(self):
307307
a = """print(1, end=' ')"""
308308
self.check(b, a)
309309

310+
def test_trailing_comma_4(self):
311+
b = """print "a ","""
312+
a = """print("a ", end=' ')"""
313+
self.check(b, a)
314+
315+
def test_trailing_comma_5(self):
316+
b = r"""print "b\t","""
317+
a = r"""print("b\t", end='')"""
318+
self.check(b, a)
319+
320+
def test_trailing_comma_6(self):
321+
b = r"""print "c\n","""
322+
a = r"""print("c\n", end='')"""
323+
self.check(b, a)
324+
325+
def test_trailing_comma_7(self):
326+
b = r"""print "d\r","""
327+
a = r"""print("d\r", end='')"""
328+
self.check(b, a)
329+
330+
def test_trailing_comma_8(self):
331+
b = r"""print "%s\n" % (1,),"""
332+
a = r"""print("%s\n" % (1,), end='')"""
333+
self.check(b, a)
334+
335+
336+
def test_trailing_comma_9(self):
337+
b = r"""print r"e\n","""
338+
a = r"""print(r"e\n", end=' ')"""
339+
self.check(b, a)
340+
310341
# >> stuff
311342

312343
def test_vargs_without_trailing_comma(self):

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy