Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug in fix_print.py fixer #596

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/libfuturize/fixes/fix_print.py
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
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