From 4d8d7feef2e2d8dc2cc79cbbeeff4fdf8032821a Mon Sep 17 00:00:00 2001 From: Jon Dufresne Date: Tue, 5 Jan 2021 03:57:26 -0800 Subject: [PATCH] Fix AssertionError on "..." outside of a continuation prompt Thanks @Zac-HD for the report and fix! Fixes #69 --- blacken_docs.py | 3 +-- tests/blacken_docs_test.py | 13 +++++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/blacken_docs.py b/blacken_docs.py index c9646c0..6d4dfaa 100644 --- a/blacken_docs.py +++ b/blacken_docs.py @@ -127,8 +127,7 @@ def finish_fragment() -> None: for line in match['code'].splitlines(): orig_line, line = line, line.lstrip() continuation_match = PYCON_CONTINUATION_RE.match(line) - if continuation_match: - assert fragment is not None + if continuation_match and fragment is not None: fragment += line[continuation_match.end():] + '\n' else: finish_fragment() diff --git a/tests/blacken_docs_test.py b/tests/blacken_docs_test.py index 11de4d4..abfe602 100644 --- a/tests/blacken_docs_test.py +++ b/tests/blacken_docs_test.py @@ -672,3 +672,16 @@ def test_format_src_rst_pycon_preserves_output_indentation(): ) after, _ = blacken_docs.format_str(before, BLACK_MODE) assert after == before + + +def test_format_src_rst_pycon_elided_traceback(): + before = ( + '.. code-block:: pycon\n' + '\n' + ' >>> 1 / 0\n' + ' Traceback (most recent call last):\n' + ' ...\n' + ' ZeroDivisionError: division by zero\n' + ) + after, _ = blacken_docs.format_str(before, BLACK_MODE) + assert after == before