Skip to content

stdout odd flushing requirement with ANSI control code #157945

Description

@Berbe

Bug report

Bug description:

Here is a section of what I am doing mid-script:

from time import sleep


print(f"\x1b[1K\rPart", end="")
# Conditions for printing, removed for the sake of simplifying
print(" - Appendix", end="", flush=True)
sleep(1) # Other processing occurs
# Resetting line before other outputs
print("\x1b[1K\r", end="")

This works fine:

print(f"\x1b[1K\rPart", end="")
print(" - Appendix", end="")

This does not:

from time import sleep


print(f"\x1b[1K\rPart", end="")
print(" - Appendix", end="")
sleep(1)

Somehow, the - Appendix print is not flushed before the end of script (after sleep here)

Forced with:

from time import sleep


print(f"\x1b[1K\rPart", end="")
print(" - Appendix", end="", flush=True)
sleep(1)

Since the flush organically happens at the end of the script, any non-flushed print after one having leveraged the 1K ANSI code & carriage return sequence will never be displayed before the line erasure at the end of the section.
This seems like some buggy line flush detection, forcing it on standard file descriptors (here stdout) feeling unreasonable.

CPython versions tested on:

3.13

Operating systems tested on:

Linux

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions