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

Method assertNamespaceMatches

Lib/test/test_runpy.py:87–125  ·  view source on GitHub ↗

Check two namespaces match. Ignores any unspecified interpreter created names

(self, result_ns, expected_ns)

Source from the content-addressed store, hash-verified

85 "has_location", "submodule_search_locations"]
86
87 def assertNamespaceMatches(self, result_ns, expected_ns):
88 """Check two namespaces match.
89
90 Ignores any unspecified interpreter created names
91 """
92 # Avoid side effects
93 result_ns = result_ns.copy()
94 expected_ns = expected_ns.copy()
95 # Impls are permitted to add extra names, so filter them out
96 for k in list(result_ns):
97 if k.startswith("__") and k.endswith("__"):
98 if k not in expected_ns:
99 result_ns.pop(k)
100 if k not in expected_ns["nested"]:
101 result_ns["nested"].pop(k)
102 # Spec equality includes the loader, so we take the spec out of the
103 # result namespace and check that separately
104 result_spec = result_ns.pop("__spec__")
105 expected_spec = expected_ns.pop("__spec__")
106 if expected_spec is None:
107 self.assertIsNone(result_spec)
108 else:
109 # If an expected loader is set, we just check we got the right
110 # type, rather than checking for full equality
111 if expected_spec.loader is not None:
112 self.assertEqual(type(result_spec.loader),
113 type(expected_spec.loader))
114 for attr in self.CHECKED_SPEC_ATTRIBUTES:
115 k = "__spec__." + attr
116 actual = (k, getattr(result_spec, attr))
117 expected = (k, getattr(expected_spec, attr))
118 self.assertEqual(actual, expected)
119 # For the rest, we still don't use direct dict comparison on the
120 # namespace, as the diffs are too hard to debug if anything breaks
121 self.assertEqual(set(result_ns), set(expected_ns))
122 for k in result_ns:
123 actual = (k, result_ns[k])
124 expected = (k, expected_ns[k])
125 self.assertEqual(actual, expected)
126
127 def check_code_execution(self, create_namespace, expected_namespace):
128 """Check that an interface runs the example code correctly

Callers 1

check_code_executionMethod · 0.95

Calls 9

listClass · 0.85
getattrFunction · 0.85
setFunction · 0.85
assertIsNoneMethod · 0.80
copyMethod · 0.45
startswithMethod · 0.45
endswithMethod · 0.45
popMethod · 0.45
assertEqualMethod · 0.45

Tested by

no test coverage detected