diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2017-03-12 09:12:30 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-03-12 09:12:30 (GMT) |
commit | 8999caeb003ed292011e98b8a30746cce787a125 (patch) | |
tree | e7c097ea653977792ff10ff936a6c1790ef63d6f /Modules | |
parent | 202fda55c2dffe27125703225e5af92254602dc6 (diff) | |
download | cpython-8999caeb003ed292011e98b8a30746cce787a125.zip cpython-8999caeb003ed292011e98b8a30746cce787a125.tar.gz cpython-8999caeb003ed292011e98b8a30746cce787a125.tar.bz2 |
bpo-15695: Implemented StgDict.__sizeof__(). (#509)
Diffstat (limited to 'Modules')
-rw-r--r-- | Modules/_ctypes/stgdict.c | 22 |
1 files changed, 21 insertions, 1 deletions
diff --git a/Modules/_ctypes/stgdict.c b/Modules/_ctypes/stgdict.c index 1ccaf2f..3496c57 100644 --- a/Modules/_ctypes/stgdict.c +++ b/Modules/_ctypes/stgdict.c @@ -48,6 +48,21 @@ PyCStgDict_dealloc(StgDictObject *self) PyDict_Type.tp_dealloc((PyObject *)self); } +static PyObject * +PyCStgDict_sizeof(StgDictObject *self, void *unused) +{ + Py_ssize_t res; + + res = _PyDict_SizeOf((PyDictObject *)self); + res += sizeof(StgDictObject) - sizeof(PyDictObject); + if (self->format) + res += strlen(self->format) + 1; + res += self->ndim * sizeof(Py_ssize_t); + if (self->ffi_type_pointer.elements) + res += (self->length + 1) * sizeof(ffi_type *); + return PyLong_FromSsize_t(res); +} + int PyCStgDict_clone(StgDictObject *dst, StgDictObject *src) { @@ -106,6 +121,11 @@ PyCStgDict_clone(StgDictObject *dst, StgDictObject *src) return 0; } +static struct PyMethodDef PyCStgDict_methods[] = { + {"__sizeof__", (PyCFunction)PyCStgDict_sizeof, METH_NOARGS}, + {NULL, NULL} /* sentinel */ +}; + PyTypeObject PyCStgDict_Type = { PyVarObject_HEAD_INIT(NULL, 0) "StgDict", @@ -134,7 +154,7 @@ PyTypeObject PyCStgDict_Type = { 0, /* tp_weaklistoffset */ 0, /* tp_iter */ 0, /* tp_iternext */ - 0, /* tp_methods */ + PyCStgDict_methods, /* tp_methods */ 0, /* tp_members */ 0, /* tp_getset */ 0, /* tp_base */ |