]> granicus.if.org Git - python/commitdiff
[3.6] bpo-12414: Update code_sizeof() to take in account co_extra memory. (#1168...
authorDong-hee Na <donghee.na92@gmail.com>
Thu, 20 Apr 2017 08:26:25 +0000 (17:26 +0900)
committerSerhiy Storchaka <storchaka@gmail.com>
Thu, 20 Apr 2017 08:26:25 +0000 (11:26 +0300)
Lib/test/test_sys.py
Misc/ACKS
Misc/NEWS
Objects/codeobject.c

index df9ebd40859e21991cb04326e45a0dc38208e949..e151f493d4e36c95961b80020b2f52767056a0ff 100644 (file)
@@ -913,13 +913,15 @@ class SizeofTest(unittest.TestCase):
             return inner
         check(get_cell().__closure__[0], size('P'))
         # code
-        check(get_cell().__code__, size('6i13P'))
-        check(get_cell.__code__, size('6i13P'))
+        def check_code_size(a, expected_size):
+            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 inner():
                 return x
             return inner
-        check(get_cell2.__code__, size('6i13P') + 1)
+        check_code_size(get_cell2.__code__, size('6i13P') + calcsize('n'))
         # complex
         check(complex(0,1), size('2d'))
         # method_descriptor (descriptor object)
index 72070e19f65701b05d042b64e35d1b50b72ab364..ac2c4edc73357619d3fae16ca3a40fcb2ea7b48b 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -1060,6 +1060,7 @@ R. David Murray
 Matti Mäki
 Jörg Müller
 Kaushik N
+Dong-hee Na
 Dale Nagata
 John Nagle
 Takahiro Nakayama
index cb9fab656ec692d443a44878864094c9915b9f7d..b1f79d612402811e5316390e6ce8b3a0b8f3f9a7 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,10 @@ What's New in Python 3.6.2 release candidate 1?
 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-29935: Fixed error messages in the index() method of tuple, list and deque
index df8b9538fe9ed5d60f242c1988145a13dd2080f3..22c4f856cd83e6f89895d03d57f5b7073ef011e6 100644 (file)
@@ -446,11 +446,15 @@ code_dealloc(PyCodeObject *co)
 static PyObject *
 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)
-        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);
 }