source: trunk/misc/python3/audit-dict-for-loops.py

Last change on this file was cc7a3bc, checked in by Itamar Turner-Trauring <itamar@…>, at 2020-09-28T15:54:21Z

Audit script to find potentially broken for loops.

  • Property mode set to 100644
File size: 1.3 KB
Line 
1"""
2The following code is valid in Python 2:
3
4for x in my_dict.keys():
5    if something(x):
6        del my_dict[x]
7
8But broken in Python 3.
9
10One solution is:
11
12for x in list(my_dict.keys()):
13    if something(x):
14        del my_dict[x]
15
16Some but not all code in Tahoe has been changed to that. In other cases, the code was left unchanged since there was no `del`.
17
18However, some mistakes may have slept through.
19
20To help catch cases that were incorrectly ported, this script runs futurize on all ported modules, which should convert it into the `list()` form.
21You can then look at git diffs to see if any of the impacted would be buggy without the newly added `list()`.
22"""
23
24import os
25from subprocess import check_call
26
27from allmydata.util import _python3
28
29
30def fix_potential_issue():
31    for module in _python3.PORTED_MODULES + _python3.PORTED_TEST_MODULES:
32        filename = "src/" + module.replace(".", "/") + ".py"
33        if not os.path.exists(filename):
34            # Package, probably
35            filename = "src/" + module.replace(".", "/") + "/__init__.py"
36        check_call(["futurize", "-f", "lib2to3.fixes.fix_dict", "-w", filename])
37    print(
38        "All loops converted. Check diff to see if there are any that need to be commitedd."
39    )
40
41
42if __name__ == "__main__":
43    fix_potential_issue()
Note: See TracBrowser for help on using the repository browser.