def test_generator_closure(self):
def f(nonlocal_ref):
def g(local_ref):
print(local_ref, nonlocal_ref, _global_ref, unbound_ref)
yield
return g
_arg = object()
nonlocal_vars = {"nonlocal_ref": _arg}
global_vars = {"_global_ref": _global_ref}
builtin_vars = {"print": print}
unbound_names = {"unbound_ref"}
expected = inspect.ClosureVars(nonlocal_vars, global_vars,
builtin_vars, unbound_names)
self.assertEqual(inspect.getclosurevars(f(_arg)), expected)
评论列表
文章目录