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

Method _check_in_scopes

Lib/test/test_listcomps.py:99–133  ·  view source on GitHub ↗
(self, code, outputs=None, ns=None, scopes=None, raises=(),
                         exec_func=exec)

Source from the content-addressed store, hash-verified

97
98class ListComprehensionTest(unittest.TestCase):
99 def _check_in_scopes(self, code, outputs=None, ns=None, scopes=None, raises=(),
100 exec_func=exec):
101 code = textwrap.dedent(code)
102 scopes = scopes or ["module", "class", "function"]
103 for scope in scopes:
104 with self.subTest(scope=scope):
105 if scope == "class":
106 newcode = textwrap.dedent("""
107 class _C:
108 {code}
109 """).format(code=textwrap.indent(code, " "))
110 def get_output(moddict, name):
111 return getattr(moddict["_C"], name)
112 elif scope == "function":
113 newcode = textwrap.dedent("""
114 def _f():
115 {code}
116 return locals()
117 _out = _f()
118 """).format(code=textwrap.indent(code, " "))
119 def get_output(moddict, name):
120 return moddict["_out"][name]
121 else:
122 newcode = code
123 def get_output(moddict, name):
124 return moddict[name]
125 newns = ns.copy() if ns else {}
126 try:
127 exec_func(newcode, newns)
128 except raises as e:
129 # We care about e.g. NameError vs UnboundLocalError
130 self.assertIs(type(e), raises)
131 else:
132 for k, v in (outputs or {}).items():
133 self.assertEqual(get_output(newns, k), v, k)
134
135 def test_lambdas_with_iteration_var_as_default(self):
136 code = """

Calls 8

dedentMethod · 0.45
subTestMethod · 0.45
formatMethod · 0.45
indentMethod · 0.45
copyMethod · 0.45
assertIsMethod · 0.45
itemsMethod · 0.45
assertEqualMethod · 0.45

Tested by

no test coverage detected