diff options
Diffstat (limited to 'Lib')
| -rw-r--r-- | Lib/test/test_funcattrs.py | 9 | ||||
| -rw-r--r-- | Lib/types.py | 7 |
2 files changed, 16 insertions, 0 deletions
diff --git a/Lib/test/test_funcattrs.py b/Lib/test/test_funcattrs.py index 35fd657ec0..11d68cc75e 100644 --- a/Lib/test/test_funcattrs.py +++ b/Lib/test/test_funcattrs.py @@ -83,6 +83,15 @@ class FunctionPropertiesTest(FuncAttrsTest): self.assertEqual(c[0].__class__.__name__, "cell") self.cannot_set_attr(f, "__closure__", c, AttributeError) + def test_cell_new(self): + cell_obj = types.CellType(1) + self.assertEqual(cell_obj.cell_contents, 1) + + cell_obj = types.CellType() + msg = "shouldn't be able to read an empty cell" + with self.assertRaises(ValueError, msg=msg): + cell_obj.cell_contents + def test_empty_cell(self): def f(): print(a) try: diff --git a/Lib/types.py b/Lib/types.py index ce4652f371..53b588da75 100644 --- a/Lib/types.py +++ b/Lib/types.py @@ -15,6 +15,13 @@ CodeType = type(_f.__code__) MappingProxyType = type(type.__dict__) SimpleNamespace = type(sys.implementation) +def _cell_factory(): + a = 1 + def f(): + nonlocal a + return f.__closure__[0] +CellType = type(_cell_factory()) + def _g(): yield 1 GeneratorType = type(_g()) |
