Skip to content

Commit 23d7f12

Browse files
committed
use new generic __dict__ descriptor implementations
1 parent 8eb1269 commit 23d7f12

4 files changed

Lines changed: 6 additions & 141 deletions

File tree

Modules/_functoolsmodule.c

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -155,44 +155,8 @@ static PyMemberDef partial_memberlist[] = {
155155
{NULL} /* Sentinel */
156156
};
157157

158-
static PyObject *
159-
partial_get_dict(partialobject *pto)
160-
{
161-
if (pto->dict == NULL) {
162-
pto->dict = PyDict_New();
163-
if (pto->dict == NULL)
164-
return NULL;
165-
}
166-
Py_INCREF(pto->dict);
167-
return pto->dict;
168-
}
169-
170-
static int
171-
partial_set_dict(partialobject *pto, PyObject *value)
172-
{
173-
PyObject *tmp;
174-
175-
/* It is illegal to del p.__dict__ */
176-
if (value == NULL) {
177-
PyErr_SetString(PyExc_TypeError,
178-
"a partial object's dictionary may not be deleted");
179-
return -1;
180-
}
181-
/* Can only set __dict__ to a dictionary */
182-
if (!PyDict_Check(value)) {
183-
PyErr_SetString(PyExc_TypeError,
184-
"setting partial object's dictionary to a non-dict");
185-
return -1;
186-
}
187-
tmp = pto->dict;
188-
Py_INCREF(value);
189-
pto->dict = value;
190-
Py_XDECREF(tmp);
191-
return 0;
192-
}
193-
194158
static PyGetSetDef partial_getsetlist[] = {
195-
{"__dict__", (getter)partial_get_dict, (setter)partial_set_dict},
159+
{"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict},
196160
{NULL} /* Sentinel */
197161
};
198162

Modules/_io/iobase.c

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -159,19 +159,6 @@ iobase_closed_get(PyObject *self, void *context)
159159
return PyBool_FromLong(IS_CLOSED(self));
160160
}
161161

