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) # capable client)
ser.apply_settings(settings) ser.apply_settings(settings)
except KeyboardInterrupt: except KeyboardInterrupt:
sys.stdout.write("\n") print(flush=True)
break break
except socket.error as msg: except socket.error as msg:
logging.error(str(msg)) logging.error(str(msg))

View File

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

View File

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

View File

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