Skip to content

py/modbuiltins: Pass input() prompt through to mp_hal_readline.#19172

Open
dmachinewhisperer wants to merge 1 commit into
micropython:masterfrom
dmachinewhisperer:fix/input-prompt-to-readline
Open

py/modbuiltins: Pass input() prompt through to mp_hal_readline.#19172
dmachinewhisperer wants to merge 1 commit into
micropython:masterfrom
dmachinewhisperer:fix/input-prompt-to-readline

Conversation

@dmachinewhisperer
Copy link
Copy Markdown

###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/unix standard variant builds clean
  • tests/run-tests.py -d basics — 543 tests pass, 0 fail.
  • Smoke-tested over piped stdin: 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_readline whose body
ignores the prompt argument 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_print in place and have
shared/readline skip its own print when called from input()
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.

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
Copy link
Copy Markdown

codecov Bot commented May 2, 2026

Codecov Report

❌ Patch coverage is 0% with 9 lines in your changes missing coverage. Please review.
✅ Project coverage is 98.43%. Comparing base (9f396bb) to head (88017d9).

Files with missing lines Patch % Lines
py/modbuiltins.c 0.00% 9 Missing ⚠️
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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@github-actions
Copy link
Copy Markdown

github-actions Bot commented May 2, 2026

Code size report:

Reference:  docs/develop/porting: Update session log for example port. [9f396bb]
Comparison: py/modbuiltins: Pass input() prompt through to mp_hal_readline. [merge of 88017d9]
  mpy-cross:    +0 +0.000% 
   bare-arm:    +0 +0.000% 
minimal x86:    +0 +0.000% 
   unix x64:   +96 +0.011% standard
      stm32:   +56 +0.014% PYBV10
      esp32:   +56 +0.003% ESP32_GENERIC
     mimxrt:   +56 +0.015% TEENSY40
        rp2:   +48 +0.005% RPI_PICO_W
       samd:   +56 +0.020% ADAFRUIT_ITSYBITSY_M4_EXPRESS
  qemu rv32:   +58 +0.013% VIRT_RV32

@dpgeorge dpgeorge added the py-core Relates to py/ directory in source label May 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

py-core Relates to py/ directory in source

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants