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-103997: Automatically dedent the argument to "-c" by Erotemic · Pull Request #103998 · python/cpython

Conversation

@Erotemic

@Erotemic Erotemic commented Apr 29, 2023 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Contributor

This is an implementation of the idea proposed in #103997.

It intercepts the argument passed to -c, and removes common leading whitespace from each line in the argument.

Given an input string, the algorithm overview is:

  • split the string into lines
  • count the number of leading whitespace characters for each line
  • keep track of the minimum leading spaces, but ignore lines that contain no non-whitespace characters.
  • if number of common whitespace charcters is non-zero, then loop over all lines again and remove that number of leading spaces (again ignoring the lines that are entirely whitespace).
  • rejoin the new lines into a new string and continue the pymain-run-command function with that.

Big thanks to @sunmy2019 who really helped clean this PR up.

  • Issue: Auto dedent -c arguments #103997

Sorry, something went wrong.

Uh oh!

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

All reactions
@bedevere-bot

bedevere-bot commented Apr 29, 2023

Copy link
Copy Markdown

Most changes to Python require a NEWS entry.

Please add it using the blurb_it web app or the blurb command-line tool.

All reactions

Sorry, something went wrong.

Uh oh!

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

@bedevere-bot bedevere-bot mentioned this pull request Apr 29, 2023
Auto dedent -c arguments #103997
Closed
@bedevere-bot bedevere-bot added the awaiting review label Apr 29, 2023
@ghost

ghost commented Apr 29, 2023 •
edited by ghost
Loading

Uh oh!

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

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.
CLA signed

All reactions

Sorry, something went wrong.

Uh oh!

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

@Erotemic
Erotemic force-pushed the dedent_pymain_command branch from 71c6b63 to 51c1320 Compare April 29, 2023 23:20
@Erotemic

Erotemic commented Apr 30, 2023

Copy link
Copy Markdown
Contributor Author

I was able to cobble my way through the C docs and write what I think is a reasonable attempt at a pure C dedent function (I forgot how fun -- albiet time consuming --- pointer logic can be).

To highlight some of the corner cases that need to be accounted for, this is the test case I'm using locally:

python -c "
import subprocess
# Use $ to note when a line will have all whitespace
lines = '''
         $

  $
    data = \"\"\"
    
    this data has newlines above and below  $
                            $

    \"\"\"
    if 1:         $
        print(123)$

    print(12345)
    print(repr(data))
'''.replace('$', '')
subprocess.run(['./python', '-c', lines])
"

I still haven't handled tabs, but I think my space logic is correct. I do need help vetting my C code and fixing the memory and security problems with it.

All reactions

Sorry, something went wrong.

Uh oh!

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

sunmy2019
sunmy2019 reviewed Apr 30, 2023
View reviewed changes
Comment thread Modules/main.c Resolved

Uh oh!

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

@sunmy2019

sunmy2019 commented Apr 30, 2023 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Member

Delete all your wchar_t stuff. It is error-prone and unnecessary.

Delete all your _unicode_dedent. It is lengthy. Do utf_8_bytes_dedent is much more concise and simpler.

All reactions

Sorry, something went wrong.

Uh oh!

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

sunmy2019
sunmy2019 reviewed May 1, 2023
View reviewed changes
Comment thread Modules/main.c Resolved

Uh oh!

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

Comment thread Modules/main.c Resolved

Uh oh!

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

@sunmy2019

sunmy2019 commented May 1, 2023

Copy link
Copy Markdown
Member

You should act fast since 3.12 release window will soon close. No new feature after May 8.
https://peps.python.org/pep-0693/

Can you give me write access to your branch?

All reactions

Sorry, something went wrong.

Uh oh!

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

@Erotemic
Erotemic force-pushed the dedent_pymain_command branch from 06667b7 to 9649590 Compare May 1, 2023 02:08
@Erotemic

Erotemic commented May 1, 2023

Copy link
Copy Markdown
Contributor Author

@sunmy2019 I gave you access to my cpython fork.

sunmy2019 reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

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

@sunmy2019

sunmy2019 commented May 1, 2023

Copy link
Copy Markdown
Member

I got one thought:

textwrap.dedent remove space and tabs.
https://docs.python.org/3/library/textwrap.html#textwrap.dedent

textwrap.dedent("""
 \t\t1
   2""")
'\n\t\t1\n  2'

It requires remembering the exact "space and tab prefix". Should we implement this?

All reactions

Sorry, something went wrong.

Uh oh!

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

@Erotemic

Erotemic commented May 1, 2023

Copy link
Copy Markdown
Contributor Author

It requires remembering the exact "space and tab prefix". Should we implement this?

