Skip to content

Commit 4af9d37

Browse files
Introduce NoExtraItems as a C-level singleton
Implemented _Py_NoExtraItemsStruct and _Py_NoExtraItemsStruct
1 parent 25b9728 commit 4af9d37

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

Objects/typevarobject.c

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,60 @@ PyTypeObject _PyNoDefault_Type = {
123123

124124
PyObject _Py_NoDefaultStruct = _PyObject_HEAD_INIT(&_PyNoDefault_Type);
125125

126+
/* NoExtraItems: a marker object for TypeDict extra-items when it's unset. */
127+
128+
static PyObject *
129+
NoExtraItems_repr(PyObject *op)
130+
{
131+
return PyUnicode_FromString("typing.NoExtraItems");
132+
}
133+
134+
static PyObject *
135+
NoExtraItems_reduce(PyObject *op, PyObject *Py_UNUSED(ignored))
136+
{
137+
return PyUnicode_FromString("NoExtraItems");
138+
}
139+
140+
static PyMethodDef noextraitems_methods[] = {
141+
{"__reduce__", NoExtraItems_reduce, METH_NOARGS, NULL},
142+
{NULL, NULL}
143+
};
144+
145+
static PyObject *
146+
noextraitems_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
147+
{
148+
if (PyTuple_GET_SIZE(args) || (kwargs && PyDict_GET_SIZE(kwargs))) {
149+
PyErr_SetString(PyExc_TypeError, "NoExtraItemsType takes no arguments");
150+
return NULL;
151+
}
152+
return (PyObject *)&_Py_NoExtraItemsStruct;
153+
}
154+
155+
static void
156+
noextraitems_dealloc(PyObject *obj)
157+
{
158+
/* Immortal singleton: never actually deallocates. */
159+
_Py_SetImmortal(obj);
160+
}
161+
162+
PyDoc_STRVAR(noextraitems_doc,
163+
"NoExtraItemsType()\n"
164+
"--\n\n"
165+
"The type of the NoExtraItems singleton.");
166+
167+
PyTypeObject _PyNoExtraItems_Type = {
168+
PyVarObject_HEAD_INIT(&PyType_Type, 0)
169+
"NoExtraItemsType",
170+
.tp_dealloc = noextraitems_dealloc,
171+
.tp_repr = NoExtraItems_repr,
172+
.tp_flags = Py_TPFLAGS_DEFAULT,
173+
.tp_doc = noextraitems_doc,
174+
.tp_methods = noextraitems_methods,
175+
.tp_new = noextraitems_new,
176+
};
177+
178+
PyObject _Py_NoExtraItemsStruct = _PyObject_HEAD_INIT(&_PyNoExtraItems_Type);
179+
126180
typedef struct {
127181
PyObject_HEAD
128182
PyObject *value;

0 commit comments

Comments
 (0)