8000 gh-133349: Enable auto-indent for pdb's multi-line mode by gaogaotiantian · Pull Request #133350 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-133349: Enable auto-indent for pdb's multi-line mode #133350

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 5, 2025

Conversation

gaogaotiantian
Copy link
Member
@gaogaotiantian gaogaotiantian commented May 3, 2025

@gaogaotiantian gaogaotiantian requested a review from iritkatriel May 3, 2025 16:04
@gaogaotiantian
Copy link
Member Author

Sorry for trying to squeeze this in 3.14 before beta freeze :) I think this should be pretty straightforward to review and it could help user experiences.

@gaogaotiantian
Copy link
Member Author

Hey @iritkatriel , do you think we still have a chance to merge this before beta freeze?

last_line = last_line.rstrip('\r\n')
indent = len(last_line) - len(last_line.lstrip())
if last_line.endswith(":"):
indent += 4
Copy link
Member

Choose a reason for hiding this comment

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

Is indent always 4?

Copy link
Member Author

Choose a reason for hiding this comment

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

We kind of dictated there, it is consistent with how we auto-fill the space when we hit <tab>. However, we can be smart and search for the history for the last indent. I have the time to do it and it should not be rocket science. Do you want me to do that?

Copy link
Member

Choose a reason for hiding this comment

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

Up to you. This is fine.

Copy link
Member Author

Choose a reason for hiding this comment

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

Let's land this today and if people are complaining, we can treat that as a bug and fix it later :)

@gaogaotiantian gaogaotiantian merged commit 0eeaa0e into python:main May 5, 2025
43 checks passed
@gaogaotiantian gaogaotiantian deleted the pdb-auto-indent branch May 5, 2025 17:48
nascheme pushed a commit to nascheme/cpython that referenced this pull request May 5, 2025
@vstinner
Copy link
Member
vstinner commented May 5, 2025

test_pdb.test_multiline_auto_indent() fails on FreeBSD:

FAIL: test_multiline_auto_indent (test.test_pdb.PdbTestReadline.test_multiline_auto_indent)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/home/vstinner/python/main/Lib/test/test_pdb.py", line 4870, in test_multiline_auto_indent
    self.assertIn(b'42', output)
    ~~~~~~~~~~~~~^^^^^^^^^^^^^^^
AssertionError: b'42' not found in bytearray(b"def f(x):\r\nif x > 0:\r\nx += 1\r\nreturn x\r\nelse:\r\nreturn -x\r\n\r\nf(-21-21)\r\nc\r\n> <string>(2)<module>()\r\n(Pdb) def f(x):\r\n...       if x > 0:\r\n...           x += 1\r\n...           return x\r\n...           else:\r\n*** SyntaxError: invalid syntax\r\n(Pdb) return -x\r\n*** Invalid argument: -x\r\n      Usage: r(eturn)\r\n(Pdb) \r\n*** Invalid argument: -x\r\n      Usage: r(eturn)\r\n(Pdb) f(-21-21)\r\n*** NameError: name \'f\' is not defined\r\n(Pdb) c\r\n")

Reformatted output (the bytearray):

"def f(x):\r\n
if x > 0:\r\n
x += 1\r\n
return x\r\n
else:\r\n
return -x\r\n
\r\n
f(-21-21)\r\n
c\r\n
> <string>(2)<module>()\r\n
(Pdb) def f(x):\r\n
...       if x > 0:\r\n
...           x += 1\r\n
...           return x\r\n
...           else:\r\n
*** SyntaxError: invalid syntax\r\n
(Pdb) return -x\r\n
*** Invalid argument: -x\r\n
      Usage: r(eturn)\r\n
(Pdb) \r\n
*** Invalid argument: -x\r\n
      Usage: r(eturn)\r\n
(Pdb) f(-21-21)\r\n
*** NameError: name \'f\' is not defined\r\n
(Pdb) c\r\n
"

And then test_multiline_indent_completion() hangs.

@gaogaotiantian
Copy link
Member Author

I think the easy way is just to skip these tests on freebsd. This is a test issue, where freebsd does not consider \x08 a backspace. We might be able to find a character that actually works on freebsd, but I don't have a machine to test it out. It's a tier3 support so maybe we just let it go?

@vstinner
Copy link
Member
vstinner commented May 6, 2025

I have a FreeBSD machine, I can try some changes if you want. But if I have no clue why \x08 is not treated as backspace.

I would also be fine with skipping the test on FreeBSD.

@gaogaotiantian
Copy link
Member Author

Could you try \x7f and see if that works?

@vstinner
Copy link
Member
vstinner commented May 7, 2025

Could you try \x7f and see if that works?

test_multiline_auto_indent() still fails if I replace \x08 with \x7f.

@vstinner
Copy link
Member
vstinner commented May 7, 2025

I wrote #133566 to skip the two tests on FreeBSD.

@tanloong
Copy link
Contributor
tanloong commented May 8, 2025

freebsd does not consider \x08 a backspace

Just a remark, I tried and found that FreeBSD does interpret \x08 as backspace. The problem is that the auto-indented whitespaces are undeletable for some reason.

""" test-pdb-auto-indent.py """
import textwrap
from test.support.pty_helper import run_pty

script = textwrap.dedent("""
    import pdb; pdb.Pdb().set_trace()
""")

input = b"def g\x08f(x):                # g is successfully deleted and replaced by f:\n"
input += b"if x > 0:
8000
\n"
input += b"x += 1\n"
input += b"return x\n"
input += b"\x08\x08\x08\x08else:           # fails to delete the auto-indented whitespaces:\n"
input += b"return -x\n"
input += b"\n"
input += b"f(-21-21)\n"
input += b"c\n"

output = run_pty(script, input)
print(output)
$ ./python ./test-pdb-auto-indent.py | sed -E 's/\\r\\n/\n/g'
bytearray(b"def g\x08 \x08f(x):                # g is successfully deleted and replaced by f:
if x > 0:
x += 1
return x
else:           # fails to delete the auto-indented whitespaces:
return -x

f(-21-21)
c
> <string>(2)<module>()
(Pdb) def f(x):                # g is successfully deleted and replaced by f:
...       if x > 0:
...           x += 1
...           return x
...           else:           # fails to delete the auto-indented whitespaces:
*** SyntaxError: invalid syntax
(Pdb) return -x
*** Invalid argument: -x
      Usage: r(eturn)
(Pdb) 
*** Invalid argument: -x
      Usage: r(eturn)
(Pdb) f(-21-21)
*** NameError: name \'f\' is not defined
(Pdb) c
")

The weird thing is that it happens only in the pseudo-terminal opened by run_pty(). In a normal terminal (kitty+bash) it can't be reproduced, the auto-indented whitespaces are deletable by pressing backspaces.

simplescreenrecorder-2025-05-08_19.03.22.mp4

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.

4 participants
0