]> 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 crash on some power hugging cases (#2806)
authorJelle Zijlstra <jelle.zijlstra@gmail.com>
Thu, 27 Jan 2022 00:47:36 +0000 (16:47 -0800)
committerGitHub <noreply@github.com>
Thu, 27 Jan 2022 00:47:36 +0000 (16:47 -0800)
Found by the fuzzer. Repro case:

python -m black -c 'importA;()<<0**0#'

src/black/linegen.py
src/black/lines.py
src/blib2to3/pytree.py
tests/data/power_op_newline.py [new file with mode: 0644]
tests/test_format.py

index 9fbdfadba6adcdd348efc7039a5a987b88efc0fb..ac60ed1986d82cff4fc0309ebf372bdba8912eb1 100644 (file)
@@ -942,6 +942,7 @@ def generate_trailers_to_omit(line: Line, line_length: int) -> Iterator[Set[Leaf
                 if (
                     prev
                     and prev.type == token.COMMA
+                    and leaf.opening_bracket is not None
                     and not is_one_tuple_between(
                         leaf.opening_bracket, leaf, line.leaves
                     )
@@ -969,6 +970,7 @@ def generate_trailers_to_omit(line: Line, line_length: int) -> Iterator[Set[Leaf
             if (
                 prev
                 and prev.type == token.COMMA
+                and leaf.opening_bracket is not None
                 and not is_one_tuple_between(leaf.opening_bracket, leaf, line.leaves)
             ):
                 # Never omit bracket pairs with trailing commas.
index c602aa69ce9b65472b838ded24815ae7be6eb7cb..7d50f02aebc16b1787239dcf3f41625464b29dc5 100644 (file)
@@ -277,7 +277,9 @@ class Line:
         if self.is_import:
             return True
 
-        if not is_one_tuple_between(closing.opening_bracket, closing, self.leaves):
+        if closing.opening_bracket is not None and not is_one_tuple_between(
+            closing.opening_bracket, closing, self.leaves
+        ):
             return True
 
         return False
index bd86270b8e232d1230fde477b38ff4ca2836d710..b203ce5b2ac897b895aa28f31d81c4c38809c4e6 100644 (file)
@@ -386,7 +386,8 @@ class Leaf(Base):
     value: Text
     fixers_applied: List[Any]
     bracket_depth: int
-    opening_bracket: "Leaf"
+    # Changed later in brackets.py
+    opening_bracket: Optional["Leaf"] = None
     used_names: Optional[Set[Text]]
     _prefix = ""  # Whitespace and comments preceding this token in the input
     lineno: int = 0  # Line where this token starts in the input
@@ -399,6 +400,7 @@ class Leaf(Base):
         context: Optional[Context] = None,
         prefix: Optional[Text] = None,
         fixers_applied: List[Any] = [],
+        opening_bracket: Optional["Leaf"] = None,
     ) -> None:
         """
         Initializer.
@@ -416,6 +418,7 @@ class Leaf(Base):
             self._prefix = prefix
         self.fixers_applied: Optional[List[Any]] = fixers_applied[:]
         self.children = []
+        self.opening_bracket = opening_bracket
 
     def __repr__(self) -> str:
         """Return a canonical string representation."""
@@ -448,6 +451,7 @@ class Leaf(Base):
             self.value,
             (self.prefix, (self.lineno, self.column)),
             fixers_applied=self.fixers_applied,
+            opening_bracket=self.opening_bracket,
         )
 
     def leaves(self) -> Iterator["Leaf"]:
diff --git a/tests/data/power_op_newline.py b/tests/data/power_op_newline.py
new file mode 100644 (file)
index 0000000..85d434d
--- /dev/null
@@ -0,0 +1,10 @@
+importA;()<<0**0#
+
+# output
+
+importA
+(
+    ()
+    << 0
+    ** 0
+)  #
index a4619b4a652e85c5cee070b28cc44cebe6270956..88f084ea47815da2cf0951a50634253a9e0e8cb5 100644 (file)
@@ -256,3 +256,9 @@ def test_python38() -> None:
 def test_python39() -> None:
     source, expected = read_data("python39")
     assert_format(source, expected, minimum_version=(3, 9))
+
+
+def test_power_op_newline() -> None:
+    # requires line_length=0
+    source, expected = read_data("power_op_newline")
+    assert_format(source, expected, mode=black.Mode(line_length=0))