Skip to content

Comments

Fix prompt() per-call kwargs permanently changing session state#2051

Open
Fridayai700 wants to merge 1 commit intoprompt-toolkit:mainfrom
Fridayai700:fix-prompt-password-persistence
Open

Fix prompt() per-call kwargs permanently changing session state#2051
Fridayai700 wants to merge 1 commit intoprompt-toolkit:mainfrom
Fridayai700:fix-prompt-password-persistence

Conversation

@Fridayai700
Copy link

Summary

Fixes #967. When calling session.prompt(is_password=True), the is_password attribute was permanently set on the session, affecting all subsequent prompt() calls. The same issue applied to every other kwarg accepted by prompt() and prompt_async().

Changes

  • Save original values of overridden attributes before applying per-call kwargs
  • Restore them in a finally block after prompt() / prompt_async() returns (or raises)
  • Remove duplicate is_password entry in the _fields tuple
  • Add test verifying attributes are restored after a per-call override

Before

session = PromptSession()
session.prompt('Password: ', is_password=True)
# session.is_password is now True permanently
session.prompt('Name: ')  # still masked!

After

session = PromptSession()
session.prompt('Password: ', is_password=True)
# session.is_password is restored to False
session.prompt('Name: ')  # works as expected

Notes

The original code had a comment saying save/restore was removed because it was confusing and did not really serve a use case — but #967 shows it does. This implementation is minimal: it only saves/restores attributes that are actually passed as kwargs in a given call, so setting session.editing_mode = EditingMode.VI between calls still works as expected.

When calling session.prompt(is_password=True), the is_password attribute
was permanently set on the session, affecting all subsequent prompt()
calls. This fix saves the original values of any attributes overridden
by per-call kwargs and restores them in a finally block after the prompt
returns (or raises).

Also fixes:
- Duplicate 'is_password' entry in _fields tuple
- Same save/restore pattern applied to prompt_async()

Fixes prompt-toolkit#967.
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.

Single is_password prompt changes all further prompts

1 participant