fix(logging): Avoid crashes when flushing if sys.stdout is not available

Closes https://github.com/espressif/esptool/pull/1064

Closes https://github.com/espressif/esptool/issues/1063
This commit is contained in:
Radim Karniš
2025-02-11 11:55:08 +01:00
parent cb0d334676
commit 5176b67453
4 changed files with 9 additions and 19 deletions

View File

@@ -127,7 +127,7 @@ def main():
# capable client)
ser.apply_settings(settings)
except KeyboardInterrupt:
sys.stdout.write("\n")
print(flush=True)
break
except socket.error as msg:
logging.error(str(msg))

View File

@@ -628,9 +628,8 @@ class EspEfusesBase(object):
% (action, "" if action.endswith("\n") else ". ")
)
if not do_not_confirm:
print("Type 'BURN' (all capitals) to continue.")
# required for Pythons which disable line buffering, ie mingw in mintty
sys.stdout.flush()
print("Type 'BURN' (all capitals) to continue.", flush=True)
# Flush required for Pythons which disable line buffering, ie mingw in mintty
yes = input()
if yes != "BURN":
print("Aborting.")

View File

@@ -7,7 +7,6 @@ import hashlib
import io
import os
import struct
import sys
import time
import zlib
import itertools
@@ -139,8 +138,7 @@ def detect_chip(
detect_port.connect(
connect_mode, connect_attempts, detecting=True, warnings=False
)
log.print("Detecting chip type...", end="")
sys.stdout.flush()
log.print("Detecting chip type...", end="", flush=True)
chip_magic_value = detect_port.read_reg(
ESPLoader.CHIP_DETECT_MAGIC_REG_ADDR
)
@@ -182,8 +180,7 @@ def load_ram(esp, args):
log.print("RAM boot...")
for seg in image.segments:
size = len(seg.data)
log.print(f"Downloading {size} bytes at {seg.addr:08x}...", end=" ")
sys.stdout.flush()
log.print(f"Downloading {size} bytes at {seg.addr:08x}...", end=" ", flush=True)
esp.mem_begin(
size, div_roundup(size, esp.ESP_RAM_BLOCK), esp.ESP_RAM_BLOCK, seg.addr
)
@@ -218,7 +215,6 @@ 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.print_overwrite(f"Read {f.tell()} bytes", last_line=True)
log.print("Done!")
@@ -640,10 +636,8 @@ def write_flash(esp, args):
percent = 100 * (seq + 1) // blocks
log.set_progress(percent)
log.print_overwrite(
"Writing at 0x%08x... (%d %%)"
% (address + bytes_written, percent)
f"Writing at {address + bytes_written:#010x}... ({percent} %)"
)
sys.stdout.flush()
block = image[0 : esp.FLASH_WRITE_SIZE]
if compress:
# feeding each compressed block into the decompressor lets us
@@ -696,8 +690,7 @@ def write_flash(esp, args):
image = original_image
break
except SerialException:
log.print(".", end="")
sys.stdout.flush()
log.print(".", end="", flush=True)
else:
raise # Reconnect limit reached

View File

@@ -628,8 +628,7 @@ class ESPLoader(object):
self.sync()
return None
except FatalError as e:
log.print(".", end="")
sys.stdout.flush()
log.print(".", end="", flush=True)
time.sleep(0.05)
last_error = e
@@ -722,8 +721,7 @@ class ESPLoader(object):
"reset the chip manually."
)
log.print("Connecting...", end="")
sys.stdout.flush()
log.print("Connecting...", end="", flush=True)
last_error = None
reset_sequence = self._construct_reset_strategy_sequence(mode)