MCPcopy Index your code
hub / github.com/RustPython/RustPython / traverse_compare

Method traverse_compare

Lib/test/support/ast_helper.py:15–46  ·  view source on GitHub ↗
(a, b, missing=object())

Source from the content-addressed store, hash-verified

13 # instead of string building, it traverses the two trees
14 # in lock-step.
15 def traverse_compare(a, b, missing=object()):
16 if type(a) is not type(b):
17 self.fail(f"{type(a)!r} is not {type(b)!r}")
18 if isinstance(a, ast.AST):
19 for field in a._fields:
20 if isinstance(a, ast.Constant) and field == "kind":
21 # Skip the 'kind' field for ast.Constant
22 continue
23 value1 = getattr(a, field, missing)
24 value2 = getattr(b, field, missing)
25 # Singletons are equal by definition, so further
26 # testing can be skipped.
27 if value1 is not value2:
28 traverse_compare(value1, value2)
29 elif isinstance(a, list):
30 try:
31 for node1, node2 in zip(a, b, strict=True):
32 traverse_compare(node1, node2)
33 except ValueError:
34 # Attempt a "pretty" error ala assertSequenceEqual()
35 len1 = len(a)
36 len2 = len(b)
37 if len1 > len2:
38 what = "First"
39 diff = len1 - len2
40 else:
41 what = "Second"
42 diff = len2 - len1
43 msg = f"{what} list contains {diff} additional elements."
44 raise self.failureException(msg) from None
45 elif a != b:
46 self.fail(f"{a!r} != {b!r}")
47 traverse_compare(ast1, ast2)

Callers

nothing calls this directly

Calls 4

isinstanceFunction · 0.85
getattrFunction · 0.85
lenFunction · 0.85
failMethod · 0.45

Tested by

no test coverage detected