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 | """ |
---|
2 | The following code is valid in Python 2: |
---|
3 | |
---|
4 | for x in my_dict.keys(): |
---|
5 | if something(x): |
---|
6 | del my_dict[x] |
---|
7 | |
---|
8 | But broken in Python 3. |
---|
9 | |
---|
10 | One solution is: |
---|
11 | |
---|
12 | for x in list(my_dict.keys()): |
---|
13 | if something(x): |
---|
14 | del my_dict[x] |
---|
15 | |
---|
16 | Some 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 | |
---|
18 | However, some mistakes may have slept through. |
---|
19 | |
---|
20 | To help catch cases that were incorrectly ported, this script runs futurize on all ported modules, which should convert it into the `list()` form. |
---|
21 | You can then look at git diffs to see if any of the impacted would be buggy without the newly added `list()`. |
---|
22 | """ |
---|
23 | |
---|
24 | import os |
---|
25 | from subprocess import check_call |
---|
26 | |
---|
27 | from allmydata.util import _python3 |
---|
28 | |
---|
29 | |
---|
30 | def 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 | |
---|
42 | if __name__ == "__main__": |
---|
43 | fix_potential_issue() |
---|
Note: See
TracBrowser
for help on using the repository browser.