backtrader.utils.flushfile module

Flush File Module - Auto-flushing file wrapper for Windows.

This module provides a wrapper for file-like objects that automatically flushes after each write. On Windows platforms, it replaces sys.stdout and sys.stderr with auto-flushing versions to ensure immediate output.

Classes:

flushfile: Wrapper that auto-flushes after each write. StdOutDevNull: Null output device that suppresses stdout.

Note

This is primarily for Windows compatibility where output buffering can cause delayed display of stdout/stderr.

class backtrader.utils.flushfile.flushfile[source]

Bases: object

File wrapper that auto-flushes after each write.

This class wraps a file-like object and ensures that each write operation is immediately flushed to the underlying file descriptor.

f

The underlying file-like object.

Note

On Windows, this module automatically wraps sys.stdout and sys.stderr with flushfile instances.

__init__(f)[source]

Initialize the flushfile wrapper.

Parameters:

f – File-like object to wrap (typically sys.stdout or sys.stderr).

write(x)[source]

Write data to the file and immediately flush.

Parameters:

x – Data to write to the file.

flush()[source]

Flush the underlying file buffer.

class backtrader.utils.flushfile.StdOutDevNull[source]

Bases: object

Null output device that suppresses stdout.

When active, all writes to stdout are discarded. The original stdout can be restored by calling the stop() method.

stdout

The original sys.stdout saved for restoration.

__init__()[source]

Initialize StdOutDevNull and replace sys.stdout.

write(x)[source]

Discard written data instead of outputting.

Parameters:

x – Data to discard.

flush()[source]

No-op flush method for compatibility.

stop()[source]

Restore the original sys.stdout.