diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2015-12-22 06:22:05 (GMT) |
---|---|---|
committer | Serhiy Storchaka <storchaka@gmail.com> | 2015-12-22 06:22:05 (GMT) |
commit | 3987e26e7547fdd71112e70caad3e037ef8257aa (patch) | |
tree | 56b0f238b550cd659acbbc8969960d624f829a79 /Objects | |
parent | 266514ae930495c42c99de396131feb98c64bc90 (diff) | |
parent | 0ce7a3a34cec0e625f63d46390a8c1d2e167494f (diff) | |
download | cpython-3987e26e7547fdd71112e70caad3e037ef8257aa.zip cpython-3987e26e7547fdd71112e70caad3e037ef8257aa.tar.gz cpython-3987e26e7547fdd71112e70caad3e037ef8257aa.tar.bz2 |
Issue #25914: Fixed and simplified OrderedDict.__sizeof__.
Diffstat (limited to 'Objects')
-rw-r--r-- | Objects/dictobject.c | 12 | ||||
-rw-r--r-- | Objects/odictobject.c | 22 |
2 files changed, 10 insertions, 24 deletions
diff --git a/Objects/dictobject.c b/Objects/dictobject.c index 752853f..6f57db0 100644 --- a/Objects/dictobject.c +++ b/Objects/dictobject.c @@ -2557,7 +2557,7 @@ dict_tp_clear(PyObject *op) static PyObject *dictiter_new(PyDictObject *, PyTypeObject *); -PyObject * +Py_ssize_t _PyDict_SizeOf(PyDictObject *mp) { Py_ssize_t size, res; @@ -2570,7 +2570,7 @@ _PyDict_SizeOf(PyDictObject *mp) in the type object. */ if (mp->ma_keys->dk_refcnt == 1) res += sizeof(PyDictKeysObject) + (size-1) * sizeof(PyDictKeyEntry); - return PyLong_FromSsize_t(res); + return res; } Py_ssize_t @@ -2579,6 +2579,12 @@ _PyDict_KeysSize(PyDictKeysObject *keys) return sizeof(PyDictKeysObject) + (DK_SIZE(keys)-1) * sizeof(PyDictKeyEntry); } +PyObject * +dict_sizeof(PyDictObject *mp) +{ + return PyLong_FromSsize_t(_PyDict_SizeOf(mp)); +} + PyDoc_STRVAR(getitem__doc__, "x.__getitem__(y) <==> x[y]"); PyDoc_STRVAR(sizeof__doc__, @@ -2626,7 +2632,7 @@ static PyMethodDef mapp_methods[] = { DICT___CONTAINS___METHODDEF {"__getitem__", (PyCFunction)dict_subscript, METH_O | METH_COEXIST, getitem__doc__}, - {"__sizeof__", (PyCFunction)_PyDict_SizeOf, METH_NOARGS, + {"__sizeof__", (PyCFunction)dict_sizeof, METH_NOARGS, sizeof__doc__}, {"get", (PyCFunction)dict_get, METH_VARARGS, get__doc__}, diff --git a/Objects/odictobject.c b/Objects/odictobject.c index 4e51f4d..c15b408 100644 --- a/Objects/odictobject.c +++ b/Objects/odictobject.c @@ -940,27 +940,7 @@ PyDoc_STRVAR(odict_sizeof__doc__, ""); static PyObject * odict_sizeof(PyODictObject *od) { - PyObject *pylong; - Py_ssize_t res, temp; - - pylong = _PyDict_SizeOf((PyDictObject *)od); - if (pylong == NULL) - return NULL; - res = PyLong_AsSsize_t(pylong); - Py_DECREF(pylong); - if (res == -1 && PyErr_Occurred()) - return NULL; - - /* instance dict */ - pylong = _PyDict_SizeOf((PyDictObject *)od->od_inst_dict); - if (pylong == NULL) - return NULL; - temp = PyLong_AsSsize_t(pylong); - Py_DECREF(pylong); - if (temp == -1 && PyErr_Occurred()) - return NULL; - res += temp; - + Py_ssize_t res = _PyDict_SizeOf((PyDictObject *)od); res += sizeof(_ODictNode *) * _odict_FAST_SIZE(od); /* od_fast_nodes */ if (!_odict_EMPTY(od)) { res += sizeof(_ODictNode) * PyODict_SIZE(od); /* linked-list */ |