Skip to content

fix #1057: Preserve indentation when pasting code blocks - #1059

Open
Piyush13104 wants to merge 1 commit into
bpython:mainfrom
Piyush13104:fix-1057
Open

Piyush13104 wants to merge 1 commit into
bpython:mainfrom
Piyush13104:fix-1057

Conversation

@Piyush13104

Copy link
Copy Markdown

Fixes #1057.

Pasting indented code could lose its indentation entirely. Reproduced by
driving bpython in a pty and rendering the screen through a terminal
emulator; there turned out to be two independent causes.

Tab events were dropped. just_simple_events() discarded any event
with len(e) > 1 that was not explicitly whitelisted, and <TAB> was
not on the list, so tab-indented code lost all indentation and produced
the reporter's exact error. preprocess.leading_tabs_to_spaces() exists
to convert pasted tabs, but was unreachable because tabs never survived
to reach it.

Runs of spaces were collapsed. compress_paste_event() treats a run
of identical named keys as a held-down key. <SPACE> is a named key in
curtsies, so any read of 8 or more consecutive spaces (the paste
threshold is 7 bytes) collapsed to one. Twelve pasted spaces produced a
single space.

just_simple_events(), is_simple_event() and compress_paste_event()
each kept their own copy of the same list of text-producing keys and had
drifted apart, which is how <TAB> was missed. They now share a
CONTENT_KEYS table.

Fixing those exposed a third problem: a statement at column 0 after a
block was swallowed into it, because pasted code has no blank line to
close the block. separate_blocks() inserts one. It distinguishes a
block awaiting its blank line from a buffer held open by an unclosed
bracket by testing whether appending blank lines makes it compile, and
leaves elif/else/except/finally and comments alone, so
if/else and try/except pastes are untouched.

The script from the issue now pastes and runs correctly. Added 17 tests
covering tab- and space-indented pastes, deep indentation, continuing
clauses, unindented bracket continuations, decorators, multi-line
strings and backslash continuations.

@codecov

codecov Bot commented Oct 19, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 97.80220% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 76.31%. Comparing base (1a919d3) to head (0ef6e2b).
⚠️ Report is 48 commits behind head on main.

Files with missing lines Patch % Lines
bpython/curtsiesfrontend/repl.py 77.77% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1059      +/-   ##
==========================================
- Coverage   76.51%   76.31%   -0.21%     
==========================================
  Files          62       61       -1     
  Lines        8308     8346      +38     
==========================================
+ Hits         6357     6369      +12     
- Misses       1951     1977      +26     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] python3-bpython-0:0.26-3.fc44.noarch removes all indentation of pasted code.

1 participant