GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-133710: Enhance pyrepl auto-indent by tanloong · Pull Request #140710 · python/cpython

Conversation

@tanloong

@tanloong tanloong commented Oct 28, 2025 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

This modifies _should_auto_indent() to scan characters from the start of the buffer up to the target position pos, tracking context to ignore irrelavant content within strings, then checks if the conditions for auto-indent are met.

It uses a stack (str_delims) to track quotes (" or '). When inside a string (stack not empty), characters like # or : are treated as literal text and are ignored. A flag (in_comment) marks when a # (not inside a string) has been encountered, ignoring all subsequent characters on that line as comment text. It tracks the indentation level of the last non-whitespace character (lastchar_line_indent), which helps ensure the colon is at a valid indentation level to trigger auto-indent.

The function returns True if the last non-whitespace character (outside comments/strings) is a colon (:), and the indentation level of the line where pos resides is not greater than the indentation of that colon.

Hi @wiggin15, as this _should_auto_indent() function is modified based on your original implementation (#119606), if you could help review when you have some spare time, I would be really appreciated.

  • Issue: Improve PyREPL auto-indentation #133710

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
tanloong added 7 commits May 10, 2025 04:55
@tanloong
Refactor _should_auto_indent()
6e80edb
@tanloong
Add tests
e4ef49d
@tanloong
Don't indent if cursor line is already indented
2113f88
@tanloong
Add test
dee8302
@tanloong
Merge branch 'main' into enhance-pyrepl-indent
7a332a4
@tanloong
Add test_dont_indent_already_indented
be762c5
@tanloong
blurb
20603d3
@tanloong
tanloong requested review from ambv, lysnikolaou and pablogsal as code owners October 28, 2025 15:49
@bedevere-app bedevere-app Bot mentioned this pull request Oct 28, 2025
Improve PyREPL auto-indentation #133710
Open
@bedevere-app bedevere-app Bot added the awaiting review label Oct 28, 2025
@tanloong
fix Sphinx syntax error
df35507
ambv
ambv reviewed Jan 5, 2026
View reviewed changes
Comment thread Lib/_pyrepl/readline.py Outdated
Comment on lines +297 to +302
# update stack
if char in "\"'" and (i == 0 or buffer[i - 1] != "\\"):
if str_delims and str_delims[-1] == char:
str_delims.pop()
else:
str_delims.append(char)

@ambv ambv Jan 5, 2026

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You'd also have to add f-string and t-string support to that. So you'd have to parse braces, but only if they're not escaped and they're in the right string prefixes.

At this stage, I'm afraid this function will balloon into a poor reimplementation of a decent chunk of the tokenizer. But we already use the tokenizer properly for syntax highlighting. We even have specific functionality to discover unterminated multiline strings.

Look at _pyrepl.utils and think about how that could be reused here instead of the manual approach.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@tanloong tanloong Jan 12, 2026

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your review! I have made the change to rely on _pyrepl.utils.gen_colors for a tokenizer-based parsing of strings and comments.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
tanloong added 3 commits January 13, 2026 00:28
@tanloong
reuse _pyrepl.utils.gen_colors to determine whether to indent
fb7adc5
@tanloong
revert unrelated changes
106aa66
@tanloong
Merge branch 'main' into enhance-pyrepl-indent
7f115cd
@github-actions

github-actions Bot commented May 1, 2026

Copy link
Copy Markdown

This PR is stale because it has been open for 30 days with no activity.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@github-actions github-actions Bot added the stale Stale PR or inactive for long period of time. label May 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@ambv ambv ambv left review comments
@pablogsal pablogsal Awaiting requested review from pablogsal pablogsal is a code owner
@lysnikolaou lysnikolaou Awaiting requested review from lysnikolaou lysnikolaou is a code owner

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

awaiting review stale Stale PR or inactive for long period of time.

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

2 participants

@tanloong @ambv
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.