162-
static PyObject *
163-
iobase_get_dict(PyObject *self)
164-
{
165-
PyObject **dictptr = _PyObject_GetDictPtr(self);
166-
PyObject *dict;
167-
assert(dictptr);
168-
dict = *dictptr;
169-
if (dict == NULL)
170-
dict = *dictptr = PyDict_New();
171-
Py_XINCREF(dict);
172-
return dict;
173-
}
174-
175162
PyObject *
176163
_PyIOBase_check_closed(PyObject *self, PyObject *args)
177164
{
@@ -714,7 +701,7 @@ static PyMethodDef iobase_methods[] = {
714701
};
715702

716703
static PyGetSetDef iobase_getset[] = {
717-
{"__dict__", (getter)iobase_get_dict, NULL, NULL},
704+
{"__dict__", PyObject_GenericGetDict, NULL, NULL},
718705
{"closed", (getter)iobase_closed_get, NULL, NULL},
719706
{NULL}
720707
};

Objects/exceptions.c

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -177,36 +177,6 @@ static PyMethodDef BaseException_methods[] = {
177177
{NULL, NULL, 0, NULL},
178178
};
179179

180-
181-
static PyObject *
182-
BaseException_get_dict(PyBaseExceptionObject *self)
183-
{
184-
if (self->dict == NULL) {
185-
self->dict = PyDict_New();
186-
if (!self->dict)
187-
return NULL;
188-
}
189-
Py_INCREF(self->dict);
190-
return self->dict;
191-
}
192-
193-
static int
194-
BaseException_set_dict(PyBaseExceptionObject *self, PyObject *val)
195-
{
196-
if (val == NULL) {
197-
PyErr_SetString(PyExc_TypeError, "__dict__ may not be deleted");
198-
return -1;
199-
}
200-
if (!PyDict_Check(val)) {
201-
PyErr_SetString(PyExc_TypeError, "__dict__ must be a dictionary");
202-
return -1;
203-
}
204-
Py_CLEAR(self->dict);
205-
Py_INCREF(val);
206-
self->dict = val;
207-
return 0;
208-
}
209-
210180
static PyObject *
211181
BaseException_get_args(PyBaseExceptionObject *self)
212182
{
@@ -320,7 +290,7 @@ BaseException_set_cause(PyObject *self, PyObject *arg) {
320290

321291

322292
static PyGetSetDef BaseException_getset[] = {
323-
{"__dict__", (getter)BaseException_get_dict, (setter)BaseException_set_dict},
293+
{"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict},
324294
{"args", (getter)BaseException_get_args, (setter)BaseException_set_args},
325295
{"__traceback__", (getter)BaseException_get_tb, (setter)BaseException_set_tb},
326296
{"__context__", (getter)BaseException_get_context,

Objects/funcobject.c

Lines changed: 3 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -244,42 +244,6 @@ static PyMemberDef func_memberlist[] = {
244244
{NULL} /* Sentinel */
245245
};
246246

247-
static PyObject *
248-
func_get_dict(PyFunctionObject *op)
249-
{
250-
if (op->func_dict == NULL) {
251-
op->func_dict = PyDict_New();
252-
if (op->func_dict == NULL)
253-
return NULL;
254-
}
255-
Py_INCREF(op->func_dict);
256-
return op->func_dict;
257-
}
258-
259-
static int
260-
func_set_dict(PyFunctionObject *op, PyObject *value)
261-
{
262-
PyObject *tmp;
263-
264-
/* It is illegal to del f.func_dict */
265-
if (value == NULL) {
266-
PyErr_SetString(PyExc_TypeError,
267-
"function's dictionary may not be deleted");
268-
return -1;
269-
}
270-
/* Can only set func_dict to a dictionary */
271-
if (!PyDict_Check(value)) {
272-
PyErr_SetString(PyExc_TypeError,
273-
"setting function's dictionary to a non-dict");
274-
return -1;
275-
}
276-
tmp = op->func_dict;
277-
Py_INCREF(value);
278-
op->func_dict = value;
279-
Py_XDECREF(tmp);
280-
return 0;
281-
}
282-
283247
static PyObject *
284248
func_get_code(PyFunctionObject *op)
285249
{
@@ -476,7 +440,7 @@ static PyGetSetDef func_getsetlist[] = {
476440
(setter)func_set_kwdefaults},
477441
{"__annotations__", (getter)func_get_annotations,
478442
(setter)func_set_annotations},
479-
{"__dict__", (getter)func_get_dict, (setter)func_set_dict},
443+
{"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict},
480444
{"__name__", (getter)func_get_name, (setter)func_set_name},
481445
{"__qualname__", (getter)func_get_qualname, (setter)func_set_qualname},
482446
{NULL} /* Sentinel */
@@ -831,22 +795,12 @@ cm_get___isabstractmethod__(classmethod *cm, void *closure)
831795
Py_RETURN_FALSE;
832796
}
833797

834-
static PyObject *
835-
cm_get___dict__(PyObject *cm, void *closure)
836-
{
837-
PyObject **dictptr = _PyObject_GetDictPtr(cm);
838-
if (*dictptr == NULL)
839-
*dictptr = PyDict_New();
840-
Py_XINCREF(*dictptr);
841-
return *dictptr;
842-
}
843-
844798
static PyGetSetDef cm_getsetlist[] = {
845799
{"__isabstractmethod__",
846800
(getter)cm_get___isabstractmethod__, NULL,
847801
NULL,
848802
NULL},
849-
{"__dict__", (getter)cm_get___dict__, NULL, NULL, NULL},
803+
{"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict, NULL, NULL},
850804
{NULL} /* Sentinel */
851805
};
852806

@@ -1020,22 +974,12 @@ sm_get___isabstractmethod__(staticmethod *sm, void *closure)
1020974
Py_RETURN_FALSE;
1021975
}
1022976

1023-
static PyObject *
1024-
sm_get___dict__(PyObject *sm, void *closure)
1025-
{
1026-
PyObject **dictptr = _PyObject_GetDictPtr(sm);
1027-
if (*dictptr == NULL)
1028-
*dictptr = PyDict_New();
1029-
Py_XINCREF(*dictptr);
1030-
return *dictptr;
1031-
}
1032-
1033977
static PyGetSetDef sm_getsetlist[] = {
1034978
{"__isabstractmethod__",
1035979
(getter)sm_get___isabstractmethod__, NULL,
1036980
NULL,
1037981
NULL},
1038-
{"__dict__", (getter)sm_get___dict__, NULL, NULL, NULL},
982+
{"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict, NULL, NULL},
1039983
{NULL} /* Sentinel */
1040984
};
1041985

0 commit comments

Comments
 (0)