Added additional __sizeof__ implementations and addressed comments made in
Issue3122.
This commit is contained in:
parent
cc3f2b1d16
commit
9be2ec109b
|
@ -408,14 +408,11 @@ class SizeofTest(unittest.TestCase):
|
|||
self.file.close()
|
||||
test.test_support.unlink(test.test_support.TESTFN)
|
||||
|
||||
def check_sizeof(self, o, size, size2=None):
|
||||
"""Check size of o. Possible are size and optionally size2)."""
|
||||
def check_sizeof(self, o, size):
|
||||
result = sys.getsizeof(o)
|
||||
msg = 'wrong size for %s: got %d, expected ' % (type(o), result)
|
||||
if (size2 != None) and (result != size):
|
||||
self.assertEqual(result, size2, msg + str(size2))
|
||||
else:
|
||||
self.assertEqual(result, size, msg + str(size))
|
||||
msg = 'wrong size for %s: got %d, expected %d' \
|
||||
% (type(o), result, size)
|
||||
self.assertEqual(result, size, msg)
|
||||
|
||||
def calcsize(self, fmt):
|
||||
"""Wrapper around struct.calcsize which enforces the alignment of the
|
||||
|
@ -438,6 +435,13 @@ class SizeofTest(unittest.TestCase):
|
|||
check(buffer(''), size(h + '2P2Pil'))
|
||||
# builtin_function_or_method
|
||||
check(len, size(h + '3P'))
|
||||
# bytearray
|
||||
samples = ['', 'u'*100000]
|
||||
for sample in samples:
|
||||
x = bytearray(sample)
|
||||
check(x, size(vh + 'iPP') + x.__alloc__() * self.c)
|
||||
# bytearray_iterator
|
||||
check(iter(bytearray()), size(h + 'PP'))
|
||||
# cell
|
||||
def get_cell():
|
||||
x = 42
|
||||
|
@ -507,6 +511,17 @@ class SizeofTest(unittest.TestCase):
|
|||
check(float(0), size(h + 'd'))
|
||||
# sys.floatinfo
|
||||
check(sys.float_info, size(vh) + self.P * len(sys.float_info))
|
||||
# frame
|
||||
import inspect
|
||||
CO_MAXBLOCKS = 20
|
||||
x = inspect.currentframe()
|
||||
ncells = len(x.f_code.co_cellvars)
|
||||
nfrees = len(x.f_code.co_freevars)
|
||||
extras = x.f_code.co_stacksize + x.f_code.co_nlocals +\
|
||||
ncells + nfrees - 1
|
||||
check(x, size(vh + '12P3i') +\
|
||||
CO_MAXBLOCKS*struct.calcsize('3i') +\
|
||||
self.P + extras*self.P)
|
||||
# function
|
||||
def func(): pass
|
||||
check(func, size(h + '9P'))
|
||||
|
@ -545,7 +560,7 @@ class SizeofTest(unittest.TestCase):
|
|||
# listreverseiterator (list)
|
||||
check(reversed([]), size(h + 'lP'))
|
||||
# long
|
||||
check(0L, size(vh + 'H'))
|
||||
check(0L, size(vh + 'H') - self.H)
|
||||
check(1L, size(vh + 'H'))
|
||||
check(-1L, size(vh + 'H'))
|
||||
check(32768L, size(vh + 'H') + self.H)
|
||||
|
@ -570,6 +585,29 @@ class SizeofTest(unittest.TestCase):
|
|||
check(iter(xrange(1)), size(h + '4l'))
|
||||
# reverse
|
||||
check(reversed(''), size(h + 'PP'))
|
||||
# set
|
||||
# frozenset
|
||||
PySet_MINSIZE = 8
|
||||
samples = [[], range(10), range(50)]
|
||||
s = size(h + '3P2P') +\
|
||||
PySet_MINSIZE*struct.calcsize('lP') + self.l + self.P
|
||||
for sample in samples:
|
||||
minused = len(sample)
|
||||
if minused == 0: tmp = 1
|
||||
# the computation of minused is actually a bit more complicated
|
||||
# but this suffices for the sizeof test
|
||||
minused = minused*2
|
||||
newsize = PySet_MINSIZE
|
||||
while newsize <= minused:
|
||||
newsize = newsize << 1
|
||||
if newsize <= 8:
|
||||
check(set(sample), s)
|
||||
check(frozenset(sample), s)
|
||||
else:
|
||||
check(set(sample), s + newsize*struct.calcsize('lP'))
|
||||
check(frozenset(sample), s + newsize*struct.calcsize('lP'))
|
||||
# setiterator
|
||||
check(iter(set()), size(h + 'P3P'))
|
||||
# slice
|
||||
check(slice(1), size(h + '3P'))
|
||||
# str
|
||||
|
@ -600,16 +638,7 @@ class SizeofTest(unittest.TestCase):
|
|||
# we need to test for both sizes, because we don't know if the string
|
||||
# has been cached
|
||||
for s in samples:
|
||||
basicsize = size(h + 'PPlP') + usize * (len(s) + 1)
|
||||
check(s, basicsize, size2=basicsize + sys.getsizeof(str(s)))
|
||||
# XXX trigger caching encoded version as Python string
|
||||
s = samples[1]
|
||||
try:
|
||||
getattr(sys, s)
|
||||
except AttributeError:
|
||||
pass
|
||||
finally:
|
||||
check(s, basicsize + sys.getsizeof(str(s)))
|
||||
check(s, size(h + 'PPlP') + usize * (len(s) + 1))
|
||||
# weakref
|
||||
import weakref
|
||||
check(weakref.ref(int), size(h + '2Pl2P'))
|
||||
|
|
|
@ -3106,6 +3106,19 @@ bytes_reduce(PyByteArrayObject *self)
|
|||
return Py_BuildValue("(O(Ns)N)", Py_TYPE(self), latin1, "latin-1", dict);
|
||||
}
|
||||
|
||||
PyDoc_STRVAR(sizeof_doc,
|
||||
"B.__sizeof__() -> int\n\
|
||||
\n\
|
||||
Returns the size of B in memory, in bytes");
|
||||
static PyObject *
|
||||
bytes_sizeof(PyByteArrayObject *self)
|
||||
{
|
||||
Py_ssize_t res;
|
||||
|
||||
res = sizeof(PyByteArrayObject) + self->ob_alloc * sizeof(char);
|
||||
return PyInt_FromSsize_t(res);
|
||||
}
|
||||
|
||||
static PySequenceMethods bytes_as_sequence = {
|
||||
(lenfunc)bytes_length, /* sq_length */
|
||||
(binaryfunc)PyByteArray_Concat, /* sq_concat */
|
||||
|
@ -3138,6 +3151,7 @@ static PyMethodDef
|
|||
bytes_methods[] = {
|
||||
{"__alloc__", (PyCFunction)bytes_alloc, METH_NOARGS, alloc_doc},
|
||||
{"__reduce__", (PyCFunction)bytes_reduce, METH_NOARGS, reduce_doc},
|
||||
{"__sizeof__", (PyCFunction)bytes_sizeof, METH_NOARGS, sizeof_doc},
|
||||
{"append", (PyCFunction)bytes_append, METH_O, append__doc__},
|
||||
{"capitalize", (PyCFunction)stringlib_capitalize, METH_NOARGS,
|
||||
_Py_capitalize__doc__},
|
||||
|
|
|
@ -508,6 +508,29 @@ frame_clear(PyFrameObject *f)
|
|||
}
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
frame_sizeof(PyFrameObject *f)
|
||||
{
|
||||
Py_ssize_t res, extras, ncells, nfrees;
|
||||
|
||||
ncells = PyTuple_GET_SIZE(f->f_code->co_cellvars);
|
||||
nfrees = PyTuple_GET_SIZE(f->f_code->co_freevars);
|
||||
extras = f->f_code->co_stacksize + f->f_code->co_nlocals +
|
||||
ncells + nfrees;
|
||||
// subtract one as it is already included in PyFrameObject
|
||||
res = sizeof(PyFrameObject) + (extras-1) * sizeof(PyObject *);
|
||||
|
||||
return PyInt_FromSsize_t(res);
|
||||
}
|
||||
|
||||
PyDoc_STRVAR(sizeof__doc__,
|
||||
"F.__sizeof__() -> size of F in memory, in bytes");
|
||||
|
||||
static PyMethodDef frame_methods[] = {
|
||||
{"__sizeof__", (PyCFunction)frame_sizeof, METH_NOARGS,
|
||||
sizeof__doc__},
|
||||
{NULL, NULL} /* sentinel */
|
||||
};
|
||||
|
||||
PyTypeObject PyFrame_Type = {
|
||||
PyVarObject_HEAD_INIT(&PyType_Type, 0)
|
||||
|
@ -537,7 +560,7 @@ PyTypeObject PyFrame_Type = {
|
|||
0, /* tp_weaklistoffset */
|
||||
0, /* tp_iter */
|
||||
0, /* tp_iternext */
|
||||
0, /* tp_methods */
|
||||
frame_methods, /* tp_methods */
|
||||
frame_memberlist, /* tp_members */
|
||||
frame_getsetlist, /* tp_getset */
|
||||
0, /* tp_base */
|
||||
|
|
|
@ -3441,9 +3441,9 @@ long_sizeof(PyLongObject *v)
|
|||
{
|
||||
Py_ssize_t res;
|
||||
|
||||
res = sizeof(PyLongObject) + abs(v->ob_size) * sizeof(digit);
|
||||
res = v->ob_type->tp_basicsize;
|
||||
if (v->ob_size != 0)
|
||||
res -= sizeof(digit);
|
||||
res += abs(v->ob_size) * sizeof(digit);
|
||||
return PyInt_FromSsize_t(res);
|
||||
}
|
||||
|
||||
|
|
|
@ -1956,6 +1956,18 @@ done:
|
|||
|
||||
PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
|
||||
|
||||
static PyObject *
|
||||
set_sizeof(PySetObject *so)
|
||||
{
|
||||
Py_ssize_t res;
|
||||
|
||||
res = sizeof(PySetObject);
|
||||
if (so->table != so->smalltable)
|
||||
res = res + (so->mask + 1) * sizeof(setentry);
|
||||
return PyInt_FromSsize_t(res);
|
||||
}
|
||||
|
||||
PyDoc_STRVAR(sizeof_doc, "S.__sizeof__() -> size of S in memory, in bytes");
|
||||
static int
|
||||
set_init(PySetObject *self, PyObject *args, PyObject *kwds)
|
||||
{
|
||||
|
@ -2023,6 +2035,8 @@ static PyMethodDef set_methods[] = {
|
|||
reduce_doc},
|
||||
{"remove", (PyCFunction)set_remove, METH_O,
|
||||
remove_doc},
|
||||
{"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS,
|
||||
sizeof_doc},
|
||||
{"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
|
||||
symmetric_difference_doc},
|
||||
{"symmetric_difference_update",(PyCFunction)set_symmetric_difference_update, METH_O,
|
||||
|
@ -2144,6 +2158,8 @@ static PyMethodDef frozenset_methods[] = {
|
|||
issuperset_doc},
|
||||
{"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
|
||||
reduce_doc},
|
||||
{"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS,
|
||||
sizeof_doc},
|
||||
{"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
|
||||
symmetric_difference_doc},
|
||||
{"union", (PyCFunction)set_union, METH_VARARGS,
|
||||
|
|
|
@ -7898,20 +7898,8 @@ PyDoc_STRVAR(p_format__doc__,
|
|||
static PyObject *
|
||||
unicode__sizeof__(PyUnicodeObject *v)
|
||||
{
|
||||
PyObject *res = NULL, *defsize = NULL;
|
||||
|
||||
res = PyInt_FromSsize_t(sizeof(PyUnicodeObject) +
|
||||
return PyInt_FromSsize_t(sizeof(PyUnicodeObject) +
|
||||
sizeof(Py_UNICODE) * (v->length + 1));
|
||||
if (v->defenc) {
|
||||
defsize = PyObject_CallMethod(v->defenc, "__sizeof__", NULL);
|
||||
if (defsize == NULL) {
|
||||
Py_DECREF(res);
|
||||
return NULL;
|
||||
}
|
||||
res = PyNumber_Add(res, defsize);
|
||||
Py_DECREF(defsize);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
PyDoc_STRVAR(sizeof__doc__,
|
||||
|
|
Loading…
Reference in New Issue