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()) diff --git a/Objects/cellobject.c b/Objects/cellobject.c index 86bebb9604..9a232ff76b 100644 --- a/Objects/cellobject.c +++ b/Objects/cellobject.c @@ -20,6 +20,25 @@ PyCell_New(PyObject *obj) return (PyObject *)op; } +static PyObject * +cell_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) +{ + PyObject *return_value = NULL; + PyObject *obj = NULL; + + if (!_PyArg_NoKeywords("cell", kwargs)) { + goto exit; + } + /* min = 0: we allow the cell to be empty */ + if (!PyArg_UnpackTuple(args, "cell", 0, 1, &obj)) { + goto exit; + } + return_value = PyCell_New(obj); + +exit: + return return_value; +} + PyObject * PyCell_Get(PyObject *op) { @@ -156,4 +175,13 @@ PyTypeObject PyCell_Type = { 0, /* tp_methods */ 0, /* tp_members */ cell_getsetlist, /* tp_getset */ + 0, /* tp_base */ + 0, /* tp_dict */ + 0, /* tp_descr_get */ + 0, /* tp_descr_set */ + 0, /* tp_dictoffset */ + 0, /* tp_init */ + 0, /* tp_alloc */ + (newfunc)cell_new, /* tp_new */ + 0, /* tp_free */ };