Skip to content

Commit b799a3c

Browse files
committed
upload.py - more exception handling fixes
- don't check exc_info() in `finally`, it only works without `except` with just `try` and `finally` see https://docs.python.org/3/reference/compound_stmts.html#try after `except Exception as e:` block, `e` is already deleted - handle a rare case when esptool code does not close 'erase_file'. printing paths may cause encoding issues, so just fall through silently
1 parent 71764d2 commit b799a3c

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

tools/upload.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,11 @@
3939
# https://github.com/esp8266/Arduino/issues/6755#issuecomment-553208688
4040
if thisarg == "erase_flash":
4141
write_option = '--erase-all'
42+
# 'erase_region' is using a temporary file filled with 0xff
4243
elif thisarg == 'erase_region':
4344
erase_addr = sys.argv.pop(0)
4445
erase_len = sys.argv.pop(0)
46+
# 'write_flash' and everything else is used as-is
4547
elif thisarg == 'write_flash':
4648
write_addr = sys.argv.pop(0)
4749
binary = sys.argv.pop(0)
@@ -56,19 +58,23 @@
5658

5759
erase_file = ''
5860
if erase_addr:
59-
# Generate temporary empty (0xff) file
60-
eraser = tempfile.mkstemp()
61-
erase_file = eraser[1]
62-
os.write(eraser[0], bytearray([0xff] * int(erase_len, 0)))
63-
os.close(eraser[0])
61+
erase_fd, erase_file = tempfile.mkstemp()
62+
os.write(erase_fd, b"\xff" * int(erase_len, 0))
63+
os.close(erase_fd)
6464
cmdline = cmdline + [erase_addr, erase_file]
6565

66+
exit_code = 0
67+
6668
try:
6769
esptool.main(cmdline)
6870
except Exception as e:
69-
sys.stderr.write('\nA fatal esptool.py error occurred: %s' % e)
70-
finally:
71-
if erase_file:
71+
sys.stderr.write(f"\nA fatal upload.py error occurred: {repr(e)}")
72+
exit_code = 2
73+
74+
if erase_file:
75+
try:
7276
os.remove(erase_file)
73-
if any(sys.exc_info()):
74-
sys.exit(2)
77+
except:
78+
pass
79+
80+
sys.exit(exit_code)

0 commit comments

Comments
 (0)