[3.6] bpo-12414: Update code_sizeof() to take in account co_extra memory. (#1168) (#1198)

This commit is contained in:
Dong-hee Na 2017-04-20 17:26:25 +09:00 committed by Serhiy Storchaka
parent 83a90b9aed
commit df5df13fdc
4 changed files with 17 additions and 6 deletions

View file

@ -913,13 +913,15 @@ class SizeofTest(unittest.TestCase):
return inner return inner
check(get_cell().__closure__[0], size('P')) check(get_cell().__closure__[0], size('P'))
# code # code
check(get_cell().__code__, size('6i13P')) def check_code_size(a, expected_size):
check(get_cell.__code__, size('6i13P')) self.assertGreaterEqual(sys.getsizeof(a), expected_size)
check_code_size(get_cell().__code__, size('6i13P'))
check_code_size(get_cell.__code__, size('6i13P'))
def get_cell2(x): def get_cell2(x):
def inner(): def inner():
return x return x
return inner return inner
check(get_cell2.__code__, size('6i13P') + 1) check_code_size(get_cell2.__code__, size('6i13P') + calcsize('n'))
# complex # complex
check(complex(0,1), size('2d')) check(complex(0,1), size('2d'))
# method_descriptor (descriptor object) # method_descriptor (descriptor object)

View file

@ -1060,6 +1060,7 @@ R. David Murray
Matti Mäki Matti Mäki
Jörg Müller Jörg Müller
Kaushik N Kaushik N
Dong-hee Na
Dale Nagata Dale Nagata
John Nagle John Nagle
Takahiro Nakayama Takahiro Nakayama

View file

@ -10,6 +10,10 @@ What's New in Python 3.6.2 release candidate 1?
Core and Builtins Core and Builtins
----------------- -----------------
- bpo-12414: sys.getsizeof() on a code object now returns the sizes
which includes the code struct and sizes of objects which it references.
Patch by Dong-hee Na.
- bpo-29949: Fix memory usage regression of set and frozenset object. - bpo-29949: Fix memory usage regression of set and frozenset object.
- bpo-29935: Fixed error messages in the index() method of tuple, list and deque - bpo-29935: Fixed error messages in the index() method of tuple, list and deque

View file

@ -446,11 +446,15 @@ code_dealloc(PyCodeObject *co)
static PyObject * static PyObject *
code_sizeof(PyCodeObject *co, void *unused) code_sizeof(PyCodeObject *co, void *unused)
{ {
Py_ssize_t res; Py_ssize_t res = _PyObject_SIZE(Py_TYPE(co));
_PyCodeObjectExtra *co_extra = (_PyCodeObjectExtra*) co->co_extra;
res = _PyObject_SIZE(Py_TYPE(co));
if (co->co_cell2arg != NULL && co->co_cellvars != NULL) if (co->co_cell2arg != NULL && co->co_cellvars != NULL)
res += PyTuple_GET_SIZE(co->co_cellvars) * sizeof(unsigned char); res += PyTuple_GET_SIZE(co->co_cellvars) * sizeof(Py_ssize_t);
if (co_extra != NULL)
res += co_extra->ce_size * sizeof(co_extra->ce_extras[0]);
return PyLong_FromSsize_t(res); return PyLong_FromSsize_t(res);
} }