Fix stdout flushing for input() calls in notebook cells #16951
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.
When executing notebook cells containing
input()
functions, stdout was not being flushed consistently before prompting for user input. This caused print statements and prompts to remain buffered, making it difficult for users to see what they needed to respond to.Problem
Consider this common interactive notebook pattern:
Before this fix, the output would be inconsistent - sometimes users would see the prompt immediately, other times the output would remain buffered until after they provided input, making the notebook confusing to use.
Solution
This PR implements a startup code provider that automatically monkey-patches Python's built-in
input()
function when Python kernels start. The patched function:sys.stdout.flush()
to ensure all pending output is displayedinput()
function with all arguments preservedThe monkey patch is installed via kernel startup code that runs early in the kernel initialization process:
Implementation
InputFlushStartupCodeProvider
: New startup code provider that injects the monkey patchTesting
Now users will consistently see all print output before being prompted for input, making interactive notebooks much more usable.
Fixes #13039.
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.