Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Python3 RuntimeError - dictionary changed size during iteration #6646

Merged
merged 3 commits into from
Oct 28, 2022

Conversation

wsycqyz
Copy link
Contributor

@wsycqyz wsycqyz commented Oct 28, 2022

Description of PR

Summary:
Fix Python3 RuntimeError - dictionary changed size during iteration

Type of change

  • Bug fix
  • Testbed and Framework(new/improvement)
  • Test case(new/improvement)

Back port request

  • 201911
  • 202012
  • 202205

Approach

What is the motivation for this PR?

Python3 migration: In Python3, you cannot add/remove elements in a dictionary while iterating through it.

How did you do it?

Use copy(). This way you iterate over the original dictionary fields and on the fly can change the desired dict.

How did you verify/test it?

Both Python2 and Python3 can run bgp/test_bgp_facts.py successfully.

Any platform specific information?

N/A

Supported testbed topology if it's a new test case?

N/A

Documentation

N/A

@azure-pipelines
Copy link

The pre-commit check detected issues in the files touched by this pull request.
The detected issues may be old or new. For new issues, please try to fix them.

For old issues, it is not mandatory to fix them because they were not caused by this change. It is unfair to blame
author of this pull request. But if you can take extra effort to fix the old issues as well, that would be great!

Detailed pre-commit check results:
trim trailing whitespace.................................................Passed
fix end of files.........................................................Passed
check yaml...........................................(no files to check)Skipped
check for added large files..............................................Passed
check python ast.........................................................Passed
flake8...................................................................Failed
- hook id: flake8
- exit code: 1

tests/common/plugins/conditional_mark/init.py:126:5: F841 local variable 'e' is assigned to but never used
tests/common/plugins/conditional_mark/init.py:129:1: E302 expected 2 blank lines, found 1
tests/common/plugins/conditional_mark/init.py:158:1: E302 expected 2 blank lines, found 1
tests/common/plugins/conditional_mark/init.py:212:1: E302 expected 2 blank lines, found 1
tests/common/plugins/conditional_mark/init.py:228:121: E501 line too long (174 > 120 characters)
tests/common/plugins/conditional_mark/init.py:242:1: E302 expected 2 blank lines, found 1
tests/common/plugins/conditional_mark/init.py:270:1: E302 expected 2 blank lines, found 1
tests/common/plugins/conditional_mark/init.py:298:1: E302 expected 2 blank lines, found 1
tests/common/plugins/conditional_mark/init.py:449:5: F841 local variable 'e' is assigned to but never used
tests/common/plugins/conditional_mark/init.py:456:92: E502 the backslash is redundant between brackets
tests/common/plugins/conditional_mark/init.py:457:5: E128 continuation line under-indented for visual indent
...
[truncated extra lines, please run pre-commit locally to view full check results]

To run the pre-commit checks locally, you can follow below steps:

  1. Ensure that default python is python3. In sonic-mgmt docker container, default python is python2. You can run
    the check by activating the python3 virtual environment in sonic-mgmt docker container or outside of sonic-mgmt
    docker container.
  2. Ensure that the pre-commit package is installed:
sudo pip install pre-commit
  1. Go to repository root folder
  2. Install the pre-commit hooks:
pre-commit install
  1. Use pre-commit to check staged file:
pre-commit
  1. Alternatively, you can check committed files using:
pre-commit run --from-ref <commit_id> --to-ref <commit_id>

@azure-pipelines
Copy link

The pre-commit check detected issues in the files touched by this pull request.
The detected issues may be old or new. For new issues, please try to fix them.

For old issues, it is not mandatory to fix them because they were not caused by this change. It is unfair to blame
author of this pull request. But if you can take extra effort to fix the old issues as well, that would be great!

Detailed pre-commit check results:
trim trailing whitespace.................................................Passed
fix end of files.........................................................Passed
check yaml...........................................(no files to check)Skipped
check for added large files..............................................Passed
check python ast.........................................................Passed
flake8...................................................................Failed
- hook id: flake8
- exit code: 1

tests/common/plugins/conditional_mark/init.py:464:9: E128 continuation line under-indented for visual indent
tests/common/plugins/conditional_mark/init.py:488:121: E501 line too long (131 > 120 characters)
tests/common/plugins/conditional_mark/init.py:490:121: E501 line too long (131 > 120 characters)

To run the pre-commit checks locally, you can follow below steps:

  1. Ensure that default python is python3. In sonic-mgmt docker container, default python is python2. You can run
    the check by activating the python3 virtual environment in sonic-mgmt docker container or outside of sonic-mgmt
    docker container.
  2. Ensure that the pre-commit package is installed:
sudo pip install pre-commit
  1. Go to repository root folder
  2. Install the pre-commit hooks:
pre-commit install
  1. Use pre-commit to check staged file:
pre-commit
  1. Alternatively, you can check committed files using:
pre-commit run --from-ref <commit_id> --to-ref <commit_id>

@wsycqyz wsycqyz merged commit 979eb20 into sonic-net:master Oct 28, 2022
lolyu pushed a commit to lolyu/sonic-mgmt that referenced this pull request Dec 19, 2022
…Azure/sonic-mgmt into internal

Fix merge conflicts.
Commit history:

- Modified args for some of the calls (sonic-net#6605)
- Run telemetry tests on all DUT types in a multi-dut setup (sonic-net#6098)
- [dualtor]: Fix loopback route removal (sonic-net#6555)
- Added support for one portChannel and one routed interface if two por… (sonic-net#5331)
- Use config_reload when recovering from internal interfaces that are in down state (sonic-net#6076)
- Added python fixture for resetting config changes to fix nightly backend snmp test failures (sonic-net#6607)
- Fix Python3 RuntimeError - dictionary changed size during iteration (sonic-net#6646)
- [mx] Add docs for mx topo (sonic-net#6634)
- [topo_mx] Add new topo for BMC Mgmt ToR Router (sonic-net#6636)
- [mx] Enhance announce routes to support MX topo (sonic-net#6635)
- Merge branch 'azure-master' into dev/yaqiangzhu/internal_merge
@wsycqyz wsycqyz deleted the py3_migration_20221028 branch January 31, 2023 02:35
wsycqyz added a commit to wsycqyz/sonic-mgmt that referenced this pull request Apr 3, 2023
…onic-net#6646)

Summary:
Fix Python3 RuntimeError - dictionary changed size during iteration

What is the motivation for this PR?
Python3 migration: In Python3, you cannot add/remove elements in a dictionary while iterating through it.

How did you do it?
1. Use copy(). This way you iterate over the original dictionary fields and on the fly can change the desired dict.
2. Fix pre-commit issues.

How did you verify/test it?
Both Python2 and Python3 can run bgp/test_bgp_facts.py successfully.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants