From c1fbdb37143fa7fce309c2d18bbe9b3bb91f97c2 Mon Sep 17 00:00:00 2001 From: Samuel Barata <26015448+samuelbarata@users.noreply.github.com> Date: Sun, 9 Feb 2025 22:21:11 +0000 Subject: [PATCH] Fix RuntimeError('dictionary keys changed during iteration') (#59) * Fix RuntimeError('dictionary keys changed during iteration') * change clab management network configuration * fix ipv4 management subnet * fix ipv4 management subnet; delete clab.bak --- .clab/ci-topology.yml | 5 +++++ napalm_srl/srl.py | 20 ++++++++++++++------ 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/.clab/ci-topology.yml b/.clab/ci-topology.yml index f49ebdf..9d6af56 100644 --- a/.clab/ci-topology.yml +++ b/.clab/ci-topology.yml @@ -1,6 +1,11 @@ --- name: napalm-ci_cd +mgmt: + network: clab_mgmt + ipv4-subnet: 172.20.20.0/24 + ipv6-subnet: 2001:172:20:20::/64 + topology: kinds: srl: diff --git a/napalm_srl/srl.py b/napalm_srl/srl.py index d13eb0a..74ee92e 100644 --- a/napalm_srl/srl.py +++ b/napalm_srl/srl.py @@ -2874,15 +2874,23 @@ def _decodeVal(self, val): ) def _dictToList(self, aDict): - for key in aDict.keys(): + keys_to_update = {} + keys_to_delete = [] + + for key in list(aDict.keys()): # Use list() to avoid modifying during iteration if key.startswith("___"): - aDict[key[3:]] = [ + keys_to_update[key[3:]] = [ self._dictToList(val) if isinstance(val, dict) else val for val in aDict[key].values() ] - del aDict[key] - else: - if isinstance(aDict[key], dict): - aDict[key] = self._dictToList(aDict[key]) + keys_to_delete.append(key) # Mark for deletion + elif isinstance(aDict[key], dict): + aDict[key] = self._dictToList(aDict[key]) + + # Apply updates outside the loop + aDict.update(keys_to_update) + for key in keys_to_delete: + del aDict[key] + return aDict