[Python-checkins] cpython: Issue #15456: Fix code __sizeof__ after #12399 change.

martin.v.loewis python-checkins at python.org
Thu Jul 26 22:23:40 CEST 2012


http://hg.python.org/cpython/rev/5093cfdff2a9
changeset:   78290:5093cfdff2a9
user:        Martin v. Löwis <martin at v.loewis.de>
date:        Thu Jul 26 22:23:23 2012 +0200
summary:
  Issue #15456: Fix code __sizeof__ after #12399 change.
Patch by Serhiy Storchaka.

files:
  Lib/test/test_sys.py |   6 ++++++
  Misc/NEWS            |   3 +++
  Objects/codeobject.c |  18 +++++++++++++++++-
  3 files changed, 26 insertions(+), 1 deletions(-)


diff --git a/Lib/test/test_sys.py b/Lib/test/test_sys.py
--- a/Lib/test/test_sys.py
+++ b/Lib/test/test_sys.py
@@ -702,6 +702,12 @@
         check(get_cell().__closure__[0], size(h + 'P'))
         # code
         check(get_cell().__code__, size(h + '5i9Pi3P'))
+        check(get_cell.__code__, size(h + '5i9Pi3P'))
+        def get_cell2(x):
+            def inner():
+                return x
+            return inner
+        check(get_cell2.__code__, size(h + '5i9Pi3P') + 1)
         # complex
         check(complex(0,1), size(h + '2d'))
         # method_descriptor (descriptor object)
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,9 @@
 Core and Builtins
 -----------------
 
+- Issue #15456: Fix code __sizeof__ after #12399 change.
+  Patch by Serhiy Storchaka.
+
 - Issue #15404: Refleak in PyMethodObject repr.
 
 - Issue #15394: An issue in PyModule_Create that caused references to
diff --git a/Objects/codeobject.c b/Objects/codeobject.c
--- a/Objects/codeobject.c
+++ b/Objects/codeobject.c
@@ -375,6 +375,17 @@
 }
 
 static PyObject *
+code_sizeof(PyCodeObject *co, void *unused)
+{
+    Py_ssize_t res;
+
+    res = sizeof(PyCodeObject);
+    if (co->co_cell2arg != NULL && co->co_cellvars != NULL)
+        res += PyTuple_GET_SIZE(co->co_cellvars) * sizeof(unsigned char);
+    return PyLong_FromSsize_t(res);
+}
+
+static PyObject *
 code_repr(PyCodeObject *co)
 {
     int lineno;
@@ -480,6 +491,11 @@
 
 /* XXX code objects need to participate in GC? */
 
+static struct PyMethodDef code_methods[] = {
+    {"__sizeof__", (PyCFunction)code_sizeof, METH_NOARGS},
+    {NULL, NULL}                /* sentinel */
+};
+
 PyTypeObject PyCode_Type = {
     PyVarObject_HEAD_INIT(&PyType_Type, 0)
     "code",
@@ -508,7 +524,7 @@
     offsetof(PyCodeObject, co_weakreflist),     /* tp_weaklistoffset */
     0,                                  /* tp_iter */
     0,                                  /* tp_iternext */
-    0,                                  /* tp_methods */
+    code_methods,                       /* tp_methods */
     code_memberlist,                    /* tp_members */
     0,                                  /* tp_getset */
     0,                                  /* tp_base */

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list