Skip to content

Commit

Permalink
fix(logging): Avoid crashes when flushing if sys.stdout is not available
Browse files Browse the repository at this point in the history
Closes #1064

Closes #1063
  • Loading branch information
radimkarnis committed Feb 11, 2025
1 parent cb0d334 commit 5176b67
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 19 deletions.
2 changes: 1 addition & 1 deletion esp_rfc2217_server/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
5 changes: 2 additions & 3 deletions espefuse/efuse/base_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -628,9 +628,8 @@ def confirm(action, do_not_confirm):
% (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.")
Expand Down
15 changes: 4 additions & 11 deletions esptool/cmds.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import io
import os
import struct
import sys
import time
import zlib
import itertools
Expand Down Expand Up @@ -139,8 +138,7 @@ def check_if_stub(instance):
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
)
Expand Down Expand Up @@ -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
)
Expand Down Expand Up @@ -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!")

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
6 changes: 2 additions & 4 deletions esptool/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -628,8 +628,7 @@ def _connect_attempt(self, reset_strategy, mode="default_reset"):
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

Expand Down Expand Up @@ -722,8 +721,7 @@ def connect(
"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)
Expand Down

0 comments on commit 5176b67

Please sign in to comment.