Skip to content

Commit 8999cae

Browse files
bpo-15695: Implemented StgDict.__sizeof__(). (#509)
1 parent 202fda5 commit 8999cae

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

Modules/_ctypes/stgdict.c

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,21 @@ PyCStgDict_dealloc(StgDictObject *self)
4848
PyDict_Type.tp_dealloc((PyObject *)self);
4949
}
5050

51+
static PyObject *
52+
PyCStgDict_sizeof(StgDictObject *self, void *unused)
53+
{
54+
Py_ssize_t res;
55+
56+
res = _PyDict_SizeOf((PyDictObject *)self);
57+
res += sizeof(StgDictObject) - sizeof(PyDictObject);
58+
if (self->format)
59+
res += strlen(self->format) + 1;
60+
res += self->ndim * sizeof(Py_ssize_t);
61+
if (self->ffi_type_pointer.elements)
62+
res += (self->length + 1) * sizeof(ffi_type *);
63+
return PyLong_FromSsize_t(res);
64+
}
65+
5166
int
5267
PyCStgDict_clone(StgDictObject *dst, StgDictObject *src)
5368
{
@@ -106,6 +121,11 @@ PyCStgDict_clone(StgDictObject *dst, StgDictObject *src)
106121
return 0;
107122
}
108123

124+
static struct PyMethodDef PyCStgDict_methods[] = {
125+
{"__sizeof__", (PyCFunction)PyCStgDict_sizeof, METH_NOARGS},
126+
{NULL, NULL} /* sentinel */
127+
};
128+
109129
PyTypeObject PyCStgDict_Type = {
110130
PyVarObject_HEAD_INIT(NULL, 0)
111131
"StgDict",
@@ -134,7 +154,7 @@ PyTypeObject PyCStgDict_Type = {
134154
0, /* tp_weaklistoffset */
135155
0, /* tp_iter */
136156
0, /* tp_iternext */
137-
0, /* tp_methods */
157+
PyCStgDict_methods, /* tp_methods */
138158
0, /* tp_members */
139159
0, /* tp_getset */
140160
0, /* tp_base */

0 commit comments

Comments
 (0)