Visitar URL original
Match children by name in bytecode comparison · RustPython/RustPython@acda3d0 · GitHub
Skip to content

Commit acda3d0

Browse files
committed
Match children by name in bytecode comparison
Use name-based matching for child code objects instead of positional matching, preventing false diffs when co_consts ordering differs between CPython and RustPython.
1 parent 6de84a6 commit acda3d0

1 file changed

Lines changed: 31 additions & 11 deletions

File tree

‎scripts/compare_bytecode.py‎

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -97,19 +97,39 @@ def compare_code(cp_code, rp_code, path=""):
9797
if diffs:
9898
results.append((full, diffs))
9999

100+
# Match children by name to handle different co_consts ordering
100101
cp_ch = cp_code.get("children", [])
101102
rp_ch = rp_code.get("children", [])
102-
for i in range(max(len(cp_ch), len(rp_ch))):
103-
if i < len(cp_ch) and i < len(rp_ch):
104-
results.extend(compare_code(cp_ch[i], rp_ch[i], full))
105-
elif i < len(cp_ch):
106-
results.append(
107-
(full + "/" + cp_ch[i]["name"], [(-1, "extra in CPython", None)])
108-
)
109-
else:
110-
results.append(
111-
(full + "/" + rp_ch[i]["name"], [(-1, None, "extra in RustPython")])
112-
)
103+
104+
# Group by name, preserving order for duplicates
105+
from collections import defaultdict
106+
107+
cp_by_name = defaultdict(list)
108+
rp_by_name = defaultdict(list)
109+
for c in cp_ch:
110+
cp_by_name[c["name"]].append(c)
111+
for c in rp_ch:
112+
rp_by_name[c["name"]].append(c)
113+
114+
all_names = list(dict.fromkeys(c["name"] for c in cp_ch))
115+
for c in rp_ch:
116+
if c["name"] not in cp_by_name:
117+
all_names.append(c["name"])
118+
119+
for name in all_names:
120+
cp_list = cp_by_name.get(name, [])
121+
rp_list = rp_by_name.get(name, [])
122+
for i in range(max(len(cp_list), len(rp_list))):
123+
if i < len(cp_list) and i < len(rp_list):
124+
results.extend(compare_code(cp_list[i], rp_list[i], full))
125+
elif i < len(cp_list):
126+
results.append(
127+
(full + "/" + name, [(-1, "extra in CPython", None)])
128+
)
129+
else:
130+
results.append(
131+
(full + "/" + name, [(-1, None, "extra in RustPython")])
132+
)
113133

114134
return results
115135

0 commit comments

Comments
 (0)