Probably, but off the top of my head I'm not sure what the most elegant way to do it would be. I suppose instead of maintaining the tab and space counts, it would be sufficient to maintain the current shortest whitespace sequence, and then check how much of that sequence new lines match, shortening the sequence as necessary. I can give that a try.

On a different note, I'm sure that new features will want tests associated with them, but I'm not sure where the other "-c" tests live. Do you know where the best spot to put tests would be?

All reactions

Sorry, something went wrong.

Uh oh!

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

@sunmy2019

sunmy2019 commented May 1, 2023 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Member

I got one thought:

textwrap.dedent remove space and tabs.

@Erotemic, take a look. I have implemented it with production quality at d336ac7.

Edit your #103998 (comment) to make it clear and concise, so that we can attract more reviewers.

All reactions

Sorry, something went wrong.

Uh oh!

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

@sunmy2019

sunmy2019 commented May 1, 2023

Copy link
Copy Markdown
Member

On a different note, I'm sure that new features will want tests associated with them, but I'm not sure where the other "-c" tests live. Do you know where the best spot to put tests would be?

I guess Lib/test/test_cmd_line.py

All reactions

Sorry, something went wrong.

Uh oh!

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

@sunmy2019

sunmy2019 commented May 1, 2023 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Member

We need someone with write access to review this.

@vstinner as the recent maintainer of this file.
@hauntsaninja

All reactions

Sorry, something went wrong.

Uh oh!

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

Erotemic added 13 commits May 1, 2023 11:48
@Erotemic
Very rough proof-of-concept
16be08f
📜🤖 Added by blurb_it.
@Erotemic
Cleanups and comments
e88216b
@Erotemic
Fix bad decref, only trigger if command starts with a newline
bcb7c77
@Erotemic
wchar dedent
fb8985a
@Erotemic
tweaks
26f27a8
@Erotemic
Use new char* implementation
417eff8
@Erotemic
Rename function
924e0a6
@Erotemic
tweaks
9f95672
@Erotemic
More tweaks
3f4a78b
@Erotemic
Replace strncmp with direct char comparison
97f2079
@Erotemic
Remove debug code
04435eb
@Erotemic
Made new function static
4c4eca9
@Erotemic
Handwritten char iter and _PyBytesWriter_
f9c969b
@Erotemic

Erotemic commented Apr 10, 2025

Copy link
Copy Markdown
Contributor Author

@AA-Turner CI issues have been resolved.

Since this patch was last updated the "Core and Builtins" news section had spaces in its path name removed, so the file for this patch had to be moved to the new correct location.

The other issue was a c compiler warning, which was elevated to an error by CI policy. The usage of _PyUnicode_Dedent did not have an explicit include in the c file that used it. Adding the explicit include of pycore_unicodeobject.h addressed the problem.

All reactions

Sorry, something went wrong.

Uh oh!

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

@Erotemic
Erotemic requested a review from AA-Turner April 10, 2025 16:25
@Erotemic
Merge branch 'main' into dedent_pymain_command
8e5cc7f
AA-Turner
AA-Turner approved these changes Apr 17, 2025
View reviewed changes

@AA-Turner AA-Turner left a comment

Copy link
Copy Markdown
Member

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.

This broadly looks fine to me, a minor suggestion on the test cases. I'll leave @methane or someone else to merge, though.

A

Sorry, something went wrong.

Uh oh!

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

sunmy2019 reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
Comment thread Lib/test/test_cmd_line.py Resolved

Uh oh!

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

Comment thread Lib/test/test_cmd_line.py Resolved

Uh oh!

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

Comment thread Lib/test/test_cmd_line.py Resolved

Uh oh!

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

sunmy2019 and others added 2 commits April 17, 2025 23:21
@sunmy2019 @AA-Turner
Apply suggestions from code review
cd14a00
Co-authored-by: Adam Turner <[email protected]>
@sunmy2019 @AA-Turner
Resolve Comments
07d2273
Co-authored-by: Adam Turner <[email protected]>
picnixz
picnixz requested changes Apr 17, 2025
View reviewed changes

@picnixz picnixz left a comment

Copy link
Copy Markdown
Member

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.

Please update the refcount.dat file (don't remember if it's in Tools, Doc or somewhere else).

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread Include/internal/pycore_unicodeobject.h Resolved

Uh oh!

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

Comment thread Objects/unicodeobject.c Resolved

Uh oh!

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

Comment thread Objects/unicodeobject.c Resolved

Uh oh!

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

Comment thread Objects/unicodeobject.c Resolved

Uh oh!

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

Comment thread Objects/unicodeobject.c Resolved

Uh oh!

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

Comment thread Misc/NEWS.d/next/Core_and_Builtins/2023-04-29-23-15-38.gh-issue-103997.BS3uVt.rst Resolved

