]> git.madduck.net Git - etc/vim.git/commitdiff

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

Fix print() function on Python 2 (#754)
authorAndy Freeland <andy@andyfreeland.net>
Thu, 14 Mar 2019 23:42:54 +0000 (16:42 -0700)
committerJelle Zijlstra <jelle.zijlstra@gmail.com>
Thu, 14 Mar 2019 23:42:54 +0000 (16:42 -0700)
Fixes #752

black.py
tests/data/python2_print_function.py [new file with mode: 0755]
tests/test_black.py

index b7276663679737cdae4a77a11df7a2c683e2b800..c44bc9b46f6167f416f7c313acad509653bab8c7 100644 (file)
--- a/black.py
+++ b/black.py
@@ -726,13 +726,13 @@ def get_grammars(target_versions: Set[TargetVersion]) -> List[Grammar]:
     if not target_versions:
         return GRAMMARS
     elif all(not version.is_python2() for version in target_versions):
-        # Python 2-compatible code, so don't try Python 3 grammar.
+        # Python 3-compatible code, so don't try Python 2 grammar
         return [
             pygram.python_grammar_no_print_statement_no_exec_statement,
             pygram.python_grammar_no_print_statement,
         ]
     else:
-        return [pygram.python_grammar]
+        return [pygram.python_grammar_no_print_statement, pygram.python_grammar]
 
 
 def lib2to3_parse(src_txt: str, target_versions: Iterable[TargetVersion] = ()) -> Node:
diff --git a/tests/data/python2_print_function.py b/tests/data/python2_print_function.py
new file mode 100755 (executable)
index 0000000..81b8d8a
--- /dev/null
@@ -0,0 +1,16 @@
+#!/usr/bin/env python2
+from __future__ import print_function
+
+print('hello')
+print(u'hello')
+print(a, file=sys.stderr)
+
+# output
+
+
+#!/usr/bin/env python2
+from __future__ import print_function
+
+print("hello")
+print(u"hello")
+print(a, file=sys.stderr)
index 7c99f00906b31d3054ff8493911011e2f6b17954..645eec7ae787afd7e66797bdcec65ae8cd4c91c7 100644 (file)
@@ -461,6 +461,14 @@ class BlackTestCase(unittest.TestCase):
         # black.assert_equivalent(source, actual)
         black.assert_stable(source, actual, black.FileMode())
 
+    @patch("black.dump_to_file", dump_to_stderr)
+    def test_python2_print_function(self) -> None:
+        source, expected = read_data("python2_print_function")
+        mode = black.FileMode(target_versions={black.TargetVersion.PY27})
+        actual = fs(source, mode=mode)
+        self.assertFormatEqual(expected, actual)
+        black.assert_stable(source, actual, mode)
+
     @patch("black.dump_to_file", dump_to_stderr)
     def test_python2_unicode_literals(self) -> None:
         source, expected = read_data("python2_unicode_literals")