Skip to content

Commit

Permalink
Fix bug in fix_print.py fixer
Browse files Browse the repository at this point in the history
When the print ends with a non-space whitespace character, an extra
space character should not be printed at the end.
  • Loading branch information
Andrew Bjonnes committed Nov 11, 2021
1 parent c66309c commit dffc579
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/libfuturize/fixes/fix_print.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,16 @@ def transform(self, node, results):
if args and args[-1] == Comma():
args = args[:-1]
end = " "

# try to determine if the string ends in a non-space whitespace character, in which
# case there should be no space at the end of the conversion
string_leaves = [leaf for leaf in args[-1].leaves() if leaf.type == token.STRING]
if (
string_leaves
and string_leaves[-1].value[0] != "r" # "raw" string
and string_leaves[-1].value[-3:-1] in (r"\t", r"\n", r"\r")
):
end = ""
if args and args[0] == pytree.Leaf(token.RIGHTSHIFT, u">>"):
assert len(args) >= 2
file = args[1].clone()
Expand Down
31 changes: 31 additions & 0 deletions tests/test_future/test_libfuturize_fixers.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,37 @@ def test_trailing_comma_3(self):
a = """print(1, end=' ')"""
self.check(b, a)

def test_trailing_comma_4(self):
b = """print "a ","""
a = """print("a ", end=' ')"""
self.check(b, a)

def test_trailing_comma_5(self):
b = r"""print "b\t","""
a = r"""print("b\t", end='')"""
self.check(b, a)

def test_trailing_comma_6(self):
b = r"""print "c\n","""
a = r"""print("c\n", end='')"""
self.check(b, a)

def test_trailing_comma_7(self):
b = r"""print "d\r","""
a = r"""print("d\r", end='')"""
self.check(b, a)

def test_trailing_comma_8(self):
b = r"""print "%s\n" % (1,),"""
a = r"""print("%s\n" % (1,), end='')"""
self.check(b, a)


def test_trailing_comma_9(self):
b = r"""print r"e\n","""
a = r"""print(r"e\n", end=' ')"""
self.check(b, a)

# >> stuff

def test_vargs_without_trailing_comma(self):
Expand Down

0 comments on commit dffc579

Please sign in to comment.