Uh oh!

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

Comment thread Lib/test/test_cmd_line.py Resolved

Uh oh!

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

Comment thread Lib/test/test_cmd_line.py Resolved

Uh oh!

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

Comment thread Misc/NEWS.d/next/Core_and_Builtins/2023-04-29-23-15-38.gh-issue-103997.BS3uVt.rst Resolved

Uh oh!

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

Comment thread Misc/NEWS.d/next/Core_and_Builtins/2023-04-29-23-15-38.gh-issue-103997.BS3uVt.rst Resolved

Uh oh!

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

@bedevere-app bedevere-app Bot added awaiting changes and removed awaiting merge labels Apr 17, 2025
@bedevere-app

bedevere-app Bot commented Apr 17, 2025

Copy link
Copy Markdown

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

All reactions

Sorry, something went wrong.

Uh oh!

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

@picnixz

picnixz commented Apr 17, 2025

Copy link
Copy Markdown
Member

Ah, I've deleted a comment by mistake. My comment was about the construction Py_INCREF(x); return x; which can be replaced by return Py_NewRef(x);.

sunmy2019 reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

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

@sunmy2019
Refactor implementation
ed6e17b
@sunmy2019

sunmy2019 commented Apr 18, 2025

Copy link
Copy Markdown
Member

I have made the requested changes; please review again

All reactions

Sorry, something went wrong.

Uh oh!

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

@sunmy2019
Merge branch 'main' into dedent_pymain_command
d1edb1b
@AA-Turner
AA-Turner requested a review from picnixz April 18, 2025 03:59
methane
methane reviewed Apr 18, 2025
View reviewed changes
Comment thread Objects/unicodeobject.c Resolved

Uh oh!

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

Comment thread Objects/unicodeobject.c Resolved

Uh oh!

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

methane added 3 commits April 18, 2025 15:55
@methane
Apply suggestions from code review
4c78c57
@methane
add what's new entry
38d2a4e
@methane
Document dedentation of command in version 3.14
42b6330
methane
methane approved these changes Apr 18, 2025
View reviewed changes

@methane methane left a comment

Copy link
Copy Markdown
Member

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.

-c option in idle and pdb should be dedented too. But it can be separated PR.

Sorry, something went wrong.

Uh oh!

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

All reactions
@bedevere-app bedevere-app Bot added awaiting merge and removed awaiting changes labels Apr 18, 2025
picnixz
picnixz approved these changes Apr 18, 2025
View reviewed changes
Comment thread Doc/using/cmdline.rst Resolved

Uh oh!

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

Comment thread Doc/whatsnew/3.14.rst Resolved

Uh oh!

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

Comment thread Misc/NEWS.d/next/Core_and_Builtins/2023-04-29-23-15-38.gh-issue-103997.BS3uVt.rst Resolved

Uh oh!

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

Comment thread Objects/unicodeobject.c Resolved

Uh oh!

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

Comment thread Objects/unicodeobject.c Resolved

Uh oh!

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

@picnixz
Apply suggestions from code review
98c17e5
@methane
methane merged commit fc0ec29 into python:main Apr 18, 2025
@bedevere-app bedevere-app Bot removed the awaiting merge label Apr 18, 2025
@sunmy2019

sunmy2019 commented Apr 18, 2025

Copy link
Copy Markdown
Member

Please update the refcount.dat file (don't remember if it's in Tools, Doc or somewhere else).

I overlooked this. I will check it later.

All reactions

Sorry, something went wrong.

Uh oh!

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

@picnixz

picnixz commented Apr 18, 2025

Copy link
Copy Markdown
Member

It's fine, this is just for internal knowledge

All reactions

Sorry, something went wrong.

Uh oh!

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

@Erotemic Erotemic mentioned this pull request May 5, 2025
ENH: auto-profile stdin or literal snippets pyutils/line_profiler#338
Merged
@roryyorke roryyorke mentioned this pull request Jun 7, 2026
-c code argument should be dedented RustPython/RustPython#8055
Closed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@vstinner vstinner vstinner left review comments
@AA-Turner AA-Turner AA-Turner approved these changes
@sunmy2019 sunmy2019 sunmy2019 left review comments
@Eclips4 Eclips4 Eclips4 left review comments
@methane methane methane approved these changes
@picnixz picnixz picnixz approved these changes
@ericsnowcurrently ericsnowcurrently Awaiting requested review from ericsnowcurrently ericsnowcurrently is a code owner

Uh oh!

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

Assignees

No one assigned

Labels

None yet

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.

10 participants

@Erotemic @bedevere-bot @sunmy2019 @methane @AA-Turner @ofek @ericsnowcurrently @picnixz @vstinner @Eclips4
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.