Issue3122.
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
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
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'))
# 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)
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
# 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'))
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 */
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__},
}
}
+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)
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 */
{
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);
}
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)
{
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,
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,
static PyObject *
unicode__sizeof__(PyUnicodeObject *v)
{
- PyObject *res = NULL, *defsize = NULL;
-
- res = 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;
+ return PyInt_FromSsize_t(sizeof(PyUnicodeObject) +
+ sizeof(Py_UNICODE) * (v->length + 1));
}
PyDoc_STRVAR(sizeof__doc__,