> ./python repro.py
run main 1056488
init 1056490 at 151009034069836
re-import main 1056491 at 151009045273212
re-import main 1056492 at 151009051787587
> ./python repro.py | tee /dev/null
run main 1056607
init 1056610 at 151113770440639
re-import main 1056611 at 151113781130002
init 1056610 at 151113770440639
re-import main 1056612 at 151113787814593
init 1056610 at 151113770440639
The output to be the same when stdout is redirected as when it is not.
Bug report
Bug description:
Files
a/__init__.py
repro.py
Reproduction
acontaining the__init__.pyfile aboverepro.pyscript above, ensuring the module created is onPYTHONPATHResult
Expected
The output to be the same when stdout is redirected as when it is not.
Analysis
This is due to fork server preloading a module that writes to
stdout, but not flushing it. When a child process is spawned it inherits the buffered data and spuriously outputs it when it flushes itsstdout. Note that #126631 prevents__main__from being preloaded, so at present this will not be triggered by printing from__main__.CPython versions tested on:
CPython main branch
Operating systems tested on:
Linux
Linked PRs