diff -r 0238eafb68da Lib/test/test_sys.py --- a/Lib/test/test_sys.py Wed Oct 14 18:25:31 2015 +0200 +++ b/Lib/test/test_sys.py Fri Oct 16 19:26:14 2015 +0300 @@ -1112,6 +1112,26 @@ class SizeofTest(unittest.TestCase): # weakcallableproxy check(weakref.proxy(int), size('2Pn2P')) + def check_slots(self, obj, base, extra): + self.assertEqual(sys.getsizeof(obj), + sys.getsizeof(base) + struct.calcsize(extra)) + def test_slots(self): + # check all subclassable types defined in Objects/ that allow + # non-empty __slots__ + check = self.check_slots + class BA(bytearray): + __slots__ = 'a', 'b', 'c' + check(BA(), bytearray(), '3P3P') + class D(dict): + __slots__ = 'a', 'b', 'c' + check(D(), {}, '3P') + class L(list): + __slots__ = 'a', 'b', 'c' + check(L(), [], '3P') + class S(set): + __slots__ = 'a', 'b', 'c' + check(S(), set(), '3P') + def test_pythontypes(self): # check all types defined in Python/ size = test.support.calcobjsize diff -r 0238eafb68da Modules/_collectionsmodule.c --- a/Modules/_collectionsmodule.c Wed Oct 14 18:25:31 2015 +0200 +++ b/Modules/_collectionsmodule.c Fri Oct 16 19:26:14 2015 +0300 @@ -1479,7 +1479,7 @@ deque_sizeof(dequeobject *deque, void *u Py_ssize_t res; Py_ssize_t blocks; - res = sizeof(dequeobject); + res = _PyObject_SIZE(Py_TYPE(deque)); blocks = (deque->leftindex + Py_SIZE(deque) + BLOCKLEN - 1) / BLOCKLEN; assert(deque->leftindex + Py_SIZE(deque) - 1 == (blocks - 1) * BLOCKLEN + deque->rightindex); diff -r 0238eafb68da Modules/_decimal/_decimal.c --- a/Modules/_decimal/_decimal.c Wed Oct 14 18:25:31 2015 +0200 +++ b/Modules/_decimal/_decimal.c Fri Oct 16 19:26:14 2015 +0300 @@ -4529,7 +4529,7 @@ dec_sizeof(PyObject *v, PyObject *dummy { Py_ssize_t res; - res = sizeof(PyDecObject); + res = _PyObject_SIZE(Py_TYPE(v)); if (mpd_isdynamic_data(MPD(v))) { res += MPD(v)->alloc * sizeof(mpd_uint_t); } diff -r 0238eafb68da Modules/_elementtree.c --- a/Modules/_elementtree.c Wed Oct 14 18:25:31 2015 +0200 +++ b/Modules/_elementtree.c Fri Oct 16 19:26:14 2015 +0300 @@ -847,7 +847,7 @@ static Py_ssize_t _elementtree_Element___sizeof___impl(ElementObject *self) /*[clinic end generated code: output=bf73867721008000 input=70f4b323d55a17c1]*/ { - Py_ssize_t result = sizeof(ElementObject); + Py_ssize_t result = _PyObject_SIZE(Py_TYPE(self)); if (self->extra) { result += sizeof(ElementObjectExtra); if (self->extra->children != self->extra->_children) diff -r 0238eafb68da Modules/_io/bufferedio.c --- a/Modules/_io/bufferedio.c Wed Oct 14 18:25:31 2015 +0200 +++ b/Modules/_io/bufferedio.c Fri Oct 16 19:26:14 2015 +0300 @@ -423,7 +423,7 @@ buffered_sizeof(buffered *self, void *un { Py_ssize_t res; - res = sizeof(buffered); + res = _PyObject_SIZE(Py_TYPE(self)); if (self->buffer) res += self->buffer_size; return PyLong_FromSsize_t(res); diff -r 0238eafb68da Modules/_io/bytesio.c --- a/Modules/_io/bytesio.c Wed Oct 14 18:25:31 2015 +0200 +++ b/Modules/_io/bytesio.c Fri Oct 16 19:26:14 2015 +0300 @@ -991,7 +991,7 @@ bytesio_sizeof(bytesio *self, void *unus { Py_ssize_t res; - res = sizeof(bytesio); + res = _PyObject_SIZE(Py_TYPE(self)); if (self->buf && !SHARED_BUF(self)) res += _PySys_GetSizeOf(self->buf); return PyLong_FromSsize_t(res); diff -r 0238eafb68da Modules/_pickle.c --- a/Modules/_pickle.c Wed Oct 14 18:25:31 2015 +0200 +++ b/Modules/_pickle.c Fri Oct 16 19:26:14 2015 +0300 @@ -4077,7 +4077,7 @@ static Py_ssize_t { Py_ssize_t res, s; - res = sizeof(PicklerObject); + res = _PyObject_SIZE(Py_TYPE(self)); if (self->memo != NULL) { res += sizeof(PyMemoTable); res += self->memo->mt_allocated * sizeof(PyMemoEntry); @@ -6476,7 +6476,7 @@ static Py_ssize_t { Py_ssize_t res; - res = sizeof(UnpicklerObject); + res = _PyObject_SIZE(Py_TYPE(self)); if (self->memo != NULL) res += self->memo_size * sizeof(PyObject *); if (self->marks != NULL) diff -r 0238eafb68da Modules/_struct.c --- a/Modules/_struct.c Wed Oct 14 18:25:31 2015 +0200 +++ b/Modules/_struct.c Fri Oct 16 19:26:14 2015 +0300 @@ -1924,7 +1924,7 @@ s_sizeof(PyStructObject *self, void *unu Py_ssize_t size; formatcode *code; - size = sizeof(PyStructObject) + sizeof(formatcode); + size = _PyObject_SIZE(Py_TYPE(self)) + sizeof(formatcode); for (code = self->s_codes; code->fmtdef != NULL; code++) size += sizeof(formatcode); return PyLong_FromSsize_t(size); diff -r 0238eafb68da Modules/arraymodule.c --- a/Modules/arraymodule.c Wed Oct 14 18:25:31 2015 +0200 +++ b/Modules/arraymodule.c Fri Oct 16 19:26:14 2015 +0300 @@ -1743,7 +1743,7 @@ array_array___sizeof___impl(arrayobject /*[clinic end generated code: output=d8e1c61ebbe3eaed input=805586565bf2b3c6]*/ { Py_ssize_t res; - res = sizeof(arrayobject) + self->allocated * self->ob_descr->itemsize; + res = _PyObject_SIZE(Py_TYPE(self)) + self->allocated * self->ob_descr->itemsize; return PyLong_FromSsize_t(res); } diff -r 0238eafb68da Modules/itertoolsmodule.c --- a/Modules/itertoolsmodule.c Wed Oct 14 18:25:31 2015 +0200 +++ b/Modules/itertoolsmodule.c Fri Oct 16 19:26:14 2015 +0300 @@ -2109,7 +2109,7 @@ product_sizeof(productobject *lz, void * { Py_ssize_t res; - res = sizeof(productobject); + res = _PyObject_SIZE(Py_TYPE(lz)); res += PyTuple_GET_SIZE(lz->pools) * sizeof(Py_ssize_t); return PyLong_FromSsize_t(res); } @@ -2440,7 +2440,7 @@ combinations_sizeof(combinationsobject * { Py_ssize_t res; - res = sizeof(combinationsobject); + res = _PyObject_SIZE(Py_TYPE(co)); res += co->r * sizeof(Py_ssize_t); return PyLong_FromSsize_t(res); } @@ -2781,7 +2781,7 @@ cwr_sizeof(cwrobject *co, void *unused) { Py_ssize_t res; - res = sizeof(cwrobject); + res = _PyObject_SIZE(Py_TYPE(co)); res += co->r * sizeof(Py_ssize_t); return PyLong_FromSsize_t(res); } @@ -3129,7 +3129,7 @@ permutations_sizeof(permutationsobject * { Py_ssize_t res; - res = sizeof(permutationsobject); + res = _PyObject_SIZE(Py_TYPE(po)); res += PyTuple_GET_SIZE(po->pool) * sizeof(Py_ssize_t); res += po->r * sizeof(Py_ssize_t); return PyLong_FromSsize_t(res); diff -r 0238eafb68da Modules/mmapmodule.c --- a/Modules/mmapmodule.c Wed Oct 14 18:25:31 2015 +0200 +++ b/Modules/mmapmodule.c Fri Oct 16 19:26:14 2015 +0300 @@ -719,7 +719,7 @@ mmap__sizeof__method(mmap_object *self, { Py_ssize_t res; - res = sizeof(mmap_object); + res = _PyObject_SIZE(Py_TYPE(self)); if (self->tagname) res += strlen(self->tagname) + 1; return PyLong_FromSsize_t(res); diff -r 0238eafb68da Modules/parsermodule.c --- a/Modules/parsermodule.c Wed Oct 14 18:25:31 2015 +0200 +++ b/Modules/parsermodule.c Fri Oct 16 19:26:14 2015 +0300 @@ -397,7 +397,7 @@ parser_sizeof(PyST_Object *st, void *unu { Py_ssize_t res; - res = sizeof(PyST_Object) + _PyNode_SizeOf(st->st_node); + res = _PyObject_SIZE(Py_TYPE(st)) + _PyNode_SizeOf(st->st_node); return PyLong_FromSsize_t(res); } diff -r 0238eafb68da Objects/bytearrayobject.c --- a/Objects/bytearrayobject.c Wed Oct 14 18:25:31 2015 +0200 +++ b/Objects/bytearrayobject.c Fri Oct 16 19:26:14 2015 +0300 @@ -2905,7 +2905,7 @@ bytearray_sizeof_impl(PyByteArrayObject { Py_ssize_t res; - res = sizeof(PyByteArrayObject) + self->ob_alloc * sizeof(char); + res = _PyObject_SIZE(Py_TYPE(self)) + self->ob_alloc * sizeof(char); return PyLong_FromSsize_t(res); } diff -r 0238eafb68da Objects/codeobject.c --- a/Objects/codeobject.c Wed Oct 14 18:25:31 2015 +0200 +++ b/Objects/codeobject.c Fri Oct 16 19:26:14 2015 +0300 @@ -384,7 +384,7 @@ code_sizeof(PyCodeObject *co, void *unus { Py_ssize_t res; - res = sizeof(PyCodeObject); + res = _PyObject_SIZE(Py_TYPE(co)); if (co->co_cell2arg != NULL && co->co_cellvars != NULL) res += PyTuple_GET_SIZE(co->co_cellvars) * sizeof(unsigned char); return PyLong_FromSsize_t(res); diff -r 0238eafb68da Objects/dictobject.c --- a/Objects/dictobject.c Wed Oct 14 18:25:31 2015 +0200 +++ b/Objects/dictobject.c Fri Oct 16 19:26:14 2015 +0300 @@ -2560,7 +2560,7 @@ PyObject * Py_ssize_t size, res; size = DK_SIZE(mp->ma_keys); - res = sizeof(PyDictObject); + res = _PyObject_SIZE(Py_TYPE(mp)); if (mp->ma_values) res += size * sizeof(PyObject*); /* If the dictionary is split, the keys portion is accounted-for diff -r 0238eafb68da Objects/listobject.c --- a/Objects/listobject.c Wed Oct 14 18:25:31 2015 +0200 +++ b/Objects/listobject.c Fri Oct 16 19:26:14 2015 +0300 @@ -2324,7 +2324,7 @@ list_sizeof(PyListObject *self) { Py_ssize_t res; - res = sizeof(PyListObject) + self->allocated * sizeof(void*); + res = _PyObject_SIZE(Py_TYPE(self)) + self->allocated * sizeof(void*); return PyLong_FromSsize_t(res); } diff -r 0238eafb68da Objects/odictobject.c --- a/Objects/odictobject.c Wed Oct 14 18:25:31 2015 +0200 +++ b/Objects/odictobject.c Fri Oct 16 19:26:14 2015 +0300 @@ -950,8 +950,6 @@ odict_sizeof(PyODictObject *od) if (res == -1 && PyErr_Occurred()) return NULL; - res += sizeof(PyODictObject) - sizeof(PyDictObject); - /* instance dict */ pylong = _PyDict_SizeOf((PyDictObject *)od->od_inst_dict); if (pylong == NULL) diff -r 0238eafb68da Objects/setobject.c --- a/Objects/setobject.c Wed Oct 14 18:25:31 2015 +0200 +++ b/Objects/setobject.c Fri Oct 16 19:26:14 2015 +0300 @@ -2001,7 +2001,7 @@ set_sizeof(PySetObject *so) { Py_ssize_t res; - res = sizeof(PySetObject); + res = _PyObject_SIZE(Py_TYPE(so)); if (so->table != so->smalltable) res = res + (so->mask + 1) * sizeof(setentry); return PyLong_FromSsize_t(res);