py/modbuiltins: Pass input() prompt through to mp_hal_readline.#19172
Open
dmachinewhisperer wants to merge 1 commit into
Open
py/modbuiltins: Pass input() prompt through to mp_hal_readline.#19172dmachinewhisperer wants to merge 1 commit into
dmachinewhisperer wants to merge 1 commit into
Conversation
builtins.input() prints its prompt argument via mp_obj_print() and then calls mp_hal_readline(&line, "") with an empty string. The prompt parameter of mp_hal_readline is therefore unused for any caller reached through input(). This rules out a useful pattern for out-of-tree ports say a custom mp_hal_readline that wants to handle prompt and read atomically for example a remote line editor that issues a single request containing both the prompt to display and the buffer to fill has no way to recover the prompt from input() because by the time the readline call arrives the prompt has already been written to stdout as a separate stream. This change captures the rendered prompt into a vstr (so non-string arguments still go through PRINT_STR), passes the C string to mp_hal_readline, and drops the now-redundant explicit print. Signed-off-by: Asogwa Emmanuel <asogwaemmanuel36@gmail.com>
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #19172 +/- ##
==========================================
- Coverage 98.46% 98.43% -0.04%
==========================================
Files 176 176
Lines 22811 22818 +7
==========================================
Hits 22460 22460
- Misses 351 358 +7 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
Code size report: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
###Summary
builtins.input() prints its prompt argument via mp_obj_print() and then calls mp_hal_readline(&line, "") with an empty string. The prompt parameter of mp_hal_readline is therefore unused for any caller reached through input().
This rules out a useful pattern for out-of-tree ports say a custom mp_hal_readline that wants to handle prompt and read atomically for example a remote line editor that issues a single request containing both the prompt to display and the buffer to fill has no way to recover the prompt from input() because by the time the readline call arrives the prompt has already been written to stdout as a separate stream.
This change captures the rendered prompt into a vstr (so non-string arguments still go through PRINT_STR), passes the C string to mp_hal_readline, and drops the now-redundant explicit print.
Testing
ports/unixstandard variant builds cleantests/run-tests.py -d basics— 543 tests pass, 0 fail.input('Name: '),input(),input(42),input('µ -> ')prompt printed exactly once,return values correct.
Trade-offs and Alternatives
Out-of-tree ports that define a custom
mp_hal_readlinewhose bodyignores the
promptargument would now silently lose prompt display.Every in-tree implementation already prints the prompt, so this is a
contract-tightening rather than a contract-breaking change for
in-tree code, but it is worth flagging for downstream maintainers.
An alternative would be to leave the
mp_obj_printin place and haveshared/readlineskip its own print when called frominput()that would preserve the old contract but requires a new flag or a
second entry point, which is more invasive than the present change.
Generative AI
I used generative AI tools when creating parts of the PR text, but the code change and testing and submission was done by a human.