mirror of
https://github.com/meshcore-dev/meshcore_py.git
synced 2026-06-11 03:56:16 +00:00
G7: M03+M05+M07 — cleanup: TypeError guard, dead code removal, None normalization
Why: Three minor cleanup fixes. M03 adds an else branch in set_flood_scope so unsupported scope types raise TypeError instead of UnboundLocalError. M05 removes the dead `out_path_len >> 6` shift in update_contact (high bits always zero due to reader masking) and initializes path_hash_mode=0 explicitly. M07 normalizes three `return None` paths in get_contacts to return Event(EventType.ERROR, ...) so callers can rely on the return type always being Event. Refs: Forensics report findings M03, M05, M07
This commit is contained in:
@@ -43,13 +43,17 @@ class ContactCommands(CommandHandlerBase):
|
|||||||
logger.debug("Timeout while getting contacts")
|
logger.debug("Timeout while getting contacts")
|
||||||
for future in pending: # cancel all futures
|
for future in pending: # cancel all futures
|
||||||
future.cancel()
|
future.cancel()
|
||||||
return None
|
return Event(EventType.ERROR, {"reason": "timeout waiting for contacts"})
|
||||||
|
|
||||||
for future in done:
|
for future in done:
|
||||||
event = await future
|
event = await future
|
||||||
if event is None or event.type != EventType.NEXT_CONTACT:
|
if event is None:
|
||||||
for future in pending:
|
for f in pending:
|
||||||
future.cancel()
|
f.cancel()
|
||||||
|
return Event(EventType.ERROR, {"reason": "no event received during contacts retrieval"})
|
||||||
|
if event.type != EventType.NEXT_CONTACT:
|
||||||
|
for f in pending:
|
||||||
|
f.cancel()
|
||||||
return event
|
return event
|
||||||
|
|
||||||
futures = []
|
futures = []
|
||||||
@@ -64,7 +68,7 @@ class ContactCommands(CommandHandlerBase):
|
|||||||
|
|
||||||
except asyncio.TimeoutError:
|
except asyncio.TimeoutError:
|
||||||
logger.debug(f"Timeout receiving contacts")
|
logger.debug(f"Timeout receiving contacts")
|
||||||
return None
|
return Event(EventType.ERROR, {"reason": "asyncio timeout receiving contacts"})
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.debug(f"Command error: {e}")
|
logger.debug(f"Command error: {e}")
|
||||||
return Event(EventType.ERROR, {"error": str(e)})
|
return Event(EventType.ERROR, {"error": str(e)})
|
||||||
@@ -116,7 +120,9 @@ class ContactCommands(CommandHandlerBase):
|
|||||||
path_hash_mode = int(path.split(":")[1])
|
path_hash_mode = int(path.split(":")[1])
|
||||||
path = path.split(":")[0].replace(":","")
|
path = path.split(":")[0].replace(":","")
|
||||||
else: # use device one by default
|
else: # use device one by default
|
||||||
path_hash_mode = contact["out_path_len"] >> 6 # would fallback to previous val
|
# out_path_len is pre-masked (& 0x3F) in reader.py, so high bits are always 0;
|
||||||
|
# the actual path_hash_mode is fetched from the device query below.
|
||||||
|
path_hash_mode = 0
|
||||||
res = await self.send_device_query()
|
res = await self.send_device_query()
|
||||||
if not res is None and res.type != EventType.ERROR:
|
if not res is None and res.type != EventType.ERROR:
|
||||||
if "path_hash_mode" in res.payload:
|
if "path_hash_mode" in res.payload:
|
||||||
|
|||||||
@@ -313,6 +313,8 @@ class MessagingCommands(CommandHandlerBase):
|
|||||||
elif isinstance (scope, bytes): # scope has been sent directly as byte
|
elif isinstance (scope, bytes): # scope has been sent directly as byte
|
||||||
logger.debug(f"Directly setting scope to {scope}")
|
logger.debug(f"Directly setting scope to {scope}")
|
||||||
scope_key = scope
|
scope_key = scope
|
||||||
|
else:
|
||||||
|
raise TypeError(f"set_flood_scope: unsupported scope type {type(scope).__name__}")
|
||||||
|
|
||||||
logger.debug(f"Setting scope to {scope_key.hex()}")
|
logger.debug(f"Setting scope to {scope_key.hex()}")
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user