Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

encapsulate sys.stdout.flush() into logger, guard against None sys.stdout (ESPTOOL-1008) #1064

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions esptool/cmds.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def check_if_stub(instance):
connect_mode, connect_attempts, detecting=True, warnings=False
)
log.print("Detecting chip type...", end="")
sys.stdout.flush()
log.flush()
chip_magic_value = detect_port.read_reg(
ESPLoader.CHIP_DETECT_MAGIC_REG_ADDR
)
Expand Down Expand Up @@ -183,7 +183,7 @@ def load_ram(esp, args):
for seg in image.segments:
size = len(seg.data)
log.print(f"Downloading {size} bytes at {seg.addr:08x}...", end=" ")
sys.stdout.flush()
log.flush()
esp.mem_begin(
size, div_roundup(size, esp.ESP_RAM_BLOCK), esp.ESP_RAM_BLOCK, seg.addr
)
Expand Down Expand Up @@ -218,7 +218,7 @@ def dump_mem(esp, args):
percent = f.tell() * 100 // args.size
log.set_progress(percent)
log.print_overwrite(f"{f.tell()} bytes read... ({percent} %)")
sys.stdout.flush()
log.flush()
log.print_overwrite(f"Read {f.tell()} bytes", last_line=True)
log.print("Done!")

Expand Down Expand Up @@ -643,7 +643,7 @@ def write_flash(esp, args):
"Writing at 0x%08x... (%d %%)"
% (address + bytes_written, percent)
)
sys.stdout.flush()
log.flush()
block = image[0 : esp.FLASH_WRITE_SIZE]
if compress:
# feeding each compressed block into the decompressor lets us
Expand Down Expand Up @@ -697,7 +697,7 @@ def write_flash(esp, args):
break
except SerialException:
log.print(".", end="")
sys.stdout.flush()
log.flush()
else:
raise # Reconnect limit reached

Expand Down
4 changes: 2 additions & 2 deletions esptool/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,7 @@ def _connect_attempt(self, reset_strategy, mode="default_reset"):
return None
except FatalError as e:
log.print(".", end="")
sys.stdout.flush()
log.flush()
time.sleep(0.05)
last_error = e

Expand Down Expand Up @@ -723,7 +723,7 @@ def connect(
)

log.print("Connecting...", end="")
sys.stdout.flush()
log.flush()
last_error = None

reset_sequence = self._construct_reset_strategy_sequence(mode)
Expand Down
14 changes: 14 additions & 0 deletions esptool/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ def set_progress(self, percentage: float):
"""
pass

@abstractmethod
def flush():
"""
Flushes the current loging to stdout
"""
pass

class EsptoolLogger(TemplateLogger):
ansi_red = "\033[1;31m"
Expand Down Expand Up @@ -131,6 +137,14 @@ def set_progress(self, percentage: float):
"""
pass

@abstractmethod
def flush():
try:
sys.stdout.flush()
except AttributeError:
# to handle certain contexts where sys.stdout is not available (None)
pass

def set_logger(self, new_logger):
self.__class__ = new_logger.__class__

Expand Down
Loading