sjmikler / progress-table

Display progress as a pretty table in the command line.
MIT License
90 stars 3 forks source link

[BUG] Outputs to wrong sys.stdout when default arguments are used #15

Closed sehoffmann closed 6 months ago

sehoffmann commented 6 months ago

Problem

If one replaces sys.stdout with a custom stream, progress-table does still output to the old stream (usually standard out), even if the table is created and initialized AFTER the stream has been replaced.

The reason for this is the default argument file=sys.stdout, that gets evaluated when the module is imported for the first time and NOT when an actual table object is created. This can be hard to spot!

Suggested Solution

Replace the default argument with file=None and set it to sys.stdout in the constructor. This delays the evaluation of sys.stdout to the time at which a table object is actually created.

Even better: Officially accept None values for file as part of the interface and, in that case, retrieve sys.stdout on each write/flush instead of on object creation. This would allow one to redirect the output of progress-table even long after a table object has been created. The old behavior, i.e. sticking to a single, specific stream for all eternity, can still be achieved by explicitly passing file=sys.stdout to the constructor.

sjmikler commented 6 months ago

Nice catch! I like the "Even better" solution. This should be fixed with this PR: #16

sehoffmann commented 6 months ago

Wow, thanks for the super quick fix :+1:

sjmikler commented 6 months ago

Merged in 1.2.2.