MCPcopy Index your code
hub / github.com/python/cpython / assertNamespaceMatches

Method assertNamespaceMatches

Lib/test/test_runpy.py:88–126  ·  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

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

Callers 1

check_code_executionMethod · 0.95

Calls 8

listClass · 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