bdevice.py: Protect _set_pagesize against exceptions.

pull/24/head
Peter Hinch 2024-01-10 08:20:39 +00:00
rodzic a5ebd63bfa
commit 9a9c588f9b
1 zmienionych plików z 15 dodań i 10 usunięć

Wyświetl plik

@ -100,6 +100,7 @@ class EepromDevice(BlockDevice):
if page_size is None: # Measure it
self._psize(16) # Conservative
old = self[:129] # Save old contents (nonvolatile!)
try:
self._psize(256) # Ambitious
r = (16, 32, 64, 128) # Legal page sizes + 256
for x in r:
@ -111,6 +112,10 @@ class EepromDevice(BlockDevice):
ps = 256
self._psize(ps)
self[:129] = old
except: # If anything goes wrong, restore old data and raise
for n, v in enumerate(old):
self[n] = v
raise
else: # Validated page_size was supplied
self._psize(page_size)